PKU 1305 Fermat vs. Pythagoras
http://poj.org/problem?id=1305
整数Nが与えられる。
このうち、x^2+y^2=z^2 かつ x,y,zは互いに素で、0
set相当遅いです。
int app[1000001]; main(){ int t; while(cin>>t){ memset(app,0,sizeof(app)); int a1=0; for(int m=2;m*m<=t;++m){ for(int n=0;n<m;n+=1+(m&1)){ if(__gcd(m,n)!=1 || m%2+n%2==2)continue; int x=m*m-n*n; int y=2*m*n; int z=n*n+m*m; if(z>t)break; ++a1; int mu=1; while(mu*z<=t){ app[x*mu]=app[y*mu]=app[z*mu]=1; ++mu; } } } cout<<a1<<' '<<t-accumulate(app,app+t+1,0)<<endl; } }