2015ACM/ICPC亚洲区沈阳站-重现赛 HUD 5514 Frogs (容斥原理+GCD)


Frogs

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


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
 

【题意】

 给   n 只青蛙, m 个石头; 第i个青蛙 每次跳 ai个石头    石头围城环 在 0-m-1 内循环

求 n只青蛙 不重复跳过的 石头标号总数;


【思路】

容斥原理+ GCD


示例   2 12   9,10

可以发现  9 ——  走的石头为  3 6 9   10—— 走的石头为 2 4 6 8 10   重复点为6  容斥原理

 走的步数为 2 3 4 6 8 9 10      可以发现   是 的倍数GCD(ai,m)   即 2 和3的倍数

m的因子为  1 2 3 4 6 8 12  

是GCD(ai,m) 倍数的是

2  3 4 6

  2  4  6  8 10    (5个)

  3  6  9     ( 3个)

  4   8   (2 个)

  6    (1 个)


容斥原理 应用   :   用 vis  标记  2  3 4  6   这样的数   标记为 1

                              用 num 来记录     2   3   4  6  中 存在倍数的数

    num【】一次为     0    0   1   2            然后    ans +=  前x 项和  * (vis【i】— num【i】)   (非常巧妙)


【代码实现】


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%d\n",n)
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=50005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

ll inv[maxn*2];
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
ll inv2(ll b){return qpow(b,MOD-2);}

ll n,m;
ll a[maxn];
ll vis[maxn];
ll qoop[maxn];
ll num[maxn];
ll cot;
void init()
{
    cot=0;
    for(int i=1;i<=sqrt(m);i++)
    {
        if(m%i==0)
        {
            qoop[++cot]=i;
            if(i*i!=m)
                qoop[++cot]=m/i;
        }
    }
}
int main()
{
    int t;
    cin>>t;
    int cont=0;
    while(t--)
    {
        scanf("%lld %lld",&n,&m);
        mem(a,0);
        mem(num,0);
        mem(vis,0);
        mem(qoop,0);
        init();
        sort(qoop+1,qoop+cot+1);
        for(int i=1;i<=n;i++)
        {
            ll x;
            scanf("%lld",&x);
            ll temp= gcd(x,m);
            for(int j=1;j<=cot;j++)
            {
                if(qoop[j]%temp==0)
                    vis[j]=1;
            }
        }
        ll ans=0;
        for(int i=1;i<=cot;i++)
        {
            if(vis[i]!=num[i])
            {
                ll tm= (m-1)/ qoop[i];

                ans+= tm * (tm+1)/2 *qoop[i]  * (vis[i]-num[i]);

                for(int j=i+1;j<=cot;j++)
                {
                    if(qoop[j]%qoop[i]==0)
                    {
                        num[j]+=vis[i]-num[i];
                    }
                }
            }
        }

        printf("Case #%d: %lld\n",++cont,ans);
    }
    return 0;
}


123

posted @ 2017-10-02 20:32  Sizaif  阅读(174)  评论(0编辑  收藏  举报