您的位置:首页 > 其它

HDU - 5514 - Frogs 【完美使用欧拉函数 -> 也可容斥】

2017-09-26 08:35 330 查看

Frogs

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2713 Accepted Submission(s): 885

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

2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

题意:就是一个m长的环,n个青蛙都在0点上,每个跳跃的步长为aiai,可以跳无限次,问n个青蛙经过环上所有的点的坐标和

分析:参考的大牛的思路,的确很巧,一开始没想到这样做,给上链接我是大牛,简单说下思路吧。随便推下可得知,对于每个青蛙,在环上的坐标之差为gcd(ai,m)gcd(ai,m),即例如环长12,一个青蛙步长为9,那么在环上的坐标为:0,3,6,9,如果还有一个青蛙的步长为10的话,同理在环上的坐标为:0,2,4,6,8,10,如果就两个青蛙的话,就好求了,但如果青蛙的数量都的话,就得用容斥的思想去思考了,这时呢所有的坐标应该是0,2,3,4,6,8,9,10,怎么分组正是欧拉函数的精髓啊,这样分,把m的所有因子求出来分别是2,3,6

2,10=2∗(1+5)=2∗(phi[6]∗6/2)2,10=2∗(1+5)=2∗(phi[6]∗6/2)

3,9 =3∗(1+3)=3∗(phi[4]∗4/2)3,9 =3∗(1+3)=3∗(phi[4]∗4/2)

4,8 =4∗(1+2)=4∗(phi[3]∗3/2)4,8 =4∗(1+2)=4∗(phi[3]∗3/2)

6 =6∗(1)=6∗(phi[2]∗6/2) 6 =6∗(1)=6∗(phi[2]∗6/2)

到这里应该就可以发现欧拉的精髓拉吧,如果该因子是任意步长的整数倍的时候就可以独立的分为一组,利用欧拉函数的性质,即(小于n且n互质的数的和为 phi
*n/2),接下来就是怎么把因子求出来,或者怎么求“步长”这些小事了,容斥的在另一篇博文上oAo

参考代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> G,fac;//G为所谓的”步长“   fac为因子
int getPhi(int n){
int m = (int)sqrt(n+0.5);
int ans = n;
for(int i = 2;i <= m;i++){
if(!(n%i)){
ans = ans/i*(i-1);
while(!(n%i)) n /= i;
}
}
if(n > 1) ans = ans/n*(n-1);
return ans;
}
int main(){
int T;cin>>T;
for(int TT = 1;TT <= T;TT++){
cout<<"Case #"<<TT<<": ";
fac.clear();
G.clear();
int n,m,x,tag = 0;cin>>n>>m;
for(int i = 0;i < n;i++){
cin>>x;
int g = __gcd(x,m);
if(g == 1)tag = 1;
G.push_back(g);
}
if(tag){
cout<<1ll*m*(m-1)/2<<endl;continue;
}
sort(G.begin(),G.end());
n = unique(G.begin(),G.end()) - G.begin();//去重
int mm = sqrt(m+0.5);
for(int i = 2;i <= mm;i++){
if(!(m%i)){
fac.push_back(i);
if(i*i != m) fac.push_back(m/i);
}
}
ll ans = 0;
for(int i = 0;i < fac.size();i++){
for(int j = 0;j < n;j++){
if(!(fac[i]%G[j])){
ans += 1ll*getPhi(m/fac[i])*m/2;
break;
}
}
}
cout<<ans<<endl;
}
return 0;
}


如有错误或遗漏,请私聊下UP,ths
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: