hdu 4611 模拟+规律

#include
#include
#include
#include
#define LL __int64
using namespace std;
LL gcd(LL a,LL b){
    return b==0?a:gcd(b,a%b);
}
LL lcm(LL a,LL b){
    return a/gcd(a,b)*b;
}
LL solve(LL n,LL a,LL b){
    LL sa=0,sb=b,ans=0,LCM;
    LCM=lcm(a,b);
    while(sasb){
            ans+=(sb-sa)*(sa%b)+(sa+a-sb)*(sb%a);
            sb+=b;
            sa+=a;
        }
        else{
             ans+=(sa%b)*a;
             sa+=a;
        }
        if(sa==LCM){
            LL k=n/LCM;
            ans*=k;
            sa=n/LCM*LCM;
            sb=sa+b;
        }
    }
    LL i;
    if(sa>=n){
        for(i=sa-1;i>=n;i--){
            ans-=fabs(i%a-i%b);
        }
    }
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        LL n,a,b;
        LL tt;
        scanf("%I64d%I64d%I64d",&n,&a,&b);
        if(a>b){
            tt=a;
            a=b;
            b=tt;
        }
        printf("%I64d\n",solve(n,a,b));
    }
    return 0;
}

你可能感兴趣的:(hdu,模拟)