1. Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers
  2. ranging from 0 to m 1. He thinks that standard random number generators are not good enough, so
  3. he has invented his own scheme that is intended to bring more randomness into the generated numbers.
  4. First, Georgie chooses n and generates n random integer numbers ranging from 0 to m 1. Let
  5. the numbers generated be a1, a2, . . . , an. After that Georgie calculates the sums of all pairs of adjacent
  6. numbers, and replaces the initial array with the array of sums, thus getting n1 numbers: a1 +a2, a2 +
  7. a3, . . . , an1 + an. Then he applies the same procedure to the new array, getting n 2 numbers. The
  8. procedure is repeated until only one number is left. This number is then taken modulo m. That gives
  9. the result of the generating procedure.
  10. Georgie has proudly presented this scheme to his computer science teacher, but was pointed out that
  11. the scheme has many drawbacks. One important drawback is the fact that the result of the procedure
  12. sometimes does not even depend on some of the initially generated numbers. For example, if n = 3
  13. and m = 2, then the result does not depend on a2.
  14. Now Georgie wants to investigate this phenomenon. He calls the i-th element of the initial array
  15. 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.
  16. Input
  17. Input file contains several datasets. Each datasets has n and m (1 n 100 000, 2 m 109) in a
  18. single line.
  19. Output
  20. On the first line of the output for each dataset print the number of irrelevant elements of the initial
  21. array for given n and m. On the second line print all such i that i-th element is irrelevant. Numbers
  22. on the second line must be printed in the ascending order and must be separated by spaces.
  23. Sample Input
  24. 3 2
  25. Sample Output
  26. 1
  27. 2

解题思路:

  分解质因数只用筛10^5以内的素数即可,别忘了将大于10^5的质因子另外存储。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <ctime>
  8. using namespace std;
  9. #define maxn 100010
  10. #define time_ printf("%f",double(clock())/CLOCKS_PER_SEC)
  11. int vis[maxn];
  12. vector<int> prime;
  13. vector<int> fm;
  14. int e_m[maxn];
  15. int e_c[maxn];
  16. int n,m;
  17. void pre(){
  18. int m=sqrt(maxn)+;
  19. for(int i=;i<m;i++){
  20. if(!vis[i]){
  21. for(int j=i*i;j<maxn;j+=i){
  22. vis[j]=;
  23. }
  24. }
  25. }
  26. for(int i=;i<maxn;i++)
  27. if(!vis[i]) prime.push_back(i);
  28. }
  29. void cal_e(int m,int e_m[maxn],int d){
  30. int t=m;
  31. for(int i=;i<fm.size();i++){
  32. while(t%fm[i]==){
  33. e_m[i]+=d;
  34. t/=fm[i];
  35. }
  36. if(t==)break;
  37. }
  38. }
  39. bool judge(){
  40. for(int i=;i<fm.size();i++)
  41. if(e_m[i]>e_c[i]) return false;
  42. return true;
  43. }
  44. int main(int argc, const char * argv[]) {
  45. pre();
  46. while(scanf("%d%d",&n,&m)==){
  47. memset(e_m,,sizeof e_m);
  48. memset(e_c,,sizeof e_c);
  49. fm.clear();
  50. cal_e(m,e_m,);
  51.  
  52. vector<int> ans;
  53.  
  54. int t=m;
  55. int j=;
  56. for(int i=;i<prime.size();i++){
  57. if(t%prime[i]==){
  58. fm.push_back(prime[i]);
  59. while(t%prime[i]==){
  60. e_m[j]++;
  61. t/=prime[i];
  62. }
  63. j++;
  64. }
  65. if(t==)break;
  66. }
  67. if(t!=) {fm.push_back(t);e_m[j]=;}
  68. for(int k=;k<n;k++){
  69. cal_e(n-k,e_c,);
  70. cal_e(k,e_c,-);
  71. if(judge()){
  72. ans.push_back(k);
  73. }
  74. }
  75. printf("%d\n",(int)ans.size());
  76. if(ans.size()>){
  77. printf("%d",ans[]+);
  78. for(int i=;i<ans.size();i++)
  79. printf(" %d",ans[i]+);
  80. }
  81. //else printf("\n");
  82. printf("\n");
  83. //time_;
  84. }
  85.  
  86. return ;
  87. }

UVa 1635 - Irrelevant Elements-[分解质因数]的更多相关文章

  1. UVA 1635 Irrelevant Elements

    https://vjudge.net/problem/UVA-1635 题意:n个数,每相邻两个求和,最后变成1个数,问这个数除m的余数与第几个数无关 n个数使用次数分别为C(n-1,i) i∈[0, ...

  2. UVa 1635 - Irrelevant Elements(二项式系数 + 唯一分解定理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. Irrelevant Elements UVA - 1635 二项式定理+组合数公式+素数筛+唯一分解定理

    /** 题目:Irrelevant Elements UVA - 1635 链接:https://vjudge.net/problem/UVA-1635 题意:給定n,m;題意抽象成(a+b)^(n- ...

  4. UVa 1635 (唯一分解定理) Irrelevant Elements

    经过紫书的分析,已经将问题转化为求组合数C(n-1, 0)~C(n-1, n-1)中能够被m整除的个数,并输出编号(这n个数的编号从1开始) 首先将m分解质因数,然后记录下每个质因子对应的指数. 由组 ...

  5. POJ 2167 Irrelevant Elements 质因数分解

    Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2231   Accepted: 55 ...

  6. UVa 10622 (gcd 分解质因数) Perfect P-th Powers

    题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...

  7. POJ2167 Irrelevant Elements

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

  8. uva10791 uva10780(分解质因数)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. java分解质因数

      package test; import java.util.Scanner; public class Test19 { /** * 分析:对n进行分解质因数,应先找到一个最小的质数k * 最小 ...

随机推荐

  1. Leetcode2.Add Two Numbers两数相加

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  2. pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

    pymysql错误: pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') pymy ...

  3. 10分钟学会Python

    #1. 语法 Python中没有强制的语句终止字符,代码块是通过缩进来指示的.缩进表示一个代码块的开始,逆缩进则表示一个代码块的结束.一般用4个空格来表示缩进. 声明以冒号(:)字符结束,并且开启一个 ...

  4. 阿里OSS-OSSFS

    简介 OSSFS就以把OSS作为文件系统的一部分,能让你在linux系统中把OSS bucket挂载到本地文件系统中,实现数据的共享. 主要功能 ossfs 基于s3fs 构建,具有s3fs 的全部功 ...

  5. Servlet小结(转载)

    http://www.iteye.com/topic/766418 1 .首先,什么是Servlet?        Servlet是一个Java编写的程序,此程序是在服务器端运行的,是按照Servl ...

  6. Directx11教程(7) 画一个颜色立方体

    原文:Directx11教程(7) 画一个颜色立方体       前面教程我们通过D3D11画了一个三角形,本章我们将画一个颜色立方体,它的立体感更强.主要的变动是ModelClass类,在Model ...

  7. react-jd-index

    看见一些代码的产物,会觉得非常的漂亮~感谢无私开源的程序员们~你们是最可爱的人儿~~ //index.jsx require('./app/lib/common.css'); import React ...

  8. 微信小程序云数据库——where查询和doc查询区别

    用法 条件查询where 我们也可以一次性获取多条记录.通过调用集合上的 where 方法可以指定查询条件,再调用 get 方法即可只返回满足指定查询条件的记录,比如获取用户的所有未完成的待办事项,用 ...

  9. 阿里云OSS同城冗余存储正式商业化,提供云上同城容灾能力

    近日,阿里云正式发布OSS同城冗余存储产品.这是国内目前提供同城多AZ冗余部署能力覆盖最广的云上对象存储产品,可以实现云存储的同城双活,满足企业级客户对于“发生机房级灾难事件时数据不丢失,业务不中断” ...

  10. Python基础:03序列:字符串、列表和元组

    一:序列 1:连接操作符(+) 这个操作符允许把一个序列和另一个相同类型的序列做连接,生成新的序列.语法如下:sequence1 + sequence2 该表达式的结果是一个包含sequence1和s ...