标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2862 Accepted Submission(s): 1071
1 #include<bits/stdc++.h> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #define ll __int64 6 using namespace std; 7 int n; 8 ll N,A,B,sum,gg,t,ans; 9 ll lcm; 10 ll aa,bb; 11 ll l[200005]; 12 ll sum1[200005]; 13 ll gcd(ll e,ll r) 14 { 15 if(r==0) 16 return e; 17 return gcd(r,e%r); 18 } 19 int main() 20 { 21 while(scanf("%d",&n)!=EOF) 22 { 23 for(int i=1;i<=n;i++) 24 { 25 sum=0; 26 ans=0; 27 int ggg; 28 scanf("%I64d %I64d %I64d",&N,&A,&B); 29 if(A>B) 30 { 31 t=A; 32 A=B; 33 B=t; 34 } 35 aa=bb=0; 36 lcm=A*B/gcd(A,B); 37 ll s1=A;ll s2=B;int cou=0; 38 while(s1<=lcm) 39 { 40 l[cou++]=s1; 41 s1+=A; 42 } 43 while(s2<=lcm) 44 { 45 l[cou++]=s2; 46 s2+=B; 47 } 48 sort(l,l+cou); 49 ll be=0; 50 for(int j=0;j<cou;j++) 51 { 52 sum1[j]=(l[j]-be)*abs((l[j]-1)%A-(l[j]-1)%B); 53 be=l[j]; 54 sum+=sum1[j]; 55 } 56 if(N/lcm!=0) 57 ans+=sum*(N/lcm); 58 gg=N%lcm; 59 if(gg) 60 { 61 for(int j=0;j<cou;j++) 62 { 63 if(l[j]>gg) 64 { 65 ggg=j; 66 break; 67 } 68 ans+=sum1[j]; 69 } 70 for(ll k=l[ggg-1]+1;k<=gg;k++) 71 { 72 ans=ans+abs(k%A-k%B); 73 } 74 } 75 printf("%I64d\n",ans); 76 } 77 } 78 return 0; 79 }
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5658848.html