#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 随机法判素数(费马小定理的更多相关文章

  1. HDU——5667Sequence(矩阵快速幂+费马小定理应用)

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  2. 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; ...

  3. 2014多校第一场 I 题 || HDU 4869 Turn the pokers(费马小定理+快速幂模)

    题目链接 题意 : m张牌,可以翻n次,每次翻xi张牌,问最后能得到多少种形态. 思路 :0定义为反面,1定义为正面,(一开始都是反), 对于每次翻牌操作,我们定义两个边界lb,rb,代表每次中1最少 ...

  4. hdu 4869 Turn the pokers(组合数+费马小定理)

    Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...

  5. HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)

    Sum Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  6. 数学--数论--HDU 1098 Ignatius's puzzle (费马小定理+打表)

    Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so h ...

  7. UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。

                                                    10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...

  8. hdu 4704 Sum(组合,费马小定理,快速幂)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4704: 这个题很刁是不是,一点都不6,为什么数据范围要开这么大,把我吓哭了,我kao......说笑的, ...

  9. [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

    Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...

随机推荐

  1. Qt如何读取ico文件中的image(使用QImageReader和QIcon)

    ico文件是一个容器,内部可以装载许多个image,我们可以通过QIcon的pixmap方法来获取需要的image QPixmap pixmap ( const QSize & size, M ...

  2. 当webview遇到了Slidingmenu,webView出现卡白,解决方案

    先介绍一下什么是SlidingMenu: Sliding Menu的是一种比较新的设置界面或配置界面效果,在主界面左滑或者右滑出现设置界面,能方便的进行各种操作.如Evernote.Google+.F ...

  3. AsyncTask简单入门

    关系: java.lang.Object    ↳    android.os.AsyncTask<Params, Progress, Result> 概述: AsyncTask是Andr ...

  4. Android学习笔记:ActionBar使用介绍

    一.基本概念 最权威和官方的介绍请看google的api文档 http://developer.android.com/training/basics/actionbar/setting-up.htm ...

  5. bin文件格式分析

    xip 的 bin 文件分析 一个bin 文件在存储上是按以下的结构存储的 组成:标记(7)+Image開始地址(1)+Image长度(1)            记录0地址+记录0长+记录0校验和+ ...

  6. Python 3语法小记(四)字典 dictionary

    字典是Python里面一种无序存储结构,存储的是键值对 key - value.关键字应该为不可变类型,如字符串.整数.包含不可变对象的元组. 字典的创建很简单,用 d = {key1 : value ...

  7. 高仿114la网址导航源码完整最新版

    给大家本人我精心模仿的高仿114la网址导航源码,我们都知道114la网址导航的影响力,喜欢的朋友可以下载学习一下.  由于文件较大,没有上传了,下载地址在下面有的. 附源码下载: 114la网站导航 ...

  8. 为经典版eclipse增加web and JavaEE插件

    链接地址:http://jingyan.baidu.com/article/f25ef2546cd0e2482d1b825d.html 为经典版eclipse增加web and JavaEE插件 百度 ...

  9. 先有Delphi内存对象,后有句柄(如果需要的话),最后再显示

    在设计期放上一个Panel1和Button1,然后设置Panel1.Visible:=False 这时候执行: procedure TForm1.Button4Click(Sender: TObjec ...

  10. 621 - Secret Research

     Secret Research  At a certain laboratory results of secret research are thoroughly encrypted. A res ...