题意:判断一个数是否是质数+分解质因数

sol:模板题

分解质因数用xudyh模板,注意factor返回的是无序的,factorG返回是从小到大的顺序(包括了1)

判断质数用kuangbin随机化模板

 #include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <list>
#include <cassert>
#include <complex>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
//#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define TWO(x) (1<<(x))
#define TWOL(x) (1ll<<(x))
#define clr(a) memset(a,0,sizeof(a))
#define POSIN(x,y) (0<=(x)&&(x)<n&&0<=(y)&&(y)<m)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long ll;
typedef long double LD;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<ll> VL;
typedef vector<PII> VPII;
typedef complex<double> CD;
const int inf=0x20202020;
const ll mod=;
const double eps=1e-; ll powmod(ll a,ll b) //return (a*b)%mod
{ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll powmod(ll a,ll b,ll mod) //return (a*b)%mod
{ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) //return gcd(a,b)
{ return b?gcd(b,a%b):a;}
// head namespace Factor {
const int N=;
ll C,fac[],n,mut,a[];
int T,cnt,i,l,prime[N],p[N],psize,_cnt;
ll _e[],_pr[];
vector<ll> d; inline ll mul(ll a,ll b,ll p) { //return (a*b)%p
if (p<=) return a*b%p;
else if (p<=1000000000000ll) return (((a*(b>>)%p)<<)+(a*(b&((<<)-))))%p;
else {
ll d=(ll)floor(a*(long double)b/p+0.5);
ll ret=(a*b-d*p)%p;
if (ret<) ret+=p;
return ret;
}
} void prime_table(){ //prime[1..tot]: prime[i]=ith prime
int i,j,tot,t1;
for (i=;i<=psize;i++) p[i]=i;
for (i=,tot=;i<=psize;i++){
if (p[i]==i) prime[++tot]=i;
for (j=;j<=tot && (t1=prime[j]*i)<=psize;j++){
p[t1]=prime[j];
if (i%prime[j]==) break;
}
}
} void init(int ps) { //initial
psize=ps;
prime_table();
} ll powl(ll a,ll n,ll p) { //return (a^n)%p
ll ans=;
for (;n;n>>=) {
if (n&) ans=mul(ans,a,p);
a=mul(a,a,p);
}
return ans;
} bool witness(ll a,ll n) {
int t=;
ll u=n-;
for (;~u&;u>>=) t++;
ll x=powl(a,u,n),_x=;
for (;t;t--) {
_x=mul(x,x,n);
if (_x== && x!= && x!=n-) return ;
x=_x;
}
return _x!=;
} bool miller(ll n) {
if (n<) return ;
if (n<=psize) return p[n]==n;
if (~n&) return ;
for (int j=;j<=;j++) if (witness(rand()%(n-)+,n)) return ;
return ;
} ll gcd(ll a,ll b) {
ll ret=;
while (a!=) {
if ((~a&) && (~b&)) ret<<=,a>>=,b>>=;
else if (~a&) a>>=; else if (~b&) b>>=;
else {
if (a<b) swap(a,b);
a-=b;
}
}
return ret*b;
} ll rho(ll n) {
for (;;) {
ll X=rand()%n,Y,Z,T=,*lY=a,*lX=lY;
int tmp=;
C=rand()%+;
X=mul(X,X,n)+C;*(lY++)=X;lX++;
Y=mul(X,X,n)+C;*(lY++)=Y;
for(;X!=Y;) {
ll t=X-Y+n;
Z=mul(T,t,n);
if(Z==) return gcd(T,n);
tmp--;
if (tmp==) {
tmp=;
Z=gcd(Z,n);
if (Z!= && Z!=n) return Z;
}
T=Z;
Y=*(lY++)=mul(Y,Y,n)+C;
Y=*(lY++)=mul(Y,Y,n)+C;
X=*(lX++);
}
}
} void _factor(ll n) {
for (int i=;i<cnt;i++) {
if (n%fac[i]==) n/=fac[i],fac[cnt++]=fac[i];}
if (n<=psize) {
for (;n!=;n/=p[n]) fac[cnt++]=p[n];
return;
}
if (miller(n)) fac[cnt++]=n;
else {
ll x=rho(n);
_factor(x);_factor(n/x);
}
} void dfs(ll x,int dep) {
if (dep==_cnt) d.push_back(x);
else {
dfs(x,dep+);
for (int i=;i<=_e[dep];i++) dfs(x*=_pr[dep],dep+);
}
} void norm() {
sort(fac,fac+cnt);
_cnt=;
rep(i,,cnt) if (i==||fac[i]!=fac[i-]) _pr[_cnt]=fac[i],_e[_cnt++]=;
else _e[_cnt-]++;
} vector<ll> getd() {
d.clear();
dfs(,);
return d;
} vector<ll> factor(ll n) { //return all factors of n cnt:the number of factors
cnt=;
_factor(n);
norm();
return getd();
} vector<PLL> factorG(ll n) {
cnt=;
_factor(n);
norm();
vector<PLL> d;
rep(i,,_cnt) d.push_back(make_pair(_pr[i],_e[i]));
return d;
} bool is_primitive(ll a,ll p) {
assert(miller(p));
vector<PLL> D=factorG(p-);
rep(i,,SZ(D)) if (powmod(a,(p-)/D[i].first,p)==) return ;
return ;
}
} /* *************************************************
* Miller_Rabin算法进行素数测试
*速度快,可以判断一个 < 2^63的数是不是素数
*
**************************************************/
const int S = ; //随机算法判定次数,一般8~10就够了
//计算ret = (a*b)%c
long long mult_mod(long long a,long long b,long long c)
{
a %= c;
b %= c;
long long ret = ;
long long tmp = a;
while(b)
{
if(b & )
{
ret += tmp;
if(ret > c)ret -= c;//直接取模慢很多
}
tmp <<= ;
if(tmp > c)tmp -= c;
b >>= ;
}
return ret;
}
//计算 ret = (a^n)%mod
long long pow_mod(long long a,long long n,long long mod)
{
long long ret = ;
long long temp = a%mod;
while(n)
{
if(n & )ret = mult_mod(ret,temp,mod);
temp = mult_mod(temp,temp,mod);
n >>= ;
}
return ret;
}
//通过 a^(n-1)=1(mod n)来判断n是不是素数
// n-1 = x*2^t中间使用二次判断
//是合数返回true,不一定是合数返回false
bool check(long long a,long long n,long long x,long long t)
{
long long ret = pow_mod(a,x,n);
long long last = ret;
for(int i = ; i <= t; i++)
{
ret = mult_mod(ret,ret,n);
if(ret == && last != && last != n-)return true;//合数
last = ret;
}
if(ret != )return true;
else return false;
}
//**************************************************
// Miller_Rabin算法
//是素数返回true,(可能是伪素数)
//不是素数返回false
//**************************************************
bool Miller_Rabin(long long n)
{
if( n < )return false;
if( n == )return true;
if( (n&) == )return false;//偶数
long long x = n - ;
long long t = ;
while( (x&)== )
{
x >>= ;
t++;
}
rand();/* *************** */
for(int i = ; i < S; i++)
{
long long a = rand()%(n-) + ;
if( check(a,n,x,t) )
return false;
}
return true;
} ll x,y,k,n;
int _;
int main() {
Factor::init();
cin>>_;
while (_--)
{
cin>>n;
bool ok=Miller_Rabin(n);
if (n==)
{
cout<<<<endl;
continue;
}
else if (n==)
{
cout<<"Prime"<<endl;
continue;
}
if (ok) cout<<"Prime"<<endl;
else
{
vector <PLL> p=Factor::factorG(n);
//for (vector<ll>::iterator i=p.begin();i!=p.end();i++)
// cout<<*i<<" ";
vector<PLL>::iterator i=p.begin();
//printf("%d\n",*i);
cout<<i->first<<endl;
}
}
}

明天就要去打铁了orz

poj1811 数论的更多相关文章

  1. 数论知识总结——史诗大作(这是一个flag)

    1.快速幂 计算a^b的快速算法,例如,3^5,我们把5写成二进制101,3^5=3^1*1+3^2*2+3^4*1 ll fast(ll a,ll b){ll ans=;,a=mul(a,a)))a ...

  2. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  3. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  4. 数论学习笔记之解线性方程 a*x + b*y = gcd(a,b)

    ~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...

  5. hdu 1299 Diophantus of Alexandria (数论)

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  6. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  7. bzoj2219: 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  8. hdu5072 Coprime (2014鞍山区域赛C题)(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...

  9. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

随机推荐

  1. 目录结构-内置(AJAX)帮助文档

    Discuz common.js 内置(AJAX)函数帮助文档 作者:cr180 / 整理日期:1970-01-01 / 个人站点:www.cr180.com / Discuz超级管家 showMen ...

  2. 我开源了一个ios应用,你们拿去随便玩

    今天开源一个ios应用,自己写的,你们拿去随便玩.地址是: https://github.com/huijimuhe/prankPro 光拿来玩不理清个来龙去脉玩的也不开心是吧,那我就给你们摆摆来龙去 ...

  3. [已开源/文章教程]独立开发 一个社交 APP 的源码/架构分享 (已上架)

    0x00 背景 真不是和被推荐了2天的博客园一位大神较真,从他那篇文章的索引式文章内容也学习到了很多东西,看评论区那么多对社交APP源码有兴趣的,正巧我上周把我的一个社交APP开源了,包括androi ...

  4. 阿里云Ubuntu 14.04 + Nginx + let's encrypt 搭建https访问

    参考页面: https://certbot.eff.org/#ubuntutrusty-nginx http://bbs.qcloud.com/thread-12059-1-1.html http:/ ...

  5. 遇到技嘉 Gigabyte UEFI DualBIOS问题

    我的板子是技嘉H87-D3H二手板子,用了差不多三年左右,一直感觉不错,承担着高强度的开发任务. 问题描述: 无法开机,按下电源后,主板通电状态灯亮,CPU和显卡风扇转动,显示器显示下图: 2至3秒后 ...

  6. [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)

    题目:http://codeforces.com/problemset/problem/519/E 题意:给你一个n个点的树,有m个询问(x,y),对于每个询问回答树上有多少个点和x,y点的距离相等 ...

  7. 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure

    yu Code 15 Comments  机器学习(ML),自然语言处理(NLP),信息检索(IR)等领域,评估(Evaluation)是一个必要的 工作,而其评价指标往往有如下几点:准确率(Accu ...

  8. MEF

    详情见链接:http://www.cnblogs.com/comsokey/p/MEF1.html#top

  9. CSS选择器优先级 CSS权值

    计算指定选择器的优先级:重新认识CSS的权重 标签的权值为 0,0,0,1 类的权值为 0,0,1,0 属性选择的权值为 0,0,1,1  ID的权值为 0,1,0,0 important的权值为最高 ...

  10. 一个奇怪的MySQL错误返回

    (0, '') python查询结果,乍一看还以为是下标出错了 一番调试,发现是因为 mysql数据库连接关闭上出了错. 结尾 在对数据库进行操作的时候要注意连接的开启和关闭动作规范