BUPT2017 wintertraining(15) #8E

题意

长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数。

每轮标记都选一个L,且L之间两两互质。

求L的最多种数K。以及标记之和S的最大值。

题解

对n进行分解质因数,K就是不同质因子的个数,S就是p^{a_i}之和。不过题目要求L<n,所以当S算出来是n时,再除以一下最小的质因子。

分解比较小的n(<1e9),可以直接枚举,复杂度是\(O(\sqrt n)\)。但是这里n比较大,需要用更高效的算法。

官方标程的方法:

先用Miller Rabin素数测试判断n是否是素数,是则直接返回。否则用pollard_rho算法找出用一个因子p。再递归地分解p和n/p。

代码

#include <cstdio>
#include <ctime>
#include <algorithm>
using namespace std;
typedef long long ll;
const int Times=10;
ll gcd(ll a, ll b){
while(b){
ll t=a%b;
a=b;
b=t;
}
return a;
}
ll qmul(ll a, ll b, ll m){
ll ans=0;
while(b){
if(b&1){
ans=ans+a;
if(ans>=m)ans-=m;
}
a<<=1;
if(a>=m)a-=m;
b>>=1;
}
return ans;
}
ll qpow(ll a, ll b, ll m){
ll ans=1;
while(b){
if(b&1)
ans=qmul(ans,a,m);
a=qmul(a,a,m);
b>>=1;
}
return ans;
}
bool Miller_Rabin(ll n){
if(n==2)return true;
if(n<2||!(n&1))return false;
ll m=n-1,x,y,a;
int k=0;
while(!(m&1)){
++k;
m>>=1;
}
for(int i=0;i<Times;++i){
a=rand()%(n-1)+1;
x=qpow(a,m,n);
for(int j=0;j<k;++j){
y=qmul(x,x,n);
if(y==1&&x!=1&&x!=n-1)return false;
x=y;
}
if(y!=1)return false;
}
return true;
}
ll pollard_rho(ll n, ll c){
ll i=1, k=2, x, y;
x=y=rand()%(n-1)+1;
while(1){
++i;
x=(qmul(x, x, n)+c)%n;
ll d=gcd((y-x+n)%n, n);
if(d>1&&d!=n)return d;
if(y==x)return n;
if(i==k){
y=x;
k<<=1;
}
}
}
ll fac[200],cnt;
void find(ll n,int c){
if(n==1)return;
if(Miller_Rabin(n)){
fac[++cnt]=n;
return;
}
ll p=n;
ll k=c;
while(p>=n)p=pollard_rho(p,c--);
find(p,k);
find(n/p, k);
}
int main(){
int t;ll n;
scanf("%d", &t);
while(t--){
cnt=0;
scanf("%lld", &n);
find(n, 97);
sort(fac, fac+cnt+1);
int num=0;
ll sum=0,tmp=0;
for(int i=1;i<=cnt;++i){
if(fac[i]!=fac[i-1]){
++num;
sum+=tmp;
tmp=fac[i];
}else tmp*=fac[i];
}
if(num==1)sum=n/fac[1];
else sum+=tmp;
printf("%d %lld\n",num, sum);
}
return 0;
}

【HDU - 4344】Mark the Rope(大整数分解)的更多相关文章

  1. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  2. Miller&&Pollard HDOJ 4344 Mark the Rope

    题目传送门 题意:一个长为n(n<2^63)的管子,在管子上做标记,每隔L个长度单位做一个标记,从管子头端开始,保证最后一次标记恰好在管子的尾端.让你找出有多少个这样的L(L<n),且他们 ...

  3. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  4. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

  5. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  6. [poj1811]Prime Test(Pollard-Rho大整数分解)

    问题描述:素性测试兼质因子分解 解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用 期望复杂度:$O({n^{\frac{1}{4}}})$ #include<cst ...

  7. 大整数分解质因数(Pollard rho算法)

    #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...

  8. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  9. Light OJ 1341 Aladdin and the Flying Carpet Pollard_rho整数分解+DFS

    进入a b 多少努力p, q 使p*q == a && p < q && p >= b 直接大整数分解 然后dfs所有可能的解决方案劫持 #include ...

随机推荐

  1. 全局关键字搜索:Element UI Table内容过滤\jQuery过滤器fastLiveFilter插件\BootstrapVue插件;

    ```html data:{ resultMaster: [], otableData:[], schfilter:'' } watch: { schfilter: function(val, old ...

  2. iRate---一个跳转AppStore评分弹窗

    https://www.aliyun.com/jiaocheng/357479.html 摘要:gitHub地址:https://github.com/nicklockwood/iRate可以通过配置 ...

  3. UITableView加载数据,没有数据,没有网络界面处理

    https://blog.csdn.net/chmod_r_755/article/details/53231461 俗话说的好,傻逼的APP都是相似的,牛逼的APP各有各的牛逼...但是UITabl ...

  4. PT与PX区别

    字体大小的设置单位,常用的有2种:px.pt.这两个有什么区别呢? 先搞清基本概念:px就是表示pixel,像素,是屏幕上显示数据的最基本的点: pt就是point,是印刷行业常用单位,等于1/72英 ...

  5. C# foreach内部原理

    我们知道使用foreach的一个要求是对象必须继承自IEnumerable接口 这样才可以进行迭代 那内部是怎么实现的呢 这个时候会将对应的foreach语句转换为一个while循环 并且通过Move ...

  6. C# Note32: 查漏补缺

    (1)Using的三种使用方式 (2)C#详解值类型和引用类型区别 (3)c#中字段(field)和属性(property)的区别 (4)C#中的 int? int?:表示可空类型,就是一种特殊的值类 ...

  7. Dart语法基础

    hello world // Define a function. printNumber(num aNumber) { print('The number is $aNumber.'); // Pr ...

  8. nodejs 利用zip-local模块压缩文件夹

    var zipper = require("zip-local"); zipper.sync.zip("./folder").compress().save(& ...

  9. springboot 如何操作redis

    1.首先应该引入 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  10. 浅谈基于Prism的软件系统的架构设计

    很早就想写这么一篇文章来对近几年使用Prism框架来设计软件来做一次深入的分析了,但直到最近才开始整理,说到软件系统的设计这里面有太多的学问,只有经过大量的探索才能够设计出好的软件产品,就本人的理解, ...