Codeforces Round #339 (Div. 1) C. Necklace 构造题
C. Necklace
题目连接:
http://www.codeforces.com/contest/613/problem/C
Description
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.
Input
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.
Output
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.
Sample Input
3
4 2 1
Sample Output
1
abacaba
Hint
题意
你有n个颜色的珠子,你要把所有珠子都穿成一个圆环
然后你可以从圆环的某一个地方剪开,使得这个链是一个回文串
然后问你怎么样去构造那个圆环,可以使得构成回文串的链最多
题解
考虑对称性,如果答案是ans的话,那么这ans刀一定是等分这个圆环的
每一个块内的珠子数量肯定是相同的
那么显然要满足这两个特性,ans = gcd(a[i])
有两个特殊情况需要考虑
如果存在大于1个颜色的珠子为奇数的话,答案为0,显然
如果有一个颜色的珠子为奇数的话,只要把这个珠子放在中间就好了,相当于把其他的偶数都砍一半,然后去摆放。
代码
#include<bits/stdc++.h>
using namespace std;
int p[30];
int odd = 0;
int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
int main()
{
int n;scanf("%d",&n);
int t = 0;
for(int i=0;i<n;i++)
{
scanf("%d",&p[i]);
if(p[i]%2==1)t=i,odd++;
}
if(odd>1)
{
printf("0\n");
for(int i=0;i<n;i++)
for(int j=0;j<p[i];j++)
printf("%c",'a'+i);
printf("\n");
return 0;
}
if(odd==1)
{
int ans = 0;
for(int i=0;i<n;i++)
{
if(i==t)ans=gcd(p[i],ans);
else ans=gcd(p[i]/2,ans);
}
printf("%d\n",ans);
for(int i=0;i<ans;i++)
{
for(int j=0;j<n;j++)
for(int k=0;k<p[j]/ans/2;k++)
printf("%c",'a'+j);
printf("%c",'a'+t);
for(int j=n-1;j>=0;j--)
for(int k=0;k<p[j]/ans/2;k++)
printf("%c",'a'+j);
}
}
else
{
int ans = 0;
for(int i=0;i<n;i++)
ans=gcd(p[i],ans);
printf("%d\n",ans);
for(int i=0;i<ans/2;i++)
{
for(int j=0;j<n;j++)
for(int k=0;k<p[j]/ans;k++)
printf("%c",'a'+j);
for(int j=n-1;j>=0;j--)
for(int k=0;k<p[j]/ans;k++)
printf("%c",'a'+j);
}
}
}
Codeforces Round #339 (Div. 1) C. Necklace 构造题的更多相关文章
- Codeforces Round #524 (Div. 2)(前三题题解)
这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces Round #339 (Div.2)
A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #339 (Div. 1) A. Peter and Snow Blower 计算几何
A. Peter and Snow Blower 题目连接: http://www.codeforces.com/contest/613/problem/A Description Peter got ...
- Codeforces Round #339 (Div. 2) B. Gena's Code 水题
B. Gena's Code 题目连接: http://www.codeforces.com/contest/614/problem/B Description It's the year 4527 ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- Codeforces Round #298 (Div. 2) D. Handshakes 构造
D. Handshakes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...
随机推荐
- python自动开发之第二十五天
一.组合搜索 参考: http://www.cnblogs.com/ccorz/p/5985205.html 二.JSONP 1.在同源策略下,在某个服务器下的页面是无法获取到该服务器以外的数据的,但 ...
- Linux内核的架构
GNU/Linux操作系统架构 备注:IPC进程间通.IPC(Inter-Process Communication)是共享"命名管道"的资源,它是为了让进程间通信而开放的命名管道 ...
- Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】
转自:http://www.th7.cn/system/lin/201606/167750.shtml 我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一 ...
- python基础===获取知乎标题时候,文件编码失败的总结
总结一下,关于获取到的信息编码失败. 刚才在执行代码的时候,发现一个问题: 然后修改代码如下: '''爬取知乎界面的标题''' import requests import re import sys ...
- python中的enumerate获取迭代元素的下标
以前迭代的时候,需要获取次数都是如下格式: index=1 for node in nodes: if index==3: continue print(node.text_content())ind ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff 思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为 n 的简单多边形,每次只能画一条边或者一个格子的对角 ...
- tomcat组成介绍和调优方案
1.tomcat组成介绍 1.1 目录组成介绍 1.2 启动tomcat中遇到的问题 a.启动过程中出现很多异常:因为端口被占用了 解决方式1:修改Tomcat\conf\server.xml中的默认 ...
- tab切换 jQuery
$('p.guidan-load1').click(function(){ $("p.guidan-load1").removeClass("guidan-load12& ...
- sql server中字符串无法替换空格的问题
直接上代码: select case when 'workReport'=LTRIM(RTRIM(' workReport ')) then 'trim去空格成功' when 'workReport' ...
- go run/ go install/ go build / go get的区别
go run 运行当个.go文件 go install 在编译源代码之后还安装到指定的目录 go build 加上可编译的go源文件可以得到一个可执行文件 go get = git clone + g ...