There are
m
m stones lying on a circle, and
n
n frogs are jumping over them.
The stones are numbered from 0 0 to m−1 m−1 and the frogs are numbered from 1 1 to n n. The i i-th frog can jump over exactly ai ai stones in a single step, which means from stone j mod m j 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 0 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 0 to m−1 m−1 and the frogs are numbered from 1 1 to n n. The i i-th frog can jump over exactly ai ai stones in a single step, which means from stone j mod m j 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 0 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.
meaning the total number of test cases.
For each test case, the first line contains two positive integer n n and m m - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109) (1≤n≤104, 1≤m≤109).
The second line contains n n integers a1,a2,⋯,an a1,a2,⋯,an, where ai ai denotes step length of the i i-th frog (1≤ai≤109) (1≤ai≤109).
3 2 12 9 10 3 60 22 33 66 9 96 81 40 48 32 64 16 96 42 72
Case #1: 42 Case #2: 1170Case #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; }