Description
Input
Output
Sample Input
37 29 41 43 47
Sample Output
654
题意:
给出5个数(<=50)a1,a2,a3,a4,a5 ,分别与5个未知数的3次方 联立方程=0 为a1x1^3+ a2x2^3+a3x3^3+ a4x4^3+ a5x5^3=0 |xi|<=50并xi!=0 求有多少组解。
题解
二分+map标记,先暴力出x1,x2,x3对应的a1x13+ a2x23+ a3x33 ; 存入数组中,再对应暴力 去 二分查找出等于 负的a4*x43次方+a5*x53次方 相应的下标 及对应个数;
代码:
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <queue> 10 #include <map> 11 #include <stack> 12 #define MOD 1000000007 13 #define maxn 20000001 14 using namespace std; 15 typedef long long LL; 16 int read() 17 { 18 int x=0,f=1; 19 char ch=getchar(); 20 while(ch<'0'||ch>'9') 21 { 22 if(ch=='-')f=-1; 23 ch=getchar(); 24 } 25 while(ch>='0'&&ch<='9') 26 { 27 x=x*10+ch-'0'; 28 ch=getchar(); 29 } 30 return x*f; 31 } 32 //******************************************************************* 33 __int64 a[1000005]; 34 map< int ,int > mp; 35 int t; 36 int jug(__int64 x) 37 { 38 39 int l=0; 40 int r=t; 41 int xx; 42 int mid; 43 while(l<=r) 44 { 45 mid=(l+r)/2; 46 if(a[mid]>x) 47 { 48 r=mid-1; 49 } 50 else if(a[mid]<x) 51 { 52 l=mid+1; 53 if(a[l]==x)return mp[x]; 54 } 55 else return mp[x]; 56 } 57 return 0; 58 } 59 int main() 60 { 61 62 int a1,a2,a3,a4,a5; 63 t=0; 64 scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); 65 for(int x1=-50; x1<=50; x1++) 66 { 67 if(x1==0) continue; 68 for(int x2=-50; x2<=50; x2++) 69 { 70 if(x2==0)continue; 71 a[++t]=(a1*x1*x1*x1+a2*x2*x2*x2); 72 if(mp.count(a[t])) 73 mp[a[t]]++; 74 else mp[a[t]]=1; 75 } 76 } 77 sort(a+1,a+t+1); 78 int ans=0; 79 for(int x3=-50; x3<=50; x3++) 80 { 81 if(x3==0)continue; 82 for(int x4=-50; x4<=50; x4++) 83 { 84 if(x4==0) continue; 85 for(int x5=-50; x5<=50; x5++) 86 { 87 if(x5==0) continue; 88 __int64 aaa=-1*(a3*x3*x3*x3+x4*a4*x4*x4+a5*x5*x5*x5); 89 ans+=jug(aaa); 90 } 91 } 92 } 93 printf("%d\n",ans); 94 return 0; 95 }
这是哈希标记法
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <queue> 10 #include <map> 11 #include <stack> 12 #define maxn 25000000 13 #define inf 1000000007 14 using namespace std; 15 typedef long long LL; 16 int read() 17 { 18 int x=0,f=1; 19 char ch=getchar(); 20 while(ch<'0'||ch>'9') 21 { 22 if(ch=='-')f=-1; 23 ch=getchar(); 24 } 25 while(ch>='0'&&ch<='9') 26 { 27 x=x*10+ch-'0'; 28 ch=getchar(); 29 } 30 return x*f; 31 } 32 //********************************************************** 33 34 short hash[25000001]; 35 int main() 36 { 37 int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,sum; 38 scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); 39 memset(hash,0,sizeof(hash)); 40 for(x1=-50; x1<=50; x1++) 41 { 42 if(x1==0) 43 continue; 44 for(x2=-50; x2<=50; x2++) 45 { 46 if(x2==0) 47 continue; 48 sum=(a1*x1*x1*x1+a2*x2*x2*x2)*-1; 49 if(sum<0)sum+=maxn; 50 hash[sum]++; 51 } 52 } 53 int cnt = 0; 54 for(x3=-50; x3<=50; x3++) 55 { 56 if(x3==0) 57 continue; 58 for(x4=-50; x4<=50; x4++) 59 { 60 if(x4==0) 61 continue; 62 for(x5=-50; x5<=50; x5++) 63 { 64 if(x5==0) 65 continue; 66 sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5; 67 if(sum<0)sum+=maxn; 68 cnt+=hash[sum]; 69 } 70 } 71 } 72 printf("%d\n",cnt); 73 return 0; 74 }