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).c++

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

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

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

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

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

In the second sample there is only one way to compose a necklace.ci

簡單題意rem

給你不少個珠子(第i種顏色有Ai種,顏色最多有26種,用小寫字母表示),讓你串成一條項鍊,而後項鍊有一個優美值input

優美值=優美的cut的個數it

一個優美的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代碼
相關文章
相關標籤/搜索