组合数学-习题
Q1(uva 1635):
给出长度为n(范围在[1,100000])的序列(仅仅知道长度n,具体某个元素我们并不清楚),类似差分序列的形成方法,我们这里得到这样的一个序列:
参考代码如下:
#include<cstdio> #include<vector> #include<cmath> #include<cstring> using namespace std; const int maxn1 = 1000+ 5; const int maxn2 = 100000 + 5; int prime_factor[maxn1]; int bad[maxn2]; int num;// length of prime_factor[maxn] void prime_resolve(int n) { //函数功能:找到n的所有质因子 int i; num = 0; for(i = 2;i*i <= n;i++) { if(n % i == 0) { prime_factor[num++] = i; while(n % i == 0) n /= i; } } if(n > 1) //如果没有除尽,说明n的质因子是它本身 prime_factor[num++] = n; } int main() { int n , m; while(scanf("%d%d",&n , &m) != EOF) { memset(bad , 0 , sizeof(bad)); prime_resolve(m); n--; for(int i = 0;i < num;i++) //素因子表的一层循环 { int p = prime_factor[i] , e = 0; int min_e = 0 , x = m; while(x % p == 0) {min_e++ ; x /= p;} //c(n,k) = (n-k+1)/k * c(n,k-1) 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(min_e > e) bad[k] = 1; } } int temp[maxn2]; int index = 0; for(int i = 1;i < n;i++) if(!bad[i]) temp[index++] = i+1; printf("%d\n",index); if(index){ printf("%d",temp[0]); for(int i = 1;i < index;i++) printf(" %d" , temp[i]);} printf("\n"); } }