P3498 [POI2010]KOR-Beads
Posted five20
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3498 [POI2010]KOR-Beads相关的知识,希望对你有一定的参考价值。
题目描述
Byteasar once decided to start manufacturing necklaces.
He subsequently bought a very long string of colourful coral beads for a bargain price.
Byteasar now also has a machine that, for a given (), can cut the string into pieces (or substrings) of coral beads (i.e., the first piece consists of the beads no.
, the second of , etc.).
If the length of the string (measured in coral beads) is not a multiple of , then the last piece is not used, as it has length smaller than .
From now on we denote the colours of the beads with positive integers.
Byteasar, always praising diversity, wonders how he should choose the number in order to get as many different substrings as possible.
The ends of the long string that will be cut are different: there are specific beginning and ending (rather than two interchangeable endpoints), and the machine of course starts cutting at the beginning. On the other hand, in the substrings obtained from cutting the endpoints are interchangeable, and so the substrings can be reversed. In other words, the substrings and are identical to us. Write a program that determines the optimum value of for Byteasar.
Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子串),每块有k(k>0)个珠子,如果这条珠子的长度不是k的倍数,最后一块小于k的就不要拉(nc真浪费),保证珠子的长度为正整数。 Zxl喜欢多样的项链,为她应该怎样选择数字k来尽可能得到更多的不同的子串感到好奇,子串都是可以反转的,换句话说,子串(1,2,3)和(3,2,1)是一样的。写一个程序,为Zxl决定最适合的k从而获得最多不同的子串。 例如:这一串珠子是: (1,1,1,2,2,2,3,3,3,1,2,3,3,1,2,2,1,3,3,2,1), k=1的时候,我们得到3个不同的子串: (1),(2),(3) k=2的时候,我们得到6个不同的子串: (1,1),(1,2),(2,2),(3,3),(3,1),(2,3) k=3的时候,我们得到5个不同的子串: (1,1,1),(2,2,2),(3,3,3),(1,2,3),(3,1,2) k=4的时候,我们得到5个不同的子串: (1,1,1,2),(2,2,3,3),(3,1,2,3),(3,1,2,2),(1,3,3,2)
输入输出格式
输入格式:In the first line of the standard input there is an integer () denoting the length of the string to cut.
In the second line there are positive integers (), separated by single spaces, that denote the colours of successive beads in Byteasar‘s string.
输出格式:Two integers, separated by a single space, should be printed out to the first line of the standard ouput:
the (maximum) number of different substrings that can be obtained with an optimal choice of parameter , and the number of such optimal values of .
The second line should contain integers separated by single spaces:
the values of parameter that yield an optimum solution; these can be given in arbitrary order.
输入输出样例
21
1 1 1 2 2 2 3 3 3 1 2 3 3 1 2 2 1 3 3 2 1
6 1
2
Solution:
本题字符串hash。
坑点在于子串可以反转,所以正反分别两次对原串hash,然后直接暴力枚举长度,问题出现在判重,想到用正反的hash值乘积做为新的键值,开一个set去重就好了。
代码:
1 #include<bits/stdc++.h> 2 #define il inline 3 #define ll long long 4 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) 5 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--) 6 using namespace std; 7 const int N=200005,P=131,mod=1e9+7; 8 ll Ha[N],ha[N],sum[N],q,p; 9 unsigned ll H[N]; 10 int n,a[N],tot,ans[N],cnt; 11 set<unsigned ll>vis; 12 13 il int gi(){ 14 int a=0;char x=getchar(); 15 while(x<‘0‘||x>‘9‘)x=getchar(); 16 while(x>=‘0‘&&x<=‘9‘)a=(a<<3)+(a<<1)+x-48,x=getchar(); 17 return a; 18 } 19 20 int main(){ 21 n=gi(),sum[0]=1; 22 For(i,1,n) a[i]=gi(),sum[i]=sum[i-1]*P%mod,Ha[i]=(Ha[i-1]*P%mod+a[i])%mod; 23 Bor(i,1,n) ha[i]=(ha[i+1]*P%mod+a[i])%mod; 24 For(k,1,n){ 25 for(int i=1;i+k-1<=n;i+=k) 26 q=(Ha[i+k-1]-Ha[i-1]*sum[k]%mod+mod)%mod, 27 p=(ha[i]-ha[i+k]*sum[k]%mod+mod)%mod, 28 vis.insert(p*q); 29 int c=vis.size(); 30 if(c>tot)tot=c,ans[cnt=1]=k; 31 else if(c==tot) ans[++cnt]=k; 32 vis.clear(); 33 } 34 printf("%d %d ",tot,cnt); 35 For(i,1,cnt) printf("%d ",ans[i]); 36 return 0; 37 }
以上是关于P3498 [POI2010]KOR-Beads的主要内容,如果未能解决你的问题,请参考以下文章