POJ2167Irrelevant Elements[唯一分解定理 组合数 杨辉三角]
Irrelevant Elements
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 2407 | Accepted: 597 | |
Case Time Limit: 2000MS |
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.
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
Source
题意:将n个数不断相邻合并变成1个数,求这个数%m与原数列哪些项无关
就是杨辉三角
最后每个数的系数就是C(n-1,i-1)
问题在于这个数太大了,所以用质因数分解,C(n,k)=C(n,k-1)*(n-k+1/k)递推下去
PS:不需要筛所有素数,只需要m的质因子
读入输出优化卡到469ms
// // main.cpp // poj2167 // // Created by Candy on 25/10/2016. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int N=1e5+5; typedef long long ll; inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } inline void put(int x){ if(x==0) {putchar('0');return;} char c[20];int p=0; if(x<0) putchar('-'); while(x){c[++p]=x%10+'0';x/=10;} while(p) putchar(c[p--]); } int n,m,p[N],cnt=0,em[N],e[N],ok[N]; void pf(int n){ int m=sqrt(n)+1; for(int i=2;i<=m;i++) if(n%i==0){ p[++cnt]=i; while(n%i==0) n/=i,em[cnt]++; } if(n>1) p[++cnt]=n,em[cnt]++; } int main(){ int ans=0; n=read();m=read(); pf(m); n--;//c(n,) for(int i=1;i<n;i++){ int x=n-i+1,y=i; ok[i]=1; for(int j=1;j<=cnt;j++){ while(x%p[j]==0) x/=p[j],e[j]++; while(y%p[j]==0) y/=p[j],e[j]--; if(em[j]>e[j]) ok[i]=0; } if(ok[i]) ans++; } put(ans);putchar('\n'); for(int i=1;i<=n;i++) if(ok[i]) put(i+1),putchar(' '); }
Copyright:http://www.cnblogs.com/candy99/