UVa 1635 - Irrelevant Elements-[分解质因数]
- 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-[分解质因数]的更多相关文章
- UVA 1635 Irrelevant Elements
https://vjudge.net/problem/UVA-1635 题意:n个数,每相邻两个求和,最后变成1个数,问这个数除m的余数与第几个数无关 n个数使用次数分别为C(n-1,i) i∈[0, ...
- UVa 1635 - Irrelevant Elements(二项式系数 + 唯一分解定理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Irrelevant Elements UVA - 1635 二项式定理+组合数公式+素数筛+唯一分解定理
/** 题目:Irrelevant Elements UVA - 1635 链接:https://vjudge.net/problem/UVA-1635 题意:給定n,m;題意抽象成(a+b)^(n- ...
- UVa 1635 (唯一分解定理) Irrelevant Elements
经过紫书的分析,已经将问题转化为求组合数C(n-1, 0)~C(n-1, n-1)中能够被m整除的个数,并输出编号(这n个数的编号从1开始) 首先将m分解质因数,然后记录下每个质因子对应的指数. 由组 ...
- POJ 2167 Irrelevant Elements 质因数分解
Irrelevant Elements Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2231 Accepted: 55 ...
- UVa 10622 (gcd 分解质因数) Perfect P-th Powers
题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...
- POJ2167 Irrelevant Elements
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description Young cryp ...
- uva10791 uva10780(分解质因数)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- java分解质因数
package test; import java.util.Scanner; public class Test19 { /** * 分析:对n进行分解质因数,应先找到一个最小的质数k * 最小 ...
随机推荐
- Leetcode2.Add Two Numbers两数相加
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(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 ...
- 10分钟学会Python
#1. 语法 Python中没有强制的语句终止字符,代码块是通过缩进来指示的.缩进表示一个代码块的开始,逆缩进则表示一个代码块的结束.一般用4个空格来表示缩进. 声明以冒号(:)字符结束,并且开启一个 ...
- 阿里OSS-OSSFS
简介 OSSFS就以把OSS作为文件系统的一部分,能让你在linux系统中把OSS bucket挂载到本地文件系统中,实现数据的共享. 主要功能 ossfs 基于s3fs 构建,具有s3fs 的全部功 ...
- Servlet小结(转载)
http://www.iteye.com/topic/766418 1 .首先,什么是Servlet? Servlet是一个Java编写的程序,此程序是在服务器端运行的,是按照Servl ...
- Directx11教程(7) 画一个颜色立方体
原文:Directx11教程(7) 画一个颜色立方体 前面教程我们通过D3D11画了一个三角形,本章我们将画一个颜色立方体,它的立体感更强.主要的变动是ModelClass类,在Model ...
- react-jd-index
看见一些代码的产物,会觉得非常的漂亮~感谢无私开源的程序员们~你们是最可爱的人儿~~ //index.jsx require('./app/lib/common.css'); import React ...
- 微信小程序云数据库——where查询和doc查询区别
用法 条件查询where 我们也可以一次性获取多条记录.通过调用集合上的 where 方法可以指定查询条件,再调用 get 方法即可只返回满足指定查询条件的记录,比如获取用户的所有未完成的待办事项,用 ...
- 阿里云OSS同城冗余存储正式商业化,提供云上同城容灾能力
近日,阿里云正式发布OSS同城冗余存储产品.这是国内目前提供同城多AZ冗余部署能力覆盖最广的云上对象存储产品,可以实现云存储的同城双活,满足企业级客户对于“发生机房级灾难事件时数据不丢失,业务不中断” ...
- Python基础:03序列:字符串、列表和元组
一:序列 1:连接操作符(+) 这个操作符允许把一个序列和另一个相同类型的序列做连接,生成新的序列.语法如下:sequence1 + sequence2 该表达式的结果是一个包含sequence1和s ...