Necklace CodeForces - 613C

Necklace

CodeForces - 613C

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.

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

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,c,x,a[30];
int gcd(int a,int b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}
int main(){
    scanf("%d",&n);
    int i,j,k;
    for(i = 1; i <= n; i++){
        scanf("%d",&a[i]);
    }
    for(i = 1; i <= n; i++){
        if(a[i] & 1){
            c++;//奇数个数的颜色数
            x = i;
        }
    }
    if(c > 1){//如果奇数个数的颜色数大于1个那么一定无法构成回文
        printf("0\n");//直接输出
        for(i = 1; i <= n; i++){
            while(a[i]){
                a[i]--;
                printf("%c",'a'-1+i);
            }
        }
    }
    else{
        if(c == 1){//当只有一个奇数个数的颜色时
            c = a[1];
            for(i = 2; i <= n; i++){
                c = gcd(c,a[i]);
            }//求出每种颜色个数的最大公因数,代表可以分成几组,就有几处可以cut
            printf("%d\n",c);
            for(i = 1; i <= c; i++){//对于每一组,每一组每一组的输出,每一组都是回文
                for(j = 1; j <= n; j++){
                    if(j != x){//每一组的回文两头偶数的颜色
                        for(k = 1; k <= a[j]/c/2; k++)//a[j]/c一组中这个颜色需要的总数,再除2表示一边的个数
                            printf("%c",'a'-1+j);
                    }
                }
                for(j = 1; j <= a[x]/c; j++)
                    printf("%c",'a'-1+x);//中间奇数颜色
                for(j = n; j > 0; j--){//尾部偶数颜色回文
                    if(j != x){
                        for(k = 1; k <= a[j]/c/2; k++)
                            printf("%c",'a'-1+j);
                    }
                }
            }
        }
        else{
            c = a[1];
            for(i = 2; i <= n; i++)
                c = gcd(c,a[i]);
            printf("%d\n",c);//分成c组,因为都是偶数,所以可以直接一组一组的按颜色顺序输出
            for(i = 1; i <= c/2; i++){
                for(j = 1; j <= n; j++){
                    for(k = 1; k <= a[j]/c; k++)
                        printf("%c",'a'-1+j);
                }
                for(j = n; j > 0; j--){
                    for(k = 1; k <= a[j]/c; k++)
                        printf("%c",'a'-1+j);
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值