POJ2167 Irrelevant Elements

Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

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, . . . , a n-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 <= 10 9).

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

Northeastern Europe 2004

对于一个数列,每次求出相邻两个数之和得到一个新数列。最后结果将变成一个数。问这个数除以m的余数与原数列中哪些项无关?

分析可知若该数的系数是m的倍数,则无关。

而数列的系数是杨辉三角第n-1层的各个数字。

问题转化成求组合数C(n-1, 0)~C(n-1, n-1)中能够被m整除的个数。

将m分解质因数,记录每个因数出现的次数,然后再将c分解质因数,若c对应因数出现的次数都大于等于m,则c是m的倍数。

递推即可。

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=200001;
 9 int pri[mxn],cnt=0;
10 int t[mxn];
11 int c[mxn];
12 int ans[mxn],num=0;
13 bool flag=0;
14 int n,m;
15 int main(){
16     scanf("%d%d",&n,&m);
17     int i,j;
18     for(i=2;i*i<=m;i++)//分解质因数
19         if(m%i==0){
20             pri[++cnt]=i;
21             while(m%i==0){
22                 m/=i;
23                 t[cnt]++;
24             }
25         }
26     if(m>1)pri[++cnt]=m,t[cnt]++;
27     for(i=1;i<n-1;i++){
28         int a=n-i,b=i;
29         for(j=1;j<=cnt;j++){
30             while(a%pri[j]==0){
31                 a/=pri[j];
32                 c[j]++;
33             }
34         }
35         for(j=1;j<=cnt;j++){
36             while(b%pri[j]==0){
37                 b/=pri[j];
38                 c[j]--;
39             }
40         }
41         flag=0;
42         for(j=1;j<=cnt;j++){
43             if(c[j]<t[j]){
44                 flag=1;break;
45             }
46         }
47         if(!flag){ans[++num]=i+1;}
48     }
49     printf("%d\n",num);
50     for(i=1;i<=num;i++)
51         printf("%d ",ans[i]);
52     printf("\n");
53     return 0;
54 }
时间: 2024-10-08 17:28:55

POJ2167 Irrelevant Elements的相关文章

UVa1635 - Irrelevant Elements(质因数分解)

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 b

组合数杨辉三角(Irrelevant Elements uva1635)

Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbersranging from 0 to m - 1. He thinks that standard random number generators are not good enough, sohe has invented his own scheme that is intended to bri

POJ 2167 Irrelevant Elements 质因数分解

Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2231   Accepted: 550 Case Time Limit: 2000MS Description Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers ranging from

uva 1635 - Irrelevant Elements

链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=850&problem=4510 题意:一组数字a1,a2,a3.......,不断的相邻数成一组求和形成新的一组,直到最后不可以在合并为止,问最后的答案对m取模与哪些初始的项的无关? 题解:显然无关就是要求某一项在最终的数列中的系数是m的倍数,并且显然最终系列的系数是二项分布

UVa1635 - Irrelevant Elements

通过观察发现其规律符合杨辉三角 需要注意的是最后ai的系数是C(i-1,n-1) 那么,问题就可以变成判断C(0,n-1),C(1,n-1)....C(n-1,n-1)哪些是m的倍数 只需要计算出m的唯一分解式中各个素因子在C(i-1,n-1)中的指数即可完成判断 然而为了节省时间,实际上我们只需算出m的每一个素因子在C(i-1,n-1)项中  含有几个即可 即我们将c(i-1,n-1)依次除以m的每一个素因子,直到无法整出,即可得出该项素因子的个数 紫薯上给出一个公式C(k,n)=(n-k+1

UVa1635 - Irrelevant Elements(组合数)

题意:整个式子的和可以  化简为  sigma (C(n-1,i-1)*ai) 思路:只要判断C(n-1,i-1)能否被 m整除即可. 做法是先分解m的质因数,然后计算1!~(n-1)!  包含m的质因数的个数 C(n-1,i-1) = (n-1)!/((i-1)!*(n-i)!) 只要判断 剩下的质因数的个数是否大于等于m的任一个质因数的个数即可 <pre name="code" class="cpp">#include<iostream>

uva1635 Irrelevant Elements(唯一分解定理)

题目链接:点击打开链接 题意:给定n个数a1,a2····an,依次求出相邻两个数值和,将得到一个新数列,重复上述操作,最后结果将变为一个数,问这个数除以m的余数与那些数无关?例如n=3,m=2时,第一次得到a1+a2,a2+a3,在求和得到a1+2*a2+a3,它除以2的余数和a2无关.1=<n<=10^5, 2=<m<=10^9 解题思路: 1.首先我们可以发现对于给定的n其实每项的系数就是C(n-1,i-1),所以我们只需要找到每项的系数对m取余是否为0即可 2.由于m的取值

UVa 1635 Irrelevant Elements (唯一分解定理 || 组合数学)

题目 题目大意 对于给定的\(n\)个数\(a_1\), \(a_2\), ···, \(a_n\), 依次求出相邻两数之和, 将得到一个新数列.重复上述操作, 最后结果将变成一个数.问这个数除以\(m\)的余数将与哪些数无关? 例如\(n = 3\), \(m = 2\)时, 第一次求和得到\(a_1 + a_2\), \(a_2 + a_3\), 再求和得到\(a_1 + 2a_2 + a_3\), 它除以\(2\)的余数和\(a_2\)无关.\(1 ≤ n ≤ 10^5\), \(2 ≤

【UVa1635】Irrelevant Elements - 唯一分解定理

题意 给你 \(n\) 个数,每次求出相邻两个数的和组成新数列.经过 \(n-1\) 次操作后,得到一个数.求这个数 \(mod \ m\) 与哪些项无关. 如:当 \(m=2 \ , \ n=2\) 时 \(a_1 \ , \ a_2 , a_3 \Rightarrow a_1+a_2 \ , \ a_2+a_3 \Rightarrow \ a_1+2a_2+a_3\) 则与 \(a_2\) 无关 思路 由二项式定理知道结果系数是杨辉三角的第 \(n-1\) 行,问题转换成判断有多少个 \(C