Frogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2313 Accepted Submission(s): 760
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.
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) .
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
Recommend
思路:我们很容易想到每个青蛙能跳的位置为gcd(a[i],m)的k倍,然后根据等差数列公式可以推出每只青蛙能到的位置的总和。但是每只青蛙到的有可能重复,所以这里要容斥一下。首先把m分解因数,然后用一个数组vis[i]表示该因子应该算几次,cnt[i]表示该因子已经算了一次。例如样例1,9和12的gcd为3,那么因子3、6、都应该算一次,10和12的gcd为2,那么因子2、4、6都应该算一次,故最后因子2、3、4、6都应该算一次,最后再扫一遍,在计算2的时候因子4和因子6已经被迫算了一次,所以cnt要+,在算因子3的时候因子6也被迫算了一次所以这个cnt还要+,当vis和cnt相等的时候不用算,其余情况都要算。大概就这样容斥。详情看代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1005;
int fac[maxn],len,vis[maxn],cnt[maxn];
void getfac(int x){
for(int i=1;i*i<=x;i++){
if(x%i)
continue;
fac[len++]=i;
fac[len++]=x/i;
}
}
int gcd(int a,int b){
return !b?a:gcd(b,a%b);
}
int main(){
int t;
scanf("%d",&t);
for(int tcase=1;tcase<=t;tcase++){
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
int n,m;
scanf("%d%d",&n,&m);
len=0;
getfac(m);
sort(fac,fac+len);
len--;
while(n--){
int x;
scanf("%d",&x);
int g=gcd(x,m);
for(int i=0;i<len;i++)
if(!(fac[i]%g))
vis[i]=1;
}
LL ans=0;
for(int i=0;i<len;i++){
if(vis[i]!=cnt[i]){
LL num=vis[i]-cnt[i];
LL number=(m-1)/fac[i];
ans+=num*fac[i]*(number+1)*number/2;
for(int j=i+1;j<len;j++){
if(fac[j]%fac[i])
continue;
cnt[j]+=num;
}
}
}
printf("Case #%d: %lld\n",tcase,ans);
}
}