644人阅读 评论(0)

# Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2810    Accepted Submission(s): 904

Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from 0 to m1 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 (1n104, 1m109).

The second line contains n integers a1,a2,,an, where ai denotes step length of the i-th frog (1ai109).

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

2对应： 2，4，6，8，10
3对应： 3，6，9
4对应： 4，8
6对应： 6

2：0次        3：0次        10： 0次
4：1次        8：1次
6：2次

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=10005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}

ll visit[maxn],ans[maxn],factor[maxn],map[maxn];
int main()
{
ll t,i,j,k,n,m,flag=1;
cin>>t;
while(t--)
{
mset(visit,0);
mset(ans,0);
mset(map,0);
mset(factor,0);
cin>>n>>m;
for(i=0;i<n;i++)
cin>>map[i];

k=0;
for(i=1;i*i<=m;i++)             //求因子
{
if(m%i==0)
{
if(i*i!=m)
{
factor[k++]=i;
factor[k++]=m/i;
}
else
factor[k++]=i;
}
}
sort(factor,factor+k);

for(i=0;i<n;i++)               //标记出能用到的因子
{
int g=gcd(map[i],m);
for(j=0;j<k;j++)
{
if(factor[j]%g==0)
visit[j]=1;
}
}
visit[k-1]=0;

ll sum=0;
for(i=0;i<k;i++)
{
if(visit[i]!=ans[i])
{
ll temp=(m-1)/factor[i];

sum+=(ll)temp*(temp+1)/2*factor[i]*(visit[i]-ans[i]);       //前面是前n项和的贡献，后面是容斥控制

temp=visit[i]-ans[i];
for(j=i;j<k;j++)                                    //以后因子是前面因子的倍数的话，就加上两者的差值
{
if(factor[j]%factor[i]==0)
ans[j]+=temp;
}
}
}
cout<<"Case #"<<flag++<<": "<<sum<<endl;
}
return 0;
}

个人资料
等级：
访问量： 6万+
积分： 2183
排名： 2万+
联系方式
欢迎谈论交流：1245985209
博客专栏
 ACM的进阶之路 文章：88篇 阅读：37883 CodeForce题解 文章：40篇 阅读：17504