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 ...
随机推荐
- C/C++中宏定义#pragma once与 #ifndef的区别
为了避免同一个文件被include多次,我们可以通过以下两种方式来进行宏定义: 1. #ifndef方式2. #pragma once方式 在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两 ...
- FFmpeg Android 学习(一):Android 如何调用 FFMPEG 编辑音视频
一.概述 在Android开发中,我们对一些音视频的处理比较无力,特别是编辑音视频这部分.而且在Android上对视频编辑方面,几乎没有任何API做支持,MediaCodec(硬编码)也没有做支持.那 ...
- 为什么推荐前端使用Vue.js
MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...
- 【面试篇】寒冬求职季之你必须要懂的原生JS(中)
互联网寒冬之际,各大公司都缩减了HC,甚至是采取了“裁员”措施,在这样的大环境之下,想要获得一份更好的工作,必然需要付出更多的努力. 一年前,也许你搞清楚闭包,this,原型链,就能获得认可.但是现在 ...
- 解决SVN不显示绿色图标问题
今天是上班的第50天,发现项目上的svn绿色图标没有了,于是上网查了一下,然后很简单的就找到了解决办法: 修改注册表 Windows Explorer Shell支持Overlay Icon最多15个 ...
- python不同开根号速度对比
import time import math import numpy as np def timeit1(): s = time.time() for i in range(750000): z= ...
- 微信小程序-canvas绘制文字实现自动换行
在使用微信小程序canvas绘制文字时,时常会遇到这样的问题:因为canvasContext.fillText参数为 我们只能设置文本的最大宽度,这就产生一定的了问题.如果我们绘制的文本长度不确定或者 ...
- [ Talk is Cheap Show me the CODE ] : jQuery Mobile页面布局
当我们专注地研究人类生活的空虚,并考虑荣华富贵空幻无常时,也许我们正在阿谀逢迎自己懒惰的天性. Written In The Font 为了app的手机端,我选择了 jQuery Mobile ,学 ...
- MySQL 锁信息和事务
1 锁概念 1.1 什么是锁 锁是数据库系统区别于文件系统的一个关键特性.数据库系统使用锁是为了支持对共享资源进行并发访问,提供数据的完整性和一致性.例如:操作缓冲池中的LRU列表,删除.添加.移动L ...
- Android View 的事件分发原理解析
作为一名 Android 开发者,每天接触最多的就是 View 了.Android View 虽然不是四大组件,但其并不比四大组件的地位低.而 View 的核心知识点事件分发机制则是不少刚入门同学的拦 ...