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 (1 ≤ n ≤ 100 000, 2 ≤ m ≤ 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

解题思路:

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

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
#include <ctime>
using namespace std;
#define maxn 100010
#define time_ printf("%f",double(clock())/CLOCKS_PER_SEC)
int vis[maxn];
vector<int> prime;
vector<int> fm;
int e_m[maxn];
int e_c[maxn];
int n,m;
void pre(){
int m=sqrt(maxn)+;
for(int i=;i<m;i++){
if(!vis[i]){
for(int j=i*i;j<maxn;j+=i){
vis[j]=;
}
}
}
for(int i=;i<maxn;i++)
if(!vis[i]) prime.push_back(i);
}
void cal_e(int m,int e_m[maxn],int d){
int t=m;
for(int i=;i<fm.size();i++){
while(t%fm[i]==){
e_m[i]+=d;
t/=fm[i];
}
if(t==)break;
}
}
bool judge(){
for(int i=;i<fm.size();i++)
if(e_m[i]>e_c[i]) return false;
return true;
}
int main(int argc, const char * argv[]) {
pre();
while(scanf("%d%d",&n,&m)==){
memset(e_m,,sizeof e_m);
memset(e_c,,sizeof e_c);
fm.clear();
cal_e(m,e_m,); vector<int> ans; int t=m;
int j=;
for(int i=;i<prime.size();i++){
if(t%prime[i]==){
fm.push_back(prime[i]);
while(t%prime[i]==){
e_m[j]++;
t/=prime[i];
}
j++;
}
if(t==)break;
}
if(t!=) {fm.push_back(t);e_m[j]=;}
for(int k=;k<n;k++){
cal_e(n-k,e_c,);
cal_e(k,e_c,-);
if(judge()){
ans.push_back(k);
}
}
printf("%d\n",(int)ans.size());
if(ans.size()>){
printf("%d",ans[]+);
for(int i=;i<ans.size();i++)
printf(" %d",ans[i]+);
}
//else printf("\n");
printf("\n");
//time_;
} return ;
}

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. replace all

    OPTION COPY OUTREC FINDREP=(INOUT=(X'0E',X'400E', X'0F',X'0F40'))

  2. SQL注入绕过的技巧总结

    sql注入在很早很早以前是很常见的一个漏洞.后来随着安全水平的提高,sql注入已经很少能够看到了.但是就在今天,还有很多网站带着sql注入漏洞在运行.稍微有点安全意识的朋友就应该懂得要做一下sql注入 ...

  3. WordPress使用自定义文章类型实现任意模板的方法和怎么做邮件回复

    主要就是使用了register_post_type 函数. 1.创建插件目录 新建一个文件夹用来存放插件文件,这里我就命名这个文件夹为myMood 2.创php代码文件 在刚才创建的文件夹里面新建一个 ...

  4. qt获取屏幕

    m_pDesktopWidget = QApplication::desktop(); // 屏体数量,包含扩展屏 int screenCount = m_pDesktopWidget->scr ...

  5. Thrift源码分析(一)-- 基本概念

    我所在的公司使用Thrift作为基础通信组件,相当一部分的RPC服务基于Thrift框架.公司的日UV在千万级别,Thrift很好地支持了高并发访问,并且Thrift相对简单地编程模型也提高了服务地开 ...

  6. nodeJs koa-generator脚手架

    koa-generator 脚手架 全局安装:cnpm install -g koa-generator 查看版本:koa2 --version 创建项目:koa2 project 默认的是用jade ...

  7. javascript如何将时间戳转为24小时制

    var now = new Date(parseInt(1446634507) * 1000);console.log(now.toLocaleString('chinese',{hour12:fal ...

  8. oracle-Mount

    执行nomount的所有工作,另外附加数据结构并与这些数据结构进行交互.这时,oracle从控制文件中获得信息. 可以执行的任务是: 执行数据库的完全恢复操作 重命名数据文件 改变数据库的归档状态. ...

  9. pl/sql基础知识—触发器

    n  触发器简单介绍 触发器是指隐含执行的存储过程,它不是由程序员或者是DBA来显式调用,而是因为某个操作引发执行的.当定义触发器时,必须要指定触法的事件和触发的操作,常用的触发事件包括insert, ...

  10. 【JZOJ4878】【NOIP2016提高A组集训第10场11.8】时空传送

    题目描述 经过旷久的战争,ZMiG和707逐渐培养出了深厚的感♂情.他们逃到了另一块大陆上,决定远离世间的纷争,幸福地生活在一起.钟情707的neither_nor决心要把他们拆散,他要动用手中最大杀 ...