[BZOJ3817][类欧几里得算法]Sum

题意


给定N,R
nd=1(1)d2R


类欧算法裸题
可以看这篇http://blog.csdn.net/lych_cys/article/details/51345089


#include 
#include 
#include 
#include 

using namespace std;

int n,r,T;
double t;

int gcd(int a,int b){
    return b?gcd(b,a%b):a;
}

int solve(int n,int a,int b,int c){
    if(n<=0) return 0;
    int d=gcd(gcd(a,b),c); a/=d; b/=d; c/=d;
    int tmp=(b*t+c)/a,sum=1ll*tmp*n*(n+1)>>1;
    c-=tmp*a; tmp=(b*t+c)/a*n;
    return sum+n*tmp-solve(tmp,b*b*r-c*c,a*b,-a*c);
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&r); t=sqrt(1.0*r);
        if((int)t==t)
            if((r&1)^1) printf("%d\n",n);
            else if(n&1) printf("%d\n",-1);
            else printf("%d\n",0);
        else printf("%d\n",n+4*solve(n,2,1,0)-2*solve(n,1,1,0));
    }
    return 0;
}

你可能感兴趣的:(类欧几里得算法)