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

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.

简单题意

给你很多个珠子(第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代码

时间: 2024-10-19 18:05:43

Necklace - CF613C的相关文章

Section1.1 -- Broken Necklace

Broken Necklace You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29: 1 2 1 2 r b b r b r r b r b b b r r b r r r w r b r w w b b r r

HDU5730 Shell Necklace

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 999    Accepted Submission(s): 434 Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell necklac

UVA 11054 The Necklace 转化成欧拉回路

题意比较简单,给你n个项链碎片,每个碎片的两半各有一种颜色,最后要把这n个碎片串成一个项链,要求就是相邻碎片必须是同种颜色挨着. 看了下碎片总共有1000个,颜色有50种,瞬间觉得普通方法是无法在可控时间内做出来的,因为碎片到底放哪里以及是正着放还是反着放都是不可控的. 这个时候数学建模就真的好重要了,如果我们能把颜色作为节点,一个碎片就表示两个节点连了一条路,那其实就是走了一遍欧拉回路,就意味着项链做成了. 太叼了,这个思想真心不错...LRJ书上的提示,否则我还真是想不到可以这样. 不过还有

洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace

P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A 中的项链可以用下面的字符串表示: brbrrrbbbrrrrrbrrbbrbbbbrrrrb 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收

Uva10054 The Necklace

Uva10054 欧拉回路 题意:给n个珠子,每个珠子的两端的颜色不同.问能否组成一条项链,两颗相邻的珠子相邻的两端颜色相同. 思路:把颜色看做点.珠子看做边.每一个珠子在两种颜色之间连边.无向图中任意一点的度数都为偶数,则该图具有欧拉回路.然后通过euler函数来输出即可. /* ID: onlyazh1 LANG: C++ TASK: The Necklace */ #include<iostream> #include<cmath> #include<stack>

[数据生成器]UVA10054 The Necklace

应吴老师之邀,写了个数据生成器. 目前这个数据生成器可以保证生成的数据都是合法的,且效率也还不错.只是在建立普通连通图的时候zyy偷懒了,直接把所有点串起来从而保证图的连通.如果有大神有更好的方法请不吝指教,zyy不胜感谢~~ 下面是代码: 1 #include<cstdio> 2 #include<ctime> 3 #include<cstring> 4 #include<cstdlib> 5 #include<cmath> 6 #includ

[题解]UVA10054 The Necklace

链接:http://vjudge.net/problem/viewProblem.action?id=18806 描述:给出一堆珠子,每个珠子有两种颜色,有一端颜色相同的珠子可以串在一起,问是否可以把所有珠子串在一起,并求其中一种方案. 思路:欧拉回路 以颜色作为节点,以珠子作为边建图,无向图. 下面是我的实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespac

hdu 1817 Necklace of Beads(Polya定理)

题目链接:hdu 1817 Necklace of Beads 这题的弱化版:传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 typedef long long ll; 5 6 int t,n,m,cas; 7 8 ll mypow(ll a,ll k){ 9 ll an=1; 10 while(k){ 11 if(k&1)an=an*a

SCU - 4441 Necklace(树状数组求最长上升子数列)

Necklace frog has \(n\) gems arranged in a cycle, whose beautifulness are \(a_1, a_2, \dots, a_n\). She would like to remove some gems to make them into a beautiful necklace without changing their relative order. Note that a beautiful necklace can be