ACM ICPC 2017 Warmup Contest 9 I
I. Older Brother
Your older brother is an amateur mathematician with lots of experience. However, his memory is very bad. He recently got interested in linear algebra over finite fields, but he does not remember exactly which finite fields exist. For you, this is an easy question: a finite field of order q exists if and only if q is a prime power, that is, q = p^kpk holds for some prime number pand some integer k ≥ 1. Furthermore, in that case the field is unique (up to isomorphism).
The conversation with your brother went something like this:

Input
The input consists of one integer q, satisfying 1 ≤ q ≤ 10^9109.
Output
Output “yes” if there exists a finite field of order q. Otherwise, output “no”.
样例输入1
1
样例输出1
no
样例输入2
37
样例输出2
yes
样例输入3
65536
样例输出3
yes
题目来源
ACM ICPC 2017 Warmup Contest 9
题意:问一个数n是否是一个素数p的k次方
思路:用Pollard_rho分解质因数,看一看所有的质因子是否相等。
//2017-10-24
#include <cstdlib>
#include <iostream>
#include <ctime> typedef long long LL;
#define MAXN 10000 using namespace std; LL factor[MAXN];
int tot;
const int S=; LL muti_mod(LL a,LL b,LL c){ //返回(a*b) mod c,a,b,c<2^63
a%=c;
b%=c;
LL ret=;
while (b){
if (b&){
ret+=a;
if (ret>=c) ret-=c;
}
a<<=;
if (a>=c) a-=c;
b>>=;
}
return ret;
} LL pow_mod(LL x,LL n,LL mod){ //返回x^n mod c ,非递归版
if (n==) return x%mod;
int bit[],k=;
while (n){
bit[k++]=n&;
n>>=;
}
LL ret=;
for (k=k-;k>=;k--){
ret=muti_mod(ret,ret,mod);
if (bit[k]==) ret=muti_mod(ret,x,mod);
}
return ret;
} bool check(LL a,LL n,LL x,LL t){ //以a为基,n-1=x*2^t,检验n是不是合数
LL ret=pow_mod(a,x,n),last=ret;
for (int i=;i<=t;i++){
ret=muti_mod(ret,ret,n);
if (ret== && last!= && last!=n-) return ;
last=ret;
}
if (ret!=) return ;
return ;
} bool Miller_Rabin(LL n){
LL x=n-,t=;
while ((x&)==) x>>=,t++;
bool flag=;
if (t>= && (x&)==){
for (int k=;k<S;k++){
LL a=rand()%(n-)+;
if (check(a,n,x,t)) {flag=;break;}
flag=;
}
}
if (!flag || n==) return ;
return ;
} LL gcd(LL a,LL b){
if (a==) return ;
if (a<) return gcd(-a,b);
while (b){
LL t=a%b; a=b; b=t;
}
return a;
} //找出任意质因数
LL Pollard_rho(LL x,LL c){
LL i=,x0=rand()%x,y=x0,k=;
while (){
i++;
x0=(muti_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if (d!= && d!=x){
return d;
}
if (y==x0) return x;
if (i==k){
y=x0;
k+=k;
}
}
} //递归进行质因数分解N
void findfac(LL n){
if (!Miller_Rabin(n)){
factor[tot++] = n;
return;
}
LL p=n;
while (p>=n) p=Pollard_rho(p,rand() % (n-) +);
findfac(p);
findfac(n/p);
} int main(){
int n;
while(cin>>n){
if(n == ){
cout<<"no"<<endl;
continue;
}
tot = ;
findfac(n);
bool ok = ;
for(int i = ; i < tot; i++)
if(factor[i] != factor[i-]){
ok = ;
break;
}
if(ok)cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return ;
}
ACM ICPC 2017 Warmup Contest 9 I的更多相关文章
- ACM ICPC 2017 Warmup Contest 9 L
L. Sticky Situation While on summer camp, you are playing a game of hide-and-seek in the forest. You ...
- ACM ICPC 2017 Warmup Contest 1 D
Daydreaming Stockbroker Gina Reed, the famous stockbroker, is having a slow day at work, and between ...
- 训练报告 (2014-2015) 2014, Samara SAU ACM ICPC Quarterfinal Qualification Contest
Solved A Gym 100488A Yet Another Goat in the Garden B Gym 100488B Impossible to Guess Solved C Gym ...
- 2015-2016 ACM ICPC Baltic Selection Contest
这是上礼拜三的训练赛,以前做过一次,这次仅剩B题没补.题目链接:https://vjudge.net/contest/153192#overview. A题,水题. C题,树形DP,其实是一个贪心问题 ...
- 2015-2016 ACM ICPC Baltic Selection Contest D - Journey(广搜)
- 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest
2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...
- hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
- hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...
随机推荐
- docker发布netcore网站-零散记录
安装docker 安装Linux,使用云服务器或者虚拟机 参考:http://www.cnblogs.com/wangjieguang/p/hyper-v-ubuntu.html 系统更新 sudo ...
- Android 流媒体技术见解
一.技术关键点 1. 音频采集:设置合理的采样参数,包括采样率.通道数.采样深度. 2. 视频采集:手机摄像头采集输出的格式(NV12/NV21/YV12),要注意相互间的转换,以及采集出来图像的方向 ...
- 项目笔记:2017年(SSM架构)
一.第一部分 前后端分离后的测试工具的使用(Postman): svn先更新再提交,冲突就把自己占位的地方让出,再提交: maven项目也可以用tomcat直接启动: 在mybatis.xml文件中, ...
- exec 命令简单用法 和 find 搭配用法示例
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. 看例子: find ./ -name "*.tx ...
- Scala - 快速学习09 - 函数式编程:一些操作
1- 集合类(collection) 系统地区分了可变的和不可变的集合. scala.collection包中所有的集合类 可变集合(Mutable) 顾名思义,意味着可以修改,移除或者添加一个元素. ...
- golang IO streaming
IO Streaming Streaming IO in Go,引用此文,略有修改 io.Reader和io.Writer io.Reader接口定义了从传输缓存读取数据 type Reader in ...
- BeautifuSoup的使用
BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单.
- C# 在窗体上可拖动控件
最近做了一个标签打印配置功能,需要根据客户需求自定义标签格式.显示内容,这时就用到了后台生成控件,并且其控件可在窗口中进行拖动,这里仅为记录一下实现过程,方便以后使用. 结果图: 源码: using ...
- CentOS 7.4 安装 K8S v1.11.0 集群所遇到的问题
0.引言 最近打算将现有项目的 Docker 部署到阿里云上面,但是之前是单机部署,现在阿里云上面有 3 台机器,所以想做一个 Docker 集群.之前考虑是用 Docker Swarm 来做这个事情 ...
- vue 关于图片路径的问题
在vue 中,当我们想加载assets中的图片,本人按照多年的开发经验会这样写,那是没问题的 <img src="../assets.1.jpg"/> 如果我要用v-b ...