1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f3f3fll #define debug(x) cerr<<#x<<"="<<x<<endl using namespace std; using ll=long long; using ld=long double; using pli=pair<ll,int>; using pi=pair<int,int>; template<typename A> using vc=vector<A>; template<typename A,const int N> using aya=array<A,N>; inline int read() { int s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } inline ll lread() { ll s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } inline ll gcd(ll a,ll b) { ll r=a%b; while(r) { a=b; b=r; r=a%b; } return b; } ld sr;int r; ll f(ll a,ll b,ll c,ll n) { if(!n) return 0; ll g=gcd(a,gcd(b,c)); a/=g,b/=g,c/=g;
ld t=(a*sr+b)/c;ll V=floor(t); if(V) return n*(n+1)/2*V+f(a,b-V*c,c,n); ll m=floor(t*n); return n*m-f(a*c,-b*c,a*a*r-b*b,m); } int main() { int T=read(); while(T--) { int n=read();r=read();sr=sqrtl(r); int v=floor(sr+0.5); if(v*v==r) { if(v%2==0) printf("%d\n",n); else if(n&1) printf("-1\n"); else printf("0\n"); } else printf("%lld\n",n-2*f(1,0,1,n)+4*f(1,0,2,n)); } return 0; }
|