HDU5514 Frogs(GCD+欧拉函数+数论)

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  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
 
题意:给定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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值