Frogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2042 Accepted Submission(s): 676
Problem Description
There are
m
stones lying on a circle, and
n
frogs are jumping over them.
The stones are numbered from 0 to m−1 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.
The stones are numbered from 0 to m−1 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 (1≤n≤104, 1≤m≤109) .
The second line contains n integers a1,a2,⋯,an , where ai denotes step length of the i -th frog (1≤ai≤109) .
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 (1≤n≤104, 1≤m≤109) .
The second line contains n integers a1,a2,⋯,an , where ai denotes step length of the i -th frog (1≤ai≤109) .
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
题意:
给出 m 个石头( 0,1,2,3。。。。m-1)
n 只青蛙(1,2,3。。。。n )
给出n只青蛙各自一步能跳的距离,大家都从石头0开始跳
求解:所有被跳过的石头的序号之和
题解:
由题知:
第i只青蛙,他跳过的格子,一定是gcd(a[i],m)的倍数序号的石头
如果m小一点,就用一般性的容斥原理暴力解决(统计所有最大公因数容斥一下)
但是m过于大,使用一般性容斥原理就会超时
然后逆向思考,统计m的因子(非质因子)
再统计标记gcd(a[i],m)出现过的因子
然后容斥原理,这里的容斥原理与一般的不一样,很经典的代码
LL ans=0;
for(int i=0;i<tot;i++){
if(vis[i]!=num[i]){
int t=(m-1)/a[i];
ans+=((LL)t*(t+1)/2)*a[i]*(vis[i]-num[i]);
t=vis[i]-num[i];
for(int j=i+1;j<tot;j++)
if(a[j]%a[i]==0)
num[j]+=t;
}
}
注意:
可能出现跳远距离为1的青蛙
所以下面代码中 i 不能从2开始,而且a【tot-1】==m,需要剔除这个数字
void devide(int m)
{
tot=0;
int t=sqrt(m);
for(int i=1;i<=t;i++){
if(m%i==0){
a[tot++]=i;
if(i*i!=m)
a[tot++]=m/i;
}
}
qsort(a,tot,sizeof(a[0]),cmp);
tot--;
}
AC代码
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define mem(a) memset(a,0,sizeof(a))
#define LL long long
#define MAXN 1000
int a[MAXN],tot;
int vis[MAXN],num[MAXN];
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
void devide(int m)
{
tot=0;
int t=sqrt(m);
for(int i=1;i<=t;i++){
if(m%i==0){
a[tot++]=i;
if(i*i!=m)
a[tot++]=m/i;
}
}
qsort(a,tot,sizeof(a[0]),cmp);
tot--;
}
int gcd(int a,int b)
{
while(b)
{
int t=a%b;
a=b;
b=t;
}
return a;
}
int main()
{
int T;
int n,m;
int cases=1;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
mem(vis),mem(num);
scanf("%d%d",&n,&m);
devide(m);
int temp;
for(int i=0;i<n;i++){
scanf("%d",&temp);
int t=gcd(temp,m);
for(int j=0;j<tot;j++)
if(a[j]%t==0)
vis[j]=1;
}
LL ans=0;
for(int i=0;i<tot;i++){
if(vis[i]!=num[i]){
int t=(m-1)/a[i];
ans+=((LL)t*(t+1)/2)*a[i]*(vis[i]-num[i]);
t=vis[i]-num[i];
for(int j=i+1;j<tot;j++)
if(a[j]%a[i]==0)
num[j]+=t;
}
}
printf("Case #%d: %lld\n",cases++,ans);
}
return 0;
}


796

被折叠的 条评论
为什么被折叠?



