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
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
就是杨辉三角组合数的应用啦~具体看代码
#include<bits/stdc++.h>
#define inf 3000005
using namespace std;
int f[inf];
void Findp(int m,vector<int>& prime)//唯一分解
{
int k=int(sqrt(m)+0.5);
for(int i=2;i<=k;i++)
{
if(m%i==0)
{
prime.push_back(i);
while(m%i==0) m/=i;
}
}
if(m>1) prime.push_back(m);
}
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(f,0,sizeof(f));
vector<int> prime;
Findp(m,prime);//对m的所有因数进行预处理,并存在vector中,这样子之后就只需要计算指数就可以判断了,
n--;//前移一位
for(int i=0;i<prime.size();i++)//对于每一个因数都进行比较,如果大于等于m,说明能被m整除
{
int p=prime[i],e=0;//C(n,0)=p^e
int mine=0,x=m;
while(x%p==0){x/=p,mine++;}
//C(n,k)=C(n,k-1)*(n-k+1)/k
for(int k=1;k<n;k++)
{
x=n-k+1;
while(x%p==0) {x/=p;e++;}
x=k;
while(x%p==0) {x/=p;e--;}
if(e<mine) f[k]=1;//判断该因数的个数
}
}
vector<int> ans;
for(int i=1;i<n;i++)
if(!f[i]) ans.push_back(i+1);//注意+1
cout<<ans.size()<<"\n";
if(!ans.empty())//如果是有解的
{
cout<<ans[0];
for(int i=1;i<ans.size();i++)
cout<<" "<<ans[i];
}
cout<<"\n";
}
return 0;
}