Necklace

题目地址:

https://ac.nowcoder.com/acm/problem/111227

基本思路:

分情况构造:
如果宝石个数中有两个以上的奇数,那么结果一定为,随便输出一个串即可。
如果宝石个数中仅有一个奇数,那么结果为所有数的,构造考虑输出个回文串,奇数个数的宝石放在回文串的中间位置。
如果宝石个数中没有奇数,那么结果同样为所有数的,由于对称性,只要考虑构造出个回文串。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define ull unsigned long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define debug(x) cerr << #x << " = " << x << '\n';
#define pll pair <ll, ll>
#define fir first
#define sec second
#define INF 0x3f3f3f3f
#define int ll
const int mod = (int)1e9 + 7;
void add(int &x, int y) { x += y; if(x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if(x < 0) x += mod; }

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int maxn = 30;
int n,a[maxn];
signed main() {
  IO;
  cin >> n;
  int odd = -1, cnt = 0;
  rep(i, 1, n) {
    cin >> a[i];
    if (a[i] & 1) odd = i, cnt++;
  }
  string ans;
  if (cnt >= 2) {
    cout << 0 << '\n';
    rep(i, 1, n) while (a[i]--) ans += (char) ('a' + i - 1);
    cout << ans << '\n';
    return 0;
  }
  int gcd = a[1];
  rep(i, 2, n) gcd = __gcd(gcd, a[i]);
  cout << gcd << '\n';
  if (cnt == 0) {
    string tmp;
    rep(i, 1, n) {
      rep(j, 1, a[i] / gcd) tmp += (char) ('a' + i - 1);
    }
    per(i, n, 1) {
      rep(j, 1, a[i] / gcd) tmp += (char) ('a' + i - 1);
    }
    rep(i, 1, gcd / 2) ans += tmp;
    cout << ans << '\n';
  } else {
    string tmp;
    rep(i, 1, n) {
      if (i == odd) continue;
      rep(j, 1, a[i] / gcd / 2) tmp += (char) ('a' + i - 1);
    }
    rep(i, 1, a[odd] / gcd) tmp += (char) ('a' + odd - 1);
    per(i, n, 1) {
      if (i == odd) continue;
      rep(j, 1, a[i] / gcd / 2) tmp += (char) ('a' + i - 1);
    }
    rep(i, 1, gcd) ans += tmp;
    cout << ans << '\n';
  }
  return 0;
}