Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers
ranging from 0 to m − 1. He thinks that standard random number generators are not good enough, so
he has invented his own scheme that is intended to bring more randomness into the generated numbers.
First, Georgie chooses n and generates n random integer numbers ranging from 0 to m − 1. Let
the numbers generated be a1, a2, . . . , an. After that Georgie calculates the sums of all pairs of adjacent
numbers, and replaces the initial array with the array of sums, thus getting n−1 numbers: a1 +a2, a2 +
a3, . . . , an−1 + an. Then he applies the same procedure to the new array, getting n − 2 numbers. The
procedure is repeated until only one number is left. This number is then taken modulo m. That gives
the result of the generating procedure.
Georgie has proudly presented this scheme to his computer science teacher, but was pointed out that
the scheme has many drawbacks. One important drawback is the fact that the result of the procedure
sometimes does not even depend on some of the initially generated numbers. For example, if n = 3
and m = 2, then the result does not depend on a2.
Now Georgie wants to investigate this phenomenon. He calls the i-th element of the initial array
irrelevant if the result of the generating procedure does not depend on ai. He considers various n and
m and wonders which elements are irrelevant for these parameters. Help him to find it out.
Input
Input file contains several datasets. Each datasets has n and m (1 ≤ n ≤ 100 000, 2 ≤ m ≤ 109) in a
single line.
Output
On the first line of the output for each dataset print the number of irrelevant elements of the initial
array for given n and m. On the second line print all such i that i-th element is irrelevant. Numbers
on the second line must be printed in the ascending order and must be separated by spaces.
Sample Input
3 2
Sample Output
1
2
解题思路:
分解质因数只用筛10^5以内的素数即可,别忘了将大于10^5的质因子另外存储。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
#include <ctime>
using namespace std;
#define maxn 100010
#define time_ printf("%f",double(clock())/CLOCKS_PER_SEC)
int vis[maxn];
vector<int> prime;
vector<int> fm;
int e_m[maxn];
int e_c[maxn];
int n,m;
void pre(){
int m=sqrt(maxn)+;
for(int i=;i<m;i++){
if(!vis[i]){
for(int j=i*i;j<maxn;j+=i){
vis[j]=;
}
}
}
for(int i=;i<maxn;i++)
if(!vis[i]) prime.push_back(i);
}
void cal_e(int m,int e_m[maxn],int d){
int t=m;
for(int i=;i<fm.size();i++){
while(t%fm[i]==){
e_m[i]+=d;
t/=fm[i];
}
if(t==)break;
}
}
bool judge(){
for(int i=;i<fm.size();i++)
if(e_m[i]>e_c[i]) return false;
return true;
}
int main(int argc, const char * argv[]) {
pre();
while(scanf("%d%d",&n,&m)==){
memset(e_m,,sizeof e_m);
memset(e_c,,sizeof e_c);
fm.clear();
cal_e(m,e_m,); vector<int> ans; int t=m;
int j=;
for(int i=;i<prime.size();i++){
if(t%prime[i]==){
fm.push_back(prime[i]);
while(t%prime[i]==){
e_m[j]++;
t/=prime[i];
}
j++;
}
if(t==)break;
}
if(t!=) {fm.push_back(t);e_m[j]=;}
for(int k=;k<n;k++){
cal_e(n-k,e_c,);
cal_e(k,e_c,-);
if(judge()){
ans.push_back(k);
}
}
printf("%d\n",(int)ans.size());
if(ans.size()>){
printf("%d",ans[]+);
for(int i=;i<ans.size();i++)
printf(" %d",ans[i]+);
}
//else printf("\n");
printf("\n");
//time_;
} return ;
}