Necklace - CF613C
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).

Ivan has beads of n colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.
The first line of the input contains a single number n (1 ≤ n ≤ 26) — the number of colors of beads. The second line contains after n positive integers ai — the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000.
In the first line print a single number — the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace.
Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.
3
4 2 1
1
abacaba
1
4
4
aaaa
2
1 1
0
ab
In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.
In the second sample there is only one way to compose a necklace.
简单题意
给你很多个珠子(第i种颜色有Ai种,颜色最多有26种,用小写字母表示),让你串成一条项链,然后项链有一个优美值
优美值=优美的cut的个数
一个优美的cut表示从这个地方剪断项链,使得其变成一个回文串
然后你要求出最大的优美值,然后给出一个方案
胡说题解
分情况讨论
1.如果个数中没有奇数,那么答案就是所有数字的gcd,然后构造答案就是输出gcd/2个回文串
2.如果个数中只有一个奇数,那么答案也是所有数字的gcd,然后构造答案就是输出gcd个回文串,个数为奇数的颜色放在回文串的中间
3.如果个数中有两个或以上的奇数,那么答案就是0,因为两个奇数就已经构造不出有优美cut的环来了
#include<cstdio>
using namespace std; int n,c,x,a[]; int gcd(int a,int b){
if(b==)return a;
return gcd(b,a % b);
} int main(){
scanf("%d",&n);
int i,j,k;
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)
if((a[i]&)==)c++,x=i;
if(c>){
printf("0\n");
for(i=;i<=n;i++){
while(a[i]>){
--a[i];
printf("%c",'a'-+i);
}
}
}
else
if(c==){
c=a[];
for(i=;i<=n;i++)c=gcd(c,a[i]);
printf("%d\n",c);
for(i=;i<=c;i++){
for(j=;j<=n;j++){
if(j!=x)
for(k=;k<=a[j]/c/;k++)printf("%c",'a'-+j);
}
for(j=;j<=a[x]/c;j++)printf("%c",'a'-+x);
for(j=n;j>;j--){
if(j!=x)
for(k=;k<=a[j]/c/;k++)printf("%c",'a'-+j);
}
}
}
else{
c=a[];
for(i=;i<=n;i++)c=gcd(c,a[i]);
printf("%d\n",c);
for(i=;i<=c/;i++){
for(j=;j<=n;j++){
for(k=;k<=a[j]/c;k++)printf("%c",'a'-+j);
}
for(j=n;j>;j--){
for(k=;k<=a[j]/c;k++)printf("%c",'a'-+j);
}
}
}
return ;
}
AC代码
Necklace - CF613C的更多相关文章
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 2016 Multi-University Training Contest 1 H.Shell Necklace
Shell Necklace Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- USACO section1.1 Broken Necklace
/* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...
- [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链
[BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- Accepted Necklace
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
随机推荐
- 北京Uber优步司机奖励政策(1月13日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- [Python3.X]python 实现斐波那契数列
斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一 ...
- 新版本Eclipse安装后插件都在哪里?
201903版本的Eclipse,选择win安装,下载后的安装包大小只有48.7Mb, 双击安装会会弹出类似eclipse网页,选择需要安装的类型,一般选择Java EE版本 选择好版本后,选择安装目 ...
- 抽样分布(3) F分布
定义 设U~χ2(n1), V~χ2(n2),且U,V相互独立,则称随机变量 服从自由度为(n1,n2)的F分布,记为F~F(n1,n2),其中n1叫做第一自由度,n2叫做第二自由度. F分布的概率密 ...
- 创建一个socket服务实时统计在线人数
主要是两个文件,一个是后端文件,一个是前端文件: 后端文件:有人登录了,就通知所有的正在访问的页面,把总人数+1:反之-1: 前端文件:有人登录了,通知后端,页面关闭了,通知后端,同时接收后端派发来的 ...
- spring boot 报错 Error creating bean with name
Application 启动类 要和父目录平级
- Redhat linux 安装SVN服务器 CollabNetSubversionEdge
请仔细阅读安装包自带的readme文件! ================================================= 1. 先去官网,找安装包: http://subversi ...
- http://www.yiibai.com/javalang/string_endswith.html
http://www.yiibai.com/javalang/string_endswith.html
- 关于docker 基础使用记录
Docker Hub地址:https://hub.docker.com Docker Hub 存放着 Docker 及其组件的所有资源.Docker Hub 可以帮助你与同事之间协作,并获得功能完整的 ...
- Spark GraphX 2
顶点:VertexRDD 边:EdgeRDD.Edge.EdgeDirection Triplet:EdgeTriplet 存储:PartitionStrategy 通常的存储方式有两种: ...