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.
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)
.
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
以后两两枚举的,不要用 二进制枚举,会重算很多数值。 还有dfs 的剪枝,简直不能再好。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
const int N=10010;
LL val[N],gcd[N];
vector<LL> vec;
LL ans;
LL n,m;
void dfs(int pos,LL lcm,int cnt){
if(lcm>=m)return;
if(pos>=n){
if(!cnt)return;
LL t=m/lcm; t--;
//printf("cnt:%d %lld\n",cnt,lcm);
if(cnt&1){
ans=ans+(lcm+t*lcm)*t/2;
}else{
ans=ans-(lcm+t*lcm)*t/2;
}
return;
}
if(lcm%gcd[pos]==0)return;
dfs(pos+1,lcm,cnt);
LL g=__gcd(lcm,gcd[pos]);
LL t=lcm*gcd[pos]/g;
dfs(pos+1,t,cnt+1);
}
int main()
{
int T;
scanf("%d",&T);
rep(kase,0,T) {
scanf("%lld %lld",&n,&m);
/*
vec.clear();
int mid=sqrt(m+0.5);
for(int i=1;i<=mid;i++){
if(m%i==0){
vec.push_back(i);
if(i*i!=m)vec.push_back(m/i);
}
}
sort(vec.begin(),vec.end());
*/
ans=0;int ok=0;
rep(i,0,n){
scanf("%lld",&val[i]);
gcd[i]=__gcd(val[i],m);
if(gcd[i]==1)ok=1;
}
if(!ok)dfs(0,1,0);
else ans=(m-1)*m/2;
printf("Case #%d: %lld\n",kase+1,ans);
}
return 0;
}
正统的思想,考虑到最后所有有贡献的,不过是 m的因子,因为大于他的肯定是0.
然后,直接看看用到了,那些因子,计算他前面的系数。
计算系数的时候,我们想肯定就是前面统计的后面不能再去统计。所以后面的要再减去相应的倍数,进行计算。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
const int N=10010;
LL val[N],gcd[N];
vector<LL> vec;
LL num[N];
int main()
{
int T;
scanf("%d",&T);
rep(kase,0,T) {
LL n,m;
scanf("%lld %lld",&n,&m);
LL mid=sqrt(m+0.5);
for(int i=1; i<=mid; i++) {
if(m%i==0) {
vec.push_back(i);
if(i*i!=m)vec.push_back(m/i);
}
}
sort(vec.begin(),vec.end());
int sz=vec.size();
fill(num,num+sz+1,0);
LL ans=0;
int ok=0;
rep(i,0,n) {
scanf("%lld",&val[i]);
gcd[i]=__gcd(val[i],m);
if(gcd[i]==1)ok=1;
for(int j=0; j<sz; j++) {
if(vec[j]%gcd[i]==0) {
num[j]=1;
}
}
}
if(!ok) {
for(int i=0; i<sz; i++) {
if(num[i]) {
LL t=m/vec[i];
t--;
ans=ans+(1+t)*t/2*num[i]*vec[i];
for(int j=i+1;j<sz;j++){
if(vec[j]%vec[i]==0){
num[j]-=num[i];
}
}
}
}
} else {
ans=m*(m-1)/2;
}
printf("Case #%d: %lld\n",kase+1,ans);
}
return 0;
}