hdu 4344 Mark the Rope (Miller Rabbin + Pollard rho)
来源:互联网 发布:ai cc mac破解版下载 编辑:程序博客网 时间:2024/06/13 22:22
Mark the Rope
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2764 Accepted Submission(s): 916
Problem Description
Eric has a long rope whose length is N, now he wants to mark on the rope with different colors. The way he marks the rope is:
1. He will choose a color that hasn’t been used
2. He will choose a length L (N>L>1) and he defines the mark’s value equals L
3. From the head of the rope, after every L length, he marks on the rope (you can assume the mark’s length is 0 )
4. When he chooses the length L in step 2, he has made sure that if he marks with this length, the last mark will be at the tail of the rope
Eric is a curious boy, he want to choose K kinds of marks. Every two of the marks’ value are coprime(gcd(l1,l2)=1). Now Eric wants to know the max K. After he chooses the max K kinds of marks, he wants to know the max sum of these K kinds of marks’ values.
You can assume that Eric always can find at least one kind of length to mark on the rope.
1. He will choose a color that hasn’t been used
2. He will choose a length L (N>L>1) and he defines the mark’s value equals L
3. From the head of the rope, after every L length, he marks on the rope (you can assume the mark’s length is 0 )
4. When he chooses the length L in step 2, he has made sure that if he marks with this length, the last mark will be at the tail of the rope
Eric is a curious boy, he want to choose K kinds of marks. Every two of the marks’ value are coprime(gcd(l1,l2)=1). Now Eric wants to know the max K. After he chooses the max K kinds of marks, he wants to know the max sum of these K kinds of marks’ values.
You can assume that Eric always can find at least one kind of length to mark on the rope.
Input
First line: a positive number T (T<=500) representing the number of test cases
2 to T+1 lines: every line has only a positive number N (N<263) representing the length of rope
2 to T+1 lines: every line has only a positive number N (N<263) representing the length of rope
Output
For every test case, you only need to output K and S separated with a space
Sample Input
2180198
Sample Output
3 183 22
Author
HIT
Source
2012 Multi-University Training Contest 5
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you: 4348 4347 4346 4345 4343
题解:Miller Rabbin + Pollard rho
Miller Rabbin 可以在O(s(logn)^3)的时间复杂度内判断一个数是否为素数,有2^(-s)的概率出错
Pollard rho 是基于Miller Rabbin的一种快速分解质因数的做法。
该算法的大致流程是先判断当前数是否为素数(用Miller Rabbin),如果是直接记录下该质数,直接返回。如果不是就试图找到一个因子(可以不是质因子),然后对于当前因子p,和n/p分别递归寻找质因子。
对于质因数的寻找,我们采用一种随机化的算法。我们假设要找到的质因子为p,先随机取一个x,然后用x构造y,使p=gcd(x-y,n)如果p不等于1那么就找到了一个质因子,如果找不到我们就不断的调整x,使x=x*x+c(c一般可以随机),直到x==y出现循环,则选取失败。重新选取x,重复上述过程。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define LL long long #define N 10000003using namespace std;LL n,mx,cnt,num[N],prime[N],c[N];LL mul(LL a,LL b,LL p){LL ans=0; LL base=a%p;while (b) {if (b&1) ans=(ans+base)%p;b>>=1;base=(base+base)%p;}return ans;}LL quickpow(LL num,LL x,LL p){LL ans=1; LL base=num%p;while (x) {if (x&1) ans=mul(ans,base,p);x>>=1;base=mul(base,base,p);}return ans;}bool miller_rabbin(LL n){if (n==2) return true;if (!(n&1)) return false;LL t=0,a,x,y,u=n-1;while (!(u&1)) t++,u>>=1;for (int i=0;i<=10;i++) {a=rand()*rand()%(n-1)+1;x=quickpow(a,u,n);for (int j=0;j<t;j++) {y=mul(x,x,n);if (y==1&&x!=1&&x!=n-1) return false;x=y;}if (x!=1) return false;}return true;}LL gcd(LL x,LL y){LL r;while (y) {r=x%y;x=y; y=r;}return x;}LL pollard_rho(LL n,LL c){LL i=1,k=2;LL x=rand()%(n-1)+1,y=x,p=1;while (p==1) {i++;x=(mul(x,x,n)+c)%n;p=gcd((y-x+n)%n,n);if (y==x) return n;if (i==k) y=x,k<<=1;}return p;}void solve(LL n){if (n==1) return;if (miller_rabbin(n)) {num[++cnt]=n;return;}LL p=n; while (p==n) p=pollard_rho(p,rand()%(n-1)+1);solve(p); solve(n/p);}int main(){freopen("a.in","r",stdin);freopen("my.out","w",stdout);srand(2000001001);int T; scanf("%d",&T);while (T--) {scanf("%I64d",&n);//cout<<n<<endl;mx=0; cnt=0;solve(n); int k=0;sort(num+1,num+cnt+1);prime[++k]=num[1]; c[k]=1;for (int i=2;i<=cnt;i++) if (num[i]==num[i-1]) prime[k]*=num[i]; else prime[++k]=num[i];printf("%d ",k);LL sum=0;for (int i=1;i<=k;i++) sum+=prime[i];if (k==1) sum/=num[1];printf("%I64d\n",sum);}}
0 0
- hdu 4344 Mark the Rope (Miller Rabbin + Pollard rho)
- hdu 4344 Mark the Rope
- hdu 4344 Mark the Rope
- HDU 4344 Mark the Rope
- POJ1811--miller rabin&pollard rho
- hdu - 4344 - Mark the Rope - 大数分解
- bzoj 3667: Rabin-Miller算法 (Miller_rabbin+Pollard rho)
- POJ1811 PrimeTest Miller-Robin+Pollard-Rho
- BZOJ 3667 Pollard-rho &Miller-Rabin
- hdu-4344-Mark the Rope-大数分解质因子模板
- HDU 4344 Mark the Rope Pollard_Rho大数分解
- HDU 3864 D_num Miller Rabin 质数判断+Pollard Rho大整数分解
- poj1811(Miller-Rabin(素数测试)与Pollard rho(整数的因子分解))
- POJ 1811 Prime Test(素数判定Miller-Rabin+素因子分解Pollard-rho)
- POJ 2429 GCD & LCM Inverse(素数判定Miller-Rabin+素因子分解Pollard-rho)
- POJ1811 miller-rabin素数测试pollard-rho质因子分解
- 【Pollard Rho模板】【bzoj 3667】: Rabin-Miller算法
- Miller-Rabin素数测试和Pollard-rho大整数分解
- 【Asp.net】前台的Html控件向后台传值以及前台的html的input按钮如何调用后台的方法
- Android混淆打包 引用库常见问题
- Servlet不能设置编码,设置编码无效!?
- LeetCode:Longest Substring Without Repeating Characters
- acm sdut 迷失の搜索树(实际上就是建一个二叉排序树)
- hdu 4344 Mark the Rope (Miller Rabbin + Pollard rho)
- 【教程】预装Win8/10笔记本UEFI模式改为传统BIOS启动U盘
- PHP跨数据库事务处理
- 读取Java文件到byte数组的三种方式
- lag和lead 分析函数
- Java Web开发(ER模型/DAO/JDBC/Tomcat中间件/JSP)
- 一致性hash以及python代码实现
- acm sdut 数据结构实验之查找一:二叉排序树
- COM---组件复用:包容与聚合