UVa 1635 Irrelevant Elements
来源:互联网 发布:单片机0xff是什么意思 编辑:程序博客网 时间:2024/04/30 00:20
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.
Input
Input file contains several datasets. Each datasets has n and m ( 1n100 000, 2m109) 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
12
#include <cstdio>#include <vector>#include <utility>#include <cstring>using namespace std;int array[100010];int count = 0; int bad[100010];void divide(int x, vector<pair<int,int> >& v);int main(){// 对1-100000进行质因数分解/*for(int i = 1; i <= 100000; i++)divide(i, array[i]);*/// 读入情况int n, m;while(scanf("%d %d", &n, &m) == 2){count = 0;// 对m分解质因数vector<pair<int,int> > this_v;divide(m, this_v);// 计算各个组合数的对应m质数及其指数/*vector<pair<int,int> > now_v;for(int i = 0; i < this_v.size(); i++){now_v.push_back(pair<int,int>(this_v[i].first, 0));}for(int x = 1; x <= n-1; x++){// 分别计算n-i和i的对应m质数及其指数int k1 = n-x;for(int i = 0; i < now_v.size(); i++){int j = now_v[i].first;int c = 0;while(k1 % j == 0){c++;k1 = k1 / j;}now_v[i].second += c;if(k1 == 1)break;}int k2 = x;for(int i = 0; i < now_v.size(); i++) { int j = now_v[i].first; int c = 0; while(k2 % j == 0) { c++; k2 = k2 / j; } now_v[i].second -= c; if(k2 == 1) break; }// 检查该数能否被m整除int i;for(i = 0; i < now_v.size(); i++){if(now_v[i].second < this_v[i].second)break;}if(i == now_v.size()){array[count] = x+1;count++;}}*/n = n -1;memset(bad, 0, sizeof(bad));for(int i = 0; i < this_v.size(); i++){int e = 0;for(int j = 1; j <= n-1; j++){int k1 = n-j + 1;while(k1 % this_v[i].first == 0){k1 /= this_v[i].first;e++;}int k2 = j;while(k2 % this_v[i].first == 0) { k2 /= this_v[i].first; e--; }if(e < this_v[i].second)bad[j] = 1;}}// 打印结果count = 0;vector<int> ans;for(int i = 1; i < n; i++){if(bad[i] == 0)ans.push_back(i+1);}printf("%d\n", ans.size());if(ans.size() != 0){for(int i = 0; i < ans.size(); i++){if(i == 0)printf("%d", ans[i]);elseprintf(" %d", ans[i]);}}printf("\n");/*printf("%d\n", count);for(int i = 0; i < count; i++){if(i == 0)printf("%d", array[i]);elseprintf(" %d", array[i]);}printf("\n");*/}return 0;}// 对x分解质因数,得到的质数及其指数存在v中void divide(int x, vector<pair<int,int> >& v){v = vector<pair<int,int> >();if(x < 2)return;int k = x;for(int i = 2; i*i <= x; i++){if(k % i == 0){int c = 0;while(k % i == 0){c++;k = k / i;}v.push_back(pair<int,int>(i, c));if(k == 1)break;}}if(k > 1)v.push_back(pair<int,int>(k, 1));}
首先可以证明出n个数时,最后a1-an的系数为(a+b)^(n-1)的二项式系数。只要判断每个系数是否为m的倍数即可。
但是系数有可能很大,不能直接根据递推关系来算。
书上的方法是:如果一个数包含m质因数分解的质数及其指数,那么该数是m的倍数。
于是,当一个数过大时,可以采用的方法是根据递推关系来逐步计算是否包含m质因数分解的质数及其指数
0 0
- UVa 1635 - Irrelevant Elements
- UVA 1635Irrelevant Elements
- uva 1635 - Irrelevant Elements
- uva 1635 Irrelevant Elements
- UVa 1635 Irrelevant Elements
- UVA 1635 Irrelevant Elements
- UVA 1635 Irrelevant Elements
- UVA 1635(p320)----Irrelevant Elements
- UVa 1635 - Irrelevant Elements(唯一分解)
- UVA 1635 Irrelevant Elements [唯一分解定理]
- UVA 1635 Irrelevant Elements 唯一分解定律
- Irrelevant Elements UVA
- Irrelevant Elements UVA
- Irrelevant Elements UVA
- Irrelevant Elements UVA
- Irrelevant Elements UVA
- 1635 - Irrelevant Elements
- uva-1635-Irrelevant Elements-唯一分解定理,组合数
- 2014年10月12日GRE考试issue作文题及参考范文
- Android 实践 - 2015/06/12
- SAT数学练习题及详细答案解析(6)
- mybatis- 获取自增ID
- junit注解
- UVa 1635 Irrelevant Elements
- Android存储访问及目录
- 有用的链接的保存
- Linux多线程编程(一)
- asp.net Url重写
- Android 设置缩略图,injustdecodebounds
- 对string的一些扩展函数
- 小白胡说:构造器和引用传递
- Eclipse如何切换SVN用户