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.

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的环来了

 1 #include<cstdio>
 2 using namespace std;
 3 
 4 int n,c,x,a[30];
 5 
 6 int gcd(int a,int b){
 7     if(b==0)return a;
 8     return gcd(b,a % b);
 9 }
10 
11 int main(){
12     scanf("%d",&n);
13     int i,j,k;
14     for(i=1;i<=n;i++)scanf("%d",&a[i]);
15     for(i=1;i<=n;i++)
16     if((a[i]&1)==1)c++,x=i;
17     if(c>1){
18         printf("0\n");
19         for(i=1;i<=n;i++){
20             while(a[i]>0){
21                 --a[i];
22                 printf("%c",'a'-1+i);
23             }
24         }
25     }
26     else
27     if(c==1){
28         c=a[1];
29         for(i=2;i<=n;i++)c=gcd(c,a[i]);
30         printf("%d\n",c);
31         for(i=1;i<=c;i++){
32             for(j=1;j<=n;j++){
33                 if(j!=x)
34                 for(k=1;k<=a[j]/c/2;k++)printf("%c",'a'-1+j);
35             }
36             for(j=1;j<=a[x]/c;j++)printf("%c",'a'-1+x);
37             for(j=n;j>0;j--){
38                 if(j!=x)
39                 for(k=1;k<=a[j]/c/2;k++)printf("%c",'a'-1+j);
40             }
41         }
42     }
43     else{
44         c=a[1];
45         for(i=2;i<=n;i++)c=gcd(c,a[i]);
46         printf("%d\n",c);
47         for(i=1;i<=c/2;i++){
48             for(j=1;j<=n;j++){
49                 for(k=1;k<=a[j]/c;k++)printf("%c",'a'-1+j);
50             }
51             for(j=n;j>0;j--){
52                 for(k=1;k<=a[j]/c;k++)printf("%c",'a'-1+j);
53             }
54         }
55     }
56     return 0;
57 }
AC代码

 

posted @ 2016-05-25 17:26  Randolph87  阅读(808)  评论(0编辑  收藏  举报