hdu 5514 Frogs

本文探讨了多只青蛙从起点跳跃覆盖多个圆周上石头的问题,通过数学方法确定每只青蛙能够到达的所有石头,并使用容斥原理计算不重复的石头总数及其编号之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Frogs

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


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:   6032  6031  6030  6029  6028 
 
题意:有n只青蛙,起始位置为0,每只青蛙固定每次能跳a[i]个石头,石头编号为0~m-1,问所有青蛙能到达的石头的编号的总和为多少。

思路:我们很容易想到每个青蛙能跳的位置为gcd(a[i],m)的k倍,然后根据等差数列公式可以推出每只青蛙能到的位置的总和。但是每只青蛙到的有可能重复,所以这里要容斥一下。首先把m分解因数,然后用一个数组vis[i]表示该因子应该算几次,cnt[i]表示该因子已经算了一次。例如样例1,9和12的gcd为3,那么因子3、6、都应该算一次,10和12的gcd为2,那么因子2、4、6都应该算一次,故最后因子2、3、4、6都应该算一次,最后再扫一遍,在计算2的时候因子4和因子6已经被迫算了一次,所以cnt要+,在算因子3的时候因子6也被迫算了一次所以这个cnt还要+,当vis和cnt相等的时候不用算,其余情况都要算。大概就这样容斥。详情看代码:

#include<bits/stdc++.h> 
using namespace std;
typedef long long LL;
const int maxn=1005;
int fac[maxn],len,vis[maxn],cnt[maxn];
void getfac(int x){
	for(int i=1;i*i<=x;i++){
		if(x%i)
			continue;
		fac[len++]=i;
		fac[len++]=x/i;
	}
}
int gcd(int a,int b){
	return !b?a:gcd(b,a%b);
}
int main(){
	int t;
	scanf("%d",&t);
	for(int tcase=1;tcase<=t;tcase++){
		memset(vis,0,sizeof(vis));
		memset(cnt,0,sizeof(cnt));
		int n,m;
		scanf("%d%d",&n,&m);
		len=0;
		getfac(m);
		sort(fac,fac+len);
		len--;
		while(n--){
			int x;
			scanf("%d",&x);
			int g=gcd(x,m);
			for(int i=0;i<len;i++)
				if(!(fac[i]%g))
					vis[i]=1;
		}
		LL ans=0;
		for(int i=0;i<len;i++){
			if(vis[i]!=cnt[i]){
				LL num=vis[i]-cnt[i];
				LL number=(m-1)/fac[i];
				ans+=num*fac[i]*(number+1)*number/2;
				for(int j=i+1;j<len;j++){
					if(fac[j]%fac[i])
						continue;
					cnt[j]+=num;
				}
			}
		}
		printf("Case #%d: %lld\n",tcase,ans);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值