国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

hdu 4344 Mark the Rope (Miller Rabbin + Pollard rho)

2019-11-08 18:35:13
字體:
來源:轉載
供稿:網友

Mark the Rope

Time Limit: 20000/10000 MS (java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2764    Accepted Submission(s): 916PRoblem DescriptionEric 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 used2. He will choose a length L (N>L>1) and he defines the mark’s value equals L3. 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 ropeEric 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. InputFirst line: a positive number T (T<=500) representing the number of test cases2 to T+1 lines: every line has only a positive number N (N<263) representing the length of rope OutputFor every test case, you only need to output K and S separated with a space Sample Input
2180198 Sample Output
3 183 22 AuthorHIT Source2012 Multi-University Training Contest 5 Recommendzhuyuanchen520   |   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);	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 葵青区| 惠东县| 太保市| 宁安市| 鄄城县| 成都市| 巩义市| 武宣县| 景德镇市| 延安市| 贵溪市| 双桥区| 东兰县| 康乐县| 楚雄市| 上林县| 嘉定区| 平安县| 咸宁市| 平邑县| 威宁| 泸州市| 康定县| 宜川县| 隆安县| 沛县| 会同县| 鄯善县| 八宿县| 抚宁县| 密山市| 临沭县| 建平县| 社旗县| 三原县| 冷水江市| 界首市| 宁明县| 大化| 广东省| 峡江县|