标签:void amp 复杂 复杂度 sdn 比较 reg space ref
#include<bits/stdc++.h>
#define R register int
#define ll long long
#define db double
using namespace std;
int T;ll n,r,ans,t;db q;
int gi(){
R x=0,k=1;char c=getchar();
while(c!=‘-‘&&(c<‘0‘||c>‘9‘))c=getchar();
if(c==‘-‘)k=-1,c=getchar();
while(c<=‘9‘&&c>=‘0‘)x=(x<<3)+(x<<1)+c-‘0‘,c=getchar();
return x*k;
}
ll Gcd(ll x,ll y){return y?Gcd(y,x%y):x;}
ll sol(ll a,ll b,ll c,ll n){
if(n==1)return (a*q+b)/c;
if(n==0)return 0;
ll gcd=Gcd(a,Gcd(b,c));
a/=gcd,b/=gcd,c/=gcd;
ll k=(a*q+b)/c;
if(k==0){
ll m=((a*q+b)/c*n);
return m*n-sol(a*c,-b*c,a*a*r-b*b,m);
}
else return k*(n*(n+1)/2)+sol(a,b-c*k,c,n);
}
void cheat(){
if(!(t&1))printf("%lld\n",n);
else if(n&1)puts("-1");
else puts("0");
}
int main(){
T=gi();
while(T--){
n=gi(),r=gi(),q=sqrt(r),t=q;
if(t*t==r){cheat();continue;}
ans=n+4ll*sol(1,0,2,n)-2ll*sol(1,0,1,n);
printf("%lld\n",ans);
}
return 0;
}
标签:void amp 复杂 复杂度 sdn 比较 reg space ref
原文地址:https://www.cnblogs.com/Tyher/p/10089585.html