当前位置: 代码迷 >> 综合 >> bzoj2081 Beads
  详细解决方案

bzoj2081 Beads

热度:33   发布时间:2024-01-13 10:26:52.0

枚举答案暴力就可以了,复杂度 O(nlog2n)

#include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
#define LL unsigned long long
const int maxn=200010,p=13331;
LL h[maxn],g[maxn],pow[maxn];
int a[maxn],n,ans;
set<LL> s;
vector<int> v;
int main()
{int cnt;LL x,y;scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d",&a[i]);for (int i=1;i<=n;i++) h[i]=h[i-1]*p+a[i];for (int i=1;i<=n;i++) g[i]=g[i-1]*p+a[n-i+1];pow[0]=1;for (int i=1;i<=n;i++) pow[i]=pow[i-1]*p;for (int i=1;i<=n;i++){cnt=0;s.clear();for (int j=1;j+i-1<=n;j+=i){x=h[j+i-1]-h[j-1]*pow[i];y=g[n-j+1]-g[n-j-i+1]*pow[i];x*=y;if (!s.count(x)) s.insert(x),cnt++;}if (cnt>ans){v.clear();v.push_back(i);ans=cnt;}else if (cnt==ans) v.push_back(i);}printf("%d %d\n",ans,(int)v.size());for (int i=0;i<v.size();i++) printf("%d%c",v[i],i==v.size()-1?'\n':' ');
}