日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[HDU 4344]Mark the Rope(Pollard_rho+Miller_Rabin)

發布時間:2024/4/13 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [HDU 4344]Mark the Rope(Pollard_rho+Miller_Rabin) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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.

Solution

質因數分解一下就好了,應該是道模板題

然而我忘記還有質因數只有一個的情況,L不能等于N啊

QvQ拍了好久都沒找到錯在哪,后來隨手輸了個1024發現事情有蹊蹺…

#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<vector> #include<map> using namespace std; typedef long long LL; LL T,ans=0; vector<LL>v; map<LL,LL>num; LL gcd(LL a,LL b) {return b?gcd(b,a%b):a; } LL mul(LL a,LL b,LL p) {LL res=0;while(b){if(b&1)res=(res+a)%p;a=(a+a)%p;b>>=1;}return res; } LL pow(LL a,LL n,LL p) {LL res=1;while(n){if(n&1)res=mul(res,a,p);a=mul(a,a,p);n>>=1;}return res; } bool check(LL a,LL n,LL m,LL cnt) {LL x=pow(a,m,n),y=x;for(int i=1;i<=cnt;i++){x=mul(x,x,n);if(x==1&&y!=1&&y!=n-1)return 1;y=x;}return x!=1; } bool Miller_Rabin(LL n) {if(n==2)return 1;if(n<=1||n&1==0)return 0;LL m=n-1,cnt=0;while(m&1==0)cnt++,m>>=1;for(int i=1;i<=7;i++){if(check(rand()%(n-1)+1,n,m,cnt))return 0;}return 1; } LL rho(LL n,LL c) {LL i=1,k=2,x=rand()%(n-1)+1,y=x,d;while(1){x=(mul(x,x,n)+c)%n;d=x>y?gcd(x-y,n):gcd(y-x,n);if(d>1)return d;if(y==x)return n;if(i==k)y=x,k<<=1;i++;} } void solve(LL n) {if(n==1)return;if(Miller_Rabin(n)){if(!num[n]){v.push_back(n),ans++,num[n]=1;}num[n]*=n;return;}LL p=n;while(p==n)p=rho(n,rand()%(n-1)+1);solve(p);solve(n/p); } int main() {scanf("%lld",&T);while(T--){LL n;scanf("%lld",&n);v.clear(),num.clear(),ans=0;solve(n);if(v[0]==n)ans--;printf("%lld ",ans);LL sum=0;for(int i=0;i<ans;i++)sum+=num[v[i]];if(ans==1)sum/=v[0];printf("%lld\n",sum);}return 0; }

?

轉載于:https://www.cnblogs.com/Zars19/p/6840576.html

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的[HDU 4344]Mark the Rope(Pollard_rho+Miller_Rabin)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。