HDU 4344 大数分解大素数判定
这里贴个模板吧。反正是不太理解
看原题就可以理解用法!!
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
#define Times 10
typedef __int64 LL;
map<LL,int>m;
LL Random(LL n)
{
return ((double)rand()/RAND_MAX*n+0.5);
}
LL multi(LL a,LL b,LL mod)
{
LL ans=0;
while(b)
{
if(b&1)
{
b--;
ans=(ans+a)%mod;
}
else
{
b/=2;
a=(a+a)%mod;
}
}
return ans;
}
LL Pow(LL a,LL b,LL mod)
{
LL ans=1;
while(b)
{
if(b&1)
{
b--;
ans=multi(ans,a,mod);
}
else
{
b/=2;
a=multi(a,a,mod);
}
}
return ans;
}
bool witness(LL a,LL n)
{
LL d=n-1;
while(!(d&1))
d>>=1;
LL t=Pow(a,d,n);
while(d!=n-1 && t!=1 && t!=n-1)
{
t=multi(t,t,n);
d<<=1;
}
return t==n-1 || d&1;
}
bool miller_rabin(LL n)
{
if(n==2)
return true;
if(n<2||!(n&1))
return false;
for(int i=1;i<=Times;i++)
{
LL a=Random(n-2)+1;
if(!witness(a,n))
return false;
}
return true;
}
LL gcd(LL a,LL b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
LL pollard_rho(LL n,LL c)
{
LL x,y,d,i=1,k=2;
x=Random(n-1)+1;
y=x;
while(1)
{
i++;
x=(multi(x,x,n)+c)%n;
d=gcd(y-x,n);
if(1<d&&d<n)
return d;
if(y==x)
return n;
if(i==k)
{
y=x;
k<<=1;
}
}
}
void find(LL n,LL c)
{
if(n==1)
return ;
if(miller_rabin(n))
{
m[n]++;
return ;
}
LL p=n;
while(p>=n)
p=pollard_rho(p,c--);
find(p,c);
find(n/p,c);
}
int main()
{
int t;
cin>>t;
while(t--)
{
LL n;
cin>>n;
m.clear();
find(n,2013724);
if(m.size()==1)
cout<<1<<" "<<n/m.begin()->first<<endl;
else
{
LL ans=0;
map<LL,int>::iterator it=m.begin();
for(;it!=m.end();it++)
ans+=Pow(it->first,it->second,n);
cout<<m.size()<<" "<<ans<<endl;
}
}
return 0;
}
HDU 4344 大数分解大素数判定的更多相关文章
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法!
公钥密码之RSA密码算法大素数判定:Miller-Rabin判定法! 先存档再说,以后实验报告还得打印上交. Miller-Rabin大素数判定对于学算法的人来讲不是什么难事,主要了解其原理. 先来灌 ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- Miller Robin大素数判定
Miller Robin算法 当要判断的数过大,以至于根n的算法不可行时,可以采用这种方法来判定素数. 用于判断大于2的奇数(2和偶数需要手动判断),是概率意义上的判定,因此需要做多次来减少出错概率. ...
- HDU 5901 Count primes 大素数计数
题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...
- CSU 1552: Friends 图论匹配+超级大素数判定
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 163 Solved: 34[Submit][Status][Web Boa ...
- HDU 4344 随机法判素数(费马小定理
#include <cstdio> #include <ctime> #include <cmath> #include <algorithm> usi ...
- HDU 4910 Problem about GCD 找规律+大素数判断+分解因子
Problem about GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- poj 1811 随机素数和大数分解(模板)
Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...
随机推荐
- #Spring实战第二章学习笔记————装配Bean
Spring实战第二章学习笔记----装配Bean 创建应用对象之间协作关系的行为通常称为装配(wiring).这也是依赖注入(DI)的本质. Spring配置的可选方案 当描述bean如何被装配时, ...
- 【Dataset】Goodbooks-10k: 图书推荐数据
当前推荐领域一些公开的据集都是关于电影和音乐的(比如Netflix.Movielens等),没有关于图书推荐的数据.本文将要介绍的就是一份用于图书推荐的数据集,该数据来源于goodreads网站,包含 ...
- python安装Django
现在有很多建站系统,很多都是基于php的,比如WordPress. 而Django 是老牌基于Python的CMS框架了,一直听说很强大,甚至曾经很红的Ruby On Rails都参考了它的很多概念, ...
- Python攻击
python DOS攻击 2版本 #!/usr/bin/env python import socket import time import threading #Pressure Test,d ...
- ng2 搭建本地开发环境
git clone https://github.com/angular/quickstart.git quickstart cd quickstart npm install npm start h ...
- DPDK Qos之报文处理流水线
原创翻译,转载请注明出处. 下面是一个支持Qos的复杂报文处理流水线的图: 流水线是通过DPDP可重用的软件库构建出来的.在流水线里实现QoS主要是如下模块:policer,dropper,shced ...
- element-ui的el-tabel组件怎么使用type=“expand”实现表格嵌套并且在子表格没有数据的时候隐藏展开按钮
效果如下: 试过很多种办法,思路都在怎么控制<el-table-column type="expand">里面的type上,比如使用v-show等等,但是发现,要不就是 ...
- Hiberante可配置参数
###################### ### Query Language ### ###################### ## define query language consta ...
- 【AtCoder ARC076】F Exhausted? 霍尔定理+线段树
题意 N个人抢M个椅子,M个椅子排成一排 ,第i个人只能坐[1,Li]∪[Ri,M],问最多能坐多少人 $i$人连边向可以坐的椅子构成二分图,题意即是求二分图最大完美匹配,由霍尔定理,答案为$max( ...
- AGC017C Snuke and Spells(巧妙的线段覆盖模型)
题目大意: 给出n个球,每个球上都有数字,然后每次都进行如下操作 如果当前的球总共有k个,那么就把球上数字为k的所有球都消除掉 注意到,并不是每种情况都可以全部消光,所以你可以选择若干球,把它们标号改 ...