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$ \le$n$ \le$100 000, 2$ \le$m$ \le$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 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
1
2
通过化简,只需求c(i,n-1)%m是否等于0即可。
由于数据取值范围很大,直接求c(i,n-1)并不现实,故只需对m的每个素因子进行考虑;
用factor[i][j]存储其素因子及其个数,易得素因子个数不超过25个。
然后求出任意N!的所含素因子个数,并存在t[maxn][25]中;
由于c(i,n-1)=(n-1)!/((i)!*(n-i-1)!),那么对于c(i,n-1),
其各素因子个数s[i][j]=t[n-1][j]-t[i][j]-t[n-i-1][j];
判断其是否被m整除只需比较其和m的各素因子数目即可。
玛德输出好坑,输出0后还需要加空行。
#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#define LL long long
using namespace std;
#define maxn 100005
int l,c;
int prime[maxn],v[maxn],factor[25][2],t[maxn][25],s[maxn][25],a[maxn];
void p()
{
LL i,j,n=maxn,m;
c=0;
m=(LL)sqrt(n+0.5);
memset(v,0,sizeof(v));
for(i=2;i<=m;i++)
if(!v[i]){
for(j=i*i;j<=n;j+=i)
v[j]=1;
}
for(j=2;j<=n;j++){
if(!v[j]){
prime[c++]=j;
}
}
}
int f(int n,int m)
{
int t=m,sum=0;
while(t<=n){
sum+=n/t;
t*=m;
}
return sum;
}
bool fen(int n)
{
l=0;
for(int i=0;n>1&&i<c;i++){
if(n%prime[i]==0){
factor[l][0]=prime[i];
int temp=1;
n/=prime[i];
while(n%prime[i]==0){
n/=prime[i];
temp++;
}
factor[l++][1]=temp;
}
}
if(n>maxn) return false;
else return true;
}
int main()
{
int n,m;
p();
while(~scanf("%d%d",&n,&m)){
if(fen(m)==true){
int j=0;
for(int i=0;i<n;i++){
for(int j=0;j<l;j++){
t[i][j]=f(i,factor[j][0]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<l;j++){
s[i][j]=t[n-1][j]-t[i][j]-t[n-i-1][j];
}
}
for(int i=0;i<n;i++){
int flag=1;
for(int j=0;j<l;j++){
if(s[i][j]<factor[j][1]){
flag=0;
break;
}
}
if(flag==1){
a[j++]=i+1;
}
}
printf("%d\n",j);
if(j>0){
printf("%d",a[0]);
for(int i=1;i<j;i++){
printf(" %d",a[i]);
}
}
printf("\n");
}
else{
printf("0\n");
printf("\n");
}
memset(t,0,sizeof(t));
memset(s,0,sizeof(s));
}
return 0;
}