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
先介绍一下中途相遇法:
中途相遇法,我的看法是用空间换取时间的一种算法(具体的我也没查)。
解决这道题第一种方法很容易就能想到四层暴力循环,因为我一直秉持着暴力出奇迹的想法,然后就超时了。。
先来说一下一些特殊的情况吧,比如当a,b,c,d全部大于0或者小于零的时候是不成立的,所以不需要跑,只需要进行简单的判断即可,因为数据量比较大所以这一步的判断是非常重要的!!!
1.暴力破解----超时
2.第一次优化,枚举a,b,c,对d进行二分查找来确定,如果不加刚才讨论的特殊情况是会超时的,加上就没问题了;
时间复杂度:O(n^3*log(n))//我也不太会判断,可能是吧(:笑哭
#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
int arr[maxn];
int max_sum[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
return 1;
}
return 0;
}
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)){
cout<<0<<endl;
continue;
}
int num=0;
map<int,int> mp;
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
for(int k=1;k<=100;k++){
int temp=(a*i*i+b*j*j+c*k*k);
// if(!mp.count(temp)){
// mp[temp]++;
// }else{
// mp[temp]=1;
// }
int left=1,right=100;
while(left<=right){
int mid=(right+left)/2;
if(temp>=0){
if(d*mid*mid+temp>0){
left=mid+1;
}else if(d*mid*mid+temp==0){
num++;
break;
}else{
right=mid-1;
}
}else{
if(d*mid*mid+temp<0){
left=mid+1;
}else if(d*mid*mid+temp==0){
num++;
break;
}else{
right=mid-1;
}
}
}
}
}
}
printf("%d\n",num*16);
}
return 0;
}
3.采用map,先枚举 a,b,存入map中再枚举c,d进行查找,如果符合就计入;同样如果不加判断的特殊情况还是会超时,加上就OK了;
时间复杂度:O(n^2*logn(n) )
#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
int arr[maxn];
int max_sum[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
return 1;
}
return 0;
}
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)){
cout<<0<<endl;
continue;
}
int num=0;
map<int,int> mp;
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int temp=(a*i*i+b*j*j);
if(mp.count(temp)){
mp[temp]++;
}else{
mp.insert(make_pair(temp,1));
}
}
}
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int temp=(c*i*i+d*j*j)*(-1);
if(mp.count(temp)){
num+=mp[temp];
}
}
}
printf("%d\n",num*16);
}
return 0;
}
4.将map换成unordered_map,因为不会进行排序,所以会快点,但不幸的是还是超时了,庆幸的是,加上讨论的特殊情况就不会超时了;
#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
#include <unordered_map>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
int arr[maxn];
int max_sum[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
return 1;
}
return 0;
}
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)){
cout<<0<<endl;
continue;
}
int num=0;
unordered_map<int,int> mp;
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int temp=(a*i*i+b*j*j);
if(mp.count(temp)){
mp[temp]++;
}else{
mp.insert(make_pair(temp,1));
}
}
}
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int temp=(c*i*i+d*j*j)*(-1);
if(mp.count(temp)){
num+=mp[temp];
}
}
}
printf("%d\n",num*16);
}
return 0;
}
5.采用hash,我到这里才觉得真正用到了中途相遇的思想,用空间来换取时间,
定义两个hash数组,和使用map的方法类似,但是hash用的是数组的下标,所以必须保证是正数,分开存储是比较好的方法;
第一步,如果a*i*i+b*j*j>=0的话,就存入hash1[]中,<0就取绝对值存入hash2[]中;
第二布,枚举x3,x4,如果c*x3*x3+d*x4*x4>0的话,因为必须和是0,所以num加的是存储负数的哈希数组对应的值,num+=hash2[temp];如果<0,num+=hash1[-temp]
时间复杂度:O(n^2)
#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
//#include <unordered_map>
using namespace std;
typedef long long LL;
const int maxn=1000000+100;
int Hash1[maxn];//>0
int Hash2[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
return 1;
}
return 0;
}
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)){
cout<<0<<endl;
continue;
}
memset(Hash1,0,sizeof(Hash1));
memset(Hash2,0,sizeof(Hash2));
int num=0;
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int temp=(a*i*i+b*j*j);
if(temp>=0) Hash1[temp]++;
else Hash2[-temp]++;
}
}
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int temp=c*i*i+d*j*j;
if(temp>0) num+=Hash2[temp];
else num+=Hash1[-temp];
}
}
printf("%d\n",num*16);
}
return 0;
}
比较重要的就是那个特殊情况的判断,真的十分重要!
好了,差不多了,有错的地方欢迎大佬留言!