HDU5514 Frogs(GCD+欧拉函数+数论)
2017-08-04 20:02
459 查看
Frogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2351 Accepted Submission(s): 772
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
题意:给定m块石头,石头有编号从0开始到m-1,石头以圆形摆放再给定n只青蛙的步长,青蛙都从0开始跳跃,求出所有能被青蛙跳过的石头的编号和(重复跳过的只能算一次)
思路:因为石头围成的是一个圆,所以在0到m-1青蛙的步长是gcd(ai,m);
从第一个样例来看 2 12 9 10,两只青蛙的步长是9和10,m是12,那对第一只青蛙来说gcd(9,12)=3,第二只青蛙gcd(10,12)=2.
第一只:0 3 6 9
第二只:0 2 4 6 8 10,明显的是有石头被重复占领,那么我们只要规定x这块石头只能被gcd(x,m)的步长的青蛙占领,那么分成不同的集合这样就能消除重复了
这也就是m的因子
2: 2 10
3: 3 9
4: 4 8
6: 6
只用把m的因子中能被任意个gcd(ai,m)整除的集合元素相加就是,最后的答案了
那么怎么求集合的和呢?
2+10=2∗(1+5)
3+9=3∗(1+3)
4+8=4∗(1+2)
6=6∗1
所有可得如果这个集合满足要求那么,集合里面的元素和为x这个集合乘以与小于m/x互质的数的和即可,又有欧拉函数得小于n的与其互质的数的和为phi(n)*n/2,即x*phi(m/x)*m/x/2=phi(m/x)*m/2
下面只要分解m的因子再判断能否被gcd(ai,m)整除求所有和即可
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
long long a[10005],cnt[10005];
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;
}
long long Phi(long long x){
long long ans = x;
for(long long i=2; i*i<=x; i++){
if(x%i==0){
ans -= ans/i;
while(x%i==0) x /= i;
}
}
if(x > 1) ans -= ans/x;
return ans;
}
int main()
{
int t;
scanf("%d",&t);
int cas=1;
while(t--)
{
long long n,m;
scanf("%lld%lld",&n,&m);
int flag=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
a[i]=gcd(a[i],m);
if(a[i]==1)
flag=1;
}
printf("Case #%d: ",cas++);
if(flag==1)
{
printf("%lld\n",m*(m-1)/2);
continue;
}
sort(a,a+n);
n=unique(a,a+n)-a;
int num=0;
for(int i=1;i*i<=m;i++)
if(m%i==0)
{
cnt[num++]=m/i;
cnt[num++]=i;
}
sort(cnt,cnt+num);
long long ans=0;
for(int i=1;i<num-1;i++)
for(int j=0;j<n;j++)
if(cnt[i]%a[j]==0)
{
ans+=Phi(m/cnt[i])*m/2;
break;
}
printf("%lld\n",ans);
}
return 0;
}
相关文章推荐
- HDU-4983-Goffi and GCD【数论】【欧拉函数】
- 【数论】gcd|扩展gcd|素数筛法|快速幂|欧拉函数(各种模板)
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)
- (hdu 2588 gcd)<数论—欧拉函数>
- hdu1787 GCD Again(数论:欧拉函数)
- GCD - Extreme (II) (数论,欧拉函数)
- 【数论】【筛法求素数】【欧拉函数】bzoj2818 Gcd
- HDU1695-GCD(数论-欧拉函数-容斥)
- [BZOJ2818] Gcd (数论,欧拉函数,线性筛)
- HDU 5780 BestCoder Round #85 gcd (数论---欧拉函数)
- hdu 2588 GCD [欧拉函数]【数论】*
- hdu - GCD -欧拉函数 & GCD原理 - 数论
- 【hdu - 1787 GCD Again (数论、欧拉函数)】
- 从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)
- FZU 1969 数论 GCD+欧拉函数应用
- HDU 5514 Frogs(欧拉函数+数论YY)
- 2018-01-15 HDU 5514 Ver.A 数论 GCD 欧拉函数 总结归纳
- HDU1695-GCD(数论-欧拉函数-容斥)
- GCD nyoj1007(欧拉函数运用&&数论入门)
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)