Frogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2957 Accepted Submission(s): 946
Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.The stones are numbered from 0 to m−1 and the frogs are numbered from 1 to n. The i-th frog can jump over exactly ai stones in a single step, which means from stone j mod m to stone (j+ai) mod m (since all stones lie on a circle).All frogs start their jump at stone 0, then each of them can jump as many steps as he wants. A frog will occupy a stone when he reach it, and he will keep jumping to occupy as much stones as possible. A stone is still considered ``occupied" after a frog jumped away.They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.
Input
There are multiple test cases (no more than 20), and the first line contains an integer t,meaning the total number of test cases.For each test case, the first line contains two positive integer n and m - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109).The second line contains n integers a1,a2,⋯,an, where ai denotes step length of the i-th frog (1≤ai≤109).
Output
For each test case, you should print first the identifier of the test case and then the sum of all occupied stones' identifiers.
Sample Input
3 2 12 9 10 3 60 22 33 66 9 96 81 40 48 32 64 16 96 42 72
Sample Output
Case #1: 42 Case #2: 1170 Case #3: 1872
Source
- 观察对于n和m较小的时候的样例不难发现可以到达的点都是k*gcd(ai,m)<m
- 但是对于给出的n个a[i]我们不能保证在计算过程中对于同一个点只计算一次
- 考虑容斥原理解题
- gcd(ai,m)=di的结果都属于m的因数集合D(m)
- 如果 u*ai==v*aj 并且 u*ai 属于D(m),那么对于 u*ai的倍数就重复计算了一次,我们就应该在新的计算过程中减去多加的次数
- 对于多加的次数这个变量的维护可以通过记录当前因数被使用的次数来实现,1-被使用次数就是当前因数对应增量的倍数
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include