题目链接 琪亚娜世界第一可爱
有N个人围成圈坐,第一个人手里有个球,每次都顺时针传给不包括拿球的这个人在内,之后的第k个人,一直重复这个过程,直到球再次回到第一个人手里,记再回到第1个人之前,收到球的人的编号的和为fk,求所有fk的可能值,且升序排列输出。
这个题我不太清楚为什么这么做,因为我看16的是否居然才5个数,然后打表看了看,gcd(N,k)相同的时候值相同,所以就遍历所有的因数。
等我想明白了再更
#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return
#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
vector<ll> store;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
/*
rep(N,4,17){
cout<<"N:"<<N<<endl;
rep(k,1,N+1){
cout<<"K is:"<<k<<endl;
int pos=1;
do{
store.Push(pos);
pos+=k;
if(pos>N)
pos%=N;
}while(pos!=1);
int sum=0,len=store.size();
rep(i,0,len){
sum+=store[i];
cout<<store[i]<<" ";
}
cout<<"sum is:"<<sum<<endl;
store.clear();
cout<<k<<" is finish!"<<endl;
}
cout<<N<<" is finish!\n"<<endl;
}
*/
int N;
cin>>N;
store.Push(1LL*(1+N)*N/2),store.Push(1);
for(register int i=2;i*i<=N;i++){ //as difference
if(N%i)
continue;
ll num=N/i;
store.Push((N+2-i)*num/2);
if(i*i!=N){
num=N/num;
store.Push((N+2-N/i)*num/2);
}
}
sort(store.begin(),store.end());
int len=store.size();
rep(i,0,len){
cout<<store[i]<<" ";
}
cout<<endl;
re 0;
}
首先,将原来从1起的编号改为从0起,那么传递x次后,假设到达c手中,那么有
x
⋅
k
m
o
d
  
N
=
c
x\cdot k \mod N = c
x⋅kmodN=c
也就是
x
⋅
k
−
y
⋅
N
=
c
x \cdot k -y \cdot N = c
x⋅k−y⋅N=c
根据Bezout’s theorem
Let a,b∈ Z be any two integers, then there exist u,v in Z such that
au+bv=GCD(a,b).
设a,b是整数,则存在整数x,y,使得ax+by=gcd(a,b),(a,b)代表最大公因数,则设a,b是不全为零的整数,则存在整数x,y,使得ax+by=(a,b)。
可知c一定为GCD(k,N)的倍数,所以对于k来说,其对应的序列为GCD(k,N)的公差,首项为0的等差数列,注意我们现在是从0起,所以在最后要加上项数。又因k∈[0,N-1],故GCD(k,N)一定是N的因数,所以遍历所有的因数求出其对应的fk就是答案了