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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2021HDU多校9 - 7073 Integers Have Friends 2.0(随机数)

發(fā)布時(shí)間:2024/4/11 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2021HDU多校9 - 7073 Integers Have Friends 2.0(随机数) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:點(diǎn)擊查看

題目大意:給出一個(gè)長度為 nnn 的序列,找到一個(gè)長度最長的子序列,滿足存在一個(gè)模數(shù) mmm,使得這個(gè)子序列中的所有數(shù)取模后相等

題目分析:挺有意思的一道題,首先假如取 m=2m=2m=2 的話,那么所有數(shù)字非奇即偶,所以答案至少是 ?n2?\lceil \frac{n}{2}\rceil?2n??

假如我們隨機(jī)選兩個(gè)位置 x,yx,yx,y,滿足 x≠yx\neq yx?=y,那么這兩個(gè)位置同時(shí)位于答案中的概率至少為 14\frac{1}{4}41?,也就是說選不中的概率為 34\frac{3}{4}43?

再假如我們選擇的 xxxyyy 屬于答案中,那么 mmm 可行的取值是 ∣(ax?ay)|(a_x-a_y)(ax??ay?),篩出質(zhì)因子后每次 O(n)O(n)O(n) 去計(jì)算答案就可以了

那么對上述操作執(zhí)行 KKK 次,每次都選不中答案的概率就是 (34)K(\frac{3}{4})^K(43?)K,當(dāng) K=40K=40K=40 時(shí),這個(gè)數(shù)值就是 10?510^{-5}10?5 級別的了

需要注意的是,答案記得初始化為 ans=1ans=1ans=1,因?yàn)樯厦娴牟僮髦荒苡?jì)算答案大于等于二的情況

因?yàn)?HDU 的上古評測機(jī),還是建議打個(gè)歐拉篩優(yōu)化一下篩質(zhì)因子的過程,能快不少

代碼:

// Problem: Integers Have Friends 2.0 // Contest: Virtual Judge - HDU // URL: https://vjudge.net/problem/HDU-7073 // Memory Limit: 262 MB // Time Limit: 5000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; mt19937_64 eng(time(NULL)); LL a[N]; int n; int cal(LL x,LL y) {int cnt=0;for(int i=1;i<=n;i++) {cnt+=a[i]%x==y;}return cnt; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {scanf("%d",&n);uniform_int_distribution<int>ran(1,n);for(int i=1;i<=n;i++) {scanf("%lld",a+i);}int ans=1;for(int K=1;K<=30;K++) {int l=-1,r=-1;while(l==r) {l=ran(eng),r=ran(eng);}LL tmp=llabs(a[l]-a[r]);for(int i=2;1LL*i*i<=tmp;i++) {if(tmp%i==0) {ans=max(ans,cal(i,a[l]%i));while(tmp%i==0) {tmp/=i;}}}if(tmp>1) {ans=max(ans,cal(tmp,a[l]%tmp));}}printf("%d\n",ans);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的2021HDU多校9 - 7073 Integers Have Friends 2.0(随机数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。