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 #446 (Div. 1) B. Gluttony 构造 + 补集思想

    传送门 文章目录 题意: 思路: 题意: 给你一个数组aaa,保证aaa中每个数都互不相同,让你构造一个数组bbb,满足对于任意的S=x1,x2,...,xk,1≤xi≤n,0≤k<nS={x_ ...

  2. Codeforces Round #592 (Div. 2) F. Chips 构造 + 细节

    传送门 文章目录 题意: 思路: 题意: 思路: 恶心的构造题,思路很简单但是代码细节很多,搞了半天. 根据题目的性质不难发现,如果有两个相同颜色的球相邻,那么他们的颜色永远不会改变. 根据这个性质, ...

  3. Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)

    CF链接  http://codeforces.com/contest/1011/problem/A Natasha is going to fly to Mars. She needs to bui ...

  4. Codeforces Round #196 (Div. 2) A. Puzzles 水题

    A. Puzzles Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  5. Codeforces Round #249 Div.2 435 BD两题题解

    B D B 给个长度最多为18的数字,交换相邻数字不超过k次,求能够产生的最大数字. 该贪心的要贪心. 很明显把大的数字换到最高位显然更好. 我们从最高位开始枚举iii,从i" role=& ...

  6. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  7. 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

    题目传送门 1 /* 2 题意:删除若干行,使得n行字符串成递增排序 3 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 4 */ 5 /************* ...

  8. 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation

    题目传送门 1 /* 2 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 3 这样保证不会超出边界并且以防其余的数相邻绝对值差>k 4 ...

  9. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 1 /* 2 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 3 */ 4 /**************************************** ...

  10. Codeforces Round #742 (Div. 2) F. One-Four Overload 构造 + 二分图染色

    传送门 文章目录 题意: 思路: 题意: 给你一个n∗mn*mn∗m的矩形,包含...和XXX,你有两种颜色,你需要给...染色使得每个XXX上下左右相邻的...其两种颜色个数相同,输出一种合法方案. ...

最新文章

  1. Arduino vs Raspberry Pi vs BeagleBone
  2. linux下配置DHCP中继代理
  3. v-if和v-show
  4. SAP MTA打包的一些常见错误
  5. 4-数据结构-串的学习
  6. 24时区,GMT,UTC,DST,CST时间详解
  7. RPC远程过程调用概念及实现
  8. access建立两个字段唯一索引_面试官:谈谈你对mysql索引的认识?
  9. C语言数据结构之图的邻接矩阵的应用实例
  10. 【高手推荐:主题下载】
  11. 矩阵求导术(二)——矩阵对矩阵的求导
  12. php sqlserver 日期转字符串,sqlserver  时间(datetime)转换成字符串
  13. 【C#】WinForm 之 SQL Server 服务监控器(避免开机启动服务)
  14. 解除百度网盘下载限速
  15. PDF转图片怎么转?分享两种转换小技巧
  16. jquery给日期赋值_关于jQuery赋值
  17. 国庆作业之感想与总结
  18. 密码学学习笔记(五)——CKKS同态加密方案
  19. VScode使用服务器的Python解释器
  20. [37期]猎鹰组战记

热门文章

  1. 七牛云异步抓取java_带你玩转七牛云存储——高级篇
  2. C语言定义定长整型数组,C语言变长讯息定义:柔性数组
  3. 多线程中的互斥控制程序代码_互斥锁解决 Python 中多线程共享全局变量的问题...
  4. django图片上传到oss_从攻防角度看oss安全(二)
  5. java适配器模式 场景_详解Java适配器模式
  6. 查看Linux版本命令
  7. dbeaver生成结构图_DBeaver的简易操作和建议(一个神奇的数据库操作软件)
  8. loadGrid layui
  9. 次时代Java编程(一):续 vertx-sync实践
  10. ElasticSearch(1)CentOS安装ElasticSearch测试CRUD