POJ 2167 Irrelevant Elements
来源:互联网 发布:淘宝图 编辑:程序博客网 时间:2024/06/03 23:07
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
12
Source
Northeastern Europe 2004
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
唯一分解定理+组合数~
题意为一个长为n的数列,每次合并相邻两个数,合并n-1次成为一个数,问最后这个数除m的余数与哪些数无关。
所有的数中,系数是m的倍数的数即为所求,所以题目可以转化为c(n-1,1)到c(n-1,n-1)中是m的倍数的数。
把m分解,记录系数,再与各组合数的次数对比,小于该组合数时,该组合数可行。
很巧妙的一点是在分解时直接o(n)循环即可,每次除掉i,这样除出来的数一定是质数,因为合数会在循环到它的质因子时除掉。
#include<cstdio>#include<cstring>#define ll long longll n,m,a[2][100001],cnt,tmp[100001],ans[100001];int main(){scanf("%lld%lld",&n,&m);for(int i=2;i*i<=m;i++) if(!(m%i)) { a[0][++cnt]=i; while(!(m%i)) m/=i,a[1][cnt]++; }if(m!=1) a[0][++cnt]=m,a[1][cnt]=1;for(int i=1;i<n-1;i++){int x=n-i,y=i;for(int j=1;j<=cnt;j++) if(!(x%a[0][j])) { while(!(x%a[0][j])) x/=a[0][j],tmp[j]++; }for(int j=1;j<=cnt;j++) if(!(y%a[0][j])) { while(!(y%a[0][j])) y/=a[0][j],tmp[j]--; }int flag=0;for(int j=1;j<=cnt;j++) if(a[1][j]>tmp[j]) { flag=1;break; }if(!flag) ans[++ans[0]]=i+1;}printf("%lld\n",ans[0]);for(int i=1;i<=ans[0];i++) printf("%lld ",ans[i]);printf("\n");return 0;}
1 0
- POJ 2167 Irrelevant Elements
- POJ 2167 Irrelevant Elements
- POJ 2167 Irrelevant Elements
- POJ 2167 Irrelevant Elements (math,brute force)
- POJ 2167 Irrelevant Elements 质因数分解
- UVa 1635 - Irrelevant Elements
- UVA 1635Irrelevant Elements
- uva 1635 - Irrelevant Elements
- uva 1635 Irrelevant Elements
- 1635 - Irrelevant Elements
- UVa 1635 Irrelevant Elements
- UVA 1635 Irrelevant Elements
- UVA 1635 Irrelevant Elements
- UVa1635 Irrelevant Elements
- poj2167 Irrelevant Elements
- poj2167 Irrelevant Elements
- #UVA1635#Irrelevant Elements
- Irrelevant Elements UVA
- 3.44
- 互联网项目系统架构经验浅谈
- 矩阵快速幂模板
- java读书笔记:练习对象引用传递
- html中table使用详解+代码
- POJ 2167 Irrelevant Elements
- 工作日志
- 一道有关Java null笔试题引发的思考
- [Leetcode] Search in Rotated Sorted Array II
- Android 带小圆圈的倒计时圆形进度条
- 微信公众号开发中遇到的问题——支付回调,分享,获取openId(三)
- Table Compression
- php最新Laravel框架5.3视频学习
- 各种 Java Thread State 第一分析法则