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. WCF ChannelFactory

    public static class WcfExtensions{    public static void Using<T>(this T client, Action<T&g ...

  2. webpack配置vue项目

    npm init 安装webpack,webpack-cli 新建src目录,在src目录下新建main.js

  3. closest和parents方法区别

    今天第一次看到closest方法,以前也从来没用过. 该方法从元素本身开始往上查找,返回最近的匹配的祖先元素. 1.closest查找开始于自身,parents开始于元素父级 2.closest向上查 ...

  4. RedHat6.2离线安装vncserver

    准备安装包 tigervnc-server-1.1.0-5.el6_4.1.x86_64.rpm pixman-0.32.8-1.el6.x86_64.rpm pixman-devel-0.32.8- ...

  5. Kubernetes排错:用容器的元数据提供新思路

    在这篇文章中,让我们讨论一下Kubernetes中的元数据(Metadata),以及如何利用它来监控系统的性能. 元数据(Metadata) 是一个较为高大上的词.它的含义是"用来描述其他数 ...

  6. React Native错误汇总(持续更新)

    错误1 Element type is invalid-: 错误描述: Element type is invalid: expected a String(for built-in componen ...

  7. 捕捉WPF应用程序中XAML代码解析异常

    原文:捕捉WPF应用程序中XAML代码解析异常 由于WPF应用程序中XAML代码在很多时候是运行时加载处理的.比如DynamicResource,但是在编译或者运行的过程中,编写的XAML代码很可能有 ...

  8. winfrom 中 label 文字随着窗体大小变化

    在进行winfrom 开发过程中,窗体中的文字需要随着窗体大小变化,否则会影响窗体的美观和客户的体验. 之前曾经试过几种方法效果都不满意,例如将label的Dock 属性设置为fill.这样的设置对解 ...

  9. PHP笔试题(11道题)详解

    题目一 <?php echo -10%3; ?> 答案:-1. 考查:优先级. 因为-的优先级比%求余的优先级低, 也就是-(10%3). 2 题目二: print (int)pow(2, ...

  10. composer基本使用

    一.Composer的安装 1.下载Composer 2.Composer安装 1).Composer安装前请确保已经安装了php:打开命令行窗口输入php -v可以查看php的当前版本号. 3.局部 ...