2021HDU多校9 - 7073 Integers Have Friends 2.0(随机数)
題目鏈接:點擊查看
題目大意:給出一個長度為 nnn 的序列,找到一個長度最長的子序列,滿足存在一個模數 mmm,使得這個子序列中的所有數取模后相等
題目分析:挺有意思的一道題,首先假如取 m=2m=2m=2 的話,那么所有數字非奇即偶,所以答案至少是 ?n2?\lceil \frac{n}{2}\rceil?2n??
假如我們隨機選兩個位置 x,yx,yx,y,滿足 x≠yx\neq yx?=y,那么這兩個位置同時位于答案中的概率至少為 14\frac{1}{4}41?,也就是說選不中的概率為 34\frac{3}{4}43?
再假如我們選擇的 xxx 和 yyy 屬于答案中,那么 mmm 可行的取值是 ∣(ax?ay)|(a_x-a_y)∣(ax??ay?),篩出質因子后每次 O(n)O(n)O(n) 去計算答案就可以了
那么對上述操作執行 KKK 次,每次都選不中答案的概率就是 (34)K(\frac{3}{4})^K(43?)K,當 K=40K=40K=40 時,這個數值就是 10?510^{-5}10?5 級別的了
需要注意的是,答案記得初始化為 ans=1ans=1ans=1,因為上面的操作只能計算答案大于等于二的情況
因為 HDU 的上古評測機,還是建議打個歐拉篩優化一下篩質因子的過程,能快不少
代碼:
// 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; }總結
以上是生活随笔為你收集整理的2021HDU多校9 - 7073 Integers Have Friends 2.0(随机数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021牛客多校10 - Train W
- 下一篇: CodeForces - 1559D2