Frogs (hdu5514)——2015ACM/ICPC亞洲區瀋陽站(容斥定理)

There are mm stones lying on a circle, and nn frogs are jumping over them. 
The stones are numbered from 00 to m1m−1 and the frogs are numbered from 11 to nn. Theii-th frog can jump over exactly aiai stones in a single step, which means from stone j mod mj mod m to stone (j+ai) mod m(j+ai) mod m (since all stones lie on a circle). 

All frogs start their jump at stone 00, 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.
InputThere are multiple test cases (no more than 2020), and the first line contains an integer tt
meaning the total number of test cases. 

For each test case, the first line contains two positive integer nn and mm - the number of frogs and stones respectively (1n104, 1m109)(1≤n≤104, 1≤m≤109)

The second line contains nn integers a1,a2,,ana1,a2,⋯,an, where aiai denotes step length of the ii-th frog (1ai109)(1≤ai≤109). OutputFor 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個石頭(這m個石頭是圍繞一圈的,編號從0到m-1),這n個青蛙每個青蛙每次可以跳a[i]步,然後算出所有被青蛙跳過的石頭的編號的和;
解題思路:
這是一個容斥的題目,通過觀察可以看到,青蛙跳過的石頭爲gcd(a[i],m)的倍數,並且不超過m-1,所以被跳過的石頭一定是m的因子,所以先找出m的因子,在分別用一個數組標記那些因子被跳過,一個數組標記這個因子算 了幾遍,因爲是gcd(a[i],m)倍數,所以可以利用等差數列進行求和。
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> using namespace std; typedef long long ll; const ll MAXN=1e6+5; ll gcd(ll x,ll y) {     if(y==0)         return x;     else         return gcd(y,x%y); } ll tmp[MAXN],a[MAXN]; ll vis[MAXN],num[MAXN]; int main() {     int T,n,m;     scanf("%d",&T);     int su=0;     while(T--)     {         su++;         memset(tmp,0,sizeof(tmp));         memset(a,0,sizeof(a));         memset(vis,0,sizeof(vis));         memset(num,0,sizeof(num));         int cnt=0;         scanf("%d%d",&n,&m);         for(int i=1; i<=sqrt(m); i++)         {             if(m%i==0)             {                 if(i*i==(m))                 {                     tmp[cnt]=i;                     cnt++;                 }                 else                 {                     tmp[cnt]=i;                     cnt++;                     tmp[cnt]=m/i;                     cnt++;                 }             }         }         sort(tmp,tmp+cnt);         ll ans=0;         for(int i=1;i<=n;i++)         {             scanf("%d",&a[i]);             int x=gcd(a[i],m);             for(int j=0;j<cnt-1;j++)//tmp[cnt]是本身,所以不用這項了             {                     if(tmp[j]%x==0)                         {vis[j]=1;}             }         }         for(int i=0;i<cnt-1;i++)         {             if(num[i]!=vis[i])             {                 ans+=(m/tmp[i])*(m/tmp[i]-1)/2*tmp[i]*(vis[i]-num[i]);                 //cout<<ans<<" ";                 //m/tmp[i])*(m/tmp[i]-1)/2*tmp[i]這個是等差數列前n項和,(m/tmp[i])=a1+an,(m/tmp[i]-1)是項數                 //這是其實把這個等差數列變爲原來的1/tmp[i]倍,然後再乘上tmp[i];                 int s=(vis[i]-num[i]);                 for(int j=i+1;j<cnt-1;j++)                 {                     if(tmp[j]%tmp[i]==0)                     num[j]+=s;                 }             }         }         printf("Case #%d: ",su);         cout<<ans<<endl;     }     return 0; }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章