日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Equations HDU - 1496(哈希或三层for循环)求满足公式有多少种情况

發布時間:2023/12/4 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Equations HDU - 1496(哈希或三层for循环)求满足公式有多少种情况 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:求x在(-100<=x<=100)區間上,已知a,b,c,d,滿足a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 的情況有多少種

思路:很明顯四層for循環肯定超時,可用三層for循環來判斷,或是,兩兩組合,求多少種情況。分正負故最后

要乘上2^4;

題目

Consider equations having the following form:?

a*x1^2+b*x2^2+c*x3^2+d*x4^2=0?
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.?

It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.?

Determine how many solutions satisfy the given equation.?

Input

The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.?
End of file.

Output

For each test case, output a single line containing the number of the solutions.?

Sample Input

1 2 3 -4 1 1 1 1

Sample Output

39088 0

AC代碼

哈希:

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int f1[1000005],f2[1000005];//f1保存得數是正的 f2保存得數是負的 int a,b,c,d; int main() {while(~scanf("%d%d%d%d",&a,&b,&c,&d)){if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0)) //abcd全部大于0或者小于0,肯定無解。要加上這個,不然超時{printf("0\n");continue;}memset(f1,0,sizeof(f1));memset(f2,0,sizeof(f2));for(int i=1; i<=100; i++)for(int j=1; j<=100; j++){int ans=a*i*i+b*j*j;if(ans>=0) f1[ans]++;else f2[-ans]++;}int sum=0;for(int i=1; i<=100; i++)for(int j=1; j<=100; j++){int ans=c*i*i+d*j*j;if(ans>0) sum+=f2[ans];else sum+=f1[-ans];}printf("%d\n",16*sum);//每個解有正有負,結果有2^4種}return 0; }

三層for循環:

#include<iostream> #include<cstdio> #include<cmath>/*暴力*/ #include<cstring> using namespace std; int main() {int a,b,c,d;while(~scanf("%d%d%d%d",&a,&b,&c,&d)){if(a>0&&b>0&&c>0&&d>0||a<0&&b<0&&c<0&&d<0){printf("0\n");continue;}int sum=0;for(int i=1; i<=100; i++)//因為正負號不同不影響平方的效果,所以只枚舉正數for(int j=1; j<=100; j++)for(int l=1; l<=100; l++){int f=a*i*i+b*j*j+c*l*l;if(f%d==0){int h=sqrt(abs((0-f)/d));if(h<=100&&h>0&&h*h==abs((0-f)/d)&&f+d*h*h==0)sum++;}}printf("%d\n",sum*16);}return 0; }

?

總結

以上是生活随笔為你收集整理的Equations HDU - 1496(哈希或三层for循环)求满足公式有多少种情况的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。