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 test(s)
Input
3
4 2 1
Output
1
abacaba
Input
1
4
Output
4
aaaa
Input
2
1 1
Output
0
ab
Note

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的更多相关文章

  1. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  2. 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 ...

  3. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  4. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  5. USACO section1.1 Broken Necklace

    /* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...

  6. [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链

    [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...

  7. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  8. Accepted Necklace

    Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 2660 Accepted Necklace

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...

随机推荐

  1. <进阶版>Markdown指南

    有道云笔记内置Markdown编辑器和使用指南. “进阶版”有道云笔记Markdown指南,教你如何进一步掌握待办.清单.流程图和甘特图. 0 待办和清单 待办事项和清单在日常工作.生活中经常被使用. ...

  2. php session存入redis

    php的会话默认以文件的形式存在,可以配知道NOSQL中,既可以提高访问速度又能好好的实现回话共享,在后期做负载均衡时实现多台服务器session 同步也是比较方便: 一:在php配置文件中改 修改p ...

  3. 初试Docker on Debian on VirtualBox

    一直以来都对Docker如雷贯耳,很想尝试一下但都被各种忙给耽误了,最近由于项目调试,需要安装 Oracle 和 SQL Server 数据库,但又不想安装到本机系统里,于是下决心啃一下docker这 ...

  4. hdu1051 Wooden Sticks(贪心+排序,逻辑)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. Android开发-API指南-<activity>

    <activity> 英文原文:http://developer.android.com/guide/topics/manifest/activity-element.html 采集(更新 ...

  6. Hadoop第二课:Hadoop集群环境配置

    一.Yum配置 1.检查Yum是否安装 rpm -qa|grep yum 2.修改yum源,我使用的是163的镜像源(http://mirrors.163.com/),根据自己的系统选择源, #进入目 ...

  7. parity 注记词

    spousal tint untold around rosy daintily unrated sheep choice showpiece chirping gala

  8. 阿里云服务器 操作实战 部署C语言开发环境(vim配置,gcc) 部署J2EE网站(jdk,tomcat)

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 博客总结 : 设置SecureCRT ...

  9. 自定义View 和 ViewGroup

    一. 自定义View介绍 自定义View时, 继承View基类, 并实现其中的一些方法. (1) ~ (2) 方法与构造相关 (3) ~ (5) 方法与组件大小位置相关 (6) ~ (9) 方法与触摸 ...

  10. 《剑指offer》---寻找反转数组最小值

    本文算法使用python3实现 1.题目描述:   把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...