hdu 5514 Frogs 容斥

Frogs

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


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
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5932  5931  5930  5929  5928 
 

Statistic |  Submit |  Discuss |  Note

     题意:给出n个青蛙[1,n],m个石头[0,m),青蛙i如果当前在x ,每次可以跳到(x+a[i])%m,称作(x+a[i])%m个石头可占领

     问所有可占领的石头编号之和。

  

     解法:一个数的因数数量其实并不多。


   关键点是得到对于青蛙i,每个能够跳到的石头都是gcd(a[i],m)的倍数。


  这是因为  a[i]*x- m*y=c,c代表当前所在格子,x代表跳了多少步。要想有整数解必须有gcd(a[i],m)|c。


 现在不从n的角度,而是从m的因子角度出发考虑问题。


先找出m的所有因子,并从小到大排序(方便后续解题),对所有应该被算一个的因子其need标记为1。


之后从小到大将因子加入到ans,如果因子a被算了,那么因子k*a的实际计算次数也必须同步更新。




#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
#define ysk(x)  (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn=10000    ;

int n,m,a[maxn+5],fac[20010],nfac,need[20010],num[20010];//20010: sqrt(1e9)*2+

ll S[20010];

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
void getfac()
{
    nfac=0;
    for(int i=1;i*i<=m;i++) if(m%i==0)
    {
        fac[nfac++]=i;
        if(i*i!=m) fac[nfac++]=m/i;

    }
    sort(fac,fac+nfac);
    nfac--;//位置m超出范围,不予考虑。
}


void solve()
{

    for0(i,nfac)
    {
        ll k=(m-1)/fac[i];
        S[i]=   fac[i]*(k+1)*k/2;
    }


    mes(need,0,nfac);
    for1(i,n)
    {
        int g=gcd(a[i],m);
        for0(j,nfac) if(fac[j]%g==0)
        {
            need[j]=1;
        }
    }

    mes(num,0,nfac);
    ll ans=0;


    for0(i,nfac) if(need[i]!=num[i])
    {
        ans+=   S[i]*(need[i]-num[i]);
        for(int j=i+1;j<nfac;j++) if(fac[j]%fac[i]==0)
        {
            num[j]+=need[i]-num[i];
        }
    }


    printf("%lld\n",ans);

}
int main()
{
   std::ios::sync_with_stdio(false);
   int T,kase=0;cin>>T;
   while(T--)
   {
       cin>>n>>m;
       for1(i,n) cin>>a[i];
       printf("Case #%d: ",++kase);
       getfac();
       solve();
   }
   return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值