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. 微服务开源生态报告 No.6

    「微服务开源生态报告」,汇集各个开源项目近期的社区动态,帮助开发者们更高效的了解到各开源项目的最新进展. 社区动态包括,但不限于:版本发布.人员动态.项目动态和规划.培训和活动. 非常欢迎国内其他微服 ...

  2. Android开源实战:使用MVP+Retrofit开发一款文字阅读APP

    文字控 使用MVP+Retrofit开发的一款文艺APP,它是一个非常优美的文字阅读应用,界面基本上符合material design设计规范. 在该项目中,我采用的是MVP架构,该架构目前在Andr ...

  3. 【JZOJ4922】【NOIP2017提高组模拟12.17】环

    题目描述 小A有一个环,环上有n个正整数.他有特殊的能力,能将环切成k段,每段包含一个或者多个数字.对于一个切分方案,小A将以如下方式计算优美程度: 首先对于每一段,求出他们的数字和.然后对于每段的和 ...

  4. cocos2dX 之CCAnimation/CCAnimate

    我们今天来学习cocos2dX里面的动画的制作, 有人说, 不是前面CCAction已经学过了吗? 怎么还要学, CCAction是动作, 而我们今天要学的是动画哦, 是让一个东西动起来哦, 好了进入 ...

  5. 通过DataWorks数据集成归档日志服务数据至MaxCompute进行离线分析

    通过DataWorks归档日志服务数据至MaxCompute 官方指导文档:https://help.aliyun.com/document_detail/68322.html但是会遇到大家在分区上或 ...

  6. IDEA入门(1)--lombok和Junit generator2插件的运用

    前言 最近在慕课网看到了一些视频,准备从0开始做一个电商网站.视频中的大牛用的java的IDE都是IDEA,让我很纠结.从as到MyEclipse,好不容易稍微熟悉了一下MyEclipse的基本操作, ...

  7. [linux]jenkins迁移 标签: linux服务器 2016-08-28 21:29 988人阅读 评论(20)

    我们的测试的jenkins和开发的jenkins 是分开的两个jenkins,然后测试的jenkins很久没有用,我们打算把主节点搬到另外一条服务器上面,然后出了一系列的问题,如下: 一.安装jenk ...

  8. 【NS2】使用SourceInsight阅读NS源代码全攻略(转载)

    NS的源码底层是C++,采用了C++/Tcl分裂对象模型,架构完善,堪称OOP编程的典范.但是NS源码体系庞大,源文件有2千多个,阅读起来不是特别方便,我推荐使用SourceInsight3.5.具体 ...

  9. 如何在WPF控件上应用简单的褪色透明效果?

    原文 https://dailydotnettips.com/how-to-create-simple-faded-transparent-controls-in-wpf/ 使用OpacityMask ...

  10. 13 -1 BOM和定时器

    一 BOM JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象模型,操作网页上的元 ...