您的位置:首页 > 其它

uva 1635 分解质因数+杨辉三角(二项式)+组合数递推

2016-07-10 22:25 387 查看
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

就是求c(n-1,k)(0<=k<=n-1)中哪些数可以被m整除 因为涉及除法,m的逆元不一定存在,所以根据m的质因数和c(n,k)的质因数个数来判断能不能整除(用组合数递推)

组合数的一个递推公式:c(n,k)=c(n,k-1)*(n-k+1)/k

注意:每个n对应的C中的n-1。0个解的话要多输个空行。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int MAXN=100005;
int a[MAXN],b[MAXN],c[MAXN],tot;

void factor(int n,int a[MAXN],int b[MAXN],int &tot){
int temp,i,now;
temp=(int)((double)sqrt(n)+1);
tot=0;
now=n;
for(i=2;i<temp;i++){
if(now%i==0){
a[++tot]=i;
b[tot]=0;
while(now%i==0){
++b[tot];
now/=i;
}
}
}
if(now!=1){
a[++tot]=now;
b[tot]=1;
}
}
int ans[MAXN];

int main()
{
int n,m,k;
int up,down;
while(cin>>n>>m){
int k=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
factor(m,a,b,tot);
memset(c,0,sizeof(c));
//for(int i=1;i<=tot;i++)cout<<a[i]<<" "<<b[i]<<endl;
for(int i=1;i<n-1;i++){
up=n-i;
down=i;
//cout<<up<<" "<<down<<endl;
int ok=1;
for(int j=1;j<=tot;j++){
if(up%a[j]==0){
while(up%a[j]==0){
c[j]++;
up/=a[j];
}
}
if(down%a[j]==0){
while(down%a[j]==0){
c[j]--;
down/=a[j];

}
//cout<<j<<" "<<c[j]<<endl;
}
if(c[j]<b[j]){
ok=0;
}
}
if(ok)ans[k++]=i+1;
}
if(!k)cout<<0<<endl<<endl;
else {
cout<<k<<endl;
for(int i=0;i<k;i++)printf((i==0?"%d":" %d"),ans[i]);
cout<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: