首页 > 代码库 > POJ2167 Irrelevant Elements
POJ2167 Irrelevant Elements
Time Limit: 5000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
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, . . . , a n-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.
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, . . . , a n-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 contains n and m (1 <= n <= 100 000, 2 <= m <= 10 9).
Output
On the first line of the output 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
12
Source
Northeastern Europe 2004
对于一个数列,每次求出相邻两个数之和得到一个新数列。最后结果将变成一个数。问这个数除以m的余数与原数列中哪些项无关?
分析可知若该数的系数是m的倍数,则无关。
而数列的系数是杨辉三角第n-1层的各个数字。
问题转化成求组合数C(n-1, 0)~C(n-1, n-1)中能够被m整除的个数。
将m分解质因数,记录每个因数出现的次数,然后再将c分解质因数,若c对应因数出现的次数都大于等于m,则c是m的倍数。
递推即可。
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=200001; 9 int pri[mxn],cnt=0;10 int t[mxn];11 int c[mxn];12 int ans[mxn],num=0;13 bool flag=0;14 int n,m;15 int main(){16 scanf("%d%d",&n,&m);17 int i,j;18 for(i=2;i*i<=m;i++)//分解质因数 19 if(m%i==0){20 pri[++cnt]=i;21 while(m%i==0){22 m/=i;23 t[cnt]++;24 }25 }26 if(m>1)pri[++cnt]=m,t[cnt]++;27 for(i=1;i<n-1;i++){28 int a=n-i,b=i;29 for(j=1;j<=cnt;j++){30 while(a%pri[j]==0){31 a/=pri[j];32 c[j]++;33 }34 }35 for(j=1;j<=cnt;j++){36 while(b%pri[j]==0){37 b/=pri[j];38 c[j]--;39 }40 }41 flag=0;42 for(j=1;j<=cnt;j++){43 if(c[j]<t[j]){44 flag=1;break;45 }46 }47 if(!flag){ans[++num]=i+1;}48 }49 printf("%d\n",num);50 for(i=1;i<=num;i++)51 printf("%d ",ans[i]);52 printf("\n");53 return 0;54 }
POJ2167 Irrelevant Elements
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。