Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int cnt[28]; int main() { string s; int n, tot, len; cin >> s >> n; len = s.size(); for (int i = 0; i < len; ++i) ++cnt[s[i] - 'a']; int l = 1, r = len + 1; while (l < r) { int mid = (l + r) >> 1; int need = 0; for (int i = 0; i < 28; ++i) need += (cnt[i] + mid - 1) / mid; if (need <= n) r = mid; else l = mid + 1; } if (l == len + 1) { puts("-1"); return 0; } else { tot = 0; printf("%d\n", l); for (int i = 0; i < 28; ++i) { for (int j = 1; j <= (cnt[i] + l - 1) / l; ++j) { printf("%c", 'a' + i); ++tot; } } while (tot < n) { putchar('a'); ++tot; } printf("\n"); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from math import ceil s = input () n = int (input ()) count = {} for i in s: if i in count: count[i] += 1 else: count[i] = 1 if len(count) > n: print (-1) else: if len(s) < n: print (1) ret = s else: l,h = len (s)//n, len (s) while (l < h): m = (l+h) // 2 tot = 0 for i in count: tot += ceil (count[i]/m) if tot > n: l = m+1 else: h = m print (l) ret = '' for i in count: for j in range (ceil (count[i]/l)): ret += i for i in range (n-len(ret)): ret += s[0] print (ret)
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; template <class T> inline bool gmin(T &a, T b) { if (a > b) return a = b, true; return false; } template <class T> inline bool gmax(T &a, T b) { if (a < b) return a = b, true; return false; } template <class T> T exgcd(T a, T b, T &first, T &second) { if (!b) return first = (T)1, second = (T)0, a; else { T d = exgcd(b, a % b, second, first); return second -= a / b * first, d; } } const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const double DINF = 1e10; const double EPS = 1e-9; const double PI = 3.14159265358979323846264338327950288; inline int dcmp(const double &a) { return a > EPS ? 1 : (a < -EPS ? -1 : 0); } const int MAXN = 1005; int n, m; int cnt[MAXN]; char s[MAXN]; int main() { scanf("%s", s); scanf("%d", &n); int m = strlen(s); for (int i = 0; i <= m - 1; i++) cnt[s[i] - 'a']++; int tot = 0; for (int i = 0; i <= 25; i++) if (cnt[i]) tot++; if (n < tot) printf("-1\n"); else { for (int ans = 1; ans <= m; ans++) { int rest = n; for (int i = 0; i <= 25; i++) if (cnt[i]) { rest -= cnt[i] / ans + (cnt[i] % ans != 0); } if (rest >= 0) { printf("%d\n", ans); for (int i = 0; i <= 25; i++) if (cnt[i]) { for (int j = 1; j <= cnt[i] / ans + (cnt[i] % ans != 0); j++) printf("%c", i + 'a'); } for (int i = 1; i <= rest; i++) printf("a"); printf("\n"); break; } } } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int q[26]; int n; bool Check(int x) { int req = 0; for (int i = 0; i < 26; ++i) { req += (q[i] + x - 1) / x; } return req <= n; } int main() { string s; cin >> s >> n; int dif = 0; for (int i = 0; i < s.size(); ++i) { int let = s[i] - 'a'; if (!q[let]) { ++dif; } ++q[let]; } if (dif > n) { puts("-1"); return 0; } int lb = 0, ub = 1005; while (ub - lb > 1) { int mb = (ub + lb) >> 1; if (Check(mb)) { ub = mb; } else { lb = mb; } } cout << ub << endl; int used = 0; for (int i = 0; i < 26; ++i) { int v = (q[i] + ub - 1) / ub; for (int j = 0; j < v; ++j) { ++used; cout << (char)('a' + i); } } while (used < n) { ++used; cout << 'a'; } cout << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.text.DecimalFormat; import java.util.*; import java.awt.Point; public class Newbie { static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { solver s = new solver(); int t = 1; while (t > 0) { s.sol(); t--; } out.close(); } /* static class descend implements Comparator<pair1> { public int compare(pair1 o1, pair1 o2) { if (o1.pop != o2.pop) return (int) (o1.pop - o2.pop); else return o1.in - o2.in; } }*/ static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream), 32768); token = null; } public String sn() { while (token == null || !token.hasMoreTokens()) { try { token = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int ni() { return Integer.parseInt(sn()); } public String snl() throws IOException { return br.readLine(); } public long nlo() { return Long.parseLong(sn()); } public double nd() { return Double.parseDouble(sn()); } public int[] na(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.ni(); return a; } public long[] nal(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nlo(); return a; } } static class ascend implements Comparator<pair> { public int compare(pair o1, pair o2) { return o2.b - o1.b; } } static class extra { static boolean v[] = new boolean[100001]; static List<Integer> l = new ArrayList<>(); static int t; static void shuffle(int a[]) { for (int i = 0; i < a.length; i++) { int t = (int) Math.random() * a.length; int x = a[t]; a[t] = a[i]; a[i] = x; } } static void shufflel(long a[]) { for (int i = 0; i < a.length; i++) { int t = (int) Math.random() * a.length; long x = a[t]; a[t] = a[i]; a[i] = x; } } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } static boolean valid(int i, int j, int r, int c) { if (i >= 0 && i < r && j >= 0 && j < c) { // System.out.println(i+" /// "+j); return true; } else { // System.out.println(i+" //f "+j); return false; } } static void seive() { for (int i = 2; i < 101; i++) { if (!v[i]) { t++; l.add(i); for (int j = 2 * i; j < 101; j += i) v[j] = true; } } } static int binary(LinkedList<Integer> a, long val, int n) { int mid = 0, l = 0, r = n - 1, ans = 0; while (l <= r) { mid = (l + r) >> 1; if (a.get(mid) == val) { r = mid - 1; ans = mid; } else if (a.get(mid) > val) r = mid - 1; else { l = mid + 1; ans = l; } } return (ans + 1); } static long fastexpo(long x, long y) { long res = 1; while (y > 0) { if ((y & 1) == 1) { res *= x; } y = y >> 1; x = x * x; } return res; } static long lfastexpo(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } /* void dijsktra(int s, List<pair> l[], int n) { PriorityQueue<pair> pq = new PriorityQueue<>(new ascend()); int dist[] = new int[100005]; boolean v[] = new boolean[100005]; for (int i = 1; i <= n; i++) dist[i] = 1000000000; dist[s] = 0; for (int i = 1; i < n; i++) { if (i == s) pq.add(new pair(s, 0)); else pq.add(new pair(i, 1000000000)); } while (!pq.isEmpty()) { pair node = pq.remove(); v[node.a] = true; for (int i = 0; i < l[node.a].size(); i++) { int v1 = l[node.a].get(i).a; int w = l[node.a].get(i).b; if (v[v1]) continue; if ((dist[node.a] + w) < dist[v1]) { dist[v1] = dist[node.a] + w; pq.add(new pair(v1, dist[v1])); } } } }*/ } static class pair { int a; int b; public pair(int a, int b) { this.a = a; this.b = b; } } static class pair1 { pair p; int in; public pair1(pair a, int n) { this.p = a; this.in = n; } } static int inf = 5000013; static class solver { DecimalFormat df = new DecimalFormat("0.00000000"); extra e = new extra(); long mod = (long) (1000000007); void sol() throws IOException { String s = sc.sn(); int n = s.length(); int k = sc.ni(); char c[] = s.toCharArray(); int inf[] = new int[26]; int add[] = new int[26]; StringBuilder sb = new StringBuilder(); int cnt = 0; for (int i = 0; i < n; i++) { if (inf[c[i] - 'a'] == 0) { sb.append(c[i]); cnt++; } inf[c[i] - 'a']++; } if (cnt > k) { System.out.println(-1); System.exit(0); } k -= cnt; PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> ((int) Math.ceil((inf[x] * 1.0) / add[x]) - (int) Math.ceil((inf[y] * 1.0) / add[y])) * -1); for (int i = 0; i < 26; i++) { if (inf[i] > 0) { add[i]++; pq.add(i); } } while (k-- > 0 && !pq.isEmpty()) { int t = pq.poll(); sb.append((char) (t + 'a')); add[t]++; pq.add(t); } int ans = -1; for (int i = 0; i < 26; i++) { if (inf[i] > 0) ans = Math.max(ans, (int) Math.ceil((inf[i] * 1.0) / add[i])); } c = sb.toString().toCharArray(); Arrays.sort(c); out.println(ans); out.println(String.valueOf(c)); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> struct T { char c; double need, curr; T(char x, double y, double z) : c(x), need(y), curr(z){}; bool operator<(const T &o) const { return need / curr < o.need / o.curr; } }; using namespace std; int N; map<char, double> M, M2; priority_queue<T> Q; string ret, S; int main() { cin >> S >> N; for (char c : S) M[c] += 1; if (M.size() > N) cout << "-1"; else { for (pair<char, double> p : M) Q.push(T(p.first, p.second, 1)), ret += p.first, M2[p.first] = 1; N -= M.size(); while (N > 0) { T t('a', 0, 0); t = Q.top(); Q.pop(); Q.push(T(t.c, t.need, t.curr + 1)); ret += t.c; M2[t.c] += 1; N--; } double occ = 0; for (pair<char, double> p : M) occ = max(occ, p.second / M2[p.first]); occ += 0.9999999; cout << (int)occ << "\n" << ret; } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s = input() n = int(input()) cnt = {} for c in s: if cnt.get(c) == None: cnt[c] = 1 else: cnt[c] += 1 if (n < len(cnt)): print(-1) else: ansNum = 0; while(True): ansNum+=1 l = 0; char = [] for c, v in cnt.items(): need = (v + ansNum -1)//ansNum l+= need char.append((c, need)) if (l > n): continue print(ansNum) ans = "".join([str(c[0])*c[1] for c in char]) ans = ans + 'a'*(n - len(ans)) print(ans) break
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Banana implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { char[] x = in.next().toCharArray(); int n = in.ni(), unique = 0; int[] cnt = new int[26]; for (char c : x) { cnt[c - 'a']++; if (cnt[c - 'a'] == 1) unique++; } if (unique > n) { out.println(-1); return; } int lo = 1, hi = 1005, min = 1005; while (lo <= hi) { int mid = (lo + hi) / 2; int total = 0; for (int i = 0; i < 26; i++) { if (cnt[i] > 0) { if (cnt[i] <= mid) total++; else { total += (cnt[i] / mid) + (cnt[i] % mid != 0 ? 1 : 0); } } } if (total <= n) { min = Math.min(min, mid); hi = mid - 1; } else { lo = mid + 1; } } char[] result = new char[n]; int idx = 0; for (int i = 0; i < 26; i++) { if (cnt[i] > 0) { if (cnt[i] <= min) result[idx++] = (char) ('a' + i); else { int times = (cnt[i] / min) + (cnt[i] % min != 0 ? 1 : 0); for (int j = 0; j < times; j++) { result[idx++] = (char) ('a' + i); } } } } while (idx < n) { result[idx++] = 'a'; } out.println(min); for (int i = 0; i < result.length; i++) { out.print(result[i]); } } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (Banana instance = new Banana()) { instance.solve(); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { map<char, int> mp; int n, M = 0; string s; cin >> s >> n; int a[200]; for (char i : s) mp[i]++; for (int k = 1; k <= 1000; k++) { string ans; map<char, int> aux(mp); for (char i : s) { if (aux[i] > 0) { ans += i; aux[i] -= k; } } if (ans.length() > n) continue; bool good = true; for (char i : s) { if (aux[i] > 0) good = false; } if (good) { while (ans.length() < n) ans += s[0]; cout << k << "\n" << ans; return 0; } } cout << -1; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int j, nrmin, s1, mij, u, p, nr1, k, max, x, nr, n, i, z, y, a[1100]; char cc, s[1100], b[1100]; int main() { gets(s); scanf("%d", &n); x = strlen(s); for (i = 0; i < x; i++) a[s[i]]++; p = 1; u = 10000; y = -1; while (p <= u) { mij = (p + u) / 2; nr = mij; s1 = 0; for (i = 'a'; i <= 'z'; i++) { nrmin = a[i] / nr; if (a[i] % nr != 0) nrmin++; s1 = s1 + nrmin; } if (s1 <= n) { u = mij - 1; y = mij; } else p = mij + 1; } printf("%d", y); if (y > -1) { nr = y; s1 = 0; for (i = 'a'; i <= 'z'; i++) { nrmin = a[i] / nr; if (a[i] % nr != 0) nrmin++; s1 = s1 + nrmin; a[i] = nrmin; } printf("\n"); for (i = 'a'; i <= 'z'; i++) for (j = 1; j <= a[i]; j++) printf("%c", (char)i); for (i = s1 + 1; i <= n; i++) printf("k"); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
k = raw_input() li = [0 for x in xrange(26)] l = raw_input() l = int(l) for x in k: li[ord(x) - ord('a')] = li[ord(x) -ord('a')] + 1 temp = sum([1 for x in li if x > 0]) if temp > l: print -1 else: lo = 0 hi = 10000 pre = hi while lo <= hi: mid = (lo + hi) / 2 c = 0; for x in li: if mid > 0: c += (x + mid - 1) / mid else: c = 100000000 if c > l: lo = mid+1 else: hi = mid-1 prev = mid print prev ans = [] for x in 'abcdefghijklmnopqrstuvwxyz': ans.append(x*((li[ord(x)-ord('a')] + prev -1)/prev)) t = ''.join(ans) if (len(t)) < l: t += 'a' * (l - len(t)) print t
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; FILE *in; FILE *out; const int MAX = 1024; int n, m; char a[MAX]; int main(void) { in = stdin; out = stdout; fscanf(in, "%s %d", a, &m); n = (int)strlen(a); map<char, int> q; for (int i = 0; i < n; i++) { if (q.find(a[i]) == q.end()) q[a[i]] = 0; q[a[i]]++; } if ((int)q.size() > m) { fprintf(out, "-1\n"); return 0; } int ans = 1000; int left = 1, right = 1000; while (left <= right) { int mid = (left + right) / 2; int req = 0; for (map<char, int>::iterator it = q.begin(); it != q.end(); it++) { int cur = it->second / mid; if (it->second % mid) cur++; req += cur; } if (req <= m) { ans = mid; right = mid - 1; } else left = mid + 1; } fprintf(out, "%d\n", ans); int rem = m; for (map<char, int>::iterator it = q.begin(); it != q.end(); it++) { int cur = it->second / ans; if (it->second % ans) cur++; for (int i = 0; i < cur; i++) fprintf(out, "%c", it->first); rem -= cur; } while (rem) { fprintf(out, "%c", q.begin()->first); rem--; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char buff[1003]; int cnt[26]; bool check(int ns, int n) { int s = 0; for (int i = (0); i < ((26)); i++) s += (cnt[i] + ns - 1) / ns; return s <= n; } int main() { gets(buff); int n; scanf("%d", &n); for (int i = 0; buff[i]; i++) cnt[buff[i] - 'a']++; int numChars = 26 - count(cnt, cnt + 26, 0); if (numChars > n) printf("-1\n"); else { int lo = 1, hi = 1003; while (lo < hi) { int mid = (lo + hi) >> 1; if (!check(mid, n)) lo = mid + 1; else hi = mid; } printf("%d\n", lo); int len = 0; for (int i = (0); i < ((26)); i++) { int s = (cnt[i] + lo - 1) / lo; for (int j = (0); j < ((s)); j++) printf("%c", i + 'a'); len += s; } for (; len < n; len++) printf("a"); printf("\n"); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; inline long long minOf(long long x, long long y) { return (x < y ? x : y); } inline long long maxOf(long long x, long long y) { return (x > y ? x : y); } char arr[50000000], *ptr; long long ret; string str; inline long long get_int() { bool isNeg = false; ret = 0; while (!((*ptr >= '0' && *ptr <= '9') || *ptr == '-')) ptr++; while ((*ptr >= '0' && *ptr <= '9') || *ptr == '-') { if (*ptr == '-') isNeg = true; else ret = ret * 10 + (*ptr - '0'); ptr++; } if (isNeg) ret = -ret; return ret; } inline string get_str() { str = ""; while (!((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z'))) ptr++; while ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z')) { str += char(*ptr); ptr++; } return str; } int charMap[26] = {0}; int tMap[26] = {0}; int cmap[26] = {0}; int ans; int main() { fread(arr, sizeof(char), 50000000, stdin); ptr = arr; string s = get_str(); long long n = get_int(); for (int i = 0; i < s.length(); i++) { charMap[s[i] - 'a']++; tMap[s[i] - 'a']++; } int dis = 0; for (int i = 0; i < 26; i++) if (charMap[i]) dis++; if (n < dis) { cout << -1 << endl; return 0; } int p = 0; do { p++; ans = 0; for (int j = 0; j < 26; j++) { if (tMap[j]) ans += (tMap[j] / p) + (tMap[j] % p == 0 ? 0 : 1); } } while (ans > n); string ban = ""; for (int j = 0; j < 26; j++) { if (tMap[j]) { int len = (tMap[j] / p) + (tMap[j] % p == 0 ? 0 : 1); for (int i = 0; i < len; i++) ban += char(j + 'a'); } } while (ban.length() < n) { ban += 'a'; } cout << p << endl << ban << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; map<char, int> m; char letters[30]; int numbers[30]; int mystring[30]; int ceiling(int top, int bottom) { return (top + bottom - 1) / bottom; } int main() { string s; int n; cin >> s >> n; for (int i = 0; i < (int)s.length(); i++) m[s[i]]++; int numChar = 0; for (map<char, int>::iterator it = m.begin(); it != m.end(); ++it) { letters[numChar] = it->first; numbers[numChar] = it->second; numChar++; } if (numChar > n) { cout << -1 << endl; return 0; } for (int i = 0; i < numChar; i++) mystring[i] = 1; for (int i = numChar; i < n; i++) { int needMost = -1, needMostIndex = -1; for (int j = 0; j < numChar; j++) { if (ceiling(numbers[j], mystring[j]) > needMost) { needMost = ceiling(numbers[j], mystring[j]); needMostIndex = j; } } mystring[needMostIndex]++; } int needMost = -1; for (int j = 0; j < numChar; j++) { if (ceiling(numbers[j], mystring[j]) > needMost) { needMost = ceiling(numbers[j], mystring[j]); } } cout << needMost << endl; for (int i = 0; i < numChar; i++) for (int j = 0; j < mystring[i]; j++) cout << letters[i]; cout << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e5; const int inf = (int)1e9; const double eps = 1e-8; const double pi = 3.141592653589793238462643; int main() { string s; int n; cin >> s; cin >> n; int m = ((int)(s).size()); int b[50]; for (int i = (0); i < (28); ++i) b[i] = 0; int x, k = 0; for (int i = (0); i < (((int)(s).size())); ++i) { x = s[i] - 'a'; if (b[x] == 0) { ++k; } ++b[x]; } k = n - k; if (k < 0) { cout << "-1"; return 0; } string t = ""; vector<pair<char, int> > v; vector<int> p; for (int i = (0); i < (27); ++i) { if (b[i]) { t += (i + 'a'); v.push_back(make_pair((i + 'a'), b[i])); p.push_back(1); } } int max = 0; char c; while (k) { max = 0; int j; for (int i = (0); i < (((int)(v).size())); ++i) { int e = (v[i].second % p[i] > 0) ? v[i].second / p[i] + 1 : v[i].second / p[i]; if (max < e) { c = v[i].first; max = e; j = i; } } ++p[j]; t += c; --k; } max = 0; for (int i = (0); i < (((int)(v).size())); ++i) { int e = (v[i].second % p[i] > 0) ? v[i].second / p[i] + 1 : v[i].second / p[i]; if (max < e) { max = e; } } cout << max << "\n"; cout << t; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int freq[130]; string calcular(int k, int n) { if (k == 0) return ""; string res; for (char i = 'a'; i <= 'z'; i++) { if (freq[i]) { int cant = freq[i] / k + (freq[i] % k != 0); res += string(cant, i); } } if (res.size() < n) res += string(n - res.size(), 'a'); return res; } int main() { string s; int n; cin >> s >> n; for (int i = 0; i < s.size(); i++) freq[s[i]]++; string result; int cantidad = 100000; int ini = 0; int fin = s.size() + 1; int mid; while (ini + 1 < fin) { int mid = (ini + fin) / 2; string res = calcular(mid, n); if (res.size() == n) { result = res; cantidad = min(cantidad, mid); fin = mid; } else { ini = mid; } } if (cantidad == 100000) cout << -1 << endl; else cout << cantidad << endl << result << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1e7 + 5; const int inf = 1e9; const long long OO = 1e18; const long long MOD = 1e9 + 7; int main() { int n, k = 0; string s, ans = ""; cin >> s >> n; map<char, int> mp; for (auto c : s) mp[c]++; if ((int)mp.size() > n) return cout << -1, 0; for (k = 1; k <= 1000; k++) { int sum = 0; for (auto x : mp) { sum += (int)ceil(x.second / (double)k); } if (sum <= n) break; } for (auto x : mp) { for (int i = 0; i < (int)ceil(x.second / (double)k); i++) ans += x.first; } int re = n - (int)ans.size(); for (int i = 0; i < re; i++) ans += 'a'; cout << k << "\n" << ans; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Banana { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); char[] s = sc.next().toCharArray(); int freq[] = new int[26]; for (int i = 0; i < s.length; i++) freq[s[i]-'a']++; PriorityQueue<Pair> q = new PriorityQueue<Banana.Pair>(); for (int i = 0; i < freq.length; i++) if(freq[i] != 0) q.add(new Pair(freq[i], 0, (char)(i+'a'))); StringBuilder sb = new StringBuilder(""); int n = sc.nextInt(); for (int i = 0; i < n; i++) { Pair p = q.remove(); sb.append(p.c); p.b++; q.add(p); } if(q.peek().b == 0) System.out.println("-1"); else { Pair p = q.peek(); System.out.println((p.a + (p.b-1))/p.b); System.out.println(sb); } } static class Pair implements Comparable<Pair> { int a, b; char c; public Pair(int a, int b, char c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(Pair p) { if(p.b == 0 && b == 0) return 0; if(p.b == 0 && b != 0) return 1; if(b == 0 && p.b != 0) return -1; int val1 = (a+(b-1))/b; int val2 = (p.a+(p.b-1))/p.b; return val2 - val1; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException {while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());return st.nextToken();} public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; void min_stickers(string s, int k) { int n = s.length(); vector<int> count(26, 0); vector<int> a(26, 0); for (int i = 0; i < n; i++) { count[s[i] - 'a']++; a[s[i] - 'a'] = 1; } int temp = 0; for (int i = 0; i < 26; i++) if (a[i]) temp++; if (temp > k) { printf("-1\n"); return; } int diff = k - temp; while (diff) { int idx; int maxi = 0; for (int i = 0; i < 26; i++) { if (a[i] && ceil(float(count[i]) / float(a[i])) > maxi) { maxi = ceil(float(count[i]) / float(a[i])); idx = i; } } a[idx]++; diff--; } int ans = -1; string t; for (int i = 0; i < 26; i++) { for (int j = 0; j < a[i]; j++) t += ('a' + i); if (a[i] && ceil(float(count[i]) / float(a[i])) > ans) ans = ceil(float(count[i]) / float(a[i])); } cout << ans << "\n"; cout << t << "\n"; } int main() { int k; string s; cin >> s; cin >> k; min_stickers(s, k); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import sys from collections import defaultdict from Queue import PriorityQueue class Relation: def __init__(self, r, c, l): self.r = r self.c = c self.l = l def demand(self): return (self.r + self.c - 1) / self.c def __lt__(self, other): return self.demand() > other.demand() s = raw_input() n = input() d = defaultdict(int) for c in s: d[c] += 1 if len(d) > n: print -1 exit() pq = PriorityQueue() for k in d: pq.put(Relation(d[k], 1, k)) n -= len(d) while n: rel = pq.get() rel.c += 1 pq.put(rel) n -= 1 rel = pq.get() print rel.demand() sys.stdout.write(rel.l * rel.c) while not pq.empty(): rel = pq.get() sys.stdout.write(rel.l * rel.c) print ''
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; struct node { int need, temp; char ch; bool operator<(const node& a) const { return ceil((double)need / temp) < ceil((double)a.need / a.temp); } } p[101]; void init() { for (int i = 0; i <= 100; i++) p[i].need = 0, p[i].temp = 1; } priority_queue<node> q; vector<char> ans; int main() { init(); string s; int x; cin >> s >> x; for (int i = 0; i < s.size(); i++) p[s[i] - 'a'].need++, p[s[i] - 'a'].ch = s[i]; for (int i = 0; i <= 30; i++) { if (p[i].need) { q.push(p[i]); ans.push_back(i + 'a'); } } if (x < ans.size()) { cout << -1 << endl; return 0; } int ii = ans.size(); for (int i = ii + 1; i <= x; i++) { if (q.size()) { node aa = q.top(); q.pop(); ans.push_back(aa.ch); aa.temp++; q.push(aa); } } node bb = q.top(); cout << ceil((double)bb.need / bb.temp) << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i]; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s=raw_input() n=input() S=list(set(s)) if len(S) > n: print -1;exit() from collections import defaultdict as D C=D(int) for c in s: C[c]+=1 i=1 while 1: s = 0 r = [] for k,v in C.items(): c = -(-v/i) r += k*c s += c if s <= n: break i+=1 print i print ''.join(r)+'a'*(n-len(r))
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 5e5 + 10; string s; int dp[1001][26], f[26]; vector<pair<int, int>> v; int giveres(int pos, int indx) { if (indx == v.size()) { return 0; } if (pos == 0) return 1e9; int &ret = dp[pos][indx]; if (ret != -1) return ret; ret = 1e9; for (int i = 1; i <= v[indx].second; i++) { if (i <= pos) { int xx = v[indx].second / i; if (v[indx].second % i != 0) xx++; ret = min(ret, max(giveres(pos - i, indx + 1), xx)); } } return ret; } void print(int pos, int indx) { if (indx == v.size()) { for (int i = 0; i < pos; i++) cout << "a"; return; } if (pos == 0) return; int &ret = dp[pos][indx]; for (int i = 1; i <= v[indx].second; i++) { if (i <= pos) { int xx = v[indx].second / i; if (v[indx].second % i != 0) xx++; if (ret == max(giveres(pos - i, indx + 1), xx)) { for (int j = 0; j < i; j++) cout << (char)('a' + v[indx].first); print(pos - i, indx + 1); return; } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; int len; cin >> len; for (char cc : s) f[cc - 'a']++; for (int i = 0; i < 26; i++) if (f[i]) v.push_back({i, f[i]}); if (len < v.size()) { puts("-1"); return 0; } memset(dp, -1, sizeof(dp)); cout << giveres(len, 0) << "\n"; print(len, 0); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int i, l, j, n, f[26] = {0}, cur[26] = {0}, cnt = 0; scanf("%d", &n); ; for (i = 0; i < s.size(); i++) f[s[i] - 'a']++; string ans = ""; for (i = 0; i < 26; i++) if (f[i]) { cnt++; cur[i] = 1; ans += 'a' + i; } if (cnt > n) { cout << -1 << endl; return 0; } for (i = cnt; i < n; i++) { int mx = INT_MIN, index = -1; for (j = 0; j < 26; j++) { if (cur[j] && (f[j] / cur[j]) + (f[j] % cur[j] != 0) > mx) { mx = f[j] / cur[j] + (f[j] % cur[j] != 0); index = j; } } ans += index + 'a'; cur[index]++; } cnt = 0; for (i = 0; i < 26; i++) if (cur[i]) cnt = max(cnt, f[i] / cur[i] + (f[i] % cur[i] != 0)); cout << cnt << endl << ans << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class cf335a { public static void main(String[] args) { FastIO in = new FastIO(), out = in; String s = in.next().trim(); int n = in.nextInt(); int[] f = new int[26]; for(char c : s.toCharArray()) f[c-'a']++; int ans = 1; int oo = s.length()+10; while(ans < oo && go(f,ans) > n) ans++; if(ans >= oo) { out.println("-1"); } else { out.println(ans); for(int i=0; i<26; i++) { int count = (f[i]+ans-1)/ans; for(int j=0; j<count; j++) out.print((char)(i+'a')); } int left = n-go(f,ans); for(int i=0; i<left; i++) out.print('a'); } out.close(); } static int go(int[] f, int k) { int ans = 0; for(int x : f) ans += (x+k-1)/k; return ans; } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { this(System.in,System.out); } public FastIO(InputStream in, OutputStream out) { super(new BufferedWriter(new OutputStreamWriter(out))); br = new BufferedReader(new InputStreamReader(in)); scanLine(); } public void scanLine() { try { st = new StringTokenizer(br.readLine().trim()); } catch(Exception e) { throw new RuntimeException(e.getMessage()); } } public int numTokens() { if(!st.hasMoreTokens()) { scanLine(); return numTokens(); } return st.countTokens(); } public String next() { if(!st.hasMoreTokens()) { scanLine(); return next(); } return st.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; struct cmp { bool operator()(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } }; int cnt[30]; int ans[30]; int main() { string str; int n; cin >> str >> n; memset(cnt, 0, sizeof(cnt)); memset(ans, 0, sizeof(ans)); for (int i = 0; i < str.size(); ++i) { cnt[str[i] - 'a']++; } int exist = 0; for (int i = 0; i < 26; ++i) if (cnt[i] != 0) exist++; if (exist > n) { cout << -1 << endl; return 0; } priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> q; for (int i = 0; i < 26; ++i) { if (cnt[i] != 0) { q.push(make_pair(cnt[i], i)); ans[i] = 1; } } n -= exist; while (n--) { pair<int, int> cur = q.top(); q.pop(); int x = cur.first; int y = cur.second; ans[y]++; q.push(make_pair((cnt[y] + ans[y] - 1) / ans[y], y)); } int res = 0; for (int i = 0; i < 26; ++i) { if (ans[i] != 0) res = max(res, (cnt[i] + ans[i] - 1) / ans[i]); } cout << res << endl; for (int i = 0; i < 26; ++i) { for (int j = 0; j < ans[i]; ++j) cout << (char)(i + 'a'); } cout << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; float arr[26] = {0}; set<char> s1; for (int i = 0; i < s.length(); i++) { s1.insert(s[i]); int x = s[i] - 97; arr[x]++; } if (s1.size() > n) { cout << "-1"; } else { string r(""); float x[26] = {0}; for (int i = 0; i < 26; i++) { if (arr[i] > 0) { char z = i + 97; r += z; x[i]++; } } while (n > r.length()) { float maxval = 0; int index = 0; for (int i = 0; i < 26; i++) { if (arr[i] > 0) { float rz = (float)arr[i] / x[i]; if (rz > maxval) { maxval = rz; index = i; } } } x[index]++; char z = index + 97; r += z; } int num = 0; for (int i = 0; i < 26; i++) { if (arr[i] > 0) { num = max((int)ceil(float(arr[i] / x[i])), num); } } cout << num << endl; cout << r; } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5; int n, kind, cnt[30]; char str[maxn]; int need[30], tot; bool check(int mid) { memset(need, 0, sizeof need); tot = 0; for (int i = 0; i < 26; ++i) { need[i] = ceil(double(cnt[i]) / double(mid)); tot += need[i]; if (tot > n) return false; } return true; } int binary(int left, int rght) { while (left < rght) { int mid = (left + rght) >> 1; if (check(mid)) rght = mid; else left = mid + 1; } check(rght); return rght; } int main() { scanf("%s", str); scanf("%d", &n); for (int i = 0; str[i]; ++i) ++cnt[str[i] - 'a']; for (int i = 0; i < 26; ++i) if (cnt[i]) ++kind; if (n < kind) { puts("-1"); return 0; } int ans = binary(1, 1e3); printf("%d\n", ans); for (int i = 0; i < 26; ++i) while (need[i]--) putchar('a' + i), --n; while (n) putchar('a'), --n; puts(""); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char kata[1005]; int panjang, total[30], batas; int main() { scanf("%s", kata); panjang = strlen(kata); for (int i = 0; i < panjang; ++i) ++total[kata[i] - 'a']; scanf("%d", &batas); int cek = 0; for (int i = 0; i < 26; ++i) if (total[i]) ++cek; if (cek > batas) { printf("-1\n"); return 0; } int kiri = 0; int kanan = panjang; while (kanan - kiri > 1) { int tengah = (kanan + kiri) / 2; int pakai = 0; for (int i = 0; i < 26; ++i) { pakai += (total[i] + tengah - 1) / tengah; } if (pakai > batas) kiri = tengah; else kanan = tengah; } printf("%d\n", kanan); for (int i = 0; i < 26; ++i) { int temp = (total[i] + kanan - 1) / kanan; for (int j = 0; j < temp; ++j) printf("%c", i + 'a'); batas -= temp; } for (int i = 0; i < batas; ++i) printf("a"); printf("\n"); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; string s; int n; int freq[29]; vector<char> v; int f(int x) { if (x == 0) return 0; int total = n; for (int i = 0; i < 26; i++) { double cur = ((freq[i] * 1.0) / x); int curx = ceil(cur); total -= curx; } if (total >= 0) return 1; return 0; } void doit(int x) { int total = n; for (int i = 0; i < 26; i++) { double cur = ((freq[i] * 1.0) / x); int curx = ceil(cur); total -= curx; while (curx) { v.push_back(char(i + 'a')); freq[i]--; --curx; } } while (total) { total--; v.push_back('z'); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; cin >> n; set<char> se; int len = s.length(); for (int i = 0; i < len; i++) { se.insert(s[i]); freq[s[i] - 'a']++; } if (se.size() > n) { puts("-1"); return 0; } int lo = 0; int hi = 112345678; while ((hi - lo) > 2) { int mid = (lo + hi) / 2; if (f(mid)) { hi = mid; } else lo = mid; } int ans; if (f(lo)) { ans = lo; return 0; } else while (!(f(lo)) and lo <= hi) { ++lo; ans = lo; } doit(ans); cout << ans << endl; for (auto mm : v) { cout << mm; } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const long long INFL = LLONG_MAX; const long double pi = acos(-1); int cnt[100]; int N; string s; int main() { ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0); cin >> s >> N; for (int(i) = 0, j123 = int(s.size()); (i) < j123; (i)++) cnt[s[i] - 'a']++; for (int ans = 1; ans <= 1000; ans++) { int length = 0; for (char ch = 'a'; ch <= 'z'; ch++) { length += (cnt[ch - 'a'] + ans - 1) / ans; } if (length <= N) { cout << ans << '\n'; for (char ch = 'a'; ch <= 'z'; ch++) { for (int(heheh) = 0, j123 = (cnt[ch - 'a'] + ans - 1) / ans; (heheh) < j123; (heheh)++) cout << ch; } for (int(hdeded) = 0, j123 = N - length; (hdeded) < j123; (hdeded)++) cout << 'a'; cout << '\n'; return 0; } } cout << -1 << '\n'; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char s[1005], re[1005]; int c1[27], c2[27], n, tt[27]; int jd(int x) { int i, tot = n, t1; memset(c2, 0, sizeof(c2)); for (i = 0; i < 27; i++) if (c1[i]) { t1 = (c1[i] + x - 1) / x; if (tot < t1) return 0; tot -= t1; c2[i] = t1; } c2[0] += tot; for (i = 0; i < 27; i++) tt[i] = c2[i]; return 1; } int main() { int i, j, cnt, l; while (scanf("%s%d", &s, &n) == 2) { l = strlen(s); memset(c1, 0, sizeof(c1)); for (i = 0; i < l; i++) c1[s[i] - 'a']++; cnt = 0; for (i = 0; i < 27; i++) if (c1[i]) cnt++; if (n < cnt) printf("-1\n"); else if (n >= l) { printf("1\n"); printf("%s", s); for (i = 0; i < n - l; i++) printf("a"); printf("\n"); } else { int l1 = 1, r1 = l + 1, mid; while (l1 < r1) { mid = (l1 + r1) / 2; if (jd(mid)) r1 = mid; else l1 = mid + 1; } printf("%d\n", l1); for (i = 0; i < 27; i++) if (tt[i]) { for (j = 0; j < tt[i]; j++) printf("%c", i + 'a'); } printf("\n"); } } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char s[100005]; int i, j, p[26], ans, A, k; int main() { scanf("%s", s); for (i = 0; i < strlen(s); i++) p[s[i] - 'a']++; scanf("%d", &A); int t = 0; for (i = 1; i <= 1000; i++) { ans = 0; for (j = 0; j < 26; j++) if (p[j] != 0) ans += (p[j] - 1) / i + 1; if (ans <= A) { cout << i << endl; for (j = 0; j < 26; j++) if (p[j] != 0) for (k = 1; k <= (p[j] - 1) / i + 1; k++) { if (t == A) return 0; t++; cout << char(j + 'a'); } while (t != A) { cout << 'a'; t++; } return 0; } } cout << -1; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.Scanner; public class Banana { static String sol; static int n; private static int [] freq = new int[26]; public static void main(String[] args) { Scanner fin = new Scanner(System.in); String s = fin.nextLine(); n = fin.nextInt(); int i; for (i = 0; i < s.length(); i++) { freq[s.charAt(i) - 'a'] ++; } int cnt = 0; for (i = 0; i < 26; i++) if (freq[i] != 0) cnt ++; if (cnt > n) { System.out.println(-1); return; } int p = 0; int st = 1 ; int dr = 1000; while (st <= dr) { int mid = (st + dr) >> 1; if (solve(mid) <= n) { p = mid; dr = mid - 1; } else st = mid + 1; } System.out.println(p); sol = ""; for (i = 0; i < 26; i++) { int now_need = now_need = (freq[i] + p - 1) / p; while (now_need > 0) { sol = sol +(char)(i + 'a'); now_need --; } } if (sol.length() > n) { System.out.println(-1); return; } while (sol.length() != n) sol += 'a'; System.out.println(sol); } private static int solve(int p) { int i, need = 0, now_need; for (i = 0; i < 26; i++) { now_need = (freq[i] + p - 1) / p; need += now_need; } return need; } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int N, M, T; int tb[127]; int main() { int i, j, k; char str[1005]; scanf("%s%d", str, &N); for (i = 0; str[i] != '\0'; i++) tb[str[i]]++; k = 0; for (i = 'a'; i <= 'z'; i++) if (tb[i]) k++; int st = 1, ed = 10000, mid, ans = -1; while (st <= ed) { mid = (st + ed) / 2; k = 0; for (i = 'a'; i <= 'z'; i++) k += ((int)ceil((double)tb[i] / mid)); if (k <= N) { ans = mid; ed = mid - 1; } else { st = mid + 1; } } printf("%d\n", ans); if (ans != -1) { for (i = 'a'; i <= 'z'; i++) { k = ((int)ceil((double)tb[i] / ans)); for (j = 0; j < k; j++) printf("%c", i), M++; } while (M < N) printf("a"), M++; } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; import java.lang.*; import java.util.function.*; public class Main { final static String fileName = ""; final static boolean useFiles = false; public static void main(String[] args) throws FileNotFoundException { InputStream inputStream; OutputStream outputStream; if (useFiles) { inputStream = new FileInputStream(fileName + ".in"); outputStream = new FileOutputStream(fileName + ".out"); } else { inputStream = System.in; outputStream = System.out; } InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(in, out); Debug.out = out; solver.solve(); out.close(); } } class Task { public void solve() { String s = in.next(); int[] sc = ArrayUtils.countingSort(s); int n = in.nextInt(); for (int ans = 1; ans <= s.length(); ans++) { int sum = 0; for (int j = 0; j < 26; j++) { sum += MathUtils.ceilDiv(sc[j], ans); } if (sum <= n) { out.println(ans); String result = ""; for (char c = 'a'; c <= 'z'; c++) { for (int j = 0; j < MathUtils.ceilDiv(sc[c - 'a'], ans); j++) result += c; } int cnt = n - result.length(); for (int i = 0; i < cnt; i++) result += 'a'; out.print(result); return; } } out.print(-1); } private InputReader in; private PrintWriter out; Task(InputReader in, PrintWriter out) { this.in = in; this.out = out; } } class Debug { public static PrintWriter out; public static void printObjects(Object... objects) { out.println(Arrays.deepToString(objects)); out.flush(); } } class ArrayUtils { static int[] countingSort(String s) { int[] count = new int[26]; for (int i = 0; i < s.length(); i++) { count[s.charAt(i) - 'a']++; } return count; } static int[] scaling(int[] a) { int[] types = unique(a); int[] result = new int[a.length]; for (int i = 0; i < a.length; i++) result[i] = Arrays.binarySearch(types, a[i]); return result; } static int[] filter(int[] array, Predicate<Integer> predicate) { int count = 0; for (int anArray : array) { if (predicate.test(anArray)) count++; } int[] result = new int[count]; int top = 0; for (int anArray : array) { if (predicate.test(anArray)) result[top++] = anArray; } return result; } static int[] merge(int[] a, int[] b) { int[] result = new int[a.length + b.length]; int i = 0, j = 0, k = 0; for (; i < a.length && j < b.length; k++) { if (a[i] < b[i]) result[k] = a[i++]; else result[k] = b[j++]; } for (; i < a.length; i++) result[k++] = a[i]; for (; j < b.length; j++) result[k++] = a[j]; return result; } static int[] reverse(int[] a) { int[] result = new int[a.length]; for (int i = 0; i < a.length; i++) result[i] = a[a.length - i - 1]; return result; } static int[] unique(int[] a) { a = a.clone(); Arrays.sort(a); int n = a.length; int count = 1; for (int i = 1; i < n; i++) { if (a[i] != a[i - 1]) count++; } int[] result = new int[count]; result[0] = a[0]; for (int i = 1, j = 1; i < n; i++) { if (a[i] != a[i - 1]) result[j++] = a[i]; } return result; } } class Combinatorics { private int[] f; private int size; private int mod; Combinatorics(int n, int mod) { f = new int[n]; f[0] = 1; for (int i = 1; i <= n; i++) f[i] = (int) ((f[i - 1] * ((long) i)) % mod); size = n; } int combinations(int n, int k) { if (k > n || k > size || n > size) throw new IllegalArgumentException(); long result = f[n]; result *= MathUtils.inverse(k, mod); result %= mod; result *= MathUtils.inverse(n - k, mod); return (int) (result % mod); } int factorial(int x) { if (x > size) throw new IllegalArgumentException(); return f[x]; } int arrangements(int n, int k) { if (n > size || k > size || k > n) throw new IllegalArgumentException(); return (int) ((f[n] * ((long) MathUtils.inverse(f[n - k], mod))) % mod); } int b(int n, int k) { return combinations(n + k - 1, n - 1); } } class Tuple<T1, T2> { Tuple(T1 first, T2 second) { First = first; Second = second; } T1 First; T2 Second; } class Tuple3<T1, T2, T3> { Tuple3(T1 first, T2 second, T3 third) { First = first; Second = second; Third = third; } T1 First; T2 Second; T3 Third; } class MathUtils { static int ceilDiv(int a, int b) { return (a + b - 1) / b; } static boolean isPrime(int x) { for (long i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } static int getNextPrime(int x) { for (int i = x; ; i++) { if (isPrime(i)) return i; } } static boolean[] eratosphen(int n) { boolean[] result = new boolean[n + 1]; for (int i = 2; sqr((long) i) <= n; i++) { if (!result[i]) { for (int j = i * i; j <= n; j += i) result[j] = true; } } return result; } static List<Tuple<Integer, Integer>> factorize(int x) { List<Tuple<Integer, Integer>> result = new ArrayList<Tuple<Integer, Integer>>(); for (long div = 2; div * div <= x; div++) { if (x % div == 0) { int count = 0; do { count++; x /= div; } while (x % div == 0); result.add(new Tuple<Integer, Integer>((int) div, count)); } } if (x != 1) { result.add(new Tuple<Integer, Integer>(x, 1)); } return result; } static long lcm(int a, int b) { return ((long) a) * b / gcd(a, b); } static int gcd(int a, int b) { if (a <= 0 || b <= 0) throw new IllegalArgumentException("Not positive arguments: " + a + " and " + b); return privateGcd(Math.min(a, b), Math.max(a, b)); } private static int privateGcd(int a, int b) { if (a == 0) return b; return privateGcd(b % a, a); } static double sqr(double a) { return a * a; } static long sqr(long a) { return a * a; } static int sqr(int a) { return a * a; } static int power(int base, int pow, int mod) { long s = base; long result = 1; while (pow != 0) { if ((pow & 1) > 0) { result *= s; result %= mod; } s = sqr(s) % mod; pow >>= 1; } return (int) result; } static int inverse(int x, int mod) { return power(x, mod - 2, mod); } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public double nextDouble(){ return Double.parseDouble(next()); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int l = sc.nextInt(); TreeMap<Character, Integer> counter = new TreeMap<Character, Integer>(); for (int i = 0; i < s.length(); i++) { char temp = s.charAt(i); if (!counter.containsKey(temp)) counter.put(temp, 1); else counter.put(temp, counter.get(temp) + 1); } if (counter.size() > l) { System.out.println(-1); return; } int lo = 0; int hi = 10000; int mid; while (lo + 1 < hi) { mid = (lo + hi) / 2; int c = 0; for (Map.Entry<Character, Integer> entry : counter.entrySet()) c += (entry.getValue() + mid - 1) / mid; if (c > l) lo = mid; else hi = mid; } System.out.println(hi); String ans = ""; for (Map.Entry<Character, Integer> entry : counter.entrySet()) { char[] t = new char[(entry.getValue() + hi -1) / hi]; Arrays.fill(t, entry.getKey()); String tt = new String(t); ans = ans + tt; } if (ans.length() < l) { int ll = ans.length(); for (int i = 0; i < l - ll; i++) ans = ans + "a"; } System.out.println(ans); sc.close(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353.; const long long INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7; const long long MOD2 = 998244353; const long long N = 5000 * 100 + 1; const long long N2 = 1000 * 1000 + 5; const long double PI = 3.14159265; const long long R = 250; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long power(long long x, long long y, long long p) { long long res = 1; x %= p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } bool sortbysec(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.second < b.second); } signed main() { string s; cin >> s; long long n = s.length(); long long N; cin >> N; set<long long> v; map<long long, long long> make_pair; long long ans = 0; for (long long i = 0; i < n; i++) { v.insert(s[i]); make_pair[s[i]]++; } if (v.size() > N) { cout << -1 << "\n"; } else { long long lo = 1; long long hi = 1000 * 1000; while (lo <= hi) { ans = 0; long long m = (lo + hi) / 2; for (auto x : v) { ans += (make_pair[x] - 1) / m + 1; } if (ans <= N) { hi = m - 1; } else { lo = m + 1; } } ans = 0; cout << lo << "\n"; string str = ""; for (auto x : v) { for (long long j = 0; j < (make_pair[x] - 1) / lo + 1; j++) { str += x; ans++; } } for (long long i = ans; i < N; i++) { str += 'a'; } cout << str << "\n"; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; long long int n; map<long long int, long long int> m; string s; bool check(long long int mid) { long long int i, j; long long int sum = 0; for (auto it = m.begin(); it != m.end(); it++) { sum = sum + ceil((it->second) * 1.00 / mid); } if (sum <= n) return 1; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int i, j; long long int k; cin >> s; cin >> n; if (n >= s.size()) { cout << "1\n"; cout << s; for (i = s.size() + 1; i <= n; i++) cout << s[0]; return 0; } for (i = 0; i < s.size(); i++) m[s[i] - 'a' + 1]++; long long int low = 1, high = 1000, ans = -1; while (low <= high) { long long int mid = (low + high) >> 1; if (check(mid)) { ans = mid; high = mid - 1; } else low = mid + 1; } if (ans == -1) cout << "-1\n"; else { long long int count = 0; cout << ans << "\n"; for (auto it = m.begin(); it != m.end(); it++) { for (j = 1; j <= ceil((it->second) * 1.00 / ans); j++) { count++; cout << (char)(it->first + 'a' - 1); } if (count == n) return 0; } for (i = count + 1; i <= n; i++) { cout << s[0]; } cout << "\n"; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; void precompute(void) {} int main() { int t, n, len, i; char str[1032]; int fre[32], ans, avail, iter[32]; string ansstr; pair<int, int> top; set<pair<int, int> > S; precompute(); while (scanf("%s%d", str, &n) != EOF) { len = strlen(str); memset(fre, 0, sizeof(fre)); for (i = 0; i < len; i++) fre[str[i] - 'a']++; vector<pair<int, int> > nums; for (i = 0; i < 26; i++) { if (fre[i]) { nums.push_back(make_pair(fre[i], i)); } } int cou = nums.size(); if (cou > n) { printf("-1\n"); continue; } sort(nums.rbegin(), nums.rend()); for (i = 0; i < cou; i++) { iter[nums[i].second] = 1; S.insert(make_pair(-nums[i].first, nums[i].second)); ansstr += (char)(nums[i].second + 'a'); } for (i = cou; i < n; i++) { top = *(S.begin()); S.erase(S.begin()); iter[top.second]++; S.insert(make_pair( -(fre[top.second] + iter[top.second] - 1) / iter[top.second], top.second)); ansstr += (char)(top.second + 'a'); } top = *(S.begin()); ans = top.first; printf("%d\n", -ans); cout << ansstr << endl; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; long long rdtsc() { long long tmp; asm("rdtsc" : "=A"(tmp)); return tmp; } inline int myrand() { return abs((rand() << 15) ^ rand()); } inline int rnd(int x) { return myrand() % x; } const int INF = (int)1e9 + 1; const long double EPS = 1e-9; const int maxl = (int)1e3 + 10; char s[maxl]; const int maxc = 26; int cnt[maxc]; bool solve() { int n; if (scanf("%s%d", s, &n) < 2) return 0; int len = strlen(s); for (int i = 0; i < maxc; ++i) { cnt[i] = 0; } for (int i = 0; i < len; ++i) { cnt[s[i] - 'a'] += 1; } int l = 0, r = len + 1; while (l + 1 < r) { int m = (l + r) / 2; int sum = 0; for (int i = 0; i < maxc; ++i) { sum += (cnt[i] + m - 1) / m; } if (sum > n) l = m; else r = m; } if (r <= len) { printf("%d\n", r); int left = n; for (int i = 0; i < maxc; ++i) { int cur = (cnt[i] + r - 1) / r; left -= cur; for (int j = 0; j < cur; ++j) printf("%c", 'a' + i); } assert(left >= 0); while (left > 0) { printf("a"); --left; } printf("\n"); } else printf("-1\n"); return 1; } int main() { srand(rdtsc()); while (1) { if (!solve()) break; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; import java.io.*; public class a { static long mod = 1000000009; static ArrayList<Integer>[] g; public static void main(String[] args) throws IOException { //Scanner input = new Scanner(new File("input.txt")); //PrintWriter out = new PrintWriter(new File("output.txt")); input.init(System.in); PrintWriter out = new PrintWriter((System.out)); String s = input.next(); int n = input.nextInt(); int[] f = new int[26]; for(int i = 0; i<s.length(); i++) f[s.charAt(i)-'a']++; int res = -1; String str = ""; for(int i = 1; i<=1000; i++) { int x = 0; for(int j = 0; j<26; j++) { if(f[j] == 0) continue; x += (f[j]+i-1)/i; } if(x<=n) { res = i; for(int j = 0; j<26; j++) for(int k = 0; k<(f[j]+i-1)/i; k++) str += (char)('a'+j); break; } } out.println(res); if(res>-1) { while(str.length()<n) str+='a'; } out.println(str); out.close(); } static long pow(long x, long p) { if(p==0) return 1; if((p&1) > 0) { return (x*pow(x, p-1))%mod; } long sqrt = pow(x, p/2); return (sqrt*sqrt)%mod; } static long gcd(long a, long b) { if(b==0) return a; return gcd(b, a%b); } static class input { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static String nextLine() throws IOException { return reader.readLine(); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.util.ArrayList; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author ilyakor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { String s = in.nextToken(); int n = in.nextInt(); int[] cnt = new int[300]; for (int i = 0; i < s.length(); ++i) cnt[s.charAt(i)] += 1; ArrayList<Integer> a = new ArrayList<Integer>(); for (int i = 0; i < cnt.length; i++) { if (cnt[i] > 0) a.add(i); } if (a.size() > n) { out.printLine(-1); return; } for (int res = 1; ; ++res) { int[] cnts = new int[a.size()]; int letters = 0; for (int i = 0; i < a.size(); ++i) { int need = cnt[a.get(i)]; int cur = need / res; if (need % res > 0) ++cur; cnts[i] = cur; letters += cur; } if (letters > n) continue; out.printLine(res); for (int i = 0; i < a.size(); ++i) for (int j = 0; j < cnts[i]; ++j) out.print((char)(a.get(i).intValue())); for (int i = letters; i < n; ++i) out.print('z'); out.printLine(); return; } } } class InputReader { private InputStream stream; private byte[] buffer = new byte[10000]; private int cur; private int count; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isSpace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (count == -1) { throw new InputMismatchException(); } try { if (cur >= count) { cur = 0; count = stream.read(buffer); if (count <= 0) return -1; } } catch (IOException e) { throw new InputMismatchException(); } return buffer[cur++]; } public int readSkipSpace() { int c; do { c = read(); } while (isSpace(c)); return c; } public String nextToken() { int c = readSkipSpace(); StringBuilder sb = new StringBuilder(); while (!isSpace(c)) { sb.append((char) c); c = read(); } return sb.toString(); } public int nextInt() { int sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10 + c - '0'; c = read(); } while (!isSpace(c)); res *= sgn; return res; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int n, cnt[26], k, dif; char s[2000]; bool u[26]; bool ok(int kk) { int need_n = 0; for (int i = 0; i < 26; i++) need_n += (cnt[i] + kk - 1) / kk; return (need_n <= n); } int main() { scanf("%s", s); scanf("%d", &n); dif = 0; for (int i = 0; s[i]; i++) { cnt[(int)(s[i] - 'a')]++; if (!u[(int)(s[i] - 'a')]) { u[(int)(s[i] - 'a')] = true; dif++; } } if (dif > n) { printf("-1\n"); return 0; } k = 1; while (1) { if (ok(k)) break; k++; } printf("%d\n", k); int left = n; for (int i = 0; i < 26; i++) { for (int j = 0; j < (int)((cnt[i] + k - 1) / k); j++) printf("%c", (char)('a' + i)); left -= (cnt[i] + k - 1) / k; } for (int i = 0; i < left; i++) printf("z"); printf("\n"); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; vector<int> freq(26, 0); for (int i = 0; i < (int)s.size(); ++i) freq[s[i] - 'a']++; int ans = -1; string s_ans = ""; for (int lf = 1, rt = 2000; lf <= rt;) { int mid = (lf + rt) >> 1; int total = 0; string now = ""; vector<int> need(26, 0); for (int i = 0; i < 26; ++i) { int t = freq[i] / mid; if (freq[i] % mid) t++; need[i] = t; total += need[i]; } if (total <= n) { rt = mid - 1; ans = mid; for (int i = 0; i < 26; ++i) for (int j = 0; j < need[i]; ++j) now += char(i + 'a'); for (; (int)now.size() < n;) now.push_back(now[0]); s_ans = now; } else lf = mid + 1; } cout << ans << "\n"; if (ans != -1) cout << s_ans << "\n"; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Banana { public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); sc = new StringTokenizer(br.readLine()); char[] s = nxtCharArr(); int n = nxtInt(); int[] a = new int[26]; for (int i = 0; i < s.length; i++) a[s[i] - 'a']++; int res = 0; for (int i = 0; i < a.length; i++) res += a[i] > 0 ? 1 : 0; if (res > n) out.println(-1); else { for (int i = 1; i <= s.length; i++) { int tmp = 0; for (int j = 0; j < 26; j++) tmp += a[j] / i + (a[j] % i == 0 ? 0 : 1); if (tmp <= n) { out.println(i); int cnt = 0; for (int j = 0; j < 26; j++) for (int k = 0; k < a[j] / i + (a[j] % i == 0 ? 0 : 1); k++) { out.print((char) (j + 'a')); cnt++; } for (int j = cnt; j < n; j++) out.print('a'); out.println(); break; } } } br.close(); out.close(); } static BufferedReader br; static StringTokenizer sc; static String nxtTok() throws IOException { while (!sc.hasMoreTokens()) { String s = br.readLine(); if (s == null) return null; sc = new StringTokenizer(s.trim()); } return sc.nextToken(); } static int nxtInt() throws IOException { return Integer.parseInt(nxtTok()); } static long nxtLng() throws IOException { return Long.parseLong(nxtTok()); } static double nxtDbl() throws IOException { return Double.parseDouble(nxtTok()); } static int[] nxtIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nxtInt(); return a; } static long[] nxtLngArr(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nxtLng(); return a; } static char[] nxtCharArr() throws IOException { return nxtTok().toCharArray(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.awt.Point; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.StringTokenizer; public class Abood3A { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); String s = sc.next(); int n = sc.nextInt(); int[] c = new int[26]; for (int i = 0; i < s.length(); i++) { c[s.charAt(i) - 'a']++; } ArrayList<Point> a = new ArrayList<>(); for (int i = 0; i < c.length; i++) { if(c[i] > 0) a.add(new Point(i, c[i])); } if(a.size() > n){ out.println(-1); }else{ int l = 1; int h = s.length(); int r = -1; while(l <= h){ int mid = (l + h)/2; int x = 0; for (int i = 0; i < 26; i++) { if(c[i] > 0){ x += Math.max(1, (c[i] + mid - 1) / mid); } } if(x <= n){ r = mid; h = mid - 1; }else{ l = mid + 1; } } char[] ans = new char[n]; int[] used = new int[26]; int p = 0; for (int i = 0; i < 26; i++) { for (int j = 0; j < (c[i] + r - 1) / r; j++) { ans[p++] = (char)('a' + i); } } for (int i = p; i < n; i++) { ans[i] = 'a'; } out.println(r); for (int i = 0; i < ans.length; i++) { out.print(ans[i]); } out.println(); } out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s = input() n = int(input()) d = {} for c in s: if c not in d: d[c] = 0 d[c] += 1 dcl = d.values() found = False for x in range(1,1001): if sum([(dc-1)//x+1 for dc in dcl]) <= n: found = True print(x) s = ''.join([key*((d[key]-1)//x+1) for key in sorted(d.keys())]) s += (n-len(s))*'a' print(s) break if not found: print(-1)
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; public class test { static int getLen(HashMap<Character, Integer> map, int k){ int n = 0; for (Map.Entry<Character, Integer> elem : map.entrySet()){ n += Math.ceil(((double) elem.getValue()) / k); } return n; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); int n = sc.nextInt(); HashMap<Character, Integer> map = new HashMap<>(); for (int i = 0; i < s.length(); i++) { if (map.containsKey(s.charAt(i))){ map.put(s.charAt(i), map.get(s.charAt(i)) + 1); } else { map.put(s.charAt(i), 1); } } if (n < map.size()){ System.out.println(-1); } else { int left = 1; int right = 1000; int k = 1; for (int i = 1; i <= 1000; i++) { if (getLen(map, i) <= n){ k = i; break; } } StringBuilder ans = new StringBuilder(); for (Map.Entry<Character, Integer> elem : map.entrySet()){ int size =(int) Math.ceil(((double) elem.getValue()) / k); for (int i = 0; i < size; i++) { ans.append(elem.getKey()); } } while (ans.length() < n){ ans.append(s.charAt(0)); } System.out.println(k); System.out.println(ans); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; import java.io.*; public class Main { public static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } } static final int MAX = 100005; public static void main(String[] args) { InputReader sc = new InputReader(System.in); int n; String s; TreeMap<Character,Integer> m = new TreeMap<Character,Integer>(); s = sc.next(); n = sc.nextInt(); for (int i = 0; i < s.length(); i++) { if(m.get(s.charAt(i))== null){ m.put(s.charAt(i), 1); } else{ m.put(s.charAt(i), m.get(s.charAt(i))+1); } } if (m.size() > n) { System.out.print(-1); return; } int l = 1, r = s.length(); while (l < r) { int mid = (l + r) / 2, cnt = 0; for (Map.Entry<Character, Integer> it : m.entrySet()) { cnt += Math.ceil(1.0 * it.getValue() / mid); } if (cnt <= n) { r = mid; } else { l = mid + 1; } } StringBuilder sb = new StringBuilder(); for (Map.Entry<Character, Integer> it : m.entrySet()) { for (int i = 0; i < Math.ceil(1.0 * it.getValue() / r); i++) { sb.append(it.getKey()); } } while (sb.length() < n) { sb.append('a'); } System.out.println(r); System.out.println(sb); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; /** * @author pvasilyev * @since 8/3/13 */ public class ProblemA { public static final String FILE_IN = "std.in"; public static final String FILE_OUT = "std.out"; private static boolean debugMode = true; public static void main(String[] args) throws IOException { final Scanner reader = new Scanner(new InputStreamReader(debugMode ? System.in : new FileInputStream(FILE_IN))); final PrintWriter writer = new PrintWriter(debugMode ? System.out : new FileOutputStream(FILE_OUT)); solveTheProblem(reader, writer); reader.close(); writer.close(); } private static void solveTheProblem(final Scanner reader, final PrintWriter writer) throws IOException { final char[] s = reader.nextLine().toCharArray(); final Integer n = Integer.valueOf(reader.nextLine()); char[] counts = new char[26]; for (int i = 0; i < s.length; ++i) { counts[s[i] - 'a']++; } for (int k = 1; k < 1024; ++k) { final StringBuilder builder = new StringBuilder(); for (int i = 0; i < 26; ++i) { for (int j = 0; j < (counts[i] + k - 1) / k; ++j) { builder.append((char) ('a' + i)); } } final String underTest = builder.toString(); if (underTest.length() <= n) { writer.println(k); writer.print(underTest); for (int i = 0; i < n - underTest.length(); ++i) { writer.print('a'); } return; } } writer.println("-1"); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int n, x[27], ct; char s[10001]; int main() { ct = 0; scanf("%s", s); scanf("%d", &n); for (int i = 0; s[i]; i++) x[s[i] - 'a']++; for (int i = 0; i <= 'z' - 'a'; i++) if (x[i] > 0) ct++; if (ct > n) printf("-1\n"); else { for (int i = 1;; i++) { ct = 0; for (int j = 0; j <= 'z' - 'a'; j++) ct += (x[j] + i - 1) / i; if (ct <= n) { printf("%d\n", i); for (int j = 0; j <= 'z' - 'a'; j++) for (int k = 0; k < (x[j] + i - 1) / i; k++) printf("%c", j + 'a'), n--; for (int j = 0; j < n; j++) printf("%c", 'a'); printf("\n"); break; } } } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from collections import Counter def main(): s = input() l = int(input()) d = Counter(s) if len(d) > l: print(-1) return lo = 0 hi = 10000 while lo + 1 < hi: mid = (lo + hi) // 2 c = 0 for x in iter(d.values()): c += (x + mid - 1) // mid if c > l: lo = mid else: hi = mid print(hi) ans = [] for x in iter(d.items()): ans.append(x[0] * int(((x[1] + hi - 1) / hi))) t = ''.join(ans) if len(t) < l: t += 'a' * (l - len(t)) print(t) main()
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; void setup(int value, string name) { string name_in = name + ".in"; string name_out = name + ".out"; freopen(name_in.c_str(), "r", stdin); if (value) freopen(name_out.c_str(), "w", stdout); } int main() { string str; std::cin >> str; map<char, int> bet; map<char, int> res; int arr[26]; memset(arr, 0, sizeof(arr)); for (int i = 0; i < str.size(); ++i) { bet[str[i]]++; res[str[i]] = 1; } int n; std::cin >> n; if (bet.size() > n) { std::cout << -1 << std::endl; return 0; } for (int i = res.size(); i <= n; ++i) { vector<pair<double, char> > stat; for (char ch = 'a'; ch <= 'z'; ++ch) { if (res.count(ch)) { double total = bet[ch]; double cur = res[ch]; stat.push_back(make_pair(total / cur, ch)); } } sort(stat.begin(), stat.end()); if (i == n) { std::cout << (int)ceil(stat.back().first) << std::endl; string ret = ""; for (map<char, int>::iterator it = res.begin(); it != res.end(); ++it) { ret += string(it->second, it->first); } std::cout << ret << std::endl; } else { res[stat.back().second]++; } } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; public class A { void run() throws IOException { char[] str = nl().toCharArray(); int n = ni(), m = 26, k = 0; int[] a = new int[m]; for (char c : str) { a[c - 97]++; } for (int i : a) { if (i > 0) { k++; } } if (k > n) { pw.println(-1); return; } for (int ans = 1; ans <= 1000; ans++) { int[] b = new int[m]; k = n; for (int i = 0; i < m; i++) { b[i] = (a[i] + ans - 1) / ans; k -= b[i]; } if (k >= 0) { pw.println(ans); for (int i = 0; i < m; i++) { while (--b[i] >= 0) { pw.print((char) (i + 97)); } } while (--k >= 0) { pw.print('a'); } return; } } } int[][] nm(int a_len, int a_hei) throws IOException { int[][] _a = new int[a_len][a_hei]; for (int i = 0; i < a_len; i++) for (int j = 0; j < a_hei; j++) _a[i][j] = ni(); return _a; } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int[] na(int a_len) throws IOException { int[] _a = new int[a_len]; for (int i = 0; i < a_len; i++) _a[i] = ni(); return _a; } int ni() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } String nl() throws IOException { return br.readLine(); } void tr(String debug) { if (!OJ) pw.println(" " + debug); } static PrintWriter pw; static BufferedReader br; static StringTokenizer st; static boolean OJ; public static void main(String[] args) throws IOException { long timeout = System.currentTimeMillis(); OJ = System.getProperty("ONLINE_JUDGE") != null; pw = new PrintWriter(System.out); br = new BufferedReader(OJ ? new InputStreamReader(System.in) : new FileReader(new File("A.txt"))); while (br.ready()) new A().run(); if (!OJ) { pw.println("----------------------------------"); pw.println(System.currentTimeMillis() - timeout); } br.close(); pw.close(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int n, t, ret, a[30]; char s[1005]; int solve(int k) { int req = 0; for (int i = (0); i < (26); i++) req += (a[i] + k - 1) / k; return req <= n; } int main() { scanf("%s%d", s, &n); for (int i = 0; s[i]; ++i) ++a[s[i] - 'a']; for (int i = (0); i < (26); i++) t += !!a[i]; if (t > n) puts("-1"); else { for (int i = 1, j = 1000, k; j >= i;) { k = (i + j) >> 1; if (solve(k)) ret = k, j = k - 1; else i = k + 1; } printf("%d\n", ret); t = n; for (int i = (0); i < (26); i++) for (int j = (0); j < ((a[i] + ret - 1) / ret); j++) printf("%c", i + 'a'), --t; while (t--) printf("a"); puts(""); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int freq[500]; string calcular(int k, int n) { if (k == 0) return ""; string res; char relleno; for (int i = 0; i < 500; i++) { if (!freq[i]) continue; relleno = i; int cant = freq[i] / k; if (freq[i] % k) cant++; res += string(cant, i); } if (res.size() < n) res += string(n - res.size(), relleno); return res; } int main() { string s; int n; cin >> s >> n; for (int i = 0; i < s.size(); i++) freq[s[i]]++; string result; int cantidad = 100000; int ini = 0; int fin = s.size() + 1; int mid; while (ini + 1 < fin) { int mid = (ini + fin) / 2; string res = calcular(mid, n); if (res.size() == n) { result = res; cantidad = min(cantidad, mid); fin = mid; } else { ini = mid; } } if (cantidad == 100000) cout << -1 << endl; else cout << cantidad << endl << result << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int oo = 1 << 30; const int NMAX = 1005; char s[NMAX], sol[NMAX]; int n, frecv[30], length; inline bool CHECK(int x) { int i, aux, rez = 0; for (i = 1; i <= 26; i++) if (frecv[i]) { aux = frecv[i] / x; if (frecv[i] % x != 0) aux++; rez += aux; } if (rez <= n) return 1; return 0; } int main() { int i, len, aux, minim = oo, st, dr, mij; cin.sync_with_stdio(false); cin >> (s + 1); cin >> n; len = strlen(s + 1); for (i = 1; i <= len; i++) frecv[s[i] - 'a' + 1]++; st = 1; dr = len; while (st <= dr) { mij = (st + dr) >> 1; if (CHECK(mij)) { minim = min(minim, mij); dr = mij - 1; } else st = mij + 1; } if (minim == oo) cout << "-1\n"; else { for (i = 1; i <= 26; i++) if (frecv[i]) { aux = frecv[i] / minim; if (frecv[i] % minim != 0) aux++; while (aux) sol[++length] = i + 'a' - 1, aux--; } cout << minim << "\n"; while (length < n) sol[++length] = 'a'; cout << (sol + 1); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import sys def dbg(was): if debug_mode: print 'DBG\t' + str(was) def solve(s, n): a = ord('a') z = ord('z') data = {} for char in s: dbg(char) data[char] = float(data.get(char, 0) + 1) data2 = [] for key in data.keys(): data2.append([key, data[key]]) data = data2 if len(data) > n: print "-1" return ans = [] data.sort(key=lambda x: x[1], reverse = True) for i in xrange(n - len(data)): ans.append(data[0][0]) igogo = ans.count(data[0][0]) data[0][1] = data[0][1] * igogo / (igogo + 1) data.sort(key=lambda x: x[1], reverse = True) res = data[0][1] if int(res) != res: res = int(res) + 1 else: res = int(res) print str(res) print ''.join(ans + map(lambda x: x[0], data)) if len(sys.argv) > 1: debug_mode = True datum = open(sys.argv[1], 'r') else: debug_mode = False datum = sys.stdin s = str(datum.readline().split()[0]) n = int(datum.readline()) solve(s, n)
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; int a[26], ans[26]; for (int i = 0; i < 26; i++) { a[i] = 0; ans[i] = 0; } for (int i = 0; i < s.length(); i++) a[s[i] - 'a']++; for (int i = 0; i < 26; i++) { if (a[i] != 0) { a[i]--; ans[i]++; n--; } } if (n < 0) { cout << "-1"; return 0; } while (n--) { int machau = 0, cnt = 0; int p; bool flag = 0; for (int i = 0; i < 26; i++) { if (a[i] != 0) { flag = 1; cnt = (a[i] + ans[i] - 1) / ans[i]; if (machau < cnt) { machau = cnt; p = i; } } } if (flag == 0) break; a[p]--; ans[p]++; } for (int i = 0; i < 26; i++) a[i] = 0; for (int i = 0; i < s.length(); i++) a[s[i] - 'a']++; int maxx = 0; int cnt = 0; for (int i = 0; i < 26; i++) { if (a[i] != 0) { cnt = (a[i] + ans[i] - 1) / ans[i]; if (maxx < cnt) maxx = cnt; } } cout << maxx << endl; string anss = ""; for (int i = 0; i < 26; i++) { while (ans[i]--) anss += ('a' + i); } n++; while (n--) { anss += anss[0]; } cout << anss; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; float arr[26] = {0}; set<char> s1; for (int i = 0; i < s.length(); i++) { s1.insert(s[i]); int x = s[i] - 97; arr[x]++; } if (s1.size() > n) { cout << "-1"; } else { string r(""); float x[26] = {0}; for (int i = 0; i < 26; i++) { if (arr[i] > 0) { char z = i + 97; r += z; x[i]++; } } while (n > r.length()) { float maxval = 0; int index = 0; for (int i = 0; i < 26; i++) { if (arr[i] > 0) { float rz = (float)arr[i] / x[i]; if (rz > maxval) { maxval = rz; index = i; } } } x[index]++; char z = index + 97; r += z; } int num = 0; for (int i = 0; i < 26; i++) { if (arr[i] > 0) { num = max((int)ceil(float(arr[i] / x[i])), num); } } cout << num << endl; sort(r.begin(), r.end()); cout << r; } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int MAXPOW = 20; const int MAXA = 1048577; const int MAXN = 100009; const int MAXM = 100009; char s[1009]; int n; bool canSolve(int m, const vector<pair<int, char>>& cnt) { int need = 0; for (int i = 0; i < cnt.size(); ++i) { need += (cnt[i].first + m - 1) / m; } return (need <= n); } int main() { gets(s); cin >> n; int len = strlen(s); int counter[256]; memset(counter, 0, sizeof(counter)); for (int i = 0; i < len; ++i) { ++counter[s[i]]; } vector<pair<int, char>> cnt; for (int i = 0; i < 256; ++i) { if (counter[i]) { cnt.push_back(make_pair(counter[i], i)); } } int l = 0; int r = len * len; while (l + 1 < r) { int m = (l + r) / 2; if (canSolve(m, cnt)) { r = m; } else { l = m; } } if (canSolve(r, cnt)) { cout << r << endl; for (int i = 0; i < cnt.size(); ++i) { int need = (cnt[i].first + r - 1) / r; for (int j = 0; j < need; ++j) { cout << cnt[i].second; } n -= need; } for (int i = 0; i < n; ++i) { cout << 'z'; } cout << endl; } else { cout << -1 << endl; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char s[1010]; int cnt[130] = {0}, cnt2[130] = {0}, ans[130] = {0}; double xxx[130]; int main() { int n; scanf("%s%d", s, &n); int len = strlen(s); for (int i = 0; i < len; ++i) ++cnt[s[i]]; int m = 0, n0 = n; for (int i = 'a'; i <= 'z'; ++i) if (cnt[i]) cnt2[i] = cnt[i], ++m; if (m > n) { puts("-1"); return 0; } else { n -= m; for (int i = 'a'; i <= 'z'; ++i) { xxx[i] = cnt[i]; if (cnt[i]) ans[i] = 1; } for (int i = 0; i < n; ++i) { int ma = -1, t; for (int j = 'a'; j <= 'z'; ++j) if (cnt[j]) { if ((cnt[j] + ans[j] - 1) / ans[j] > ma) { ma = (cnt[j] + ans[j] - 1) / ans[j]; t = j; } } ++ans[t]; } } int hehe = 0; for (int i = 'a'; i <= 'z'; ++i) { if (cnt2[i]) { if ((cnt2[i] + ans[i] - 1) / ans[i] > hehe) hehe = (cnt2[i] + ans[i] - 1) / ans[i]; } } printf("%d\n", hehe); for (int i = 'a'; i <= 'z'; ++i) if (ans[i]) { while (ans[i]--) printf("%c", i); } puts(""); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int n, m, l = 0, a[33], b[33], S = 0, x; char s[1111]; int main() { cin >> s >> n; m = strlen(s); if (n >= m) { cout << "1" << endl; cout << s; for (int i = 0; i < n - m; i++) cout << "a"; return 0; } for (int i = 0; i < m; i++) a[s[i] - 'a']++; for (int i = 0; i < 26; i++) if (a[i]) l++; if (l > n) { cout << "-1"; return 0; } x = 2; while (1) { S = 0; for (int i = 0; i < 26; i++) if (a[i]) { if (a[i] % x) b[i] = a[i] / x + 1; else b[i] = a[i] / x; S += b[i]; } if (S <= n) { b[0] += n - S; cout << x << endl; for (int i = 0; i < 26; i++) for (int j = 0; j < b[i]; j++) cout << (char)('a' + i); return 0; } x++; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; const int inf = 1 << 29; const double eps = 1e-8; string s; int n; int vis[maxn]; bool check(int x) { int sum = 0; for (int i = 0; i < 26; i++) { if (vis[i]) sum += (vis[i] - 1) / x + 1; } return sum <= n; } int main() { int cnt = 0; cin >> s; cin >> n; for (int i = 0; i < s.size(); i++) { if (!vis[s[i] - 'a']) { cnt++; } vis[s[i] - 'a']++; } if (cnt > n) { puts("-1"); return 0; } int l = 1, r = 1000, res; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) { res = mid; r = mid - 1; } else { l = mid + 1; } } printf("%d\n", res); int tot = 0; for (int i = 0; i < 26; i++) { if (vis[i]) { int st = (vis[i] - 1) / res + 1; tot += st; for (int j = 1; j <= st; j++) printf("%c", i + 'a'); } } tot = n - tot; while (tot--) { putchar('a'); } printf("\n"); }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; map<char, long long int> mp; long long int n; bool check(long long int k) { long long int cnt = 0; for (char x = 'a'; x <= 'z'; x++) cnt += ceil((double)mp[x] / (double)k); if (cnt <= n) return 1; return 0; } long long int solve(long long int low, long long int high) { long long int ans = 1; while (low <= high) { long long int mid = (low + high) / 2; if (check(mid)) { high = mid - 1; ans = mid; } else low = mid + 1; } return ans; } int main() { string s; cin >> s; long long int i, j; cin >> n; for (i = 0; i < s.length(); i++) mp[s[i]]++; if (mp.size() > n) { cout << "-1\n"; return 0; } long long int ans = solve(1, s.length()); cout << ans << "\n"; vector<char> v; for (char x = 'a'; x <= 'z'; x++) { long long int cnt = ceil((double)mp[x] / (double)ans); while (cnt != 0) { v.push_back(x); cnt--; } } while (v.size() != n) v.push_back('a'); for (i = 0; i < v.size(); i++) cout << v[i]; cout << "\n"; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; string s; int N; int lcount[27]; bool works(int x) { int tot = 0; for (int i = 0; i < 26; i++) { if (lcount[i]) tot += (lcount[i] - 1) / x + 1; } return tot <= N; } int main() { cin >> s >> N; for (int i = 0; i < 27; i++) lcount[i] = 0; for (int i = 0; i < s.length(); i++) lcount[(int)s[i] - 'a']++; int ccount = 0; for (int i = 0; i < 26; i++) if (lcount[i]) ccount++; if (ccount > N) { cout << "-1\n"; return 0; } int lo = 1, hi = 10000; while (lo < hi) { int mid = (lo + hi) / 2; if (works(mid)) hi = mid; else lo = mid + 1; } cout << lo << "\n"; int tot = 0; for (int i = 0; i < 26; i++) if (lcount[i]) { for (int j = 0; j < (lcount[i] - 1) / lo + 1; j++) { cout << (char)(i + 'a'); tot++; } } for (int i = tot; i < N; i++) cout << 'a'; cout << "\n"; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; public class P335A { private static class Letter implements Comparable<Letter> { char c; int total; int occu; public Letter(char c, int total, int occu) { this.c = c; this.total = total; this.occu = occu; } @Override public int compareTo(Letter other) { float x1 = (float)total/occu; float x2 = (float)(other.total)/other.occu; if (x1 > x2) return -1; if (x1 < x2) return 1; if (c < other.c) return -1; if (c > other.c) return 1; return 0; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); final int N = sc.nextInt(); HashMap<Character, Integer> count = new HashMap<Character, Integer>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (!count.containsKey(c)) { count.put(c, 0); } count.put(c, count.get(c) + 1); } int m = count.size(); if (m > N) { System.out.println(-1); return; } PriorityQueue<Letter> queue = new PriorityQueue<Letter>(); int[] occupied = new int[26]; for (char c: count.keySet()) { occupied[c-'a'] = 1; queue.add(new Letter(c, count.get(c), 1)); } for (int i = 0; i < N - m; i++) { Letter letter = queue.poll(); occupied[letter.c-'a']++; queue.add(new Letter(letter.c, letter.total, letter.occu + 1)); } Letter letter = queue.poll(); if (letter.total % letter.occu == 0) { System.out.println(letter.total/letter.occu); } else { System.out.println(letter.total/letter.occu + 1); } StringBuilder sb = new StringBuilder(); for (char c: count.keySet()) { for (int i = 0; i < occupied[c-'a']; i++) { sb.append(c); } } System.out.println(sb.toString()); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long md = M; if (e == 0) return 1; if (e % 2 == 0) { long long t = bigmod(p, e / 2, M); return (T)((t * t) % md); } long long bm = bigmod(p, e - 1, M); bm = (bm * (long long)p) % md; return (T)bm; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } int cnt[36]; char pp[1001]; char ps[1001]; double rat[36]; double tot[36]; int main() { long long a, b = 0, c = 0, d = 0, e, f, g, h, x, y, z; cin >> pp >> a; for (int i = (0); i < (strlen(pp)); ++i) cnt[pp[i] - 'a']++; for (int i = (0); i < (30); ++i) if (cnt[i] > 0) b++; if (a < b) { cout << -1 << endl; return 0; } for (int i = (0); i < (27); ++i) { rat[i] = 100000; if (cnt[i] == 0) rat[i] = 0.0; } c = 0; while (a > 0) { double p = 0; b = 0; for (int i = (0); i < (26); ++i) { if (rat[i] > p) { p = rat[i]; b = i; } } ps[c++] = b + 'a'; tot[b]++; rat[b] = (double)cnt[b] / tot[b]; a--; } double p = 0; for (int i = (0); i < (27); ++i) p = max(p, rat[i]); cout << ceil(p) << endl; cout << ps << endl; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; public class Banana { public static void main(String[] args) { Scanner input = new Scanner(System.in); int i,j,n,m,a,b,l,sum,c; char[] ss; String s,s1; while (input.hasNext()){ s = input.next(); sum = 0;s1 = "";c=0; n = input.nextInt(); l = s.length();//System.out.println(l); // ArrayList<Integer>list = new ArrayList<>(26); int[] arr = new int[26]; char[] le = new char[26]; int[] poi = new int[26]; char v, ch; for (i=0; i<l; i++){ arr[s.charAt(i) - 'a']++; } //System.out.println(arr[0]); for (i=0; i<26; i++) { poi[i]++; le[i] = (char) (97 + i); if (arr[i] > 0){ s1 += le[i]; //arr[i]--; sum ++; } } for (i=0; i<26; i++){ for (j=0; j<26; j++) { if (arr[i] > arr[j]) { a = arr[i]; arr[i] = arr[j]; arr[j] = a; ch = le[i]; le[i] = le[j]; le[j] = ch; } } } /* for (i=0; i<26; i++) System.out.print(arr[i]+"="+le[i] + " "); System.out.println();*/ //System.out.println(arr[0]); if (sum > n){ System.out.println(-1); } else { a = n - sum; j = 0; //System.out.println(arr[0] + " " + arr[1]+ " " + arr[2]); //arr[5]=77; for (i=0; i<a; i++){ poi[j]++;b = 0; s1 += le[j]; //System.out.println(arr[j] + " " + le[j] + " "+poi[j]); for (j=0; j<26; j++) { /* if (arr[j] > 0) System.out.println(arr[j]+ " " +poi[j] + " " + j); */ if (arr[j] % poi[j] == 0) c = arr[j] / poi[j]; else { c = arr[j] / poi[j]; c++; } if (arr[j] > 0 && c>b) { l = j; // System.out.println("ans " + arr[j] + " " +poi[j] + " " + j); b = c; } } // System.out.println(); j = l; //System.out.println(j); } b = 0;//aaaaaaaaaaabbc 5 for (j=0; j<26; j++) { if (arr[j] % poi[j] == 0) c = arr[j] / poi[j]; else { c = arr[j] / poi[j]; c++; } if (arr[j] > 0 && c > b) { l = j; //System.out.println(arr[j] + " " + j); b = c; } } ss = s1.toCharArray(); //System.out.println(poi[l] + " " + arr[l]); Arrays.sort(ss); s1=String.valueOf(ss); if (arr[l] % poi[l] == 0) System.out.println(arr[l] / poi[l]); else { arr[l] /= poi[l]; arr[l]++; System.out.println(arr[l]); } System.out.println(s1); } } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.util.*; public class A5{ public static void main(String[] args){ Scanner in=new Scanner(System.in); int N,cnt; String str=in.nextLine(); char[] cstr=str.toCharArray(); Arrays.sort(cstr); N=in.nextInt(); cnt=0; int[] nc=new int[26]; for(int i=0;i<str.length();i++){ nc[cstr[i]-'a']++; } StringBuilder res=new StringBuilder(); int[] nres=new int[26]; PriorityQueue<myobj> pq=new PriorityQueue<myobj>(30,new mycmp()); for(int i=0;i<26;i++) if(nc[i]>0) { cnt++; res.append((char)((int)('a')+i)); pq.add(new myobj(i,nc[i])); nres[i]=1; } /*System.out.println("# of chs: "+cnt); myobj[] test=new myobj[30]; myobj[] test1=pq.toArray(test); for(int i=0;test1[i]!=null;i++){ System.out.println("key :"+test1[i].ch+"--#: "+test1[i].cnt); } System.out.println(res); */ if(cnt>N) System.out.println(-1); else{ while(cnt<N){ myobj temp=pq.poll(); res.append((char)('a'+temp.ch)); nres[temp.ch]++; int times=nc[temp.ch]/nres[temp.ch]; if(nc[temp.ch]%nres[temp.ch]!=0) times++; pq.add(new myobj(temp.ch,times)); cnt++; } System.out.println(pq.poll().cnt); System.out.println(res); } } } class myobj{ public int ch,cnt; public myobj(int c,int t){ ch=c; cnt=t; } } class mycmp implements Comparator<myobj> { public int compare(myobj x, myobj y) { return y.cnt-x.cnt; } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class A { BufferedReader reader; StringTokenizer tokenizer; PrintWriter out; int[] cnt; int N; public void solve() throws IOException { cnt = new int[26]; char[] s = reader.readLine().toCharArray(); for(int i = 0; i < s.length; i++) cnt[ s[i]-'a' ]++; N = nextInt(); if( !OK(s.length)){ out.println(-1); return; } int l = 1; int r = s.length; while( l < r ){ int mid = (l+r)/2; if( OK(mid) ){ r = mid; } else{ l = mid+1; } } out.println(l); int to = 0; for(int i = 0; i < 26; i++){ int need = (int) Math.ceil( (double)cnt[i]/l ); for(int j = 0; j < need; j++){ out.print( (char)('a'+i) ); } to += need; } for(int i = 0; i < N-to; i++) out.print('a'); out.println(); } public boolean OK(int v){ int len = 0; for(int i = 0; i < 26; i++){ len += Math.ceil( (double)cnt[i]/v); } if( len <= N) return true; return false; } /** * @param args */ public static void main(String[] args) { new A().run(); } public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; out = new PrintWriter(System.out); solve(); reader.close(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.math.BigInteger; import java.util.*; public class memSQL { public static void main(String[] args) throws IOException { new memSQL().run(); } int[] getCnt(String s) { int cnt[] = new int[300]; for(int i = 0; i < s.length(); ++i) cnt[s.charAt(i)]++; return cnt; } Integer itercnt = 1; int getIterCnt(int[] cntin, int[] cntout) { int respos = -1; int rescnt = Integer.MIN_VALUE; for(int i = 0; i < cntout.length; ++i) if(cntout[i] > 0) { int curcnt = (cntin[i] + cntout[i] - 1) / cntout[i]; if(curcnt > rescnt) { rescnt = curcnt; respos = i; } } itercnt = rescnt; return respos; } void solve() throws IOException { String s = nextToken(); int n = nextInt(); int[] cntin = getCnt(s); int difch = 0; for(int cnt : cntin) if(cnt > 0) difch++; if(difch > n) { out.println(-1); return; } StringBuilder b = new StringBuilder(); int nrem = n; for(int i = 0; i < cntin.length && nrem > 0; ++i) if(cntin[i] > 0) { b.append((char)i); --nrem; } while(nrem > 0) { int maxpos = getIterCnt(cntin, getCnt(b.toString())); if(maxpos == -1) b.append((char)'a'); else b.append((char)maxpos); --nrem; } getIterCnt(cntin, getCnt(b.toString())); out.println(itercnt); out.println(b.toString()); } BufferedReader br; StringTokenizer st; PrintWriter out; public String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } void run() throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; br = new BufferedReader( new InputStreamReader( oj ? System.in : new FileInputStream("input.txt"))); out = new PrintWriter( oj ? System.out : new FileOutputStream("output.txt")); solve(); out.close(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> char s[1010], t[1010]; int a[27] = {0}, b[27] = {0}, n, m, x, y, l; int f(int k) { int s = 0; for (int i = 1; i <= 26; i++) if (a[i]) { s += a[i] / k; if (a[i] % k) s++; } return s; } int main() { scanf("%s", s); l = strlen(s); for (int i = 0; i < l; i++) a[s[i] - 96]++; scanf("%d", &n); x = 0; for (int i = 1; i <= 26; i++) if (a[i]) x++; if (x > n) { printf("-1\n"); return 0; } for (int i = 1; i <= l; i++) if (f(i) <= n) { x = n - f(i); printf("%d\n", i); for (int j = 1; j <= 26; j++) if (a[j]) { y = a[j] / i; if (a[j] % i) y++; while (y--) printf("%c", j + 96); } while (x--) printf("z"); printf("\n"); return 0; } }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 7; char str[1010]; int a[30]; int n; bool can(int x) { int y = 0; for (int i = 0; i < 26; i++) y += a[i] / x + (a[i] % x > 0); if (y <= n) return true; return false; } int main() { scanf("%s", str); for (int i = 0; str[i]; i++) a[str[i] - 'a']++; scanf("%d", &n); int l = 1, r = 1000; while (l <= r) { int mid = l + r >> 1; if (can(mid)) r = mid - 1; else l = mid + 1; } if (l == 1001) puts("-1"); else { int L = 0; printf("%d\n", l); for (int i = 0; i < 26; i++) { int len = a[i] / l + (a[i] % l > 0); for (int j = 0; j < len; j++) printf("%c", i + 'a'); L += len; } for (int j = L; L < n; L++) printf("a"); } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
//package cf194; import java.util.*; import java.io.*; public class A2 { Scanner in; PrintWriter out; public void solve() throws IOException { String s = in.next(); int n = in.nextInt(); int[] a = new int[500]; for (int i = 0; i < s.length(); i++) { a[s.charAt(i)]++; } int l = 0; int r = 100000; while ((r - l) > 1) { int m = (l + r) / 2; int x = 0; for (int i = 0; i < 500; i++) { x += (a[i] / m) + ( ((a[i] % m) > 0 ) ? 1 : 0); } if (x <= n) { r = m; } else { l = m; } } if (r == 100000) { System.out.println(-1); return; } System.out.println(r); int len = 0; for (int i = 0; i < 500; i++) { int k = (a[i] / r) + ( ((a[i] % r) > 0 ) ? 1 :0); for (int j = 0; j < k; j++) { System.out.print(Character.toChars(i)); len++; } } for (int i = 0; i < n - len; i++) { System.out.print('a'); } } public void run() { try { in = new Scanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] arg) { new A2().run(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int n; map<char, int> f; string s; char ch; bool solve(long long in) { int c = 0; for (auto a : f) { if (c > n) return 0; c += a.second / in + (a.second % in ? 1 : 0); } return c <= n; } int main() { ios::sync_with_stdio(false); cin >> s >> n; for (int i = 0; i < (int)s.length(); i++) { f[s[i]]++; } if ((int)f.size() > n) { cout << -1; return 0; } if (n >= (int)s.length()) { cout << 1 << endl << s; for (int i = (int)s.length(); i < n; i++) cout << s[0]; return 0; } long long low = 1, mid, high = 1000; while (1) { mid = (low + high) / 2; if (solve(low)) { mid = low; break; } else if (mid == low) { mid = high; break; } if (solve(mid)) high = mid; else low = mid + 1; } cout << mid << endl; int disp = 0; for (auto a : f) { int t = a.second / mid + (a.second % mid ? 1 : 0); for (int j = 0; j < t; j++) { cout << a.first; ch = a.first; disp++; } } while (disp < n) { cout << ch; disp++; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.StringTokenizer; import java.util.concurrent.atomic.AtomicInteger; public class A { private final static long START_TIME=System.currentTimeMillis(); private final static boolean LOG_ENABLED=true; private final static boolean ONLINE_JUDGE = LOG_ENABLED && (System.getProperty("ONLINE_JUDGE") != null); private final static String SYSTEM_ENCODING="utf-8"; private static class Logger{ private final PrintWriter logWriter=Util.newPrintWriter(System.err,true); private final DecimalFormat df=new DecimalFormat("0.000"); private void message(String type, String message, Object ... params){ if(ONLINE_JUDGE){ return; } logWriter.printf("["+type+"] "+df.format((System.currentTimeMillis()-START_TIME)/1000.0)+": "+message+"\r\n", params); } public void debug(String message, Object ... params){ message("DEBUG", message, params); } } private final static class Util{ public static PrintWriter newPrintWriter(OutputStream out, boolean autoFlush){ try { return new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(out), SYSTEM_ENCODING),autoFlush); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } } } public final static class FastScanner{ private BufferedReader reader; private StringTokenizer currentTokenizer; public FastScanner(Reader reader) { if(reader instanceof BufferedReader){ this.reader=(BufferedReader) reader; }else{ this.reader=new BufferedReader(reader); } } public String next(){ if(currentTokenizer==null || !currentTokenizer.hasMoreTokens()){ try { currentTokenizer=new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new IllegalStateException(e); } } return currentTokenizer.nextToken(); } public int nextInt(){ return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } } private final static Logger log=new Logger(); private final BufferedReader reader; private final FastScanner in; private final PrintWriter out=Util.newPrintWriter(System.out,false); public A(BufferedReader reader){ this.reader=reader; in=new FastScanner(this.reader); } public static void main(String[] args) throws IOException { log.debug("Started"); try{ new A(new BufferedReader(new InputStreamReader(System.in, SYSTEM_ENCODING))).run(); }finally{ log.debug("Stopped"); } } void run(){ solve(); out.flush(); } private void solve(){ String s=in.next(); char[] stringChars=s.toCharArray(); int n = in.nextInt(); log.debug("intput has been read"); char[] result=new char[n]; if(s.length()<=n){ for(int i=0;i<n;i++){ result[i]=stringChars[i%s.length()]; } out.println(1); out.println(new String(result)); return; } Map<Character, AtomicInteger> chars = getCharCounts(stringChars,stringChars.length); if(chars.size()>n){ out.println(-1); return; } int i=0; for(Entry<Character,AtomicInteger> e:chars.entrySet()){ result[i++]=e.getKey(); } Map<Character, AtomicInteger> currChars = getCharCounts(result, i); int maxPageCount = 2; while(i<n){ Character bestChar=null; for(Entry<Character, AtomicInteger> e:currChars.entrySet()){ int pageCount = (int)Math.ceil(chars.get(e.getKey()).doubleValue()/e.getValue().get()); if(bestChar==null||pageCount>maxPageCount){ bestChar=e.getKey(); maxPageCount=pageCount; } } currChars.get(bestChar).incrementAndGet(); result[i]=bestChar; i++; } maxPageCount = 2; for(Entry<Character, AtomicInteger> e:currChars.entrySet()){ int pageCount = (int)Math.ceil(chars.get(e.getKey()).doubleValue()/e.getValue().get()); if(pageCount>maxPageCount){ maxPageCount=pageCount; } } out.println(maxPageCount); Arrays.sort(result); out.println(new String(result)); } /** * @param stringChars * @return */ private static Map<Character, AtomicInteger> getCharCounts(char[] stringChars, int max) { Map<Character,AtomicInteger> chars=new HashMap<Character, AtomicInteger>(); for(int i=0;i<max;i++){ char c = stringChars[i]; AtomicInteger count = chars.get(c); if(count==null){ count=new AtomicInteger(0); chars.put(c, count); } count.incrementAndGet(); } return chars; } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.text.DecimalFormat; import java.util.*; import java.awt.Point; public class Newbie { static InputReader sc = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws IOException { solver s = new solver(); int t = 1; while (t > 0) { s.sol(); t--; } out.close(); } /* static class descend implements Comparator<pair1> { public int compare(pair1 o1, pair1 o2) { if (o1.pop != o2.pop) return (int) (o1.pop - o2.pop); else return o1.in - o2.in; } }*/ static class InputReader { public BufferedReader br; public StringTokenizer token; public InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream), 32768); token = null; } public String sn() { while (token == null || !token.hasMoreTokens()) { try { token = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return token.nextToken(); } public int ni() { return Integer.parseInt(sn()); } public String snl() throws IOException { return br.readLine(); } public long nlo() { return Long.parseLong(sn()); } public double nd() { return Double.parseDouble(sn()); } public int[] na(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.ni(); return a; } public long[] nal(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = sc.nlo(); return a; } } static class ascend implements Comparator<pair> { public int compare(pair o1, pair o2) { return o2.b - o1.b; } } static class extra { static boolean v[] = new boolean[100001]; static List<Integer> l = new ArrayList<>(); static int t; static void shuffle(int a[]) { for (int i = 0; i < a.length; i++) { int t = (int) Math.random() * a.length; int x = a[t]; a[t] = a[i]; a[i] = x; } } static void shufflel(long a[]) { for (int i = 0; i < a.length; i++) { int t = (int) Math.random() * a.length; long x = a[t]; a[t] = a[i]; a[i] = x; } } static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } static boolean valid(int i, int j, int r, int c) { if (i >= 0 && i < r && j >= 0 && j < c) { // System.out.println(i+" /// "+j); return true; } else { // System.out.println(i+" //f "+j); return false; } } static void seive() { for (int i = 2; i < 101; i++) { if (!v[i]) { t++; l.add(i); for (int j = 2 * i; j < 101; j += i) v[j] = true; } } } static int binary(LinkedList<Integer> a, long val, int n) { int mid = 0, l = 0, r = n - 1, ans = 0; while (l <= r) { mid = (l + r) >> 1; if (a.get(mid) == val) { r = mid - 1; ans = mid; } else if (a.get(mid) > val) r = mid - 1; else { l = mid + 1; ans = l; } } return (ans + 1); } static long fastexpo(long x, long y) { long res = 1; while (y > 0) { if ((y & 1) == 1) { res *= x; } y = y >> 1; x = x * x; } return res; } static long lfastexpo(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } /* void dijsktra(int s, List<pair> l[], int n) { PriorityQueue<pair> pq = new PriorityQueue<>(new ascend()); int dist[] = new int[100005]; boolean v[] = new boolean[100005]; for (int i = 1; i <= n; i++) dist[i] = 1000000000; dist[s] = 0; for (int i = 1; i < n; i++) { if (i == s) pq.add(new pair(s, 0)); else pq.add(new pair(i, 1000000000)); } while (!pq.isEmpty()) { pair node = pq.remove(); v[node.a] = true; for (int i = 0; i < l[node.a].size(); i++) { int v1 = l[node.a].get(i).a; int w = l[node.a].get(i).b; if (v[v1]) continue; if ((dist[node.a] + w) < dist[v1]) { dist[v1] = dist[node.a] + w; pq.add(new pair(v1, dist[v1])); } } } }*/ } static class pair { int a; int b; public pair(int a, int b) { this.a = a; this.b = b; } } static class pair1 { pair p; int in; public pair1(pair a, int n) { this.p = a; this.in = n; } } static int inf = 5000013; static class solver { DecimalFormat df = new DecimalFormat("0.00000000"); extra e = new extra(); long mod = (long) (1000000007); void sol() throws IOException { String s = sc.sn(); int n = s.length(); int k = sc.ni(); char c[] = s.toCharArray(); int inf[] = new int[26]; int add[] = new int[26]; StringBuilder sb = new StringBuilder(); int cnt = 0; for (int i = 0; i < n; i++) { if (inf[c[i] - 'a'] == 0) { sb.append(c[i]); cnt++; } inf[c[i] - 'a']++; } if (cnt > k) { System.out.println(-1); System.exit(0); } k -= cnt; PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> ((int) Math.ceil((inf[x] * 1.0) / add[x]) - (int) Math.ceil((inf[y] * 1.0) / add[y])) * -1); for (int i = 0; i < 26; i++) { if (inf[i] > 0) { add[i]++; pq.add(i); } } while (k-- > 0 && !pq.isEmpty()) { int t = pq.poll(); sb.append((char) (t + 'a')); add[t]++; pq.add(t); } int ans = -1; for (int i = 0; i < 26; i++) { if (inf[i] > 0) ans = Math.max(ans, (int) Math.ceil((inf[i] * 1.0) / add[i])); } out.println(ans); out.println(sb); } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; char a[1005]; int t[26] = {0}; int func(int m) { int c = 0, i; for (i = 0; i < 26; i++) { if (t[i] != 0) { if (t[i] % m == 0) c += (t[i] / m); else c += (t[i] / m + 1); } } return c; } int main() { int s, n, i, j, c = 0, e, mid, l = 0; for (i = 0; i < 26; i++) t[i] = 0; scanf("%s", a); cin >> n; for (i = 0; a[i] != '\0'; i++) { l++; if (t[a[i] - 'a'] == 0) c++; t[a[i] - 'a']++; } if (c > n) { cout << -1 << endl; return 0; } s = 1; e = l; int f = 0; while (s <= e) { mid = s + (e - s) / 2; j = func(mid); if (f == 1) break; if (j <= n) e = mid; else s = mid + 1; if (s == e) f = 1; } cout << mid << endl; s = mid; int r = 0; for (i = 0; i < 26; i++) { c = 0; if (t[i] % s == 0) c = (t[i] / s); else c = (t[i] / s + 1); r += c; for (j = 0; j < c; j++) { cout << (char)(97 + i); } t[i] -= c; } i = 0; while (r < n) { r++; cout << (char)(97 + i); } cout << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; long long n, l, r, mid, hasil, ans, sz; map<char, long long> m; map<char, bool> b; vector<char> v; string ss, s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s >> n; for (int i = 0; i < s.length(); i++) { if (b[s[i]] == false) { b[s[i]] = true; v.push_back(s[i]); } m[s[i]]++; } if (v.size() > n) { cout << -1 << '\n'; return 0; } l = 1; r = s.length(); while (l <= r) { string t; mid = (l + r) / 2; for (int i = 0; i < v.size(); i++) { hasil = (m[v[i]] + mid - 1) / mid; for (int j = 0; j < hasil; j++) { t += v[i]; } } if (t.length() <= n) { sz = n - t.length(); for (int i = 0; i < sz; i++) { t += v[0]; } ss = t; ans = mid; r = mid - 1; } else { l = mid + 1; } } cout << ans << '\n' << ss << '\n'; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from collections import Counter import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## import math s=input() n=int(input()) d={} for i in s: if i in d: d[i]+=1 else: d[i]=1 if len(d)>n: print(-1) exit() k=0 while True: k+=1 cnt=0 ans = "" for i in d: var=math.ceil(d[i]/k) ans+=str(i)*var cnt+=var if cnt>n: continue print(k) print(ans+"a"*(n-len(ans))) break
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; public class Banana { static HashMap<Character, Integer>c; @SuppressWarnings("unchecked") public static void main(String []args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); c=new HashMap<Character, Integer>(); String s=br.readLine(); for(int i=0;i<s.length();i++) { char a=s.charAt(i); if(c.containsKey(a))c.put(a, c.get(a)+1); else c.put(a, 1); } int n=Integer.parseInt(br.readLine()); int tam=c.size(); if(n<tam)System.out.print(-1); else { ArrayList<C>caracteres=new ArrayList<C>(); Iterator it =c.keySet().iterator(); String res=""; while (it.hasNext()){ char key=it.next().toString().charAt(0); caracteres.add(new C(key, c.get(key))); res+=key; } Collections.sort(caracteres); while(res.length()<n) { char b=caracteres.get(caracteres.size()-1).c; res+=b; caracteres.get(caracteres.size()-1).setNroHojas(); Collections.sort(caracteres); } Collections.sort(caracteres); System.out.println(caracteres.get(caracteres.size()-1).nroHojas); System.out.print(res); } } static class C implements Comparable<C> { char c; int cantR; int nroHojas; int nroV; public C(char c, int cantR) { this.c=c; this.cantR=cantR; nroV=1; nroHojas=cantR/nroV; } @Override public int compareTo(C a) { if(nroHojas<a.nroHojas)return -1; return 1; } public void setNroHojas() { nroV++; nroHojas=cantR/nroV; if(cantR%nroV!=0)nroHojas++; } } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from math import ceil a=raw_input() n=int(raw_input()) s={} for i in a: if not i in s: s[i]=1 else: s[i]+=1 tk=False if len(s)>n: print -1 tk=True if not tk: s=sorted([(s[i],i) for i in s])[::-1] ss=[[i,j,1] for i,j in s] # print s t=[i for (j,i) in s] # print "".join(t) k=s[0][0] tt=False while len(t)<n: tt=True m=0 for i,j in enumerate(ss): if j[0]>=m: im=i m=j[0] ss[im][0]*=ss[im][2] ss[im][2]+=1 ss[im][0]=1.*ss[im][0]/(ss[im][2]) # print ss[im][1], m t=t+[ss[im][1]] print int(ceil(max(i for i,_,_ in ss))) if tt else k print "".join(t)
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string s; int n; cin >> s >> n; vector<int> hist(26); const int m = s.size(); for (int i = 0; i < m; ++i) { ++hist[s[i] - 'a']; } int nc = 0; for (int i = 0; i < 26; ++i) { if (hist[i] > 0) { ++nc; } } if (nc > n) { cout << -1 << endl; return 0; } vector<int> answer(26); for (int i = 0; i < 26; ++i) { if (hist[i] > 0) { answer[i] = 1; } } for (int k = nc; k < n; ++k) { int maxreq = -1, maxidx = 0; for (int i = 0; i < 26; ++i) { if (answer[i] == 0) { continue; } int req = (hist[i] + answer[i] - 1) / answer[i]; if (req > maxreq) { maxreq = req; maxidx = i; } } ++answer[maxidx]; } int num_sheets = 0; for (int i = 0; i < 26; ++i) { if (answer[i] == 0) { continue; } int req = (hist[i] + answer[i] - 1) / answer[i]; num_sheets = max(num_sheets, req); } string sheet; for (int i = 0; i < 26; ++i) { sheet += string(answer[i], 'a' + i); } cout << num_sheets << endl << sheet << endl; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from math import ceil p = {i: 0 for i in 'abcdefghijklmnopqrstuvwxyz'} t = input() for i in t: p[i] += 1 p = {i: p[i] for i in p if p[i] > 0} n = int(input()) if len(p) > n: print(-1) elif len(t) > n: r = [[p[i], p[i], 1, i] for i in p] for i in range(n - len(p)): j = max(r) j[2] += 1 j[0] = j[1] / j[2] print(ceil(max(r)[0])) print(''.join(j[3] * j[2] for j in r)) else: print('1\n' + t * (n // len(t)) + t[: n % len(t)])
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; long long int n; map<long long int, long long int> m; string s; bool check(long long int mid) { long long int i, j; long long int sum = 0; for (auto it = m.begin(); it != m.end(); it++) { sum = sum + ceil((it->second) * 1.00 / mid); } if (sum <= n) return 1; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int i, j; long long int k; cin >> s; cin >> n; if (n >= s.size()) { cout << "1\n"; cout << s; for (i = s.size() + 1; i <= n; i++) cout << s[0]; return 0; } for (i = 0; i < s.size(); i++) m[s[i] - 'a' + 1]++; long long int low = 1, high = 1000, ans = -1; while (low <= high) { long long int mid = (low + high) >> 1; if (check(mid)) { ans = mid; high = mid - 1; } else low = mid + 1; } if (ans == -1) cout << "-1\n"; else { long long int count = 0; cout << ans << "\n"; for (auto it = m.begin(); it != m.end(); it++) { for (j = 1; j <= ceil((it->second) * 1.00 / ans); j++) { count++; cout << (char)(it->first + 'a' - 1); } } for (i = count + 1; i <= n; i++) { cout << s[0]; } cout << "\n"; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
from collections import Counter s = raw_input().strip() l = int(raw_input()) d = Counter(s) if len(d) > l: print -1 exit() lo = 0 hi = 10000 while lo + 1 < hi: mid = (lo + hi) / 2 c = 0 for x in d.itervalues(): c += (x + mid - 1) / mid if c > l: lo = mid else: hi = mid print hi ans = [] for x in d.iteritems(): ans.append(x[0] * ((x[1] + hi - 1) / hi)) t = ''.join(ans) if len(t) < l: t += 'a' * (l - len(t)) print t
PYTHON
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int Hash[28]; int main() { string input; cin >> input; int n; cin >> n; for (int i = 0; input[i]; i++) Hash[input[i] - 'a']++; for (int i = 1; i <= 1000; i++) { string result; int itr = 1; for (int j = 0; j < 26; j++) { int k = Hash[j] / i + bool(Hash[j] % i); while (k) { result += j + 'a'; k--; itr++; } } itr--; if (itr <= n) { while (itr < n) { itr++; result += 'a'; } cout << i << endl; cout << result << endl; return 0; } } cout << "-1\n"; return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; template <class T> inline T tmin(T a, T b) { return (a < b) ? a : b; } template <class T> inline T tmax(T a, T b) { return (a > b) ? a : b; } template <class T> inline void add_max(T &a, T b) { if (b > a) a = b; } template <class T> inline void add_min(T &a, T b) { if (b < a) a = b; } template <class T> inline T tabs(T a) { return (a > 0) ? a : -a; } template <class T> T gcd(T a, T b) { while (b != 0) { T c = a; a = b, b = c % b; } return a; } int n, cnt[30]; bool check(int x) { int tot = 0; for (int i = (0); i < (30); ++i) { tot += (cnt[i] + x - 1) / x; } return tot <= n; } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(false); string s, ans = ""; cin >> s >> n; memset(cnt, 0, sizeof(cnt)); for (int i = (0); i < (s.size()); ++i) cnt[s[i] - 'a']++; int tot = 0; for (int i = (0); i < (30); ++i) { if (cnt[i]) tot++; } if (tot > n) cout << -1 << endl; else { int l = 1, r = 1000; while (l < r) { int m = (l + r) >> 1; if (check(m)) r = m; else l = m + 1; } cout << r << endl; int tot = 0; for (int i = (0); i < (30); ++i) { if (cnt[i] == 0) continue; int num = (cnt[i] + r - 1) / r; tot += num; for (int j = (0); j < (num); ++j) ans += (i + 'a'); } for (int i = (tot); i < (n); ++i) ans += 't'; cout << ans << endl; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int MAX_S = 1e3 + 20; int s_len; char s[MAX_S]; int n; map<char, int> ct, need; char ans[MAX_S]; bool check(int k) { int c = 0; for (auto it = ct.begin(); it != ct.end(); ++it) c += need[it->first] = (it->second + k - 1) / k; return c <= n; } int main() { scanf("%s %d", s, &n); s_len = strlen(s); for (int i = 0; i < s_len; ++i) ++ct[s[i]]; if (ct.size() > n) { puts("-1"); return 0; } int l = 0, h = MAX_S; while (l + 1 < h) { int m = (l + h) / 2; if (check(m)) h = m; else l = m; } check(h); int e = 0; for (auto it = need.begin(); it != need.end(); ++it) for (int i = 0; i < it->second; ++i) ans[e++] = it->first; while (e < n) ans[e++] = 'z'; ans[e] = '\0'; printf("%d\n%s\n", h, ans); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; const int N = 1005; char str[N]; int num[N], current[N]; struct Item { char c; int val; Item(char _c, int _val) : c(_c), val(_val) {} bool operator<(const Item& v) const { return val < v.val; } }; int main() { int n; int diff = 0; scanf("%s", str); scanf("%d", &n); for (int i = 0; str[i]; i++) { if (num[(int)str[i]] == 0) diff++; num[(int)str[i]]++; } if (diff > n) { puts("-1"); return 0; } priority_queue<Item> q; for (int k = 'a'; k <= 'z'; k++) { if (num[k] > 0) { q.push(Item(k, num[k])); current[k] = 1; } } while (n > diff) { Item u = q.top(); q.pop(); int k = u.c; current[k]++; if (num[k] % current[k] == 0) q.push(Item(k, num[k] / current[k])); else q.push(Item(k, (int)(num[k] / current[k]) + 1)); n--; } printf("%d\n", q.top().val); for (int k = 'a'; k <= 'z'; k++) { for (int l = 1; l <= current[k]; l++) putchar((char)k); } puts(""); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; string s, t; cin >> s >> n; map<char, int> mp; for (auto c : s) { mp[c]++; } if (n < mp.size()) { cout << -1; } else { auto check = [&](int x) { int tot = 0; for (auto it : mp) { tot += (it.second + x - 1) / x; } return tot <= n; }; int l = 1, r = s.length(), mid; while (r > l) { mid = (l + r) >> 1; if (check(mid)) { r = mid; } else { l = mid + 1; } } for (auto it : mp) { t += string((it.second + l - 1) / l, it.first); } while (t.length() < n) { t.push_back('a'); } cout << l << '\n' << t; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
s = input() n = int(input()) from collections import Counter c = Counter(s) out = Counter() contrib = Counter() for letter in c: out[letter] = 1 contrib[letter] = c[letter] sum_vals = sum(out.values()) from math import ceil from fractions import Fraction if sum_vals > n: print(-1) else: while sum_vals < n: el, _ = contrib.most_common(1)[0] out[el] += 1 sum_vals += 1 contrib[el] = ceil(Fraction(c[el], out[el])) print(max(contrib.values())) print(''.join(out.elements()))
PYTHON3
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.*; import java.util.*; import java.math.*; final public class Main implements Runnable { private static boolean local; public static void main(String[] args) { if (args.length > 0 && args[0].equals("-1")) { local = true; } new Thread(null, new Main(), "mainthread", 1 << 27).start(); } public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; try { if (local) { inputStream = new FileInputStream("/home/outside/coding/workspace/java/main/io/input.txt"); outputStream = new FileOutputStream("/home/outside/coding/workspace/java/main/io/output.txt"); } else { // inputStream = new FileInputStream("file_name"); // outputStream = new FileOutputStream("file_name"); } } catch (Exception e) { throw new RuntimeException(e); } Task.in = new InputReader(inputStream); Task.out = new OutputWriter(outputStream); if (local) { Task.dout = new DebugWriter(Task.out); } new Task().solve(); Task.out.close(); } } final class Task { public static InputReader in; public static OutputWriter out; public static DebugWriter dout; public void solve() { mas = new int[SZ]; char[] s = in.readString().toCharArray(); for (int i = 0; i < s.length; ++i) ++mas[s[i] - 'a']; int n = in.readInt(); dp = new int[SZ][n + 1]; was = new boolean[SZ][n + 1]; // dout.printLine(mas); int res = f(0,n); if (res == INF) { out.printLine(-1); } else { out.printLine(res); ans = new ArrayList<>(); dfs(0, n); for (int i = 0; i < ans.size(); ++i) out.print((char)(ans.get(i) + 'a')); out.printLine(); } } final int INF = Integer.MAX_VALUE; final int SZ = 26; int[] mas; int[][] dp; boolean[][] was; int f(int let, int n) { if (let == SZ) { if (n == 0) { return 0; } else { return INF; } } if (was[let][n]) return dp[let][n]; was[let][n] = true; if (mas[let] == 0) return dp[let][n] = f(let + 1, n); int res = INF; for (int i = 1; i <= n; ++i) { int value = f(let + 1, n - i); if (value != INF) { value = Math.max(value, (mas[let] + i - 1) / i); res = Math.min(res, value); } } return dp[let][n] = res; } ArrayList<Integer> ans; void dfs(int let, int n) { if (let == SZ) return; if (mas[let] == 0) { dfs(let + 1, n); return; } for (int i = 1; i <= n; ++i) { int value = f(let + 1, n - i); if (value != INF) { value = Math.max(value, (mas[let] + i - 1) / i); if (value == f(let, n)) { for (int k = 0; k < i; ++k) ans.add(let); dfs(let + 1, n - i); return; } } } } } final class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1 << 13]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public String readToEnd() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, readInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public void close() { try { stream.close(); } catch (IOException e) { throw new RuntimeException(); } } } final class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream), 1 << 13)); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void printFormat(String format, Object... objects) { writer.printf(format, objects); } public void print(char[] objects) { writer.print(objects); } public void printLine(char[] objects) { writer.println(objects); } public void printLine(char[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(int[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(int[] objects) { print(objects); writer.println(); } public void printLine(int[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(short[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(short[] objects) { print(objects); writer.println(); } public void printLine(short[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(long[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(long[] objects) { print(objects); writer.println(); } public void printLine(long[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(double[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(double[] objects) { print(objects); writer.println(); } public void printLine(double[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(byte[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(byte[] objects) { print(objects); writer.println(); } public void printLine(byte[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void print(boolean[] objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(boolean[] objects) { print(objects); writer.println(); } public void printLine(boolean[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); } public void close() { writer.close(); } public void flush() { writer.flush(); } } final class DebugWriter { private final OutputWriter writer; public DebugWriter(OutputWriter writer) { this.writer = writer; } private void printDebugMessage() { writer.print("DEBUG:\t"); } public void printLine(Object... objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printFormat(String format, Object... objects) { printDebugMessage(); writer.printFormat(format, objects); flush(); } public void printLine(char[] objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(char[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(double[] objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(double[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(int[] objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(int[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(short[] objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(short[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(long[] objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(long[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(byte[] objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(byte[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void printLine(boolean[] objects) { printDebugMessage(); writer.printLine(objects); flush(); } public void printLine(boolean[][] objects) { for (int i = 0; i < objects.length; ++i) printLine(objects[i]); flush(); } public void flush() { writer.flush(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> char s[1010]; char ans[1010]; int n; int a[26]; int main() { scanf("%s", s); scanf("%d", &n); int len = strlen(s); for (int i = 0; i < len; i++) { a[s[i] - 'a']++; } int char_count = 0; for (int i = 0; i < 26; i++) { if (a[i] > 0) char_count++; } if (char_count > n) { printf("-1\n"); return 0; } int sum = 0; int p = 0; do { p++; sum = 0; for (int i = 0; i < 26; i++) { sum += a[i] / p + (a[i] % p > 0); } } while (sum > n); int pa = 0; for (int i = 0; i < 26; i++) { while (a[i] > 0) { a[i] -= p; ans[pa++] = 'a' + i; } } while (pa < n) { ans[pa++] = 'a'; } ans[n] = 0; printf("%d\n%s\n", p, ans); return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.List; import java.util.StringTokenizer; public class Main implements Runnable { int INF = (int) 1e9; List<Integer> edges[]; int anc[][]; int ts[], te[]; int t; private void solve() throws IOException { String s = next(); int n = nextInt(); int cnt[] = new int[26]; int dist = 0; for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); if (cnt[ch - 'a'] == 0) { dist++; } cnt[ch - 'a']++; } if (n < dist) { pw.println(-1); return; } int l = 0; int r = s.length(); while (r - l > 1) { int mid = (l + r) >> 1; int tot = 0; for (int i = 0; i < 26; ++i) { tot += ((cnt[i] + mid - 1) / mid); } if (tot > n) { l = mid; } else { r = mid; } } pw.println(r); int need = 0; for (int i = 0; i < 26; ++i) { need += (cnt[i] + r - 1) / r; for (int j = 0; j < (cnt[i] + r - 1) / r; ++j) { pw.print((char) ('a' + i)); } } while (need < n) { pw.print('x'); need++; } } BufferedReader br; StringTokenizer st; PrintWriter pw; public static void main(String args[]) { new Main().run(); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); st = null; solve(); pw.flush(); pw.close(); br.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }
JAVA
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; int N; int main() { string str; while (cin >> str >> N) { int ch[109], ar[109]; memset(ch, 0, sizeof(ch)); memset(ar, 0, sizeof(ar)); for (int i = 0, _n = str.size(); i < _n; i++) { ar[str[i] - 'a']++; } int rex = 0, rey = 0; for (int i = 0, _n = 26; i < _n; i++) if (ar[i]) rey++; if (rey > N) { cout << -1 << endl; continue; } for (int i = 1, _n = 1001; i < _n; i++) { int r = 0; for (int j = 0, _n = 26; j < _n; j++) { r += ar[j] / i + (ar[j] % i > 0); } if (r <= N) { rex = i; break; } } int t = 0; cout << rex << endl; for (int i = 0, _n = 26; i < _n; i++) { if (ar[i]) { int r = ar[i] / rex + (ar[i] % rex > 0); for (int j = 0, _n = r; j < _n; j++) cout << (char)(i + 'a'), t++; } } while (t < N) cout << "a", t++; } return 0; }
CPP
335_A. Banana
Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1. Examples Input banana 4 Output 2 baan Input banana 3 Output 3 nab Input banana 2 Output -1 Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana".
2
7
#include <bits/stdc++.h> using namespace std; ifstream fin("banana.in"); ofstream fout("banana.out"); string s, s2; int a, b, x, f[30], ct, ok, mx, p, f2[30], q[30]; int main() { int i; cin >> s; cin >> x; for (i = 0; i < s.size(); i++) { if (f[s[i] - 'a'] == 0) ct++; f[s[i] - 'a']++; } if (ct > x) cout << -1; else { ok = 1; ct = 0; for (i = 0; i < 26; i++) { f2[i] = f[i]; if (f[i] > 0) { s2 += char(i + 'a'); q[i]++; ct++; x--; } } while (ok == 1 && x) { ok = 0; mx = 0; for (i = 0; i < 26; i++) { if (f[i] > 0) ok = 1; if (q[i] > 0 && mx < (f[i] - 1) / q[i]) { p = i; mx = (f[i] - 1) / q[i]; } } if (ok == 1) { s2 += char(p + 'a'); q[p]++; ct++; } x--; } mx = 0; for (i = 0; i < 26; i++) if (q[i] > 0) mx = max(mx, (f2[i] - 1) / q[i] + 1); cout << mx << endl; cout << s2; } }
CPP