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

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. SharePoint Foundation 2013 with SP1

    终于支持在 Windows Server 2012 R2 上安装了. 下载 另外,还有一个针对SharePoint Foundation 2013的重要更新.可以在安装SP1之前或之后安装. Micr ...

  2. 获取iTextSharp 的image 报错

    获取itextsharp类库的image对象的时候报错  outofmemory  .经过艰苦的测试发现jpeg类型是可行的的 iTextSharp.text.Image je = iTextShar ...

  3. ubuntu14.04禁用guest用户登录

    打开终端(ctrl+alt+t) sudo  echo -e "[SeatDefaults]\nallow-guest=false"  > /usr/share/lightd ...

  4. nodejs学习之实现简易路由

    此前实现了个数据转发功能,但是要建本地服务器,还需要一个简易的路由功能.因为只是用于本地服务器用于自己测试用,所以不需要太完善的路由功能,所以也就不去使用express框架,而是自己实现一个简易路由, ...

  5. Google最新截屏案例详解

    Google从Android 5.0 开始,给出了截屏案例ScreenCapture,在同版本的examples的Media类别中可以找到.给需要开发手机或平板截屏应用的小伙伴提供了非常有意义的参考资 ...

  6. 微软分布式云计算框架Orleans(2):容灾与集群(1)

    在上一篇:微软分布式云计算框架Orleans(1):Hello World,我们大概了解了Orleans如何运用,当然上一篇的例子可以说是简单且无效的,因为用了Orleans不可能只写一个Hello ...

  7. [BZOJ1016][JSOI2008]最小生成树计数(结论题)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1016 分析: 首先有个性质:如果边集E.E'都可以表示一个图G的最小生成树(当然E和E ...

  8. go linux 学习记录

    1 yum install mercurial 安装mercurial包 2 yum install git 安装git包 3 yum install gcc 安装gcc 4  然后就可以下载gola ...

  9. 东大OJ-1544: GG的战争法则

    题目描述 你在桥上看风景 看风景的人在楼上看你 明月装饰了你的窗子 你装饰了我的梦 这是GG在长坂坡发出的感叹. 三年前GG莫名的穿越到了三国时期,在这三年里他看尽了各种杀戮,心里早已麻木.GG他渴望 ...

  10. C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据

    转载:http://www.cnblogs.com/kivenhou/archive/2013/02/22/2921954.html 操作List前请设置SPWeb的allowUnsafeUpdate ...