E - Necklace

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

Example

Input
34 2 1
Output
1abacaba
Input
14
Output
4aaaa
Input
21 1
Output
0ab

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.

题目的大意就是,给出一个整数n,再给出n个整数a[i],分别表示颜色为i的珠子有a[i]个。求构造一串项链,使项链从某处剖开后对称,并使能剖开处的位置最多。输出时,先输出一个整数表示可剖开处的个数,然后输出

项链。相应的数字用对应的字母表示,如第一种颜色对a,第二种颜色对b...。

那么,由于我们需要尽可能多的"剖开处",这就意味着剖开后再拼接,字符串的构造与原来一样,这种情况要尽可能的多.

继续探究(下图):

在上图中,a,b的接口就是剖开处,显然a必定是b的前缀,也是b的后缀,至于中间部分,那还是可以继续分下去,然后又进行剖割......

我们可以假设一下,a是最小的剖割单位(即子串a无法继续剖割),那由于字符串是我们来构造的,那么一个贪心的想法,子串b中部必定有若干个子串a(这样,这个字符串才满足要求)

那么,我们就可以将目标串划分为若干个子串,这些子串是回文的,且不能再划分.

那到底有几个子串?我们要分类讨论.

首先,有可能根本构不成这样的目标串.什么时候?a[i]中存在两个及以上的奇数.这样的话,在每一个相同的子串中,你无法确定,正中间放谁.

另外,如果只有1个奇数,那么这个字符一定在每一个子串的中间.那究竟最多可以划分多少个子串?gcd(a[i])个.(这个应该不用证明了吧)

而且能保证,每个子串中,某个字符出现1次的有且只有1个.(这个也不用证了吧,水一水就好了)

然后回文输出就行.

如果根本没有奇数,那怎么办?

最多能划分成gcd(a[i])/2个子串(注意并不是gcd(a[i])).为什么?我们用反证法.假设能分成gcd(a[i])个子串,那么每个子串里面包含出现奇数次的字符的个数有可能不止一个,如果真的不止一个,那就不满足要求了.

然后还是分布在两边回文输出.

 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<iostream>
 using namespace std;
 ],odd;
 ?x:gcd(y,x%y);}
 int main(){
     cin>>n,odd=;
     ; i<=n; i++) cin>>a[i],odd+=a[i]%;
     ){
         puts(");
         ; i<=n; i++)
             ; j<=a[i]; j++) printf(+(int)'a'));
         ;
     }
     ];
     ; i<=n; i++) G=gcd(G,a[i]);
     printf("%d\n",G);
     ;
     ; i<=n; i++) a[i]/=G,len+=a[i];
     ){
         ; t<=G; t++){
             ; i<=n; i++) ==)
                 ; j<=a[i]/; j++) printf(+(int)'a'));
             ; i<=n; i++) ==)
                 ; j<=a[i]; j++) printf(+(int)'a'));
             ; i--) ==)
                 ; j<=a[i]/; j++) printf(+(int)'a'));
         }
     }else{
         ; t<=G/; t++){
             ; i<=n; i++)
                 ; j<=a[i]; j++) printf(+(int)'a'));
             ; i--)
                 ; j<=a[i]; j++) printf(+(int)'a'));
         }
     }
     ;
 }

[CodeForces - 614E] E - Necklace的更多相关文章

  1. Codeforces 614E - Necklace

    614E - Necklace 思路:如果奇数超过1个,那么答案是0:否则,所有数的gcd就是答案. 构造方案:每个数都除以gcd,如果奇数个仍旧不超过1个,找奇数个那个在中间(如果没有奇数默认a), ...

  2. Codeforces Round #339 (Div. 1) C. Necklace 构造题

    C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...

  3. Codeforces 526D - Om Nom and Necklace 【KMP】

    ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...

  4. Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串

    D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. 【Codeforces 526D】Om Nom and Necklace

    Codeforces 526 D 题意:给一个字符串,求每个前缀是否能表示成\(A+B+A+B+\dots+A\)(\(k\)个\(A+B\))的形式. 思路1:求出所有前缀的哈希值,以便求每个子串的 ...

  6. Codeforces 526D Om Nom and Necklace (KMP)

    http://codeforces.com/problemset/problem/526/D 题意 给定一个串 T,对它的每一个前缀能否写成 A+B+A+B+...+B+A+B+A+B+...+B+A ...

  7. Codeforces 526.D Om Nom and Necklace

    D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. CodeForces 526D Om Nom and Necklace

    洛谷题目页面传送门 & CodeForces题目页面传送门 给定字符串\(a\),求它的每一个前缀,是否能被表示成\(m+1\)个字符串\(A\)和\(m\)个字符串\(B\)交错相连的形式, ...

  9. Codeforces 1361C - Johnny and Megan's Necklace(欧拉回路)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉这个题作为 D1C 还是蛮合适的-- 首先不难发现答案不超过 \(20\),所以可以直接暴力枚举答案并 check 答案是否 ...

随机推荐

  1. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  2. 【译】第13节---数据注解-Required

    原文:http://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-firs ...

  3. 解决:springmvc中接收date数据问题

    这里提供三种解决方案. 一.局部转换 :只是对当前Controller类有效 springMVC.xml中添加: <bean class="org.springframework.we ...

  4. 深入理解char * ,char ** ,char a[ ] ,char *a[]

    1.数组的本质 数组是多个元素的集合,在内存中分布在地址相连的单元中,所以可以通过其下标访问不同单元的元素. 2.指针 指针也是一种变量,只不过它的内存单元中保存的是一个标识其他位置的地址.由于地址也 ...

  5. _spellmod_leech_aura

    comment  备注 aura  光环ID,玩家有这个光环时候造成的伤害会转化成吸血效果 chance  每次伤害转化成吸血效果的几率 type  吸血的类型,数据库枚举类型,可以直接选取 base ...

  6. dml语句和ddl语句 区别

    delete from user删除所有记录,属于dml语句,一条记录一条记录删除.事务可以作用在dml语句上的 truncate table user;删除所有记录,属于ddl语句,将表删除,然后重 ...

  7. Java三种代理模式:静态代理、动态代理和cglib代理

    一.代理模式介绍 代理模式是一种设计模式,提供了对目标对象额外的访问方式,即通过代理对象访问目标对象,这样可以在不修改原目标对象的前提下,提供额外的功能操作,扩展目标对象的功能. 简言之,代理模式就是 ...

  8. VS2010_慢

    ZC:IntelliSense 一旦关闭,代码提示 也就没有了... ZC:IntelliSense 和 IntelliTrace,不是一个东西... 1.http://blog.csdn.net/c ...

  9. hbase非结构化数据库与结构化数据库比较

    目的:了解hbase与支持海量数据查询的特性以及实现方式 传统关系型数据库特点及局限 传统数据库事务性特别强,要求数据完整性及安全性,造成系统可用性以及伸缩性大打折扣.对于高并发的访问量,数据库性能不 ...

  10. vlookup+match高亮显示行

    VLOOPUP =VLOOKUP("*"&O3,$A$2:$B$38,2,0) 第一个参数查找值,第二个参数查找范围,第三个参数返回第几列的值,第四个参数匹配方式(“,”, ...