Help Feluda with mathematical equations
Here is the explanation of logic.
Given X2 + Y2 + n = (X + Y)2
Now expanding it
X2 + Y2 + n = X2 + Y2 + 2XY ≡ n = 2XY ≡ X = (n/2)*(1/y)
So we have to find only non-prime integral factor of “n/2” .and if “n” is odd then no integral “X” is possible so we will write 0 for all odd “n”.
Here my solution got accepted in 2.45 sec .Please help me to reduce the time complexity @ raj.nishant360@gmail.com
#include <bits/stdc++.h> using namespace std; #define LL long long bool check(LL n) { if(n==1) return true; for(LL i=2;i*i<=(n);i++) if(n%i==0) return true; return false; } int main() { int t; scanf("%d",&t); while(t--) { LL n,temp; scanf("%lld",&n); if(n==0) { printf("0 0\n"); continue; } if(n&1) { printf("0\n"); continue; } n>>=1; map<LL,int>mp; map<LL,int>::iterator t; for(LL i=1;i*i<=n;i++) { if(n%i==0) { if(check(i)){ mp[i]=1; } if(check(n/i)) mp[n/i]=1; } } printf("%d ",mp.size()); for(t=mp.begin();t!=mp.end();t++) printf("%lld ",t->first); printf("\n"); } return 0; }
No comments:
Post a Comment
Your comment is valuable to us