C. Necklace

题目连接:

http://www.codeforces.com/contest/613/problem/C

Description

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 Input

3

4 2 1

Sample Output

1

abacaba

Hint

题意

你有n个颜色的珠子,你要把所有珠子都穿成一个圆环

然后你可以从圆环的某一个地方剪开,使得这个链是一个回文串

然后问你怎么样去构造那个圆环,可以使得构成回文串的链最多

题解

考虑对称性,如果答案是ans的话,那么这ans刀一定是等分这个圆环的

每一个块内的珠子数量肯定是相同的

那么显然要满足这两个特性,ans = gcd(a[i])

有两个特殊情况需要考虑

如果存在大于1个颜色的珠子为奇数的话,答案为0,显然

如果有一个颜色的珠子为奇数的话,只要把这个珠子放在中间就好了,相当于把其他的偶数都砍一半,然后去摆放。

代码

#include<bits/stdc++.h>
using namespace std; int p[30];
int odd = 0;
int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
int main()
{
int n;scanf("%d",&n);
int t = 0;
for(int i=0;i<n;i++)
{
scanf("%d",&p[i]);
if(p[i]%2==1)t=i,odd++;
}
if(odd>1)
{
printf("0\n");
for(int i=0;i<n;i++)
for(int j=0;j<p[i];j++)
printf("%c",'a'+i);
printf("\n");
return 0;
}
if(odd==1)
{
int ans = 0;
for(int i=0;i<n;i++)
{
if(i==t)ans=gcd(p[i],ans);
else ans=gcd(p[i]/2,ans);
}
printf("%d\n",ans);
for(int i=0;i<ans;i++)
{
for(int j=0;j<n;j++)
for(int k=0;k<p[j]/ans/2;k++)
printf("%c",'a'+j);
printf("%c",'a'+t);
for(int j=n-1;j>=0;j--)
for(int k=0;k<p[j]/ans/2;k++)
printf("%c",'a'+j);
}
}
else
{
int ans = 0;
for(int i=0;i<n;i++)
ans=gcd(p[i],ans);
printf("%d\n",ans);
for(int i=0;i<ans/2;i++)
{
for(int j=0;j<n;j++)
for(int k=0;k<p[j]/ans;k++)
printf("%c",'a'+j);
for(int j=n-1;j>=0;j--)
for(int k=0;k<p[j]/ans;k++)
printf("%c",'a'+j);
}
}
}

Codeforces Round #339 (Div. 1) C. Necklace 构造题的更多相关文章

  1. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  2. Educational Codeforces Round 7 D. Optimal Number Permutation 构造题

    D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...

  3. Codeforces Round #339 (Div.2)

    A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. Codeforces Round #339 (Div. 1) A. Peter and Snow Blower 计算几何

    A. Peter and Snow Blower 题目连接: http://www.codeforces.com/contest/613/problem/A Description Peter got ...

  5. Codeforces Round #339 (Div. 2) B. Gena's Code 水题

    B. Gena's Code 题目连接: http://www.codeforces.com/contest/614/problem/B Description It's the year 4527 ...

  6. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  7. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  8. Codeforces Round #306 (Div. 2) ABCDE(构造)

    A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...

  9. Codeforces Round #298 (Div. 2) D. Handshakes 构造

    D. Handshakes Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/problem ...

随机推荐

  1. PHP路由代码

    <?php /**  * 路由  * @author 角度 QQ:1286522207  *  */ class Dispatcher extends Action {     private ...

  2. "Flags mismatch irq" register interrupt handler error

    Question : When you see the log "Flags mismatch irq ............", maybe you use the same ...

  3. 64_g1

    GAPDoc-1.5.1-12.fc26.noarch.rpm 13-Feb-2017 22:37 1082286 GAPDoc-latex-1.5.1-12.fc26.noarch.rpm 13-F ...

  4. tiny-rtems-src

    https://github.com/RTEMS/rtems-libbsd https://github.com/freebsd/freebsd/tree/642b174daddbd0efd9bb5f ...

  5. [c++,bson] linux 使用 BSON 编程[www]

    [c++,bson] linux 使用 BSON 编程 http://blog.chinaunix.net/uid-28595538-id-4987410.html 1.js db=db.getSib ...

  6. 【codeforces85D】

    去实验培训回来了……写个题先玩玩 这题给人一种平衡树的感觉 但是呢,实际上操作离线+离散化+线段树一样能做 #include<bits/stdc++.h> #define lson (o& ...

  7. VPS速度测试(4):上传下载速度、服务器带宽、Ping响应时间

    1.VPS的速度好坏经常是我们选择某一个VPS商家的重要参考指标,对于国外的VPS主机我们可以执行以下命令来测试VPS入口带宽是多少. wget https://cachefly.cachefly.n ...

  8. ES6新数据结构Set让数组去重

    function unique(array){ return Array.from(new Set(array)); } var arr = ['aa','bb','cc','',1,0,'1',1, ...

  9. hadoop3.1集成yarn ha

    1.角色分配

  10. 你想了解的轮询、长轮询和websocket都在这里了

    日常生活中,有很多需要数据的实时更新,比如群聊信息的实时更新,还有投票系统的实时刷新等 实现的方式有很多种,比如轮询.长轮询.websocket 轮询 轮询是通过设置页面的刷新频率(设置多长时间自动刷 ...