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, … , 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 contains n and m (1 <= n <= 100 000, 2 <= m <= 109).
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
1
2
组合数递推+唯一分解定理。
#include<iostream>
#include<cstdio>
using namespace std;
const int N=100005;
int n,m,cnt,num,ans[N],pri[N],c[N];
bool pd()
{
for(int i=1;i<=cnt;i++)
if(c[i]>0)
return 0;
return 1;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=2;i*i<=m;i++)
if(m%i==0)
{
pri[++cnt]=i;
while(m%i==0)
{
m/=i;
++c[cnt];
}
}
if(m!=1)
{
pri[++cnt]=m;
c[cnt]=1;
}
for(int i=1;i<=n-1;i++)
{
int a=n-i,b=i;
for(int j=1;j<=cnt;++j)
while(a%pri[j]==0)
{
a/=pri[j];
--c[j];
}
for(int j=1;j<=cnt;++j)
while(b%pri[j]==0)
{
b/=pri[j];
++c[j];
}
if(pd())
ans[++num]=i+1;
}
printf("%d\n",num);
for(int i=1;i<=num;++i)
printf("%d ",ans[i]);
printf("\n");
return 0;
}
本文介绍了一种由年轻密码分析师Georgie提出的随机数生成方案,该方案通过一系列操作最终生成一个介于0到m-1之间的随机数。文章探讨了生成过程中某些初始随机数可能对最终结果不产生影响的情况,并提供了一个算法来找出这些无关紧要的初始数值。

617

被折叠的 条评论
为什么被折叠?



