Uva--1635(数论,组合数,唯一分解)

2014-09-02 21:40:46

1635 - Irrelevant Elements

Time limit: 3.000 seconds

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 nand generates n random integer numbers ranging from 0 to m - 1. Let the numbers generated be a1a2,..., 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 + a2a2 + 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 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

思路:小白书例题,首先对 m 进行唯一素数分解,仔细分析可以发现最后剩余的一个数为 C(n-1,0)x1 + C(n-1,1)x2 + ... + C(n-1,n-2)xn-1 + C(n-1,n-1)xn,组合数有个递推式子:C(n,k) = (n-k+1)/k * C(n,k-1),只要依次判断 m 唯一分解的各个因子在这串 C(n-1,i) 中的指数是否都 >= 在 m 中,如果都大于等于的话,说明 C(n-1,i)被 m 整除,说明这个数无关。
 1 /*************************************************************************
 2     > File Name: 1635.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Tue 02 Sep 2014 08:25:14 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 typedef long long ll;
17 
18 int n,m;
19 int aprime[100005];
20 int prime[10005],pcnt;
21 vector<int> e;
22 vector<int> ce;
23 int len;
24 int ans[100005],anscnt;
25 
26 void Erato(int n){
27     int m = sqrt(n + 0.5);
28     for(int i = 2; i <= m; ++i) if(!aprime[i]){
29         for(int j = i * i; j <= n; j += i) aprime[j] = 1;
30     }
31     for(int i = 2; i <= n; ++i) if(!aprime[i]) prime[pcnt++] = i;
32 }
33 
34 int Cal_fac(int n){
35     for(int i = 0; i < pcnt; ++i) if(n % prime[i] == 0){
36         int num = 0;
37         while(n % prime[i] == 0){
38             n /= prime[i];
39             ++num;
40         }
41         e.push_back(prime[i]);
42         ce.push_back(num);
43         if(n == 1) break;
44     }
45     return n;
46 }
47 
48 void Cal(int n,int tag){
49     for(int i = 0; i < len; ++i) if(n % e[i] == 0){
50         while(n % e[i] == 0){
51             n /= e[i];
52             ce[i] += tag;
53         }
54         if(n == 1) break;
55     }
56 }
57 
58 bool Check(){
59     int flag = 1;
60     for(int i = 0; i < len; ++i) if(ce[i] > 0){
61         flag = 0;
62         break;
63     }
64     if(flag) return true;
65     return false;
66 }
67 
68 int main(){
69     Erato(100000);
70     while(scanf("%d%d",&n,&m) != EOF){
71         e.clear();
72         ce.clear();
73         anscnt = 0;
74         int d = Cal_fac(m);
75         if(d > n){
76             printf("0\n\n");
77             continue;
78         }
79         len = e.size();
80         for(int i = 1; i <= n - 1; ++i){
81             Cal(n - i,-1);
82             Cal(i,1);
83             if(Check())
84                 ans[anscnt++] = i + 1;
85         }
86         printf("%d\n",anscnt);
87         if(anscnt == 0) puts("");
88         else{
89             printf("%d",ans[0]);
90             for(int i = 1; i < anscnt; ++i) printf(" %d",ans[i]);
91             puts("");
92         }
93     }
94     return 0;
95 }

 


posted @ 2014-09-02 21:45  Naturain  阅读(162)  评论(0编辑  收藏  举报