HDU 5514 Frogs (容斥+数论)

Frogs

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


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



题意是有n只青蛙,m块石头。每只青蛙一次跳a[i]步,当这块石头被跳过,即被占领,石头是环状分布,标号是0-m,问最后青蛙占领所有的石头的标号的总和是多少。也就是求所有青蛙跳过的石头不重复的总和是多少。

这里要注意一定是不重复的石头,石头的重复与否直接决定了容斥是否进行。不难发现,每只青蛙跳过几次后总会出现循环节,也就是一直跳以前的跳过的石头,所以不妨拿第一组示例进行分析,看看所有跳过的石头跟石头总数和青蛙每次跳的步数有没有什么必要的联系。

第一次示例被跳过的石头有:2,3,4,6,8,9,10.  不难发现跳过的石头一定是gcd(a[i],m)的倍数,如果再仔细计算(此处略去一万秒仔细计算的时间,同时略去一万种仔细计算的尝试),会发现每个出现的石头都是m的因子%gcd(a[i],m)==0的倍数,然后把这几组数列举一下:

2对应: 2,4,6,8,10
3对应: 3,6,9
4对应: 4,8
6对应: 6

不难发现(好难啊!)重复次数如下:
2:0次        3:0次        10: 0次
4:1次        8:1次 
6:2次

为什么会出现重复?而且每次重复都是前面因子的倍数,所以后面的数需要用到容斥原理,然后,前面的数就是等差数列的前n项和,n=(m-1)/ 因子。
不知道哪位大神有容斥原理的通用模板,真不想自己写,哎。下篇博客见。

代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=10005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}

ll visit[maxn],ans[maxn],factor[maxn],map[maxn];
int main()
{
	ll t,i,j,k,n,m,flag=1;
	cin>>t;
	while(t--)
	{
		mset(visit,0);
		mset(ans,0);
		mset(map,0);
		mset(factor,0);
		cin>>n>>m;
		for(i=0;i<n;i++)
		cin>>map[i];
		
		k=0;
		for(i=1;i*i<=m;i++)             //求因子 
		{
			if(m%i==0)
			{
				if(i*i!=m)
				{
					factor[k++]=i;
					factor[k++]=m/i;
				}
				else
				factor[k++]=i;
			}
		}
		sort(factor,factor+k);
		
		for(i=0;i<n;i++)               //标记出能用到的因子 
		{
			int g=gcd(map[i],m);
			for(j=0;j<k;j++)
			{
				if(factor[j]%g==0)
				visit[j]=1;
			}
		}
		visit[k-1]=0;
		
		ll sum=0;
		for(i=0;i<k;i++)
		{
			if(visit[i]!=ans[i])
			{
				ll temp=(m-1)/factor[i];
				
				sum+=(ll)temp*(temp+1)/2*factor[i]*(visit[i]-ans[i]);       //前面是前n项和的贡献,后面是容斥控制
				
				temp=visit[i]-ans[i];
				for(j=i;j<k;j++)                                    //以后因子是前面因子的倍数的话,就加上两者的差值 
				{
					if(factor[j]%factor[i]==0)
					ans[j]+=temp;
				}
			}
		}
		cout<<"Case #"<<flag++<<": "<<sum<<endl;
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值