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. prestashop 首页轮播幻灯片图片修改

    后台 -> Modules -> Modules 搜索 home(中文则搜幻灯片)

  2. 基于matlab的傅里叶变换

    原文出处https://blog.csdn.net/qq_37366291/article/details/79832886 例子1 作用:使用傅里叶变换找出隐藏在噪声中的信号的频率成分.(指定信号的 ...

  3. KiCad 5.1.0 镜像圆弧后错位问题

    KiCad 5.1.0 镜像圆弧后错位问题 看官方回复这个问题将在 5.1.3 进行修复,因为这段时间在举行 KiCon 活动. 看到这个问题并不是非常严重,不是致命的,所以已经从 5.1.0 跳到 ...

  4. Java练习 SDUT-2253_分数加减法

    分数加减法 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写一个C程序,实现两个分数的加减法 Input 输入包含多 ...

  5. javascript —— 禁止通过 Enter 键提交表单

    $('btn').on('keydown', function () { return false; })

  6. Java练习 SDUT-2174_回文时间

    回文时间 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description HH 每天都会熬夜写代码,然后很晚才睡觉,但是每天早晨六点多必 ...

  7. ROS 设置串口USB软连接

    原创:未经同意,请勿转载 我们在windows 通过USB连接串口,在设备串口中可以观测到COM0或者COMx.当我们插入不同的USB口时会显示不同的COM. 在UBUNTU下,ROS下接收串口信息时 ...

  8. 8.5打包libgdx为一个桌面程序(jar包)

    简陋的地图编辑终于做好了,于是要开始制作地图了,想导出为一个windows下可用的程序,让熟人代做地图,然后找人问了下打包流程,其实跟普通java打包为jar没什么区别,记录如下: 导出类型选第三个 ...

  9. Python多版本pip安装库的问题

    引 机器上总是会有Python2.7的版本和Python3.x的版本,今天接触到一台服务器上面有Python2.7和Python3.4,想在Python3.4下安装一个TensorFlow,但不管怎么 ...

  10. oracle中的闪回

    项目中运用: 首先说明:闪回方法有一个前提,就是需要尽早的发现问题,果断的采取行动.若误操作的记录已经在UNDO表空间中被清除,则此方法就不可行了,需要另寻他法. 例如: SELECT * FROM ...