Beads
Beads
题目描述
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)
输入
共有两行,第一行一个整数n代表珠子的长度,第二行是由空格分开的颜色ai(1<=ai<=n,n<=200005)。
输出
也有两行 第一行两个整数,第一个整数代表能获得的最大不同的子串个数,第二个整数代表能获得最大值的k的个数, 第二行输出所有的k(中间有空格)。
来源
好像没有hash的题解,放一篇
暴力枚举k+正反hash
hash的数不能是11,差评。。。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define maxn 200010
#define p 1000000007
using namespace std;
int n,s[maxn],h1[maxn],h2[maxn],c[maxn];
int ans,outp[maxn],tot,t1,t2,sum;
map<int,int>f;
void get(int l,int r)
{
t1=h1[r]-h1[l-1]*c[r-l+1];
t2=h2[l]-h2[r+1]*c[r-l+1];
//cout<<t1<<' '<<t2<<' '<<l<<' '<<r<<endl;
if(!f[t1]&&!f[t2]){
f[t1]=f[t2]=1;
sum++;
}
}
int main(){
cin>>n;
c[0]=1;
for(int i=1;i<=n;i++){
scanf("%d",&s[i]);
h1[i]=h1[i-1]*p+s[i];c[i]=c[i-1]*p;
//cout<<h1[i]<<' '<<c[i]<<endl;
}
for(int i=n;i>=1;i--)h2[i]=h2[i+1]*p+s[i];
for(int k=1;k<=n;k++){
f.clear();sum=0;
for(int i=1;i+k-1<=n;i+=k)get(i,i+k-1);
if(sum>ans){
ans=sum,tot=1,outp[1]=k;
}
else if(sum==ans)outp[++tot]=k;
}
printf("%d %d\n%d",ans,tot,outp[1]);
for(int i=2;i<=tot;i++)printf(" %d",outp[i]);
cout<<endl;
return 0;
}
Beads的更多相关文章
- HDU 1817Necklace of Beads(置换+Polya计数)
Necklace of Beads Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 【USACO】beads
题目: You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, othe ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- 数学计数原理(Pólya):POJ 1286 Necklace of Beads
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7763 Accepted: 3247 ...
- poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)
Description Beads of red, blue or green colors are connected together into a circular necklace of ...
- POJ 1286 Necklace of Beads(项链的珠子)
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7874 Accepted: 3290 ...
- Necklace of Beads(polya计数)
Necklace of Beads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7451 Accepted: 3102 ...
- POJ1509 Glass Beads
Glass Beads Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 4314 Accepted: 2448 Descr ...
- POJ1509 Glass Beads(最小表示法 后缀自动机)
Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 4901 Accepted: 2765 Description Once ...
- Necklace of Beads (polya定理的引用)
Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...
随机推荐
- Virtuabox 虚拟机克隆方法
起初我觉得直接复制一个.vdi 虚拟硬盘再挂上去就可以了,没想到 Virtualbox居然提示UUID重复,看起来就是有点像com生成的那种ID, 查了一下,才知道原来不能这么用 可以通过Vritua ...
- Websocket教程SpringBoot+Maven整合(详情)
1.大话websocket及课程介绍 简介: websocket介绍.使用场景分享.学习课程需要什么基础 笔记: websocket介绍: WebSocket协议是基于TCP的一种新的网络协议.它实现 ...
- 操作系统(4)_进程同步_李善平ppt
生产者进程count++是它的临界区,消费者count--是它的临界区. 经典同步问题,死锁问题,略.
- 谭浩强 c++程序设计第一章课后习题 第10题
#include <iostream> using namespace std; int main() { int a,b,c; cout<<"请输入三个整数类型的数 ...
- wepy一些问题和解决方案
wepy一些问题和解决方案 小程序开发和传统的web开发有相识的地方,但是也有不同的地方,要区分. computed属性名和props属性名重复 如果那个组件的渲染值是重名的computed属性,每次 ...
- Java读取各种文件格式内容
所需的jar包哦也不要太记得了,大家可以搜搜,直接上代码: import java.io.BufferedInputStream; import java.io.File; import java.i ...
- 【Python学习之九】asyncio—异步IO
asyncio 这是python3.4引入的标准库,直接内置对异步IO的支持.asyncio的编程模型就是一个消息循环.从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程 ...
- PHP跨域请求nodejs
摘要:用nodejs作为服务器,php作为客服端进行跨域请求,并返回数据. 一:windows环境下的nodejs安装(以及express模板的安装):http://blog.uifanr.com/2 ...
- 关于使用Java开发Mis系统
如何使用Java开发一个小型的信息管理系统,首先我们应该知道要使用什么样的方法. 1.Java基础 2.JSP+Servlet+JavaBean JSP是服务器端的编程语言,见得比较多的是在一些网站上 ...
- 静态属性property的本质和应用
一.本质 静态属性property本质就是实现了get,set,delete三种方法 class Foo: @property def AAA(self): print('get的时候运行我啊') @ ...