HDU 4344 随机法判素数(费马小定理
#include <cstdio>
#include <ctime>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 108;
const int S = 10; ll mult_mod(ll a, ll b, ll c) {
a %= c;
b %= c;
ll ret = 0;
while(b) {
if(b&1) ret = (ret + a) % c;
a = (a + a) % c;
b >>= 1;
}
return ret;
}
ll pow_mod(ll x, ll n, ll mod) {
if(n == 1) return x % mod;
x %= mod;
ll tmp = x, ret = 1;
while(n > 0){
if(n&1) ret = mult_mod(ret, tmp, mod);
tmp = mult_mod(tmp, tmp, mod);
n >>= 1;
}
return ret;
} bool check(ll a, ll n, ll x, ll t) {
ll ret = pow_mod(a, x, n);
ll last = ret;
for(int i = 1; i <= t; i ++) {
ret = mult_mod(ret, ret, n);
if(ret == 1 && last != 1 && last != n-1) return true;
last = ret;
}
if(ret != 1) return true;
return false; }
bool Miller_Rabin(ll n) {
if(n < 2) return false;
if(n==2||n==3||n==5||n==7) return true;
if(n%2==0||n%3==0||n%5==0||n%7==0) return false; ll x = n - 1, t = 0;
while((x&1)==0) {
x >>= 1;
t ++;
}
for(int i = 0; i < S; i ++) {
ll a = rand()%(n-1) +1;
if(check(a, n, x, t)) return false;
}
return true;
}
ll gcd(ll a, ll b) {
if(a < 0) return gcd(-a, b);
if(b < 0) return gcd(a, -b);
while(a > 0 && b > 0) {
if(a > b) a %= b;
else b %= a;
}
return a+b;
}
ll Pollard_rho(ll x, ll c) {
ll i = 1, k = 2;
ll x0 = ((rand() % x) + x) % x;
ll y = x0;
while(true) {
i ++;
x0 = (mult_mod(x0, x0, x) + c)%x;
ll d = gcd(y-x0, x);
if(d != 1 && d != x) return d;
if(y == x0) return x;
if(i == k) {
y = x0;
k += k;
}
} }
ll P[N], tot; void findfac(ll n) {
if(Miller_Rabin(n)) {
P[tot++] = n;
return ;
}
ll p = n;
while(p >= n){
p = Pollard_rho(p, rand()%(n-1)+1);
}
findfac(p);
findfac(n/p); }
int main() {
int T;scanf("%d", &T);
while(T-- > 0) {
ll n;scanf("%I64d", &n); tot = 0;
findfac(n);
sort(P, P + tot);
ll t = 0, ans = 0;
for(int i = 0; i < tot; i ++) {
if(!i || P[i] != P[i-1]) {
ans += pow_mod(P[i], count(P, P+tot, P[i]), n);
t ++;
}
}
printf("%I64d %I64d\n", t, t==1?n/P[0]:ans);
}
return 0;
}
HDU 4344 随机法判素数(费马小定理的更多相关文章
- HDU——5667Sequence(矩阵快速幂+费马小定理应用)
Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total S ...
- HDU 5667 Sequence 矩阵快速幂+费马小定理
题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...
- 2014多校第一场 I 题 || HDU 4869 Turn the pokers(费马小定理+快速幂模)
题目链接 题意 : m张牌,可以翻n次,每次翻xi张牌,问最后能得到多少种形态. 思路 :0定义为反面,1定义为正面,(一开始都是反), 对于每次翻牌操作,我们定义两个边界lb,rb,代表每次中1最少 ...
- hdu 4869 Turn the pokers(组合数+费马小定理)
Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...
- HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)
Sum Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 数学--数论--HDU 1098 Ignatius's puzzle (费马小定理+打表)
Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so h ...
- UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。
10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...
- hdu 4704 Sum(组合,费马小定理,快速幂)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4704: 这个题很刁是不是,一点都不6,为什么数据范围要开这么大,把我吓哭了,我kao......说笑的, ...
- [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)
Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...
随机推荐
- Rsync、Unison及DRBD的比较
一.Rsync Rsync(remote synchronize),顾名思义,可以知道这是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的 “Rsync算法”来 ...
- 基于visual Studio2013解决算法导论之022队列实现(基于链表)
题目 基于链表的队列实现 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...
- C Primer Plus 读书笔记之C基础回顾
目标代码文件.可执行文件和库 C编程的基本策略是使用程序将源代码文件转换为可执行文件,此文件包含可以运行的机器语言代码.C分两步完成这一工作:编译和链接.编译器将源代码转换为中间代码,链接器将此中间代 ...
- 华为s5700 添加与删除vlan
新建vlan 删除vlan ① 检查该VLAN下是否存在成员接口,使用如下命令:display vlan all② 如存在成员接口,则进入该接口视图,删除该成员,否则可略过此步骤,例如:interfa ...
- #define DEBUG用法
背景: 很多时候我们写代码,想要看看函数或者算法执行的对不对.是否达到了我们想要的效果, 那么,最直接的办法是把函数或者算法所操作数据显示出来看看,这样就需要写一些cout<<直接输出的代 ...
- 网页制作之JavaScript部分 1 - 语法(复制教材内容)
一.简介 1.JavaScript它是个什么东西? 它是个脚本语言,需要有宿主文件,他的宿主文件是html文件. 2.它与Java有什么关系? 没有什么直接联系,java是Sun公司(已经没有了,被O ...
- 在Vista以上版本运行WTL程序,有时候会提示“这个程序可能安装补正确...”的错误
在Win7/Vista下,如何以兼容模式运行exe? https://msdn.microsoft.com/en-us/library/dd371711(VS.85).aspx 问题描 ...
- 安装MyEclipse Color Themes
下载地址:http://eclipsecolorthemes.org/?list=toppicks&lang=html 安装步骤如下: 1.Import---Preferences 2.选择下 ...
- Qt状态机框架
The State Machine Framework 状态机框架提供了用于创建和执行状态图的类.概念和符号是基于Harel的Statecharts: A visual formalism for c ...
- Docker学习笔记(4) — 开启Docker远程访问
默认情况下,Docker守护进程会生成一个socket(/var/run/docker.sock)文件来进程本地进程通信,而不会监听任何端口,因此只能在本地使用docker客户端或者使用Docker ...