P5596 洛谷月赛 题 题解
因为a>=0,b>=0,所以y^2-x^2>=0,所以y>x,因为都是自然数设y=x+k,化简得x=b-k^2/2*k-a;可知x仅当b-k^2%2*k-a==0且b-k^2与2*k-a同号时为一个解.枚举k即可,易知上界为b1/2或a/2
1 #include <iostream> 2 #include <cstdio> 3 #include <memory.h> 4 #include <cmath> 5 #include <stdlib.h> 6 #define int ll 7 #define re register 8 #define r(x) x=read() 9 using namespace std; 10 typedef long long ll; 11 int MAX(int a,const int &b){if(a<b)a=b;return a;} 12 int MIN(int a,const int &b){if(a>b)a=b;return b;} 13 ll read() 14 { 15 char ch=0;ll w=0,ff=1; 16 while(ch<'0'||ch>'9'){if(ch=='-')ff=-1;ch=getchar();} 17 while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();} 18 return ff*w; 19 } 20 ll a,b,k,cnt,cnt2; 21 int ans; 22 signed main() 23 { 24 r(a),r(b); 25 if(a==0&&b==0) printf("inf"),exit(0); 26 if(4*b==a*a) printf("inf"),exit(0); 27 cnt2=a/2,cnt=sqrt(b); 28 if(cnt2>cnt) swap(cnt,cnt2); 29 cnt2--,cnt++; 30 for(re ll i=cnt2,x,y;i<=cnt;++i) 31 { 32 if(i<0) continue; 33 x=b-i*i,y=2*i-a; 34 if(y!=0&&(x<=0&&y<=0||x>=0&&y>=0)&&x%y==0) ans++; 35 } 36 printf("%lld",ans); 37 return 0; 38 }