标签:
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的质因子另外存储。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <cmath> 6 #include <cstring> 7 #include <ctime> 8 using namespace std; 9 #define maxn 100010 10 #define time_ printf("%f",double(clock())/CLOCKS_PER_SEC) 11 int vis[maxn]; 12 vector<int> prime; 13 vector<int> fm; 14 int e_m[maxn]; 15 int e_c[maxn]; 16 int n,m; 17 void pre(){ 18 int m=sqrt(maxn)+1; 19 for(int i=2;i<m;i++){ 20 if(!vis[i]){ 21 for(int j=i*i;j<maxn;j+=i){ 22 vis[j]=1; 23 } 24 } 25 } 26 for(int i=2;i<maxn;i++) 27 if(!vis[i]) prime.push_back(i); 28 } 29 void cal_e(int m,int e_m[maxn],int d){ 30 int t=m; 31 for(int i=0;i<fm.size();i++){ 32 while(t%fm[i]==0){ 33 e_m[i]+=d; 34 t/=fm[i]; 35 } 36 if(t==1)break; 37 } 38 } 39 bool judge(){ 40 for(int i=0;i<fm.size();i++) 41 if(e_m[i]>e_c[i]) return false; 42 return true; 43 } 44 int main(int argc, const char * argv[]) { 45 pre(); 46 while(scanf("%d%d",&n,&m)==2){ 47 memset(e_m,0,sizeof e_m); 48 memset(e_c,0,sizeof e_c); 49 fm.clear(); 50 cal_e(m,e_m,1); 51 52 vector<int> ans; 53 54 int t=m; 55 int j=0; 56 for(int i=0;i<prime.size();i++){ 57 if(t%prime[i]==0){ 58 fm.push_back(prime[i]); 59 while(t%prime[i]==0){ 60 e_m[j]++; 61 t/=prime[i]; 62 } 63 j++; 64 } 65 if(t==1)break; 66 } 67 if(t!=1) {fm.push_back(t);e_m[j]=1;} 68 for(int k=1;k<n;k++){ 69 cal_e(n-k,e_c,1); 70 cal_e(k,e_c,-1); 71 if(judge()){ 72 ans.push_back(k); 73 } 74 } 75 printf("%d\n",(int)ans.size()); 76 if(ans.size()>0){ 77 printf("%d",ans[0]+1); 78 for(int i=1;i<ans.size();i++) 79 printf(" %d",ans[i]+1); 80 } 81 //else printf("\n"); 82 printf("\n"); 83 //time_; 84 } 85 86 return 0; 87 }
UVa 1635 - Irrelevant Elements-[分解质因数]
标签:
原文地址:http://www.cnblogs.com/Kiraa/p/5568823.html