标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define esp 0.00000000001 const int N=3e5+10,M=1e6+10,inf=1e9,mod=1e9+7; ll a[N],b[N],c[N]; ll gcd(ll x,ll y) { return y==0?x:gcd(y,x%y); } int main() { ll x,y,z,i,t; int T; scanf("%d",&T); while(T--) { scanf("%I64d%I64d%I64d",&x,&y,&z); ll gc=gcd(y,z); ll lcm=y*z/gc; int ji=1; for(i=1;i<=lcm/y;i++) a[ji++]=i*y; for(i=1;i<lcm/z;i++) a[ji++]=i*z; sort(a,a+ji); for(i=0;i<ji-1;i++) { b[i]=a[i+1]-a[i]; c[i]=abs(a[i]%y-a[i]%z); } ll sum=0; for(i=0;i<ji-1;i++) sum+=b[i]*c[i]; ll ans=0; ans+=x/lcm*sum; x%=lcm; for(i=0;i<ji-1;i++) { if(x<=0) break; if(x<b[i]) ans+=x*c[i]; else ans+=b[i]*c[i]; x-=b[i]; } printf("%I64d\n",ans); } return 0; }
hdu 4611 Balls Rearrangement 数学
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5658093.html