题意:给你一个数n,  定义m=2k-1,   {k|1<=k<=n},并且 k为素数;  当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( 2 ^ 23 ) - 1

分析:要分解m,首先要判断m是否为合数,直接用米勒拉宾判断,但是后面的大合数分解,一开始用了试除法,直接给超时。所以,有更加快速的方法。(现学的)。使用Pollard_Rho大数分解算法。

Pollard_Rho大数分解时间复杂度为n1/4 

ac代码:

#include <cstdio>
#include <algorithm>
using namespace std; typedef long long llt; int const Repeat = ;
const int N = ;
int prime[N];
bool vis[N], is_prime[N];
//利用二进制计算a*b%mod
llt multiMod(llt a, llt b, llt mod){
llt ret = 0LL;
a %= mod;
while (b){
if (b & 1LL) ret = (ret + a) % mod, --b;
b >>= 1LL;
a = (a + a) % mod;
}
return ret;
} //计算a^b%mod
llt powerMod(llt a, llt b, llt mod){
llt ret = 1LL;
a %= mod;
while (b){
if (b & 1LL) ret = multiMod(ret, a, mod), --b;
b >>= 1LL;
a = multiMod(a, a, mod);
}
return ret;
} //Miller-Rabin测试,测试n是否为素数
bool Miller_Rabin(llt n, int repeat){
if (2LL == n || 3LL == n) return true;
if (!(n & 1LL)) return false; //将n分解为2^s*d
llt d = n - 1LL;
int s = ;
while (!(d & 1LL)) ++s, d >>= 1LL; //srand((unsigned)time(0));
for (int i = ; i<repeat; ++i){//重复repeat次
llt a = rand() % (n - ) + ;//取一个随机数,[2,n-1)
llt x = powerMod(a, d, n);
llt y = 0LL;
for (int j = ; j<s; ++j){
y = multiMod(x, x, n);
if (1LL == y && 1LL != x && n - 1LL != x) return false;
x = y;
}
if (1LL != y) return false;
}
return true;
} llt Fac[];//质因数分解结果(刚返回时是无序的)
int FCnt;//质因数的个数。数组小标从0开始 llt gcd(llt a, llt b){
if (0L == a || 0L == b) return ;
if (a < ) a = -a;
if (b < ) b = -b;
while (b){
llt t = a % b;
a = b;
b = t;
}
return a;
}
llt Pollard_Rho(llt n, llt c){
llt i = , k = ;
llt x = rand() % n;
llt y = x;
while (){
++i;
x = (multiMod(x, x, n) + c) % n;
llt d = gcd(y - x, n);
if (d != 1LL && d != n) return d;
if (y == x) return n;
if (i == k) y = x, k <<= ;
}
} void find(llt n){
if (4LL == n){
Fac[] = Fac[] = 2LL;
FCnt = ;
return;
}
if (Miller_Rabin(n, Repeat)){
Fac[FCnt++] = n;
return;
} llt p;
while ((p = Pollard_Rho(n, rand() % (n - ) + )) == n); find(p);
find(n / p);
} int Prime()
{
int cnt = ;
for (int i = ; i <= N; ++i)
{
if (!vis[i])
{
prime[cnt++] = i; is_prime[i] = ;
}
for (int j = ; j < cnt&&i*prime[j] <= N; ++j)
{
vis[i*prime[j]] = ;
if (i%prime[j] == )break;
}
}
return cnt;
} llt poww(llt x, llt n)
{
llt res = ;
for (; n;x=x*x, n>>=)
if (n & )res = res*x;
return res;
}
int main()
{
int k = Prime();
llt n;
while (scanf("%lld", &n) != EOF)
{
for (int i = ; prime[i] <= n; ++i)
{
llt pnum = poww(, prime[i]) - ;
if (!Miller_Rabin(pnum, ))
{
FCnt = ;
find(pnum);
sort(Fac, Fac + FCnt);
printf("%lld", Fac[]);
for (int i = ; i < FCnt; ++i)
printf(" * %lld", Fac[i]);
printf(" = %lld = ( 2 ^ %d ) - 1\n", pnum, prime[i]);
}
}
}
}

Pollard_Rho大数分解模板题 pku-2191的更多相关文章

  1. 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811

    题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...

  2. poj 2429 Pollard_rho大数分解

    先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...

  3. poj 1236(强连通分量分解模板题)

    传送门 题意: N(2<N<100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输. 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都 ...

  4. poj 1811 随机素数和大数分解(模板)

    Sample Input 2 5 10 Sample Output Prime 2 模板学习: 判断是否是素数,数据很大,所以用miller,不是的话再用pollard rho分解 miller : ...

  5. hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)

    题目链接:https://vjudge.net/problem/HDU-2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活                                   ...

  6. HDU 2191 珍惜现在,感恩生活(多重背包模板题)

    多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...

  7. [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

    可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...

  8. 51nod 1028 大数乘法 V2 【FFT模板题】

    题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...

  9. HDU4344(大数分解)

    题目:Mark the Rope 题意就是给一个数,然后求这个数的所有因子中组成的最大的一个子集,其中1和本身除外,使得在这个子集中元素两两互素,求最大子集的元素个 数,并且求出和最大的值. 找规律就 ...

随机推荐

  1. S5PV210中断体系结构分析

    我们按照Tiny210官方的裸板程序来梳理S5PV210的中断体系. 关于 S5PV210 的中断体系结构 S5PV210 的中断控制器是由 4 个向量中断控制器(VIC). ARM PrimeCel ...

  2. 动态规划法(五)钢条切割问题(rod cutting problem)

      继续讲故事~~   我们的主人公现在已经告别了生于斯,长于斯的故乡,来到了全国最大的城市S市.这座S市,位于国家的东南部,是全国的经济中心,工商业极为发达,是这个国家的人民所向往的城市.这个到处都 ...

  3. C#中的readonly跟const用法小结

    总结一下常量和只读字段的区别: 由来: 笔者也是在看欧立奇版的<.Net 程序员面试宝典>的时候,才发现自己长久以来竟然在弄不清出两者的情况下,混用了这么长的时间.的确,const与rea ...

  4. GroupBy分组的运用和linq左连接

    最简单的分组 var conHistoryList = conHistoryData.GroupBy(g => g.personId); 就是conHistoryData是一个IQueryabl ...

  5. Ocelot中文文档-Not Supported

    Not Supported Ocelot不支持以下几种情况 块级编码(Chunked Encoding ) - Ocelot始终会获取消息体的大小并返回内容长度(Content-Length).这种情 ...

  6. elasticsearch6.7 05. Document APIs(4)Delete API

    3.Delete API delete API 可以让你删除一个特定id的文档,下面例子删除twitter索引中_doc类型.id为1的文档: DELETE /twitter/_doc/1 返回结果: ...

  7. 【Java并发编程】19、DelayQueue源码分析

    DelayQueue,带有延迟元素的线程安全队列,当非阻塞从队列中获取元素时,返回最早达到延迟时间的元素,或空(没有元素达到延迟时间).DelayQueue的泛型参数需要实现Delayed接口,Del ...

  8. Linux 为linux enterprises 6安装图形桌面教程

    为linux enterprises 6安装图形桌面教程 by:授客 QQ:1033553122 安装系统后发现没图形界面,安装Xwindow[为了避免权限不足,以root登录] 步骤1.启动图形界面 ...

  9. U8 应付款管理 单据类型 分析

    Ap_CloseBill   收付款单主表 Ap_CloseBills 收付款单子表 cVouchType 在收付款单主表中  ,用于区分单据为收款单还是付款单(48,49)  49:付款单 48:收 ...

  10. iOS动画-从UIView到Core Animation

    首先,介绍一下UIView相关的动画. UIView普通动画: [UIView beginAnimations: context:]; [UIView commitAnimations]; 动画属性设 ...