POJ 2167 Irrelevant Elements 质因数分解
Irrelevant Elements
Description
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 contains n and m (1 <= n <= 100 000, 2 <= m <= 109).
Output
On the first line of the output 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 Source |
[Submit] [Go Back] [ problem_id=2167" style="text-decoration:none">Status
[Discuss]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn=100100; int n,m;
int pm[maxn],pr[maxn],pc[maxn],pn;
vector<int> ans; void f(int x,int d)
{
for(int i=0;i<pn;i++)
while(x%pm[i]==0)
{
pc[i]+=d;
x/=pm[i];
}
} bool check()
{
for(int i=0;i<pn;i++)
{
if(pc[i]<pr[i]) return false;
}
return true;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
ans.clear(); pn=0; /**********/
for(int i=2;i*i<=m;i++)
{
if(m%i==0)
{
pm[pn]=i;
while(m%i==0)
{
pr[pn]++;
m/=i;
}
pn++;
}
}
if(m!=1)
{
pm[pn]=m;
pr[pn++]=1;
}
/**********/
n--;
for(int i=1;i<=n;i++)
{
f(n-i+1,1);
f(i,-1);
if(check())
{
ans.push_back(i+1);
}
} printf("%d\n",ans.size());
sort(ans.begin(),ans.end());
for(int i=0,sz=ans.size();i<sz;i++)
{
printf("%d ",ans[i]);
}
putchar(10);
}
return 0;
}
POJ 2167 Irrelevant Elements 质因数分解的更多相关文章
- POJ 2429 long long 质因数分解
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16206 Accepted: ...
- poj 3421 X-factor Chains——质因数分解
题目:http://poj.org/problem?id=3421 记忆化搜索竟然水过去了.仔细一想时间可能有点不对,但还是水过去了. #include<iostream> #includ ...
- POJ 1845 Sumdiv#质因数分解+二分
题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...
- Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)
一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...
- POJ2167 Irrelevant Elements
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description Young cryp ...
- UVa 1635 - Irrelevant Elements-[分解质因数]
Young cryptoanalyst Georgie is investigating different schemes of generating random integer numbers ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- 求n!质因数分解之后素数a的个数
n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...
- AC日记——质因数分解 1.5 43
43:质因数分解 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n. 对于60% ...
随机推荐
- 第3课 把文件存入Git文档库
3-1 排除不需要加入文档库的文件 Git追踪文件的方式.Git会将文件和文件夹分成以下三类: 1. 被追踪的(tracked): 2. 忽略的(ignored): 3. 不被追踪的(u ...
- 缓存,队列(Redis,RabbitMQ)
Redis Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- hdu 4297
有两个基础需要掌握: RMQ,以及LCA. RMQ:dp[i][j]表示下标从i开始,长度为2^j的一段元素中的最值.则易得状态转移如下:dp[i][j]=max/min(dp[i][j-1],dp[ ...
- HDU 4474 Yet Another Multiple Problem BFS
题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0 1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...
- centos 修改网卡信息命令
vi /etc/sysconfig/network-scripts/ifcfg-eth0
- Java 介绍比较全面的一遍文章
Java简介 Java是由Sun Microsystems公司于1995年5月推出的Java程序设计语言(以下简称Java语言)和Java平台的总称.用Java实现的HotJava浏览器(支持Java ...
- 网络开发之使用Web Service和使用WCF服务
判断是否有可用网络连接可以通过NetworkInterface类中的GetIsNetworkAvailable来实现: bool networkIsAvailable = networkInterfa ...
- React Component(dva)
Stateless Functional Components(3种方式) class App extends React.Component function App() const App= Re ...
- SLAM: 图像角点检测的Fast算法(时间阈值实验)
作为角点检测的一种快速方法,FastCornerDetect算法比Harris方法.SIft方法都要快一些,应用于实时性要求较高的场合,可以直接应用于SLAM的随机匹配过程.算法来源于2006年的Ed ...
- (转)OpenLayers3基础教程——OL3 介绍interaction
http://blog.csdn.net/gisshixisheng/article/details/46808647 概述: 本节主要讲述OL3的交互操作interaction,重点介绍draw,s ...