Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers ranging from 0
M-1. He thinks that standard random number generators are not good enough, so he has been Ted his own scheme that is intended to bring more randomness into the generated numbers. First, Georgie chooses
NAnd generates
NRandom integer numbers ranging from 0
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 implements 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 callthe
I-Th element of the initial array irrelevant if the result of the generating procedure does not depend on
AI. He considers various
NAnd
MAnd wonders which elements are irrelevant for these parameters. Help him to find it out. input file contains several datasets. Each datasets has
NAnd
M(1
N100, 2
M109) 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
NAnd
M. On the second line print all such
IThat
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
12
The sum of the entire formula can be reduced to Sigma (C (n-1, I-1) * Ai)
Idea: As long as Judge C (n-1, I-1) can be m divisible.
The solution is to first break down the prime factor of M and then calculate 1 !~ (N-1 )! Number of prime factors containing M
C (n-1, I-1) = (n-1 )! /(I-1 )! * (N-I )!)
You only need to determine whether the number of remaining prime factors is greater than or equal to the number of any prime factor of M.
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>#include <cmath>using namespace std;typedef long long LL;const int maxp = 40000+10;const int maxn = 100000+10;int n,m;bool isPrime[maxp];vector<int> ret,prime,hp,cnt,tmp;vector<int> g[maxn];void getPrime(){ memset(isPrime,true,sizeof isPrime); for(int i = 2; i < maxp; i++){ if(isPrime[i]){ prime.push_back(i); for(int j = i+i;j < maxp; j += i){ isPrime[j] = false; } } }}void getDigit(){ int tk = m; for(int i = 0; i < prime.size() && prime[i]*prime[i] <= tk; i++){ if(tk%prime[i]==0){ int k = 0; hp.push_back(prime[i]); while(tk%prime[i]==0){ tk /= prime[i]; k++; } cnt.push_back(k); } } if(tk>1){ hp.push_back(tk); cnt.push_back(1); }}void init(){ cnt.clear(); hp.clear(); ret.clear(); getDigit(); for(int i = 0; i <= n-1; i++) g[i].clear(); for(int i = 0; i <= n-1; i++) { for(int j = 0; j < hp.size(); j++){ int d = 0,t = i; while(t) { d += t/hp[j]; t /= hp[j]; } g[i].push_back(d); } }}void solve(){bool miden = false; for(int i = 2; i <= (n-1)/2+1; i++){ bool flag = true; for(int j = 0; j < hp.size(); j++){ int d = g[n-1][j]-g[i-1][j]-g[n-i][j]; if(d < cnt[j]){ flag = false; break; } } if(flag) {ret.push_back(i);if(i==(n-1)/2+1 && (n&1)) miden = true; } } tmp.clear(); tmp = ret; if(n&1){ int i; if(miden) i = tmp.size()-2; else i = tmp.size()-1; for(; i >= 0; i--){ ret.push_back(n+1-tmp[i]); } }else{ for(int i = tmp.size()-1; i >= 0; i--){ ret.push_back(n+1-tmp[i]); } } printf("%d\n",ret.size()); if(ret.size()){ printf("%d",ret[0]); for(int i = 1; i < ret.size(); i++){ printf(" %d",ret[i]); } } puts("");}int main(){ //freopen("test.txt","r",stdin); getPrime(); while(~scanf("%d%d",&n,&m)){ init(); solve(); } return 0;}
Uva1635-Irrelevant elements (prime factor decomposition)