Balls Rearrangement
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2862 Accepted Submission(s): 1071
Problem Description
Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the boxes from 0 to A-1. To find the balls easily, he puts the ball numbered x into the box numbered a if x = a mod A. Some day Bob buys B new boxes, and he wants to rearrange the balls from the old boxes to the new boxes. The new boxes are numbered from 0 to B-1. After the rearrangement, the ball numbered x should be in the box number b if x = b mod B.
This work may be very boring, so he wants to know the cost before the rearrangement. If he moves a ball from the old box numbered a to the new box numbered b, the cost he considered would be |a-b|. The total cost is the sum of the cost to move every ball, and it is what Bob is interested in now.
This work may be very boring, so he wants to know the cost before the rearrangement. If he moves a ball from the old box numbered a to the new box numbered b, the cost he considered would be |a-b|. The total cost is the sum of the cost to move every ball, and it is what Bob is interested in now.
Input
The first line of the input is an integer T, the number of test cases.(0<T<=50)
Then T test case followed. The only line of each test case are three integers N, A and B.(1<=N<=1000000000, 1<=A,B<=100000).
Then T test case followed. The only line of each test case are three integers N, A and B.(1<=N<=1000000000, 1<=A,B<=100000).
Output
For each test case, output the total cost.
Sample Input
3
1000000000 1 1
8 2 4
11 5 3
Sample Output
0
8
16
Author
SYSU
Source
题意:求 i=0~N-1 abs(i%A-i%B) 的和
题解:读懂这个题目 直接暴力必然超时..
考虑分块处理 求lcm(A,B)循环节,因为a=10^5,爆了求lcm的贡献不好求,我们想想该怎么快速求得一个lcm块中的价值。如果你随便打个表或者画画图就会发现很多连续的段内是相同的值,那么仔细观察下这些段的分界线是什么呢?是A的倍数或者B的倍数。显然在一个lcm内会有b/gcd个a的倍数,b同理。那么我们会发现一个lcm最多被分为a+b个段,然后for一遍就能统计出一个lcm的价值,对于不足lcm的部分,拿出来单独处理,尽量分段求价值,分段不足时,直接暴力。
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 ",ans); 76 } 77 } 78 return 0; 79 }