一般做法:
显然的超内存
#include<stdio.h> #include<algorithm> using namespace std; int a[300010],ans[5]; int main() { int t,i,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0; i<n; i++) scanf("%d",&a[i]); sort(a,a+n); int j=0,tmp=1; a[n++]=0; for(i=1; i<n; i++) { if(a[i]==a[i-1]) { tmp++; } else { if(tmp!=3) ans[j++]=a[i-1]; //printf("%d ",tmp); tmp=1; } } printf("%d %d ",ans[0],ans[1]); } return 0; }
正确做法:
位运算
计算出 a+b以及a*a+b*b
然后求根求出a 和 b
#include<stdio.h> #include<string.h> #include<math.h> #define eps 1e-8 int b[32]; __int64 bb[64]; int main() { int i,n,t; scanf("%d",&t); while(t--) { int x; __int64 xx; memset(b,0,sizeof(b)); memset(bb,0,sizeof(bb)); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&x); xx=(__int64)x*x; int k=0; while(x>0) { b[k++]+=x%2; x>>=1; } k=0; while(xx>0) { bb[k++]+=xx%2; xx>>=(__int64)1; } } x=0; for(i=31;i>=0;i--) { //printf("%d ",b[i]); x=(x<<1)+b[i]%3; } //printf(" %d ",x); xx=0; for(i=63;i>=0;i--) { //printf("%lld ",bb[i]); xx=(xx<<(__int64)1)+bb[i]%3; } //printf(" %I64d ",xx);//(double) int abs=sqrt((long double)(2*xx-(__int64)x*x))+eps; printf("%d %d ",(x-abs)/2,(x+abs)/2); } return 0; }
#include<stdio.h> #include<string.h> #include<math.h> #define eps 1e-8 int b[32]; __int64 bb[64]; int main() { int i,n,t; scanf("%d",&t); while(t--) { int x; __int64 xx; memset(b,0,sizeof(b)); memset(bb,0,sizeof(bb)); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&x); xx=(__int64)x*x; int k=0; while(x>0) { b[k++]+=x%2; x>>=1; } k=0; while(xx>0) { bb[k++]+=xx%2; xx>>=(__int64)1; } } x=0; for(i=31;i>=0;i--) { //printf("%d ",b[i]); x=(x<<1)+b[i]%3; } //printf(" %d ",x); xx=0; for(i=63;i>=0;i--) { //printf("%lld ",bb[i]); xx=(xx<<(__int64)1)+bb[i]%3; } //printf(" %I64d ",xx);//(double) int abs=sqrt((2*xx-(__int64)x*x))+eps;//不加(__int64),会WA printf("%d %d ",(x-abs)/2,(x+abs)/2); } return 0; }