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 ;
}
最新文章
- 原生javaScript中使用Ajax实现异步通信
- 配置org.springframework.scheduling.quartz.CronTriggerBean (转载)
- pushlet实现服务器端向客户端推送信息
- MySQL源码之Thread cache
- PHP Predefined Interfaces 预定义接口
- boxfilter 实现
- 2013Esri全球用户大会之解读Web GIS
- php调用webservice接口
- 支持向量机(五)SMO算法
- java中表示二进制、八进制、十进制、十六进制
- 登录ssh时Host key verification failed错误
- 开源框架.netCore DncZeus学习(三)增加一个菜单
- Ubuntu mysql数据库导入sql文件
- 【mysql】创建索引
- 小米正式开源 SQL 智能优化与改写工具 SOAR
- 奇怪吸引子---Arneodo
- HDU 4602 Partition (矩阵乘法)
- J2SE 8的Lambda --- 语法
- Java学习--Cookie 和session
- java的继承(编程思想)
热门文章
- vue单页应用前进刷新后退不刷新方案探讨
- 吴恩达机器学习笔记16-决策边界(decision boundary)
- PHP删除当前目录及其目录下的所有文件
- JS创建对象,数组,函数的三种方式
- [Swift]Xcode标记:MARK、TODO、FIXME
- mysql数据统计技巧备忘录
- lua模块demo(redis,http,mysql,cjson,本地缓存)
- 【雷神源码解析】无基础看懂AAC码流解析,看不懂你打我
- java的四种内部类详解
- 课程五(Sequence Models),第二 周(Natural Language Processing &; Word Embeddings) —— 0.Practice questions:Natural Language Processing &; Word Embeddings