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; long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; long long ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; long long gcd(long long a, long long b) { if (!a) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } unordered_map<char, long long> mp; string s; long long n; bool fun(long long x) { long long cnt = 0; for (auto it : mp) { long long val = it.second; cnt += it.second / x; if (it.second % x != 0) { cnt++; } } if (cnt <= n) return true; else return false; } void test_case() { cin >> s; cin >> n; for (auto it : s) { mp[it]++; } if (mp.size() > n) { cout << -1 << "\n"; return; } long long l, r; l = 1; r = 1000; long long ans = 1000; while (l <= r) { long long mid = l + (r - l) / 2; if (fun(mid)) { ans = min(ans, mid); r = mid - 1; } else { l = mid + 1; } } cout << ans << "\n"; string st = ""; for (auto it : mp) { long long val = it.second; long long cnt = 0; cnt = it.second / ans; if (it.second % ans != 0) { cnt++; } while (cnt--) { st += it.first; } } while (st.size() != n) { st += "a"; } cout << st << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(20); long long t = 1; while (t--) { test_case(); } }
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 long long MOD = 1000000007; int lettersNumbersShowed[1000]; int lettersInserted[1000]; int letters[1000]; int greedyChoice[1000]; int main(int argc, const char* argv[]) { string str; string fullAns; cin >> str; int n; scanf(" %d", &n); string ans; int timesLetterAppears = 0; int k = 1; std::sort(str.begin(), str.end()); char previous = str[0]; for (int i = 0; i < str.length(); i++) { char c = str[i]; if (i == 0) { ans[0] = str[i]; } if (c != previous) { lettersNumbersShowed[k - 1] = timesLetterAppears; letters[k - 1] = previous; lettersInserted[k - 1] = 1; greedyChoice[k - 1] = timesLetterAppears; timesLetterAppears = 1; ans[k] = str[i]; k++; } else { timesLetterAppears++; } str[i] = c; previous = c; } lettersNumbersShowed[k - 1] = timesLetterAppears; letters[k - 1] = previous; lettersInserted[k - 1] = 1; greedyChoice[k - 1] = timesLetterAppears; if (n >= k) { int a = n; int j = k; for (int i = 0; i < n - k; i++) { a = -200000000; int index; for (int i = 0; i < k; i++) { if (greedyChoice[i] > a) { a = greedyChoice[i]; index = i; } } ans[j] = letters[index]; j++; lettersInserted[index]++; greedyChoice[index] = ceil((float)lettersNumbersShowed[index] / lettersInserted[index]); } a = -200000000; int index; for (int i = 0; i < k; i++) { if (greedyChoice[i] > a) { a = greedyChoice[i]; index = i; } } a = greedyChoice[index]; printf("%d\n", a); for (int i = 0; i < n; i++) { printf("%c", ans[i]); } } else { printf("-1"); } 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
import java.io.*; import java.util.*; public class A { public static void main(String[] args) throws Throwable { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); char[] s = in.readLine().toCharArray(); int[] kohl = new int[26]; for (char c : s) { kohl[c - 'a']++; } int n = Integer.parseInt(in.readLine()), ct = 0; for (int i : kohl) { if (i > 0) { ct++; } } if (ct > n) { System.out.println(-1); } else { PriorityQueue<Pair> x = new PriorityQueue<Pair>(); for (int i = 0; i < 26; i++) { if (kohl[i] > 0) { x.add(new Pair(kohl[i], kohl[i], 1, i)); } } n -= ct; while (n > 0) { n--; Pair p = x.poll(); p.z++; p.x = p.y / p.z + (p.y % p.z > 0 ? 1 : 0); x.add(p); } String ret = ""; while (!x.isEmpty()) { Pair p = x.poll(); if (ret.length() == 0) { System.out.println(p.x); } ret += cp(p.w, p.z); } System.out.println(ret); } } static String cp(int x, int y) { String ret = ""; for (int i = 0; i < y; i++) { ret += (char)(x + 'a'); } return ret; } } class Pair implements Comparable<Pair> { public int x, y, z, w; public Pair(int x, int y, int z, int w) { this.x = x; this.y = y; this.z = z; this.w = w; } public int compareTo(Pair o) { return o.x - x; } }
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.PrintWriter; public class P335A { public static void main(String[] args) { InputReader in = new InputReader(); String s = in.next(); int n = in.nextInt(); int[] counts = new int[26]; int len = s.length(); for (int i = 0; i < len; i++) { counts[s.charAt(i) -'a']++; } int low = 0, lowN = 0; int high = 1001, highN = 1001; while (high > low + 1) { int k = (high + low) / 2; int N = 0; for (int i = 0; i < 26; i++) { int cnt = counts[i]; if (cnt != 0) { N += (cnt % k== 0) ? (cnt / k) : (cnt / k) + 1; } } if (N > n) { low = k; lowN = N; } else { high = k; highN = N; } } PrintWriter out = new PrintWriter(System.out); if (highN > n) { out.println(-1); } else { out.println(high); for (int i = 0; i < n - highN; i++) { out.print('a'); } for (int i = 0; i < 26; i++) { int cnt = counts[i]; if (cnt != 0) { int cnta = (cnt % high == 0) ? (cnt / high) : (cnt / high) + 1; for (int j = 0; j < cnta; j++) { out.print((char) ('a' + i)); } } } } out.close(); } private final static class InputReader { public java.io.BufferedReader reader; public java.util.StringTokenizer tokenizer; public InputReader() { reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (java.io.IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } 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
def get_time(num_times, stick_number): if stick_number == 0: return 10000000 else: ans = num_times/stick_number if num_times%stick_number > 0: ans += 1 return ans st = raw_input() nt = {} for c in st: if c in nt: nt[c] += 1 else: nt[c] = 1 sn = {} for c in st: sn[c] = 0 cleft = input() ans = '' while cleft > 0: best_c = '' num_turns = 0 for c, stick_number in sn.iteritems(): num = get_time(nt[c], stick_number) if num > num_turns: best_c = c num_turns = num sn[best_c] += 1 ans += best_c cleft -= 1 nsteps = 0 for c, stick_number in sn.iteritems(): nnsteps = get_time(nt[c], stick_number) if nsteps < nnsteps: nsteps = nnsteps if nsteps < 10000000: print nsteps print ans else: print '-1'
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
import java.io.*; import java.math.*; import java.math.BigInteger; import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner (System.in); String str=sc.next(); int n=sc.nextInt(); int arr[]=new int [26]; Set<Integer>set =new HashSet<>(); for(int i=0;i<str.length();i++) { arr[(int)(str.charAt(i)-'a')]++; set.add((int)(str.charAt(i)-'a')); } //System.out.println(Arrays.toString(arr)); if(set.size()>n) { System.out.println(-1); return; } double a[][]=new double [set.size()][3]; int ii=0; for(int i=0;i<26;i++) { if(arr[i]!=0) { a[ii][1]=i; a[ii][0]=arr[i]; a[ii][2]=2; ii++; } } StringBuilder s=new StringBuilder(""); for(int i=0;i<set.size();i++) { s.append((char)(a[i][1]+'a')); } n=n-set.size(); show(a); while(n>0) { a[set.size()-1][0]=a[set.size()-1][0]*(a[set.size()-1][2]-1)/a[set.size()-1][2]; s.append((char)(a[set.size()-1][1]+'a')); a[set.size()-1][2]++; show(a); n--; } if(a[set.size()-1][0]%1==0)System.out.println((int)(a[set.size()-1][0])); else System.out.println((int)(a[set.size()-1][0])+1); System.out.println(s); } public static void show(double arr[][]) { Arrays.sort(arr,new Comparator<double[]>(){ @Override public int compare(double[] a1,double a2[]) { if(a1[0]>a2[0]) { return 1; }else return -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
def check(mid): ans = [] res = 0 for c, count in d: tmp = count // mid if (count % mid != 0): tmp += 1 ans.append((c, tmp)) res += tmp return res <= n, ans a = input() n = int(input()) d = dict() t = 0 for i in a: if i not in d: d[i] = 1 t += 1 else: d[i] += 1 ans = [] res = "" for i in d: ans.append(i) res = i if n >= len(a): print(1) print(a, end = '') n -= len(a) for u in range(n): print(res, end ='') else: d = (sorted(d.items(), key = lambda d:(d[1], d[0]))) l = 1 r = len(a) res = [] count_res = 0 while (l <= r): mid = (l + r) // 2 flag, tmp = check(mid) if (flag): r = mid - 1 count_res = mid res = tmp else: l = mid + 1 #print(l, r, tmp) if (len(res) == 0): print(-1) else: ttt = 0 print(count_res) for c, sl in res: for j in range(sl): print(c, end = '') ttt += 1 n -= ttt for i in range(n): print(a[0], end = '')
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.math.BigInteger; import java.util.*; public class Template implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } class GraphBuilder { int n, m; int[] x, y; int index; int[] size; GraphBuilder(int n, int m) { this.n = n; this.m = m; x = new int[m]; y = new int[m]; size = new int[n]; } void add(int u, int v) { x[index] = u; y[index] = v; size[u]++; size[v]++; index++; } int[][] build() { int[][] graph = new int[n][]; for (int i = 0; i < n; i++) { graph[i] = new int[size[i]]; } for (int i = index - 1; i >= 0; i--) { int u = x[i]; int v = y[i]; graph[u][--size[u]] = v; graph[v][--size[v]] = u; } return graph; } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long[] readLongArray(int size) throws IOException { long[] res = new long[size]; for (int i = 0; i < size; i++) { res[i] = readLong(); } return res; } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } <T> List<T>[] createGraphList(int size) { List<T>[] list = new List[size]; for (int i = 0; i < size; i++) { list[i] = new ArrayList<>(); } return list; } public static void main(String[] args) { new Template().run(); // new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void solve() throws IOException { char[] s = readString().toCharArray(); int n = readInt(); TreeSet<Character> uniq = new TreeSet<>(); for (char x : s) { uniq.add(x); } if (uniq.size() > n) { out.println(-1); return; } int[] cnt = new int[26]; for (char x : s) { cnt[x - 'a']++; } int l = 1; int r = 100000; int pages = 0; while (l <= r) { int mid = (l + r) >> 1; int need = 0; for (int i = 0; i < 26; i++) { need += (cnt[i] + mid - 1) / mid; } if (need <= n) { pages = mid; r = mid - 1; } else { l = mid + 1; } } out.println(pages); for (int i = 0; i < 26; i++) { int c = (cnt[i] + pages - 1) / pages; while (c-- > 0) { out.print((char) ('a' + i)); n--; } } while (n-- > 0) { out.print("a"); } } }
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 static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } StreamTokenizer in; PrintWriter out; //deb//////////////////////////////////////////////// public static void deb(String n, Object n1) { System.out.println(n + " is : " + n1); } public static void deb(int[] A) { for (Object oo : A) { System.out.print(oo + " "); } System.out.println(""); } public static void deb(long[] A) { for (Object oo : A) { System.out.print(oo + " "); } System.out.println(""); } public static void deb(String[] A) { for (Object oo : A) { System.out.print(oo + " "); } System.out.println(""); } public static void deb(int[][] A) { for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo + " "); } System.out.println(""); } } public static void deb(long[][] A) { for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo + " "); } System.out.println(""); } } public static void deb(String[][] A) { for (int i = 0; i < A.length; i++) { for (Object oo : A[i]) { System.out.print(oo + " "); } System.out.println(""); } } ///////////////////////////////////////////////////////////// int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } long nextLong() throws IOException { in.nextToken(); return (long) in.nval; } class Pair<X, Y> { public X x; public Y y; public Pair(X x, Y y) { this.x = x; this.y = y; } public void setX(X x) { this.x = x; } public void setY(Y y) { this.y = y; } } boolean inR(int x, int y) { return (x >= 0) && (x < nn) && (y >= 0) && (y < nn); } static int nn; void run() throws IOException { // in = new StreamTokenizer(new BufferedReader(new FileReader("circles.in"))); // out = new PrintWriter(new FileWriter("circles.out")); in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } void solve() throws IOException { // BufferedReader re= new BufferedReader(new FileReader("C:\\Users\\ASELA\\Desktop\\A.in")); BufferedReader re = new BufferedReader(new InputStreamReader(System.in)); String S=re.readLine(); int n= Integer.parseInt(re.readLine()); int[] A= new int[26]; for (int i = 0; i < S.length(); i++) { A[(int)S.charAt(i)-(int)'a']++; } int lw=0,up=10000; while(up-lw>1){ int len=0; int c=(up+lw)/2; for (int i = 0; i < 26; i++) { double dd= (double)A[i] /(double)c; len+=(int)ceil(dd); } if(len<=n){ up=c; } else lw=c; } if(up>1001){ System.out.println("-1"); return; } System.out.println(up); String aa=""; int len=0; for (int i = 0; i < 26; i++) { double dd= (double)A[i] /(double)up; len+=(int)ceil(dd); for (int j = 0; j <(int)ceil(dd) ; j++) { aa+=(char)((int)'a'+i); } } for (int i = aa.length(); i < n; i++) { aa+="a"; } System.out.println(aa); }}
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 A { static final int LETTERS = 'z' - 'a' + 1; static final int INF = 100000; static class Counter{ int n; char c; Counter(char c, int n){ this.c = c; this.n = n; } } static int [][] memo; static List<Counter> distinct; static int rec(int i, int left){ if (i == distinct.size()){ return 0; } if (left <= 0){ return -1; } if (memo[i][left] != 0){ return memo[i][left] - 1; } int res = INF; for (int k = 1; k <= Math.min(distinct.get(i).n, left); ++k){ final long max = rec(i + 1, left - k); if (max != -1){ res = Math.min(res, Math.max((distinct.get(i).n + k - 1)/k, rec(i + 1, left - k))); } } memo[i][left] = res + 1; return res; } public static void main(String [] args){ final Scanner s = new Scanner(System.in); final String line = s.nextLine(); final int n = s.nextInt(); final Counter [] count = new Counter[LETTERS]; for (char c = 'a'; c <= 'z'; ++c){ count[c - 'a'] = new Counter(c, 0); } for (int i = 0; i < line.length(); ++i){ count[line.charAt(i) - 'a'].n++; } distinct = new ArrayList<Counter>(); for (Counter c : count){ if (c.n > 0){ distinct.add(c); } } if (distinct.size() > n){ System.out.println(-1); return; } memo = new int[distinct.size()][n + 1]; int res = rec(0, n); System.out.println(res); int left = n; final int [] out = new int[distinct.size()]; for (int i = 0; i < distinct.size() - 1; ++i){ int r = INF; int l = 1; for (int k = 1; k <= Math.min(distinct.get(i).n, left); ++k){ final int p = rec(i + 1, left - k); if (p != -1){ final int g = Math.max((distinct.get(i).n + k - 1)/k, p); if (r > g){ r = g; l = k; } } } out[i] = l; left -= l; } out[distinct.size() - 1] = left; final StringBuilder list = new StringBuilder(n); for (int i = 0; i < out.length; ++i){ for (int j = 0; j < out[i]; ++j){ list.append(distinct.get(i).c); } } System.out.println(list); s.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 int MAXSIZE = 100 + 10; int main() { string s; int n, num[26 + 10], type = 0; int num2[26 + 10]; fill(num, num + 26 + 10, 0); fill(num2, num2 + 20 + 10, 0); cin >> s; cin >> n; for (int i = 0; i < (int)s.length(); i++) { if (!num[(int)s[i] - 96]) type++; num[int(s[i]) - 96]++; } if (n < type) cout << -1 << endl; else { for (int i = 1; i <= 26; i++) if (num[i]) num2[i] = 1, n--; int maxi, pos; while (n > 0) { maxi = -1, pos = 0; for (int i = 1; i <= 26; i++) if (num2[i] > 0 && (num[i] + num2[i] - 1) / num2[i] > maxi) { maxi = (num[i] + num2[i] - 1) / num2[i]; pos = i; } n--; num2[pos]++; } maxi = -1, pos = 0; for (int i = 1; i <= 26; i++) if (num2[i] > 0 && (num[i] + num2[i] - 1) / num2[i] > maxi) { maxi = (num[i] + num2[i] - 1) / num2[i]; pos = i; } cout << maxi << endl; for (int i = 1; i <= 26; i++) for (int j = 1; j <= num2[i]; j++) cout << char(i + 96); } 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 = 1100, Maxm = 101; char str[Maxn]; int cnt[Maxm]; int n, m; bool check(int vtimes) { int sum = 0; for (int i = 0; i < (26); ++i) sum += cnt[i] / vtimes + (cnt[i] % vtimes != 0); return sum <= m; } int bSearch() { int l = 1, r = n, res = -1; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) { res = mid; r = mid - 1; } else { l = mid + 1; } } return res; } void myoutput(int vtimes) { int tmp = 0; for (int i = 0; i < (26); ++i) { int len = cnt[i] / vtimes + (cnt[i] % vtimes != 0); for (int j = 0; j < (len); ++j) printf("%c", i + 'a'), tmp++; } for (int i = (tmp); i < (m); i++) printf("f"); puts(""); } int main() { while (~scanf("%s", str)) { scanf("%d", &m); memset(cnt, 0, sizeof(cnt)); n = strlen(str); for (int i = 0; i < (n); ++i) cnt[str[i] - 'a']++; int res = bSearch(); printf("%d\n", res); if (res != -1) myoutput(res); } 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 = raw_input().strip() l = int(raw_input()) d = Counter(s) if len(d)>l: print -1 return num_sheet = 1 len_sheet = 0 while True: c = 0 for x in d.itervalues(): c += (x+num_sheet-1)/num_sheet if c <= l: print num_sheet break num_sheet += 1 res = [] for x in d.iteritems(): res.append(x[0]*((x[1]+num_sheet-1)/num_sheet)) t = ''.join(res) l1 = l-len(t) t += 'a'*l1 print t main()
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; long long ch[30]; bool check(long long k, long long n) { long long res = 0; for (long long i = 0; i < 30; i++) if (ch[i]) res += ch[i] / k + (ch[i] % k != 0); if (res <= n) return true; return false; } int main() { string s; cin >> s; long long n; cin >> n; long long L = s.size(); for (long long i = 0; i < L; i++) { ch[s[i] - 'a']++; } long long cnt = 0; for (long long i = 0; i < 30; i++) cnt += (ch[i] > 0); if (cnt > n) return cout << -1 << '\n', 0; long long ans, lo = 1, hi = 1e16; while (lo <= hi) { long long mid = (lo + hi) >> 1; if (check(mid, n)) { ans = mid; hi = mid - 1; } else lo = mid + 1; } cout << ans << '\n'; long long y = 0; for (long long i = 0; i < 30; i++) { if (ch[i]) { long long x = ch[i] / ans + (ch[i] % ans != 0); y += x; while (x) { cout << (char)(i + 'a'); x--; } } } while (y < n) cout << 'x', y++; 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 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 = 1001; int k = 1; while (left < right) { int mid = (left + right) / 2; int len = getLen(map, mid); if (len > n) { left = mid + 1; } else { k = mid; right = mid; } } 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
#include <bits/stdc++.h> using namespace std; int n; char a[1010]; int num[30]; int t[30]; int main() { while (~scanf("%s", a)) { scanf("%d", &n); memset(num, 0, sizeof(num)); for (int i = 0; a[i]; i++) num[a[i] - 'a']++; int cnt = 0; for (int i = 0; i < 26; i++) if (num[i]) cnt++; if (cnt > n) { puts("-1"); continue; } for (int i = 1;; i++) { for (int j = 0; j < 26; j++) { t[j] = num[j] / i + (num[j] % i != 0); } int s = 0; for (int j = 0; j < 26; j++) s += t[j]; if (s <= n) { printf("%d\n", i); for (int j = 0; j < 26; j++) for (int k = 0; k < t[j]; k++) printf("%c", 'a' + j); for (int j = 1; j <= n - s; j++) printf("%c", 'a'); puts(""); break; } } } }
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Classical { static double win,lose; static int itr= 0; public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); String paper= sc.next(); int N = sc.nextInt(); int[] freq= new int[26]; for(int a=0;a<paper.length();a++){ freq[paper.charAt(a)-'a']++; } for(int thing = 1;thing<=1000;thing++){ int used = 0; for(int a=0;a<26;a++){ used+=(int)Math.ceil((freq[a]+0.0)/thing); } if(used<=N){ out.println(thing); int printed = 0; for(int a=0;a<26;a++){ int temp =(int)Math.ceil((freq[a]+0.0)/thing); for(int b=0;b<temp;b++){ out.print((char)(a+'a')); printed++; } } while(printed<N){ out.print((char)('a')); printed++; } break; } if(thing==1000) out.println("-1"); } out.close(); } static class OB implements Comparable<OB>{ char c; int f; OB(int a, char b){ c=b; f=a; } @Override public int compareTo(OB o) { return o.f-this.f; } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) throws Exception{ br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(br.readLine().trim()); } public int numTokens() throws Exception { if(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine().trim()); return numTokens(); } return st.countTokens(); } public boolean hasNext() throws Exception{ if(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine().trim()); return hasNext(); } return true; } public String next() throws Exception { if(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine().trim()); return next(); } return st.nextToken(); } public double nextDouble() throws Exception{ return Double.parseDouble(next()); } public float nextFloat() throws Exception{ return Float.parseFloat(next()); } public long nextLong() throws Exception{ return Long.parseLong(next()); } public int nextInt() throws Exception{ return Integer.parseInt(next()); } public String nextLine() throws Exception{ return br.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
#include <bits/stdc++.h> using namespace std; vector<long long int> g[100005]; long long int vis[100005]; long long int prime[5000003] = {0}; long long int ncr[2005][2005]; long long int fact[1000005]; long long int inv[1000005]; vector<long long int> ans; vector<long long int> divisor(long long int n) { vector<long long int> v; for (long long int i = 1; i * i <= n; i++) { if (n % i == 0) { v.push_back(i); if (n / i != i) v.push_back(n / i); } } return v; } vector<long long int> msieve() { prime[0] = 1; prime[1] = 1; vector<long long int> v; for (long long int i = 2; i <= 10002; i++) { if (prime[i] == 0) { prime[i] = i; for (long long int j = 2 * i; j <= 10002; j += i) prime[j] = i; } } for (long long int i = 2; i < 5000002; i++) if (prime[i] == i) v.push_back(i); return v; } long long int no_of_div(long long int n) { long long int c = 1, ans = 1; while (n > 1) { if (prime[n] == prime[n / prime[n]]) c++; else { ans *= (c + 1); c = 1; } n = n / prime[n]; } return ans; } vector<long long int> no_of_fact(long long int n) { vector<long long int> v; long long int c = 0, ans = 1; while (n > 1) { v.push_back(prime[n]); c++; n = n / prime[n]; } return v; } long long binpow(long long int a, long long int b, long long int m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % m; a = ((a % m) * (a % m)) % m; b >>= 1; } return res; } void cal() { ncr[0][0] = 1; for (long long int i = 1; i <= 2004; i++) { ncr[i][0] = 1, ncr[i][i] = 1; for (long long int j = 1; j < i; j++) { ncr[i][j] = (ncr[i - 1][j] + ncr[i - 1][j - 1]); } } } long long int nck(long long int x, long long int y) { return ncr[x][y]; } void cal1() { fact[0] = 1; for (long long int i = 1; i < 1000005; i++) { fact[i] = (fact[i - 1] * i) % 998244353; } for (long long int i = 0; i < 1000005; i++) { inv[i] = binpow(fact[i], 998244353 - 2, 998244353); } } long long int ncr1(long long int x, long long int y) { return (((fact[x] * inv[y]) % 998244353) * inv[x - y]) % 998244353; } long long int gcd(long long int a, long long int b, long long int& x, long long int& y) { if (a == 0) { x = 0; y = 1; return b; } long long int x1, y1; long long int d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } long long int fun(long long int x) { long long int y = 1; while (x) { if (x % 10 != 0) y *= (x % 10); x = x / 10; } if (y < 10) return y; else return fun(y); } void solve165b() { long long int n, k; cin >> n >> k; long long int l = 0, h = n, an = n; while (l <= h) { long long int mid = (l + h + 1) / 2; long long int x = k, y = mid; while (mid / x) { y += (mid / x); x = x * k; } if (y >= n) { an = mid; h = mid - 1; } else l = mid + 1; } cout << an; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; while (t--) { string s; cin >> s; long long int a[26] = {0}; for (long long int i = 0; i < s.size(); i++) { a[s[i] - 'a']++; } long long int n; cin >> n; long long int l = 1, h = s.size(), an = s.size() + 5; while (l <= h) { long long int mid = (l + h + 1) / 2; long long int c = 0; for (long long int i = 0; i < 26; i++) c += ((a[i] + mid - 1) / mid); if (c <= n) { an = mid; h = mid - 1; } else l = mid + 1; } if (an > s.size()) cout << -1; else { cout << an << endl; long long int c = 0; for (long long int i = 0; i < 26; i++) { long long int x = (a[i] + an - 1) / an; c += x; for (long long int j = 0; j < x; j++) { char x = 'a' + i; cout << x; } } for (long long int i = c; i < n; i++) cout << "z"; } } 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 double PI = acos(-1.0); string s; int m; int n; const int maxL = 50; int veces[maxL]; void contarApariciones() { for (int i = 0; i < (int)maxL; i++) veces[i] = 0; for (int i = 0; i < (int)s.size(); i++) veces[s[i] - 'a']++; } int cantLetras() { set<char> letras; for (int i = 0; i < (int)s.size(); i++) letras.insert(s[i]); return letras.size(); } string findT(int mid) { vector<int> vecesEnT(maxL, 0); string t; for (int i = 0; i < (int)maxL; i++) { vecesEnT[i] = (veces[i] + mid - 1) / mid; for (int j = 0; j < (int)vecesEnT[i]; j++) t.push_back((char)('a' + i)); } while ((int)t.size() < n) t.push_back('a'); if ((int)t.size() > n) return "*"; else return t; } int main() { cin >> s; m = s.size(); cin >> n; contarApariciones(); if (cantLetras() > n) { cout << -1 << endl; return 0; } else { int lo = 0; int hi = m; while (lo < hi - 1) { int mid = (lo + hi) / 2; string t = findT(mid); if (t != "*") hi = mid; else lo = mid; } cout << hi << endl; cout << findT(hi) << endl; return 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
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.printf("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; const long long int MOD = 0ll; const long long int INF = 3e18; long long int power(long long int base, long long int exp) { long long int res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base); base = (base * base); exp /= 2; } return res; } long long int bitc(long long int n, long long int x) { return ((n >> x) & 1); } long long int __gcd(long long int a, long long int b) { return b == 0 ? a : __gcd(b, a % b); } long long int fsub(long long int a, long long int b, long long int p = MOD) { return ((a % p) - (b % p) + p) % p; } long long int fmult(long long int a, long long int b, long long int p = MOD) { return ((((a % p) * (b % p)) % p) + p) % p; } long long int fadd(long long int a, long long int b, long long int p = MOD) { return ((a % p + b % p) % p + p) % p; } long long int fpow(long long int n, long long int k, long long int p = MOD) { long long int r = 1; while (k > 0) { if (k & 1) r = r * n % p; n = n * n % p; k = k >> 1; } return r; } long long int inv(long long int a, long long int p = MOD) { return fpow(a, p - 2, p); } long long int fdiv(long long int a, long long int b, long long int p = MOD) { long long int yinv = inv(b); long long int ans = (a * yinv) % p; return ((ans) % p + p) % p; } long long int ceil(long long int a, long long int b) { return ceil((long double)a / b); } template <typename T> istream &operator>>(istream &in, vector<T> &a) { for (auto &item : a) { in >> item; } return in; } template <typename T, typename U> ostream &operator<<(ostream &out, pair<T, U> &a) { cout << a.first << " " << a.second; return out; } template <typename T, typename U> istream &operator>>(istream &out, pair<T, U> &a) { cin >> a.first >> a.second; return out; } template <typename T, typename U> ostream &operator<<(ostream &out, map<T, U> &a) { for (auto &item : a) { out << item << "\n"; } return out; } template <typename T> ostream &operator<<(ostream &out, vector<T> &a) { for (auto &item : a) { out << item << " "; } return out; } template <typename T> ostream &operator<<(ostream &out, vector<vector<T>> &a) { for (auto &item : a) { out << item << "\n"; } return out; } template <int D, typename T> struct Vec : public vector<Vec<D - 1, T>> { static_assert(D >= 1, "Vector dimension must be greater than zero!"); template <typename... Args> Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} }; template <typename T> struct Vec<1, T> : public vector<T> { Vec(int n = 0, T val = T()) : vector<T>(n, val) {} }; std::vector<bool> is_prime; std::vector<long long int> primes; void sieve(long long int n) { is_prime.resize(n + 2, true); primes.clear(); long long int p; for (p = 2; p * p <= n; p++) { if (is_prime[p]) { long long int i; for (i = p * p; i <= n; i += p) { is_prime[i] = false; } } } is_prime[0] = is_prime[1] = false; long long int i; for (i = 2; i <= n; i++) { if (is_prime[i]) { primes.emplace_back(i); } } } map<long long int, long long int> prime_factors(long long int n) { map<long long int, long long int> s; long long int i; long long int tc = 0; while (n % 2 == 0) { tc++; n /= 2; } if (tc > 0) { s[2] = tc; } for (i = 3; i <= sqrt(n); i += 2) { tc = 0; while (n % i == 0) { tc++; n /= i; } if (tc > 0) { s[i] = tc; } } if (n > 2) { s[n] += 1; } return s; } std::vector<long long int> fact_vec; void fact_fun(long long int n) { fact_vec.resize(n + 10); long long int i; fact_vec[0] = 1; for (i = 1; i <= n + 2; i++) { fact_vec[i] = (fact_vec[i - 1] * i) % MOD; } } std::vector<long long int> p2; void power_2(long long int n, long long int m = MOD) { long long int i; p2.emplace_back(1); for (i = 0; i < n; i++) { p2.emplace_back(fmult(p2.back(), 2)); } } long long int ncr(long long int n, long long int r) { if (r > n) return 0; return fdiv(fact_vec[n], fmult(fact_vec[r], fact_vec[n - r])); } std::vector<long long int> spf; void sieve2(long long int MAXN) { MAXN += 10; spf.resize(MAXN, 0); spf[1] = 1; for (long long int i = 2; i < MAXN; i++) spf[i] = i; for (long long int i = 4; i < MAXN; i += 2) spf[i] = 2; for (long long int i = 3; i * i < MAXN; i++) { if (spf[i] == i) { for (long long int j = i * i; j < MAXN; j += i) { if (spf[j] == j) spf[j] = i; } } } } const int mi[4] = {-1, 0, 1, 0}, mj[4] = {0, 1, 0, -1}; const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1}; map<long long int, long long int> getFactorization(long long int x) { map<long long int, long long int> ret; while (x != 1) { ret[spf[x]]++; x = x / spf[x]; } return ret; } class Sol { public: Sol() { long long int i, j; } }; int main() { long long int i, j; cout << fixed; ios::sync_with_stdio(false); cin.tie(0); long long int ti; ti = 1; while (ti--) { string s; cin >> s; long long int n; cin >> n; long long int l = 1, r = INF; map<char, long long int> sp; for (auto p : s) { sp[p]++; } map<char, long long int> m; auto check = [&](long long int x) { m.clear(); long long int sum = 0; for (auto p : sp) { long long int a = ceil(p.second, x); m[p.first] = a; sum += a; } if (sum > n) { return false; } return true; }; long long int ans = -1; while (l <= r) { long long int mid = (l + r) / 2; if (check(mid)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } if (ans == -1) { cout << ans << "\n"; } else { cout << ans << "\n"; check(ans); string res; for (auto p : m) { res += string(p.second, p.first); } while (res.length() < n) { res += 'z'; } cout << res << "\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() { int n, mas[30]; string s; cin >> ws >> s >> n; for (int i = 0; i < 30; i++) mas[i] = 0; for (int i = 0; i < s.size(); i++) { mas[s[i] - 'a']++; } int l = 0, r = 2000; while (l < r) { int s = (l + r) / 2; if (s == 0) { l = 1; break; } int n1 = n; for (int i = 0; i < 30; i++) { n1 -= mas[i] / s; if (mas[i] % s != 0) n1--; } if (n1 >= 0) r = s; else l = s + 1; } if (l > 1000) { cout << -1; return 0; } cout << l << '\n'; for (int i = 0; i < 30; i++) { for (int j = 0; j < mas[i] / l; j++) { cout << char('a' + i); n--; } if (mas[i] % l != 0) { cout << char('a' + i); n--; } } for (int i = 0; i < n; i++) cout << 'z'; 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 = raw_input() n = input() f = {} for c in s: f[c] = f.get(c, 0) + 1 if n < len(f): print -1 quit() l, r = 1, len(s) while l < r: m = l + r >> 1 if n < sum((v + m - 1) / m for v in f.itervalues()): l = m + 1 else: r = m print l a, s = '', 0 for c, v in f.iteritems(): t = (v + l - 1) / l s += t a += c * t print a + 'a' * (n - s)
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 ns, n; char s[1005]; int F[26]; bool f(int k) { int cnt = 0; for (int i = 0; i < (26); i++) if (F[i]) cnt += (F[i] - 1) / k + 1; return cnt <= n; } int main() { while (scanf("%s%d", s, &n) == 2) { memset(F, 0, sizeof(F)); ns = strlen(s); for (int i = 0; i < (ns); i++) F[s[i] - 'a']++; int ans = (1 << 30); int lo = 1, hi = ns; if (!f(hi)) { puts("-1"); continue; } while (hi - lo > 1) { int med = (lo + hi) >> 1; if (f(med)) hi = med; else lo = med; } ans = hi; if (f(1)) ans = 1; char t[n + 5]; t[n] = 0; int sz = 0; for (int i = 0; i < (26); i++) if (F[i]) for (int j = 0; j < ((F[i] - 1) / ans + 1); j++) t[sz++] = 'a' + i; while (sz < n) t[sz++] = 'a'; printf("%d\n%s\n", ans, t); } }
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, cnt; int c[30]; int need[30]; bool pos(int u) { int ret = 0; for (int i = 0; i < 26; ++i) { need[i] = (int)ceil((double)c[i] / u); if (need[i]) ret += need[i]; } return (ret <= n); } int main() { cin >> s >> n; for (int i = 0; i < s.size(); ++i) { cnt += !c[s[i] - 'a']; ++c[s[i] - 'a']; } if (cnt > n) { cout << "-1\n"; return 0; } int low = 1, hi = 1000; while (low != hi) { int mid = (low + hi) / 2; if (pos(mid)) hi = mid; else low = mid + 1; } cout << low << endl; pos(low); int t = 0; for (int i = 0; i < n; ++i) { if (t == 26) { cout << 'a'; continue; } if (need[t] == 0) { ++t; --i; continue; } cout << char(t + 'a'); --need[t]; } 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class CF335A { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(System.in); char s[] = sc.next().toCharArray(); int n = sc.nextInt(); int f[] = new int[26]; boolean found = false; for (int i = 0; i < s.length; i++) f[s[i] - 'a']++; for (int k = 1; k <= s.length; k++) { int ans = sum(f, k); if (ans <= n) { found = true; System.out.println(k); int len = 0; for (int i = 0; i < 26; i++) { int rep = (int) Math.ceil(f[i] * 1.0 / k); while (rep-- > 0) { System.out.print((char) (i + 'a')); len++; } } while (len < n) { System.out.print('a'); len++; } break; } } System.out.println(!found ? -1 : ""); } static int sum(int f[], int k) { int ans = 0; for (int i = 0; i < f.length; i++) { ans += (int) Math.ceil(f[i] * 1.0 / k); } return ans; } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { return br.readLine(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } 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; long long dbg = 0; template <typename A, typename B> ostream& operator<<(ostream& cout, pair<A, B> const& p); template <typename A> ostream& operator<<(ostream& cout, vector<A> const& v); template <typename A, typename B> istream& operator>>(istream& cin, pair<A, B>& p); template <typename A> istream& operator>>(istream& cin, vector<A>& v); int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; map<char, long long> used; long long cmp(pair<long long, char> a, pair<long long, char> b) { if (used[a.second] == used[b.second]) return a.first > b.first; return used[a.second] < used[b.second]; } void solve() { string s; cin >> s; long long n; cin >> n; map<char, long long> arr; for (auto i : s) arr[i]++; if (arr.size() > n) cout << -1 << '\n'; else { string t; long long ans = 0; for (long long i = 1;; i++) { long long sum = 0; long long pos = 0; for (auto j : arr) { sum += (j.second + i - 1) / i; } if (sum <= n) { ans = i; break; } } for (auto j : arr) { for (long long i = 0; i < (j.second + ans - 1) / ans; i++) t += j.first; } while (t.size() < n) t += 'a'; cout << ans << '\n'; cout << t << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; } template <typename A, typename B> ostream& operator<<(ostream& cout, pair<A, B> const& p) { if (dbg) { dbg = 0; return cout << "(" << p.first << ", " << p.second << ")"; } else { return cout << p.first << ' ' << p.second; } } template <typename A> ostream& operator<<(ostream& cout, vector<A> const& v) { if (dbg) { dbg = 0; cout << "["; for (long long i = 0; i < v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } else { for (long long i = 0; i < v.size() - 1; i++) cout << v[i] << ' '; return cout << v[v.size() - 1]; } } template <typename A, typename B> istream& operator>>(istream& cin, pair<A, B>& p) { cin >> p.first; return cin >> p.second; } template <typename A> istream& operator>>(istream& cin, vector<A>& v) { for (long long i = 0; i < v.size() - 1; i++) cin >> v[i]; return cin >> v[v.size() - 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
import java.io.InputStreamReader; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Hashtable; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; import java.math.BigDecimal; import java.math.BigInteger; public class palin { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(System.out); Scanner scan = new Scanner(System.in); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { String str = in.next(), res = ""; int n = in.nextInt(), count = 0; double c[] = new double[26]; for (int i = 0; i < str.length(); i++) { c[(int) str.charAt(i) - 97]++; } for (int i = 0; i < c.length; i++) { if (c[i] != 0) { count++; } } if (count > n) { out.print(-1); return; } for (int i = 1; i < 1010; i++) { int needs[] = new int[26]; for (int j = 0; j < needs.length; j++) { needs[j] = (int) Math.ceil(c[j] / i); } int sum = 0; for (int j = 0; j < needs.length; j++) { sum += needs[j]; } if (sum > n) { continue; } out.println(i); int r = 0; for (int j = 0; j < needs.length; j++) { for (int k = 0; k < needs[j]; k++) { out.print((char) (j + 97)); r++; } } for (int j = 0; j < n - r; j++) { out.print('a'); } return; } out.print(-1); } } class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = null; } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public float nextFloat() { return Float.parseFloat(next()); } public double nextDouble() { return Double.parseDouble(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; int freq[500]; string calcular(int k, int n) { 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 = -1; for (int k = s.size(); k >= 1; k--) { string res = calcular(k, n); if (res.size() == n) { result = res; cantidad = k; } } if (cantidad == -1) { 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
a={} for c in raw_input(): a[c]=a.get(c,0)+1 n=input() l,h=1,10000 while l<h: m=(l+h)/2 t=n for c in a: t-=(a[c]+m-1)/m if t<0: l=m+1 else: h=m if l>1010: print -1 else: print l t='' for c in a: t+=c*((a[c]+l-1)/l) t+='a'*(n-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; vector<int> cnt(26, 0); int gen_ans(int k) { if (k == 0) return 1001; int len = 0; for (int i = 0; i < 26; i++) { if (cnt[i] != 0) len += ceil((float)cnt[i] / k); } return len; } int main() { string s; cin >> s; int n; cin >> n; for (int i = 0; i < s.length(); i++) cnt[s[i] - 'a']++; int dis = 0; for (int i = 0; i < 26; i++) { if (cnt[i] != 0) dis++; } if (dis > n) { cout << "-1\n"; return 0; } int l = 0; int r = 1000; int mid, ans = 3; while (l <= r) { mid = (l + r) / 2; if (gen_ans(mid) <= n) { ans = mid; r = mid - 1; } else if (gen_ans(mid) > n) { l = mid + 1; } } string anst = ""; for (int i = 0; i < 26; i++) { for (int j = 0; j < ceil((float)cnt[i] / ans); j++) anst += (char)('a' + i); } if (anst.length() < n) { for (int i = anst.length(); i < n; i++) anst += 'a'; } cout << ans << "\n"; cout << anst << "\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[1010]; int cnt[30] = {0}, n, len, cNum = 0; int use[30] = {0}, t; vector<int> v; bool comp(const int &a, const int &b) { int i = cnt[a] / use[a] + (cnt[a] % use[a] == 0 ? 0 : 1); int j = cnt[b] / use[b] + (cnt[b] % use[b] == 0 ? 0 : 1); return i < j; } int main() { gets(s); scanf(" %d", &n); len = strlen(s); for (int i = 0; i < len; i++) cnt[s[i] - 'a']++; for (int i = 0; i < 26; i++) { if (cnt[i] > 0) { cNum++; use[i] = 1; v.push_back(i); } } make_heap(v.begin(), v.end(), comp); if (cNum > n) { puts("-1"); } else if (n >= len) { puts("1"); printf("%s", s); n -= len; while (n--) printf("a"); printf("\n"); } else { n -= cNum; while (n--) { t = v.front(); pop_heap(v.begin(), v.end(), comp); v.pop_back(); use[t]++; v.push_back(t); push_heap(v.begin(), v.end(), comp); } t = v.front(); n = cnt[t] / use[t] + (cnt[t] % use[t] == 0 ? 0 : 1); printf("%d\n", n); for (int i = 0; i < 26; i++) { for (int j = 0; j < use[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
import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author ffao */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Parser in = new Parser(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { int[] freq = new int[100]; int n; boolean check(int a) { int t = 0; for (int i = 0; i < 26; i++) { t += (freq[i]+a-1) / a; } return t <= n; } public void solve(int testNumber, Parser in, OutputWriter out) { String s = in.next(); n = in.nextInt(); int cnt = 0; for (int i = 0; i < s.length(); i++) { if (freq[s.charAt(i) - 'a']==0) cnt++; freq[s.charAt(i) - 'a']++; } int st = 1, end = s.length(); while (st < end) { int mid = (st+end)/2; if (check(mid)) end = mid; else st = mid+1; } if (cnt > n) { out.println(-1); return; } out.println(st); StringBuilder ans = new StringBuilder(); for (int i = 0; i < 26; i++) { int t = (freq[i] + st-1)/st; for (int j = 0; j < t; j++) ans.appendCodePoint('a' + i); } int l = ans.length(); for (int t = 0; t < n-l; t++) ans.append('a'); out.println(ans); } } class Parser { private BufferedReader din; private StringTokenizer tokenizer; public Parser(InputStream in) { din = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(din.readLine()); } catch (Exception e) { throw new UnknownError(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } class OutputWriter extends PrintWriter { public OutputWriter(Writer out) { super(out); } public OutputWriter(OutputStream out) { super(out); } }
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> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; long long max(long long a, long long b) { if (a < b) return b; return a; } long long min(long long a, long long b) { if (a < b) return a; return b; } long long X[] = {-1, 1, 0, 0}; long long Y[] = {0, 0, -1, 1}; long long XX[] = {-1, 1, 0, 0, -1, 1, -1, 1}; long long YY[] = {0, 0, -1, 1, -1, 1, 1, -1}; long long HX[] = {1, -1, 2, -2, 1, -1, -2, 2}; long long HY[] = {2, -2, 1, -1, -2, 2, 1, -1}; long long fpow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) { res = res * x % 1000000007; } x = x * x % 1000000007; n >>= 1; } return (res % 1000000007); } long long binomialCoeff(long long n, long long k) { long long C[n + 1][k + 1]; long long i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % 1000000007; } } return C[n][k]; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } void solve() { string s; cin >> s; int n; cin >> n; map<char, int> mx; for (char c : s) mx[c]++; if ((int)mx.size() > n) { cout << "-1" << "\n"; return; } for (int it = 1;; it++) { int sum = 0; vector<pair<char, int>> list; for (auto p : mx) { int need_cnt = (p.second + it - 1) / it; sum += need_cnt; list.emplace_back(p.first, need_cnt); } if (sum > n) continue; string res; for (auto p : list) res += string(p.second, p.first); res += string(n - (int)res.length(), 'a'); cout << it << "\n"; cout << res << "\n"; return; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
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 = input() n = int(input()) d = Counter() for c in s: d[c] += 1 if len(d) > n: print(-1) else: left = 0 right = 10**10 s = "" lastok = ("", 0) while left + 1 < right: mid = (left + right) // 2 s = "" for (c, cnt) in d.items(): cntnow = (cnt - 1) // mid + 1 s += c * cntnow if len(s) < n: s += 'a' * (n - len(s)) if len(s) == n: lastok = (s, mid) right = mid else: left = mid print (lastok[1]) print (lastok[0])
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 solve() { string s; long long n, i, x, y, d; cin >> s >> n; set<long long> ss; map<char, long long> mp; for (i = 0; i < s.length(); i++) { ss.insert(s[i]); mp[s[i]]++; } if (ss.size() > n) { cout << -1 << '\n'; } else { for (i = 1; i <= 1000; i++) { long long cn = 0; string ans; for (auto it : mp) { x = it.second / i; if (it.second % i != 0) { x++; } ans += string(x, it.first); cn += x; } if (cn <= n) { if (cn == n) { cout << i << '\n'; cout << ans << '\n'; } else { x = n - cn; cout << i << '\n'; cout << ans; cout << string(x, s[0]) << '\n'; } break; } } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
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() { int n, zheng = 0, mmax = 0, strc, stra, cuowu; char a[1000], b[26], c[1000]; double bb[26], sheng[26], bisheng[26], cc[26]; cin >> a; cin >> n; sort(a, a + strlen(a)); memset(c, 0, sizeof(c)); b[0] = 'a'; for (int i = 1; i < 26; i++) { b[i] = b[i - 1] + 1; } for (int i = 0; i < 26; i++) { bb[i] = 0, cc[i] = 0, sheng[i] = 0, bisheng[i] = 0; } for (int i = 0; i < strlen(a); i++) { for (int j = 0; j < 26; j++) { if (a[i] == b[j]) bb[j]++; } } for (int i = 0; i < 26; i++) { if (bb[i] != 0) zheng++; if (bb[i] > mmax) mmax = bb[i]; } stra = strlen(a); if (n > strlen(a)) { cout << 1 << endl; int cuowu = 0; for (int k = 0; k < 26; k++) { if (sheng[k] > sheng[cuowu]) cuowu = k; } for (int i = 0; i < n - stra; i++) { a[i + stra] = a[cuowu]; } for (int i = 0; i < n; i++) { cout << a[i]; } cout << endl; } else if (n < zheng) { cout << -1 << endl; } else { int j = 0, yu; for (int i = 0; i < 26; i++) { sheng[i] = bb[i]; bisheng[i] = bb[i]; if (bb[i] != 0) { c[j] = b[i]; cc[i] = 1; j++; } } yu = n - strlen(c); if (yu == 0) { cout << mmax << endl; cout << c << endl; } else { strc = strlen(c); mmax = 0; for (int i = 0; i < yu; i++) { for (int k = 0; k < 26; k++) { if (sheng[k] > sheng[mmax]) mmax = k; } cc[mmax] += 1; sheng[mmax] = bisheng[mmax] / cc[mmax]; c[i + strc] = b[mmax]; } for (int k = 0; k < 26; k++) { if (sheng[k] > sheng[mmax]) mmax = k; } cout << ceil(sheng[mmax]) << endl; cout << c << 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
#include <bits/stdc++.h> using namespace std; bool isOK(int k); string s; int n; int a[30]; int main() { cin >> s >> n; int size = s.size(); int l = ceil(size / n), r = 1000; memset(a, 0, sizeof(0)); for (int i = 0; i < size; ++i) { a[s[i] - 'a']++; } int diff = 0; for (int i = 0; i < 26; ++i) { if (a[i] != 0) { ++diff; } } if (diff > n) { cout << -1 << "\n"; return 0; } while (l <= r) { int mid = (l + r) / 2; if (isOK(mid)) { r = mid - 1; } else { l = mid + 1; } } int b[26]; memset(b, 0, sizeof(b)); for (int i = 0; i < 26; ++i) { b[i] = ceil(a[i] / (l * 1.0)); } string result = ""; for (int i = 0; i < 26; ++i) { int j = b[i]; for (int t = 0; t < j; ++t) { result.push_back('a' + i); } } int rSize = result.size(); if (rSize < n) { for (int i = 0; i < n - rSize; ++i) { result += "z"; } } cout << l << "\n" << result << "\n"; } bool isOK(int k) { long double count = 0; for (int i = 0; i < 26; ++i) { count += (long double)ceil(a[i] / (k * 1.0)); } if (count <= n) { return true; } return false; }
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.IOException; import java.io.OutputStreamWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Egor Kulikov ([email protected]) */ 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) { char[] required = in.readString().toCharArray(); int length = in.readInt(); final int[] qty = new int[26]; for (char c : required) qty[c - 'a']++; final int[] taken = new int[26]; Heap heap = new Heap(new IntComparator() { public int compare(int first, int second) { return taken[first] * qty[second] - qty[first] * taken[second]; } }, 26); for (int i = 0; i < 26; i++) { if (qty[i] != 0) heap.add(i); } if (length < heap.getSize()) { out.printLine(-1); return; } StringBuilder answer = new StringBuilder(length); for (int i = 0; i < length; i++) { int current = heap.poll(); answer.append((char)('a' + current)); taken[current]++; heap.add(current); } int max = 0; for (int i = 0; i < 26; i++) { if (qty[i] != 0) max = Math.max(max, (qty[i] - 1) / taken[i] + 1); } out.printLine(max); out.printLine(answer); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public 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 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 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } 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(); } } class Heap { private IntComparator comparator; private int size = 0; private int[] elements; private int[] at; public Heap(int maxElement) { this(10, maxElement); } public Heap(IntComparator comparator, int maxElement) { this(10, comparator, maxElement); } public Heap(int capacity, int maxElement) { this(capacity, IntComparator.DEFAULT, maxElement); } public Heap(int capacity, IntComparator comparator, int maxElement) { this.comparator = comparator; elements = new int[capacity]; at = new int[maxElement]; Arrays.fill(at, -1); } public int getSize() { return size; } public boolean isEmpty() { return size == 0; } public int add(int element) { ensureCapacity(size + 1); elements[size] = element; at[element] = size; shiftUp(size++); return at[element]; } public void shiftUp(int index) { // if (index < 0 || index >= size) // throw new IllegalArgumentException(); int value = elements[index]; while (index != 0) { int parent = (index - 1) >>> 1; int parentValue = elements[parent]; if (comparator.compare(parentValue, value) <= 0) { elements[index] = value; at[value] = index; return; } elements[index] = parentValue; at[parentValue] = index; index = parent; } elements[0] = value; at[value] = 0; } public void shiftDown(int index) { if (index < 0 || index >= size) throw new IllegalArgumentException(); while (true) { int child = (index << 1) + 1; if (child >= size) return; if (child + 1 < size && comparator.compare(elements[child], elements[child + 1]) > 0) child++; if (comparator.compare(elements[index], elements[child]) <= 0) return; swap(index, child); index = child; } } private void swap(int first, int second) { int temp = elements[first]; elements[first] = elements[second]; elements[second] = temp; at[elements[first]] = first; at[elements[second]] = second; } private void ensureCapacity(int size) { if (elements.length < size) { int[] oldElements = elements; elements = new int[Math.max(2 * elements.length, size)]; System.arraycopy(oldElements, 0, elements, 0, this.size); } } public int poll() { if (isEmpty()) throw new IndexOutOfBoundsException(); int result = elements[0]; at[result] = -1; if (size == 1) { size = 0; return result; } elements[0] = elements[--size]; at[elements[0]] = 0; shiftDown(0); return result; } } interface IntComparator { public static final IntComparator DEFAULT = new IntComparator() { public int compare(int first, int second) { if (first < second) return -1; if (first > second) return 1; return 0; } }; public int compare(int first, int second); }
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 int N = 2000001; const int INF = 100000000; void solve() { string s, d; cin >> s; int n; cin >> n; vector<int> a(26, 0), b(26); for (long long int i = 0; i < (s.size()); i++) { a[s[i] - 'a']++; } long long int k = 0, sum = 0; k = *max_element(a.begin(), a.end()); for (long long int i = 1; i < (k + 1); i++) { for (long long int j = 0; j < (a.size()); j++) { sum += ceil(a[j] * 1.0 / i); b[j] = ceil(a[j] * 1.0 / i); } if (sum <= n) { for (long long int j = 0; j < (b.size()); j++) { for (long long int k = 0; k < (b[j]); k++) d.push_back((j + 'a')); } if (n > d.size()) { while (n != d.size()) { d.push_back(('a')); } } cout << i << '\n'; cout << d; return; } sum = 0; } cout << -1; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; while (T--) { solve(); 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.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author vadimmm */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { char[] s = in.next().toCharArray(); int n = in.nextInt(); int[][] counter = new int[26][2]; for (int i = 0; i < 26; ++i) counter[i][1] = i; for (char ch : s) counter[ch - 'a'][0]++; Arrays.sort(counter, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return -Integer.compare(o1[0], o2[0]); } }); int m; for (m = 0; m < 26; ++m) if (counter[m][0] == 0) break; if (m > n) { out.println(-1); return; } int[] cnt = new int[m]; Arrays.fill(cnt, 1); for (int i = n - m; i > 0; --i) { int max = 0; for (int j = 1; j < m; ++j) if ((counter[max][0] + cnt[max] - 1) / cnt[max] < (counter[j][0] + cnt[j] - 1) / cnt[j]) max = j; ++cnt[max]; } int max = 0; for (int j = 0; j < m; ++j) if ((counter[max][0] + cnt[max] - 1) / cnt[max] < (counter[j][0] + cnt[j] - 1) / cnt[j]) max = j; out.println((counter[max][0] + cnt[max] - 1) / cnt[max]); for (int i = 0; i < m; ++i) for (int j = 0; j < cnt[i]; ++j) out.print((char) (counter[i][1] + 'a')); } } class InputReader { private static BufferedReader bufferedReader; private static StringTokenizer stringTokenizer; public InputReader(InputStream inputStream) { bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); stringTokenizer = null; } public String next() { while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { try { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return stringTokenizer.nextToken(); } 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; int n, k[30]; bool check(int s) { int ret = 0; for (int i = 0; i <= 26; i++) if (k[i]) ret += (k[i] - 1) / s + 1; if (ret > n) return false; return true; } string s; int main() { cin >> s >> n; int j; for (int i = 0; i < s.size(); i++) k[s[i] - 'a']++; for (int i = 1; i <= 1010; i++) { if (check(i)) { j = i; break; } if (i == 1010) { cout << -1; return 0; } } int c = 0; cout << j << endl; for (int i = 0; i <= 26; i++) if (k[i]) { int f = (k[i] - 1) / j + 1; c += f; for (int j = 0; j < f; j++) cout << char(i + 'a'); } for (int i = c; i < n; i++) 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.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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class A implements Runnable { private void solve() throws IOException { String s = nextToken(); int n = nextInt(); int[] f = new int[26]; int distincts = 0; for (int i = 0; i < s.length(); i++) { if (f[s.charAt(i) - 'a'] == 0) ++distincts; f[s.charAt(i) - 'a']++; } if (distincts > n) { pl(-1); return; } boolean found = false; String ret = ""; int len = -1; int lo = 1, hi = s.length() + 1; while (hi > lo) { int i = (lo + hi) / 2; StringBuilder sb = new StringBuilder(); for (int j = 0; j < f.length; j++) { if (f[j] > 0) { int ns = (f[j] / i) + (f[j] % i == 0 ? 0 : 1); while (ns-- > 0) sb.append((char) (j + 'a')); } } if (sb.length() <= n) { hi = i; while (sb.length() < n) sb.append("a"); if (sb.length() == n) { found = true; ret = sb.toString(); len = i; } } else { lo = i + 1; } } if (!found) { pl(-1); } else { pl(len); pl(ret); } } public static void main(String[] args) { new A().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new BufferedReader( new InputStreamReader(System.in))); writer = new PrintWriter(System.out); tokenizer = null; solve(); reader.close(); writer.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()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } void p(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } void pl(Object... objects) { p(objects); writer.println(); } int cc; void pf() { writer.printf("Case #%d: ", ++cc); } }
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 { static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); tokenizer = null; banana(); reader.close(); writer.close(); } static void banana() throws IOException { String s = nextToken(); int n = nextInt(); Character cc = 'a'; HashMap<Character, Integer> mp = new HashMap<Character, Integer>(); for (int i = 0; i < s.length(); i++) { Character c = s.charAt(i); cc = c; if (mp.containsKey(c)) { mp.put(c, mp.get(c) + 1); } else { mp.put(c, 1); } } if (mp.size() > n) { writer.println(-1); return; } HashMap<Character, Integer> ans = new HashMap<Character, Integer>(); for (Character c : mp.keySet()) { ans.put(c, 0); } ans.put(cc, n); int k = 1000; boolean moar = true; while (moar) { boolean good = true; for (Character c : mp.keySet()) { if (ans.get(c) * (k - 1) < mp.get(c)) { good = false; break; } } if (good) { k--; continue; } boolean done = false; for (Character c1 : mp.keySet()) { if (done) break; for (Character c2 : mp.keySet()) { if (c1 == c2) continue; if (ans.get(c1) * (k - 1) < mp.get(c1) && (ans.get(c2) - 1) * (k - 1) >= mp.get(c2)) { ans.put(c1, ans.get(c1) + 1); ans.put(c2, ans.get(c2) - 1); done = true; break; } } } if (!done) break; } StringBuilder sb = new StringBuilder(); for (Character c : ans.keySet()) { int t = ans.get(c); for (int i = 0; i < t; i++) { sb.append(c); } } writer.println(k); writer.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 str[1024], ans[1024]; int cnt[32]; bool check(int n, int k) { int i, sum = 0; for (i = 0; i < 26; ++i) { sum += (cnt[i] + k - 1) / k; } return sum <= n; } int solve(int n) { int low = 1, high = strlen(str), mid, ret = -1; while (low <= high) { mid = (low + high) >> 1; if (check(n, mid)) { ret = mid; high = mid - 1; } else { low = mid + 1; } } return ret; } int main() { int n, i, j, s, ret; while (~scanf("%s %d", str, &n)) { memset(cnt, 0, sizeof(cnt)); for (i = 0; str[i]; ++i) ++cnt[str[i] - 'a']; ret = solve(n); printf("%d\n", ret); if (~ret) { for (s = i = 0; i < 26; ++i) { for (j = 0; j < (cnt[i] + ret - 1) / ret; ++j) { ans[s++] = 'a' + i; } } for (; s < n; ++s) ans[s] = 'a'; ans[n] = 0; printf("%s\n", 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.*; import java.util.*; public class Main { private static void solve(InputReader in, OutputWriter out) { String s = in.next(); int n = in.nextInt(); Map<Character, Integer> letters = new HashMap<>(); int maxCnt = 0; for (int i = 0; i < s.length(); i++) { char letter = s.charAt(i); if (!letters.containsKey(letter)) letters.put(letter, 1); else letters.put(letter, letters.get(letter) + 1); if (letters.get(letter) > maxCnt) maxCnt = letters.get(letter); } if (letters.size() > n) out.print(-1); else { int l = 1, r = maxCnt; while (l < r) { int m = (l + r) / 2; int tmp = n; for (int cnt : letters.values()) { int len = (int) Math.ceil(cnt * 1.0 / m); tmp -= len; if (tmp < 0) break; } if (tmp >= 0) { r = m; } else { l = m + 1; } } out.println(r); int tmp = n; char let = '!'; for (char letter : letters.keySet()) { let = letter; int len = (int) Math.ceil(letters.get(letter) * 1.0 / r); tmp -= len; for (int i = 0; i < len; i++) { out.print(letter); } } for (int i = 0; i < tmp; i++) { out.print(let); } } } private static void shuffleArray(int[] array) { int index; Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { index = random.nextInt(i + 1); if (index != i) { array[index] ^= array[i]; array[i] ^= array[index]; array[index] ^= array[i]; } } } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); solve(in, out); in.close(); out.close(); } private static class InputReader { private BufferedReader br; private StringTokenizer st; InputReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); st = null; } String nextLine() { String line = null; try { line = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return line; } String next() { while (st == null || !st.hasMoreTokens()) { String line = nextLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } byte nextByte() { return Byte.parseByte(next()); } short nextShort() { return Short.parseShort(next()); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static class OutputWriter { BufferedWriter bw; OutputWriter(OutputStream os) { bw = new BufferedWriter(new OutputStreamWriter(os)); } void print(int i) { print(Integer.toString(i)); } void println(int i) { println(Integer.toString(i)); } void print(long l) { print(Long.toString(l)); } void println(long l) { println(Long.toString(l)); } void print(double d) { print(Double.toString(d)); } void println(double d) { println(Double.toString(d)); } void print(boolean b) { print(Boolean.toString(b)); } void println(boolean b) { println(Boolean.toString(b)); } void print(char c) { try { bw.write(c); } catch (IOException e) { e.printStackTrace(); } } void println(char c) { println(Character.toString(c)); } void print(String s) { try { bw.write(s); } catch (IOException e) { e.printStackTrace(); } } void println(String s) { print(s); print('\n'); } void close() { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
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.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.Reader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputStreamReader in = new InputStreamReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputStreamReader inSt, PrintWriter out) { InputReader in = new InputReader(inSt); String str = in.next(); int n = in.nextInt(); int[] a = new int[26]; for (int i = 0; i < str.length(); i++) { a[str.charAt(i) - 'a']++; } for (int k = 1; k < 1001; k++) { int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + (a[i] + k - 1) / k; } if (sum <= n) { out.println(k); int notZeroIndex = -1; for (int i = 0; i < a.length; i++) { int curLen = (a[i] + k - 1) / k; for (int j = 0; j < curLen; j++) { notZeroIndex = i; out.print((char)(i + 'a')); } } while (sum < n) { out.print((char)(notZeroIndex + 'a')); sum++; } return; } } out.print("-1"); } class InputReader { public BufferedReader reader; private String[] currentArray; int curPointer; public InputReader(InputStreamReader inputStreamReader) { reader = new BufferedReader(inputStreamReader); } public String next() { try { currentArray = null; return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public void nextChars(char[] t) { try { currentArray = null; reader.read(t); } catch (IOException e) { throw new RuntimeException(e); } } public char nextChar() { try { currentArray = null; return (char) reader.read(); } catch (IOException e) { throw new RuntimeException(e); } } public int nextInt() { if ((currentArray == null) || (curPointer >= currentArray.length)) { try { currentArray = reader.readLine().split(" "); } catch (IOException e) { throw new RuntimeException(e); } curPointer = 0; } return Integer.parseInt(currentArray[curPointer++]); } public long nextLong() { if ((currentArray == null) || (curPointer >= currentArray.length)) { try { currentArray = reader.readLine().split(" "); } catch (IOException e) { throw new RuntimeException(e); } curPointer = 0; } return Long.parseLong(currentArray[curPointer++]); } } }
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 sys import math sys.setrecursionlimit(100000) #sys.stdin = open("INP.txt", 'r') # sys.stdout = open("OUT.txt", 'w') def main(): s = input() n = int(input()) M = dict() for char in s: if char in M: M[char] += 1 else: M[char] = 1 if n < len(M): print(-1) else: l = 1 r = len(s) while l != r: k = l+(r-l)//2 smallest_n = 0 for it in M.values(): tmp = math.ceil(it/k) smallest_n += tmp if smallest_n <= n: r = k else: l = k+1 sticker = '' for char, occ in zip(M.keys(), M.values()): sticker += (char*math.ceil(occ/l)) if len(sticker) < n: sticker += (n-len(sticker))*list(M.keys())[0] print("{}\n{}".format(l, sticker)) 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; int a[30]; int n; bool check(int x) { int cont = 0; for (int i = 0; i < 30; i++) { if (a[i] != 0) cont += (int)ceil(a[i] / (double)x); } if (cont <= n) return true; return false; } int main() { string s; cin >> s; cin >> n; int len = s.size(); int mx = 0; int numOfChar = 0; for (int i = 0; i < len; i++) { if (a[s[i] - 'a'] == 0) numOfChar++; a[s[i] - 'a']++; mx = max(mx, a[s[i] - 'a']); } if (n < numOfChar) { cout << -1; return 0; } if (n == numOfChar) { cout << mx << endl; for (int i = 0; i < 30; i++) if (a[i] != 0) cout << (char)(i + 'a'); return 0; } int st = 1, en = 1000; while (st <= en) { int md = (st + en) / 2; if (check(md)) en = md - 1; else st = md + 1; } int ans = en + 1; cout << ans << endl; int cnt = 0; for (int i = 0; i < 30; i++) { if (a[i] == 0) continue; int temp = (int)ceil(a[i] / (double)ans); cnt += temp; while (temp--) { cout << (char)(i + 'a'); } } for (int i = cnt; i < n; i++) cout << '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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); char[] a = sc.next().toCharArray(); int n = sc.nextInt(); int[] occ1 = new int[27]; for(char c : a) occ1[c-'a']++; int res = -1; int occ2[] = null, c = 0; for(int sheets = 1; sheets <= 1000; sheets++) { occ2 = new int[27]; c = 0; for(int i = 0; i < 27; i++) if(occ1[i] > 0) { occ2[i] = occ1[i] / sheets; if(occ1[i] % sheets != 0) occ2[i]++; c += occ2[i]; } if(c <= n) { res = sheets; break; } } while(c++ < n) occ2[0]++; out.println(res); if(res != -1) { for(int i = 0; i < 27; i++) while(occ2[i]-- > 0) out.print((char) (i + 'a')); out.println(); } out.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream System){br = new BufferedReader(new InputStreamReader(System));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine()throws IOException{return br.readLine();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public char nextChar()throws IOException{return next().charAt(0);} public Long nextLong()throws IOException{return Long.parseLong(next());} public boolean ready() throws IOException{return br.ready();} public void waitForInput(){for(long i = 0; i < 3e9; i++);} } }
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 sys import os 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 = {} reserve = '' unique = 0 max_occur = 1 all_letters = '' for x in s: if x in d: d[x] += 1 if d[x] > max_occur: max_occur = d[x] else: d[x] = 1 unique += 1 all_letters += x if unique > n: print(-1) elif unique == n: print(max_occur) print(all_letters) else: l = 1 r = max_occur ans = max_occur check = 0 final_str = all_letters check_str = '' while l<=r: mid = l + (r-l)//2 check = 0 check_str = '' reserve = '' for i in d.keys(): if d[i] > mid: check += math.ceil(d[i]/mid) check_str += i*math.ceil(d[i]/mid) reserve += i*(d[i] - math.ceil(d[i]/mid)) else: check += 1 check_str += i reserve += i*(d[i]-1) if check <= n: final_str = '%s' % check_str if len(final_str) < n: final_str += reserve[:n-len(final_str)] if len(final_str) < n: final_str += 'a'*(n-len(final_str)) ans = mid r = mid-1 else: l = mid+1 print(ans) print(final_str)
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; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; long long n; cin >> n; long long sz = s.size(); long long a[26] = {0}; map<char, long long> m; for (long long i = 0; i < sz; i++) { a[s[i] - 'a'] += 1; m[s[i]] += 1; } long long cnt = 0; for (long long i = 0; i < 26; i++) { if (a[i]) cnt += 1; } if (cnt > n) cout << "-1" << "\n"; else { for (long long i = 1; i <= 1000; i++) { string ans = ""; for (auto c : m) { long long x = ceil(1.0 * c.second / i); for (long long i = 0; i < x; ++i) ans += c.first; } if (ans.size() <= n) { while (ans.size() < n) ans += 'a'; cout << i << "\n" << 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.*; import java.util.*; public final class Template { private static Scanner sc = new Scanner(System.in); private static int frequency[] = new int[26]; public static void main(String[] args) { String word = sc.next(); int sheetLength = sc.nextInt(); int uniqueChars = 0; for (int i = 0; i < word.length(); i++) uniqueChars += frequency[word.charAt(i) - 'a']++ == 0 ? 1 : 0; if (uniqueChars > sheetLength) System.out.println("-1"); else { int sheetLen = 1; while (true) { int length = 0; for (int i = 0; i < 26; i++) { if (frequency[i] == 0) continue; length += (int) Math.ceil((frequency[i] * 1.0)/sheetLen); } if (length <= sheetLength) break; sheetLen++; } System.out.println("" + sheetLen); StringBuilder sheet = new StringBuilder(""); for (int i = 0; i < 26; i++) { for (int x = 0; x < Math.ceil((frequency[i] * 1.0)/sheetLen); x++) sheet.append((char) (i+'a')); } while (sheet.length() < sheetLength) sheet.append("a"); System.out.println(sheet); } } }
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[1111]; int cnt[33]; struct cs { int cnt, alp; }; cs css[33]; int pc; int cmp(cs c1, cs c2) { return c1.cnt > c2.cnt; } int ks[33]; int main() { int n; scanf("%s%d", s, &n); int len = strlen(s); for (int i = 0; i != len; i++) { cnt[s[i] - 'a']++; } for (int i = 0; i != 26; i++) { if (cnt[i]) { css[pc].cnt = cnt[i]; css[pc].alp = i; pc++; } } if (pc > n) { puts("-1"); return 0; } std::sort(css, css + pc, cmp); int x = len / n + bool(len % n); do { int tot = n; for (int i = pc - 1; i >= 0; i--) { int& nd = css[i].cnt; int ki = nd / x + bool(nd % x); tot -= ki; ks[i] = ki; } if (tot >= 0) { printf("%d\n", x); int sum = 0; for (int i = 0; i != pc; i++) for (int j = 0; j != ks[i]; j++) { putchar(css[i].alp + 'a'); sum++; } for (int i = 0; i != n - sum; i++) putchar('z'); puts(""); return 0; } } while (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; string s; int n; int H[30]; int ans; vector<pair<int, int> > V; string fin; bool poss(int cnt) { int sz = V.size(); int k = 0; string tmp; for (int i = 0; i < sz; i++) { int lol = V[i].first / cnt; if (V[i].first % cnt != 0) lol++; for (int j = 0; j < lol; j++) tmp += (V[i].second + 'a'); k += lol; } int lz = tmp.size(); while (lz < n) { tmp += 'a'; lz++; } if (k <= n) { ans = cnt; fin = tmp; return true; } return false; } int main() { cin >> s; cin >> n; int sz = s.size(); for (int i = 0; i <= 26; i++) H[i] = 0; for (int i = 0; i < sz; i++) { H[s[i] - 'a']++; } int c = 0; for (int i = 0; i < 26; i++) { if (H[i] != 0) { c++; V.push_back(make_pair(H[i], i)); } } if (c > n) { cout << "-1\n"; return 0; } sort(V.begin(), V.end()); int hi = V[V.size() - 1].first; int lo = 1; int mid; ans = hi + 1; while (lo <= hi) { mid = (lo + hi) / 2; if (poss(mid)) { hi = mid - 1; } else lo = mid + 1; } cout << ans << endl; cout << fin << 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 ch, s[1000]; int len, tot, l, r, i, j, mid, b[50], tl; int main() { scanf("%c", &ch); while (ch != '\n') { if (!b[ch - 'a' + 1]) tot++; b[ch - 'a' + 1]++; scanf("%c", &ch); } scanf("%d", &len); if (len < tot) { puts("-1"); return 0; } r = 0; for (i = 1; i <= 26; i++) if (b[i] > r) r = b[i]; l = 1; while (l < r) { tl = 0; mid = (l + r) >> 1; for (i = 1; i <= 26; i++) tl += (b[i] + mid - 1) / mid; if (tl > len) l = mid + 1; else r = mid; } tl = len; for (i = 0; i <= len; i++) s[i] = 'a'; for (i = 1; i <= 26; i++) { for (j = 1; j <= (b[i] + l - 1) / l; j++) s[len--] = 'a' + i - 1; } printf("%d\n", l); for (i = 1; i <= tl; i++) printf("%c", s[i]); 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 Main { public static void main(String []args) throws IOException { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false); solve(in, out); in.close(); out.close(); } static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); } static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); } private static int fact(int n) { int ans=1; for(int i=2;i<=n;i++) ans*=i; return ans; } public static int[] generatePrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, 2, n + 1, true); for (int i = 2; i * i <= n; i++) if (prime[i]) for (int j = i * i; j <= n; j += i) prime[j] = false; int[] primes = new int[n + 1]; int cnt = 0; for (int i = 0; i < prime.length; i++) if (prime[i]) primes[cnt++] = i; return Arrays.copyOf(primes, cnt); } static class FastScanner{ BufferedReader reader; StringTokenizer st; FastScanner(InputStream stream){reader=new BufferedReader(new InputStreamReader(stream));st=null;} String next(){while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;} st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();} String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} char nextChar() {return next().charAt(0);} int[] nextIntArray(int n) {int[] arr= new int[n]; int i=0;while(i<n){arr[i++]=nextInt();} return arr;} long[] nextLongArray(int n) {long[]arr= new long[n]; int i=0;while(i<n){arr[i++]=nextLong();} return arr;} int[] nextIntArrayOneBased(int n) {int[] arr= new int[n+1]; int i=1;while(i<=n){arr[i++]=nextInt();} return arr;} long[] nextLongArrayOneBased(int n){long[]arr= new long[n+1];int i=1;while(i<=n){arr[i++]=nextLong();}return arr;} void close(){try{reader.close();}catch(IOException e){e.printStackTrace();}} } /********* SOLUTION STARTS HERE ************/ private static void solve(FastScanner in, PrintWriter out){ String s = in.next(); int len = in.nextInt(); int n = s.length(); int l=1,r=n+1; int a[] = new int[26]; for(int i=0;i<n;i++) a[s.charAt(i)-'a']++; int cnt=0; for(int i:a) if(i!=0) cnt++; if(cnt > len) {out.println(-1);return;} while(l<r){ int mid = (l+r)>>1; int need=0; for(int i=0;i<26;i++){ need += (a[i]+mid-1)/mid; } if(need <= len) r = mid; else l = mid+1; } out.println(l);cnt=0; for(int i=0;i<26;i++){ int tmp = (a[i]+l-1)/l; for(int j=0;j<tmp;j++) {out.print((char)(i+'a'));cnt++;} } while(cnt<len) {out.print("a");cnt++;} out.println(); } /************* SOLUTION ENDS HERE **********/ }
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 const N = 1010, Z = 33; int n, r[Z], t[Z]; string s; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> s >> n; for (int i = 0; i < s.size(); i++) r[s[i] - 'a']++; for (int k = 1; k < N; k++) { int res = 0; for (int i = 0; i < 26; i++) { double so = r[i], ma = k; t[i] = ceil(so / ma); res += t[i]; } if (res > n) continue; cout << k << '\n'; for (int i = 0; i < 26; i++) { for (int j = 0; j < t[i]; j++) cout << char(i + 'a'); } for (int j = res; j < n; j++) cout << 'a'; return cout << '\n', 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; int main() { int n, fr[60]; char s[1010]; while (scanf(" %s %d", s, &n) == 2) { memset(fr, 0, sizeof(fr)); for (int i = 0; s[i]; i++) fr[s[i] - 'a']++; int res = -1; for (int qs = 1; qs <= 1000; qs++) { int us = 0; for (int i = 0; i < 26; i++) us += (fr[i] + qs - 1) / qs; if (us <= n) { res = qs; break; } } printf("%d\n", res); if (res != -1) { int prn = 0; for (int i = 0; i < 26; i++) for (int k = 0; k < (fr[i] + res - 1) / res; k++) { prn++; printf("%c", i + 'a'); } for (int i = 0; i < n - prn; 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; long long cnt[200]; int main() { cin >> s; long long n = s.size(); for (int i = 0; i < 200; i++) cnt[i] = 0; for (int i = 0; i < n; i++) ++cnt[s[i]]; long long len; cin >> len; long long ll = 1, rr = n + 1; while (ll < rr) { long long mid = (ll + rr) / 2; long long need = 0; for (int i = 97; i <= 122; i++) need += (cnt[i] + mid - 1) / mid; if (need <= len) rr = mid; else ll = mid + 1; } if (ll == n + 1) { cout << -1; return 0; } int mid = ll; cout << mid << endl; int need = 0; for (int i = 97; i <= 122; i++) { int needhere = (cnt[i] + mid - 1) / mid; for (int j = 0; j < needhere; j++) { need++; cout << (char)i; } } while (need < len) { cout << 'a'; need++; } 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> #pragma GCC optimize("Ofast") using namespace std; struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); } } ___; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " "; __f(comma + 1, args...); } signed main() { string s; cin >> s; map<char, long long int> mm; for (auto it : s) mm[it]++; long long int n; cin >> n; if (n < mm.size()) { cout << -1; return 0; } long long int lo = 1, hi = 2000; while (hi > lo) { long long int mid = (hi + lo) >> 1; long long int sum = 0; for (auto it : mm) { long long int x = it.second / mid; if (it.second % mid != 0) x++; sum += x; } if (sum <= n) hi = mid; else lo = mid + 1; } string ans; char xx; for (auto it : mm) { long long int x = it.second / lo; if (it.second % lo != 0) x++; for (long long int i = 0; i++ < x;) ans += it.first; xx = it.first; } long long int dd = ans.size(); for (long long int i = 0; i++ < n - dd;) ans += xx; cout << lo << '\n'; cout << ans << '\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[424242]; int cnt[424]; int main() { scanf("%s", s); int n = strlen(s); for (int i = 32; i < 256; i++) cnt[i] = 0; for (int i = 0; i < n; i++) cnt[s[i]]++; int len; scanf("%d", &len); int ll = 1, rr = n + 1; while (ll < rr) { int mid = (ll + rr) >> 1; int need = 0; for (int i = 32; i < 256; i++) need += (cnt[i] + mid - 1) / mid; if (need <= len) rr = mid; else ll = mid + 1; } if (ll == n + 1) { printf("%d\n", -1); return 0; } int mid = ll; printf("%d\n", mid); int need = 0; for (int i = 32; i < 256; i++) { int needhere = (cnt[i] + mid - 1) / mid; for (int j = 0; j < needhere; j++) { need++; putchar(i); } } while (need < len) { putchar('a'); need++; } 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.math.BigInteger; import java.util.*; import static java.util.Arrays.*; public class A { private static final int mod = (int)1e9+7; IOFast io = new IOFast(); public void run() throws IOException { final char[] cs = io.next(); final int n = io.nextInt(); int[] cnt = new int[256]; for(int i = 0; i < cs.length; i++) { cnt[cs[i]]++; } char[] ans = new char[n]; int kind = 0; for(int c : cnt) if(c != 0) kind++; if(kind > n) { io.out.println(-1); return; } int[][] xs = new int[kind][2]; for(int i = 0, j = 0; i < cnt.length; i++) { if(cnt[i] != 0) { xs[j][0] = i; xs[j][1] = 1; j++; } } for(int j = kind; j < n; j++) { int max = 0; int mi = 0; for(int i = 0; i < kind; i++) { final int v = (cnt[xs[i][0]] + xs[i][1] - 1) / xs[i][1]; if(max < v) { mi = i; max = v; } } xs[mi][1]++; } int val = 0; for(; ; ) { int cur = n; for(int i = 0; i < kind && cur > 0; i++) { final int x = Math.min(Math.min(cnt[xs[i][0]], xs[i][1]), cur); cnt[xs[i][0]] -= x; cur -= x; } if(cur == n) break; val++; } for(int i = 0, k = 0; i < kind; i++) { for(int j = 0; j < xs[i][1]; j++) { ans[k++] = (char)xs[i][0]; } } io.out.println(val); io.out.println(new String(ans)); } void main() throws IOException { // IOFast.setFileIO("rle-size.in", "rle-size.out"); try { run(); } catch (EndOfFileRuntimeException e) { } io.out.flush(); } public static void main(String[] args) throws IOException { new A().main(); } static class EndOfFileRuntimeException extends RuntimeException { private static final long serialVersionUID = -8565341110209207657L; } static public class IOFast { private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private PrintWriter out = new PrintWriter(System.out); void setFileIO(String ins, String outs) throws IOException { in = new BufferedReader(new FileReader(ins)); out = new PrintWriter(new FileWriter(outs)); } // private static final int BUFFER_SIZE = 50 * 200000; private static int pos, readLen; private static final char[] buffer = new char[1024 * 8]; private static final char[] str = new char[500000*8*2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() throws IOException { return Integer.parseInt(nextString()); } public long nextLong() throws IOException { return Long.parseLong(nextString()); } public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } } int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; } public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(str, len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); } public String nextString() throws IOException { return new String(next()); } public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(str, len, isSpace); return Arrays.copyOf(str, len); } public double nextDouble() throws IOException { return Double.parseDouble(nextString()); } } }
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; inline int intceil(int a, int b) { if (a % b) return a / b + 1; return a / b; } int main() { string s; int n, min_l = 0; cin >> s >> n; vector<int> frecv(26, 0), sol(26, 0); for (auto &&ch : s) { if (frecv[ch - 'a'] == 0) { min_l++; sol[ch - 'a'] = 1; } frecv[ch - 'a']++; } if (min_l > n) { cout << "-1\n"; return 0; } n -= min_l; int maxid, maxval; while (n > 0) { maxid = -1; maxval = -1; for (int i = 0; i < 26; i++) { if (sol[i]) if (intceil(frecv[i], sol[i]) > maxval) { maxval = intceil(frecv[i], sol[i]); maxid = i; } } sol[maxid]++; n--; } maxval = -1; for (int i = 0; i < 26; i++) { if (sol[i]) if (intceil(frecv[i], sol[i]) > maxval) { maxval = intceil(frecv[i], sol[i]); } } cout << maxval << '\n'; for (int i = 0; i < 26; i++) { for (int j = 0; j < sol[i]; j++) cout << static_cast<char>(i + '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
import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author pttrung */ public class A { public static void main(String[] args) { Scanner in = new Scanner(new InputStreamReader(System.in)); String val = in.next(); int n = in.nextInt(); int[] count = new int[26]; for (int i = 0; i < val.length(); i++) { count[val.charAt(i) - 'a']++; } int[] copy = Arrays.copyOf(count, 26); int[] hold = new int[26]; StringBuilder b = new StringBuilder(); for (int i = 0; i < 26; i++) { if (count[i] > 0) { count[i]--; b.append((char) (i + 'a')); hold[i]++; n--; } } if (n < 0) { System.out.println(-1); System.exit(0); } while (n > 0) { int max = 0; int index = 0; for (int i = 0; i < 26; i++) { if (count[i] > 0) { int temp = (int) Math.ceil((double) copy[i] / hold[i]); if(temp > max){ max = temp; index = i; } } } if (max == 0) { break; } b.append((char) (index + 'a')); count[index]--; hold[index]++; n--; } if (n > 0) { for (int i = 0; i < n; i++) { b.append('a'); } } int result = 0; for (int i = 0; i < 26; i++) { if (hold[i] != 0) { int temp = (int) Math.ceil((double) copy[i] / hold[i]); result = Math.max(result, temp); } } char[] arr = b.toString().toCharArray(); Arrays.sort(arr); System.out.println(result); System.out.println(arr); } }
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) { new Main().run(); } class InputScanner { BufferedReader br; StringTokenizer st; boolean eof; public InputScanner(String fileName) throws FileNotFoundException { br = new BufferedReader(new FileReader(fileName)); } public InputScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; break; } } return eof ? "-1" : st.nextToken(); } String nextLine() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; break; } } return eof ? "-1" : st.nextToken("\n"); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } void close() { try { br.close(); } catch (Exception e) { } } boolean isEOF() { return eof; } } InputScanner in; PrintWriter out; public void run() { Locale.setDefault(Locale.US); try { in = new InputScanner(System.in); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } String nextToken() { return in.nextToken(); } String nextLine() { return in.nextLine(); } int nextInt() { return in.nextInt(); } long nextLong() { return in.nextLong(); } double nextDouble() { return in.nextDouble(); } void print(String str) { out.print(str); out.flush(); } void println(String str) { out.println(str); out.flush(); } void print(int i) { out.print(i); out.flush(); } void println(int i) { out.println(i); out.flush(); } void print(long l) { out.print(l); out.flush(); } void println(long l) { out.println(l); out.flush(); } void print(double d) { out.print(d); out.flush(); } void println(double d) { out.println(d); out.flush(); } void solve() throws IOException { String s = nextLine(); int n = nextInt(); int[] a = new int[26]; int[] b = new int[26]; for (char c : s.toCharArray()) { a[c - 'a']++; } int dif = 0; for (int i = 0; i < 26; i++) { dif += a[i] > 0 ? 1 : 0; } if (dif > n) { println("-1"); return; } for (int i = 1;; i++) { int c = 0; Arrays.fill(b, 0); for (int j = 0; j < 26; j++) { b[j] = (a[j] + i - 1) / i; c += b[j]; } if (c <= n) { println(i); for (int j = 0; j < 26; j++) { while (b[j]-- > 0) { print(((char) (j + 'a')) + ""); } } n -= c; StringBuilder str = new StringBuilder(""); while (n-- > 0) { str.append("x"); } println(str.toString()); return; } } } }
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 = raw_input() N = len(s) n = int(raw_input()) letters = [0]*26 difLet = "" for character in s: index = ord(character)-ord('a') letters[index] += 1 sets = set(s) diff = len(sets) for j in sets: difLet += j if n < diff: print "-1" elif n == diff: print max(letters) print difLet else: if n > N: print 1 print s + "a"*(n-N) elif n == N: print 1 print s else: toAdd = dict() used = dict() for l in sets: freq = letters[ord(l)-ord('a')] toAdd[l] = freq used[l] = 1 while (len(difLet) < n): maxToAdd = "" val = 0 for l in sets: if toAdd[l]/float(used[l]) > val: val = toAdd[l]/float(used[l]) maxToAdd = l difLet += maxToAdd used[maxToAdd] += 1 req = 0 for k in set(s): a = s.count(k) b = difLet.count(k) tmpVal = 0 if a%b == 0: tmpVal = a/b else: tmpVal = a/b + 1 if tmpVal > req: req = tmpVal print req print difLet
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
public class A { private final static boolean autoflush = false; public A () { char [] C = sc.nextChars(); int N = sc.nextInt(); int [] L = new int [200]; int [] M = new int [200]; for (char c : C) { ++L[c]; if (M[c] == 0) { M[c] = 1; --N; } } if (N < 0) exit(-1); int res = 0; while (N >= 0) { int [] D = new int [200]; for (int c = 'a'; c <= 'z'; ++c) if (M[c] > 0) D[c] = ceil(L[c], M[c]); int b = 0; for (int c = 'a'; c <= 'z'; ++c) if (D[c] > D[b]) b = c; if (N > 0) ++M[b]; else res = D[b]; --N; } StringBuilder B = new StringBuilder(); for (char c = 'a'; c <= 'z'; ++c) for (int i = 0; i < M[c]; ++i) B.append(c); print(res); exit(B); } int ceil (int p, int q) { return (p + q - 1) / q; } //////////////////////////////////////////////////////////////////////////////////// private final static MyScanner sc = new MyScanner(); private static class MyScanner { public String next() { newLine(); return line[index++]; } public int nextInt() { return Integer.parseInt(next()); } public char [] nextChars() { return next ().toCharArray (); } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = readLine().split(" "); index = 0; } } } private static void print (Object o, Object... a) { printDelim(" ", o, a); } private static void printDelim (String delim, Object o, Object... a) { pw.println(build(delim, o, a)); } private static void exit (Object o, Object... a) { print(o, a); exit(); } private static void exit() { pw.close(); System.out.flush(); System.err.println("------------------"); System.err.println("Time: " + ((millis() - t) / 1000.0)); System.exit(0); } //////////////////////////////////////////////////////////////////////////////////// private static String build (String delim, Object o, Object... a) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : a) append(b, p, delim); return b.substring(delim.length()); } private static void append(StringBuilder b, Object o, String delim) { if (o.getClass().isArray()) { int L = java.lang.reflect.Array.getLength(o); for (int i = 0; i < L; ++i) append(b, java.lang.reflect.Array.get(o, i), delim); } else if (o instanceof Iterable<?>) for (Object p : (Iterable<?>)o) append(b, p, delim); else b.append(delim).append(o); } //////////////////////////////////////////////////////////////////////////////////// private static void start() { t = millis(); } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out, autoflush); private static long t; private static long millis() { return System.currentTimeMillis(); } public static void main (String[] args) { new A(); exit(); } }
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
//package codeforces.train; import java.io.*; public class ProblemA { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out)); int[] readInts() throws IOException { String[] strings = reader.readLine().split(" "); int[] ints = new int[strings.length]; for(int i = 0; i < ints.length; i++) { ints[i] = Integer.parseInt(strings[i]); } return ints; } long[] ttl = null; int txl = 0; long readLong() throws IOException { if(ttl == null || txl >= ttl.length) { ttl = readLongs(); txl = 0; } return ttl[txl++]; } long[] readLongs() throws IOException { String[] strings = reader.readLine().split(" "); long[] longs = new long[strings.length]; for(int i = 0; i < longs.length; i++) { longs[i] = Long.parseLong(strings[i]); } return longs; } int[] tt = null; int tx = 0; int readInt() throws IOException { if(tt == null || tx >= tt.length) { tt = readInts(); tx = 0; } return tt[tx++]; } char[] foo(int[] cnt, int n, int k) { char[] ret = new char[n]; int ix = 0; for(int i = 0; i < 26; i++) { for(int j = 0; j < cnt[i]; j += k) { if(ix == n) return null; ret[ix++] = (char)('a' + i); } } for(int i = ix; i < n; i++) ret[i] = 'a'; return ret; } void solve() throws IOException { int[] cnt = new int[26]; for(char c: reader.readLine().toCharArray()) { cnt[c - 'a']++; } int n = readInt(); int lo = 0, hi = 1 << 10; while(hi - lo > 1) { int med = lo + hi >> 1; if(foo(cnt, n, med) == null) lo = med; else hi = med; } char[] ans = foo(cnt, n, hi); if(ans == null) { writer.println(-1); } else { writer.println(hi); writer.println(ans); } writer.flush(); } void multiSolve() throws IOException { int n = readInts()[0]; for(int i = 0; i < n; i++) { if(i > 0) writer.println(); solve(); } } public static void main(String[] args) throws IOException{ new ProblemA().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; const int MAX = 30; int num[MAX], x[MAX]; char s[1000 + 10]; int main() { int n; while (cin >> s >> n) { int m = n; string a; memset(num, 0, sizeof num); int len = strlen(s), sum = 0; for (int i = 0; i < len; ++i) { if (!num[s[i] - 'a']) ++sum; ++num[s[i] - 'a']; } if (sum > n) cout << "-1"; else { for (int i = 0; i < 26; ++i) { if (num[i]) { a += (i + 'a'); --m; } x[i] = 1; } while (m) { int j = 0; for (int i = 0; i < 26; ++i) { if ((num[i] - 1) / x[i] + 1 > (num[j] - 1) / x[j] + 1) j = i; } a += (j + 'a'); ++x[j]; --m; } int j = 0; for (int i = 0; i < 26; ++i) { if ((num[i] - 1) / x[i] + 1 > (num[j] - 1) / x[j] + 1) j = i; } cout << (num[j] - 1) / x[j] + 1 << endl; 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
// Don't place your source in a package import java.util.*; import java.lang.*; import java.io.*; import java.math.*; // Please name your class Main public class Main { //static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); /*static int read() throws IOException { in.nextToken(); return (int) in.nval; } static String readString() throws IOException { in.nextToken(); return in.sval; }*/ static Scanner in = new Scanner(System.in); public static void main (String[] args) throws java.lang.Exception { //InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int T=1; for(int t=0;t<T;t++){ String s=Str(); int n=Int(); Solution sol=new Solution(); sol.solution(s,n); } out.flush(); } public static long Long(){ return in.nextLong(); } public static int Int(){ return in.nextInt(); } public static String Str(){ return in.next(); } } class Solution{ //constant variable final int MAX=Integer.MAX_VALUE; final int MIN=Integer.MIN_VALUE; //Set<Integer>adjecent[]; ////////////////////////////// public void fastprint(PrintWriter out,String s){ out.print(s); } public void fastprintln(PrintWriter out,String s){ out.println(s); } public void solution(String s,int n){ String res=""; int mn=-1; Set<Character>set=new HashSet<>(); int t[]=new int[26]; for(int i=0;i<s.length();i++){ t[s.charAt(i)-'a']++; set.add(s.charAt(i)); } if(set.size()>n){//always impossible msg("-1"); return; } int l=1,r=s.length(); while(l<=r){ int mid=l+(r-l)/2; int use[]=new int[26]; for(int i=0;i<t.length;i++){ if(t[i]==0)continue; if(t[i]%mid==0){ use[i]=t[i]/mid; } else{ use[i]=(t[i]/mid)+1; } } StringBuilder str=new StringBuilder(); for(int i=0;i<use.length;i++){ if(use[i]==0)continue; for(int j=0;j<use[i];j++){ char c=(char)(i+'a'); str.append(c+""); } } if(str.length()<=n){ for(int j=str.length();j<n;j++){ str.append("a"); } res=str.toString(); mn=mid; r=mid-1; } else{ l=mid+1; } } msg(mn+""); msg(res); } /*class LCA{ int p1,p2; boolean visit[]; int res=-1; boolean good=false; public LCA(int p1,int p2){ this.p1=p1;this.p2=p2; visit=new boolean[adjecent.length]; } public boolean dfs(int root){ visit[root]=true; List<Integer>next=adjecent[root]; boolean ans=false; if(root==p1||root==p2){ ans=true; } int cnt=0; for(int c:next){ if(visit[c])continue; boolean v=dfs(c); ans|=v; if(v)cnt++; } if(cnt==2){ if(!good){ good=true; res=root; } } else if(cnt==1){ if(!good&&(root==p1||root==p2)){ good=true; res=root; } } return ans; } }*/ /*public void delete(Dnode node){ Dnode pre=node.pre; Dnode next=node.next; pre.next=next; next.pre=pre; map.remove(node.v); } public void add(int v){ Dnode node=new Dnode(v); Dnode pre=tail.pre; pre.next=node; node.pre=pre; node.next=tail; tail.pre=node; map.put(v,node); } class Dnode{ Dnode next=null; Dnode pre=null; int v; public Dnode(int v){ this.v=v; } }*/ public String tobin(int i){ return Integer.toBinaryString(i); } public void reverse(int A[]){ List<Integer>l=new ArrayList<>(); for(int i:A)l.add(i); Collections.reverse(l); for(int i=0;i<A.length;i++){ A[i]=l.get(i); } } public void sort(int A[]){ Arrays.sort(A); } public void swap(int A[],int l,int r){ int t=A[l]; A[l]=A[r]; A[r]=t; } public long C(long fact[],int i,int j){ // C(20,3)=20!/(17!*3!) // take a/b where a=20! b=17!*3! if(j>i)return 1; if(j<=0)return 1; long mod=998244353; long a=fact[i]; long b=((fact[i-j]%mod)*(fact[j]%mod))%mod; BigInteger B= BigInteger.valueOf(b); long binverse=B.modInverse(BigInteger.valueOf(mod)).longValue(); return ((a)*(binverse%mod))%mod; } public long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } //map operation public void put(Map<Integer,Integer>map,int i){ if(!map.containsKey(i))map.put(i,0); map.put(i,map.get(i)+1); } public void delete(Map<Integer,Integer>map,int i){ map.put(i,map.get(i)-1); if(map.get(i)==0)map.remove(i); } /*public void tarjan(int p,int r){ if(cut)return; List<Integer>childs=adjecent[r]; dis[r]=low[r]=time; time++; //core for tarjan int son=0; for(int c:childs){ if(ban==c||c==p)continue; if(dis[c]==-1){ son++; tarjan(r,c); low[r]=Math.min(low[r],low[c]); if((r==root&&son>1)||(low[c]>=dis[r]&&r!=root)){ cut=true; return; } }else{ if(c!=p){ low[r]=Math.min(low[r],dis[c]); } } } }*/ //helper function I would use public void remove(Map<Integer,Integer>map,int i){ map.put(i,map.get(i)-1); if(map.get(i)==0)map.remove(i); } public void ascii(String s){ for(char c:s.toCharArray()){ System.out.print((c-'a')+" "); } msg(""); } public int flip(int i){ if(i==0)return 1; else return 0; } public boolean[] primes(int n){ boolean A[]=new boolean[n+1]; for(int i=2;i<=n;i++){ if(A[i]==false){ for(int j=i+i;j<=n;j+=i){ A[j]=true; } } } return A; } public void msg(String s){ System.out.println(s); } public void msg1(String s){ System.out.print(s); } public int[] kmpPre(String p){ int pre[]=new int[p.length()]; int l=0,r=1; while(r<p.length()){ if(p.charAt(l)==p.charAt(r)){ pre[r]=l+1; l++;r++; }else{ if(l==0)r++; else l=pre[l-1]; } } return pre; } public boolean isP(String s){ int l=0,r=s.length()-1; while(l<r){ if(s.charAt(l)!=s.charAt(r))return false; l++;r--; } return true; } public int find(int nums[],int x){//union find => find method if(nums[x]==x)return x; int root=find(nums,nums[x]); nums[x]=root; return root; } public int[] copy1(int A[]){ int a[]=new int[A.length]; for(int i=0;i<a.length;i++)a[i]=A[i]; return a; } public void printd1(double A[]){ for(double i:A)System.out.print(i+" "); System.out.println(); } public void print1(int A[]){ for(long i:A)System.out.print(i+" "); System.out.println(); } public void print1(long A[]){ for(long i:A)System.out.print(i+" "); System.out.println(); } public void print2(int A[][]){ for(int i=0;i<A.length;i++){ for(int j=0;j<A[0].length;j++){ System.out.print(A[i][j]+" "); }System.out.println(); } } public int min(int a,int b){ return Math.min(a,b); } public int[][] matrixdp(int[][] grid) { if(grid.length==0)return new int[][]{}; int res[][]=new int[grid.length][grid[0].length]; for(int i=0;i<grid.length;i++){ for(int j=0;j<grid[0].length;j++){ res[i][j]=grid[i][j]+get(res,i-1,j)+get(res,i,j-1)-get(res,i-1,j-1); } } return res; } public int get(int grid[][],int i,int j){ if(i<0||j<0||i>=grid.length||j>=grid[0].length)return 0; return grid[i][j]; } public int[] suffixArray(String s){ int n=s.length(); Suffix A[]=new Suffix[n]; for(int i=0;i<n;i++){ A[i]=new Suffix(i,s.charAt(i)-'a',0); } for(int i=0;i<n;i++){ if(i==n-1){ A[i].next=-1; }else{ A[i].next=A[i+1].rank; } } Arrays.sort(A); for(int len=4;len<A.length*2;len<<=1){ int in[]=new int[A.length]; int rank=0; int pre=A[0].rank; A[0].rank=rank; in[A[0].index]=0; for(int i=1;i<A.length;i++){//rank for the first two letter if(A[i].rank==pre&&A[i].next==A[i-1].next){ pre=A[i].rank; A[i].rank=rank; }else{ pre=A[i].rank; A[i].rank=++rank; } in[A[i].index]=i; } for(int i=0;i<A.length;i++){ int next=A[i].index+len/2; if(next>=A.length){ A[i].next=-1; }else{ A[i].next=A[in[next]].rank; } } Arrays.sort(A); } int su[]=new int[A.length]; for(int i=0;i<su.length;i++){ su[i]=A[i].index; } return su; } } //suffix array Struct class Suffix implements Comparable<Suffix>{ int index; int rank; int next; public Suffix(int i,int rank,int next){ this.index=i; this.rank=rank; this.next=next; } @Override public int compareTo(Suffix other) { if(this.rank==other.rank){ return this.next-other.next; } return this.rank-other.rank; } public String toString(){ return this.index+" "+this.rank+" "+this.next+" "; } } class Wrapper implements Comparable<Wrapper>{ int spf;int cnt; public Wrapper(int spf,int cnt){ this.spf=spf; this.cnt=cnt; } @Override public int compareTo(Wrapper other) { return this.spf-other.spf; } } class Node{//what the range would be for that particular node boolean state=false; int l=0,r=0; int ll=0,rr=0; public Node(boolean state){ this.state=state; } } class Seg1{ int A[]; public Seg1(int A[]){ this.A=A; } public void update(int left,int right,int val,int s,int e,int id){ if(left<0||right<0||left>right)return; if(left==s&&right==e){ A[id]+=val; return; } int mid=s+(e-s)/2; //[s,mid] [mid+1,e] if(left>=mid+1){ update(left,right,val,mid+1,e,id*2+2); }else if(right<=mid){ update(left,right,val,s,mid,id*2+1); }else{ update(left,mid,val,s,mid,id*2+1); update(mid+1,right,val,mid+1,e,id*2+2); } } public int query(int i,int add,int s,int e,int id){ if(s==e&&i==s){ return A[id]+add; } int mid=s+(e-s)/2; //[s,mid] [mid+1,e] if(i>=mid+1){ return query(i,A[id]+add,mid+1,e,id*2+2); }else{ return query(i,A[id]+add,s,mid,id*2+1); } } } class MaxFlow{ public static List<Edge>[] createGraph(int nodes) { List<Edge>[] graph = new List[nodes]; for (int i = 0; i < nodes; i++) graph[i] = new ArrayList<>(); return graph; } public static void addEdge(List<Edge>[] graph, int s, int t, int cap) { graph[s].add(new Edge(t, graph[t].size(), cap)); graph[t].add(new Edge(s, graph[s].size() - 1, 0)); } static boolean dinicBfs(List<Edge>[] graph, int src, int dest, int[] dist) { Arrays.fill(dist, -1); dist[src] = 0; int[] Q = new int[graph.length]; int sizeQ = 0; Q[sizeQ++] = src; for (int i = 0; i < sizeQ; i++) { int u = Q[i]; for (Edge e : graph[u]) { if (dist[e.t] < 0 && e.f < e.cap) { dist[e.t] = dist[u] + 1; Q[sizeQ++] = e.t; } } } return dist[dest] >= 0; } static int dinicDfs(List<Edge>[] graph, int[] ptr, int[] dist, int dest, int u, int f) { if (u == dest) return f; for (; ptr[u] < graph[u].size(); ++ptr[u]) { Edge e = graph[u].get(ptr[u]); if (dist[e.t] == dist[u] + 1 && e.f < e.cap) { int df = dinicDfs(graph, ptr, dist, dest, e.t, Math.min(f, e.cap - e.f)); if (df > 0) { e.f += df; graph[e.t].get(e.rev).f -= df; return df; } } } return 0; } public static int maxFlow(List<Edge>[] graph, int src, int dest) { int flow = 0; int[] dist = new int[graph.length]; while (dinicBfs(graph, src, dest, dist)) { int[] ptr = new int[graph.length]; while (true) { int df = dinicDfs(graph, ptr, dist, dest, src, Integer.MAX_VALUE); if (df == 0) break; flow += df; } } return flow; } } class Edge { int t, rev, cap, f; public Edge(int t, int rev, int cap) { this.t = t; this.rev = rev; this.cap = cap; } } /*class Seg{ int l,r; int min=Integer.MAX_VALUE; Seg left=null,right=null; int tree[]; public Seg(int l,int r){ this.l=l; this.r=r; if(l!=r){ int mid=l+(r-l)/2; if(l<=mid)left=new Seg(l,mid); if(r>=mid+1)right=new Seg(mid+1,r); if(left!=null)min=Math.min(left.min,min); if(right!=null)min=Math.min(right.min,min); }else{ min=tree[l]; } } public int query(int s,int e){ if(l==s&&r==e){ return min; } int mid=l+(r-l)/2; //left : to mid-1, if(e<=mid){ return left.query(s,e); } else if(s>=mid+1){ return right.query(s,e); }else{ return Math.min(left.query(s,mid),right.query(mid+1,e)); } } public void update(int index){ if(l==r){ min=tree[l]; return; } int mid=l+(r-l)/2; if(index<=mid){ left.update(index); }else{ right.update(index); } this.min=Math.min(left.min,right.min); } }*/ class Fenwick { int tree[];//1-index based int A[]; int arr[]; public Fenwick(int[] A) { this.A=A; arr=new int[A.length]; tree=new int[A.length+1]; int sum=0; for(int i=0;i<A.length;i++){ update(i,A[i]); } } public void update(int i, int val) { arr[i]+=val; i++; while(i<tree.length){ tree[i]+=val; i+=(i&-i); } } public int sumRange(int i, int j) { if(i>j)return 0; return pre(j+1)-pre(i); } public int pre(int i){ int sum=0; while(i>0){ sum+=tree[i]; i-=(i&-i); } return sum; } }
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 a[26], b[26]; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) a[s[i] - 'a']++; int n; cin >> n; int counter = 0; string result = ""; for (int i = 0; i < 26; i++) { if (a[i] != 0) { counter++; b[i] = 1; result += (char)'a' + i; } } if (counter > n) { cout << -1 << endl; return 0; } for (int i = 0; i < n - counter; i++) { int best = -1, bestId = -1; for (int j = 0; j < 26; j++) { if (a[j] == 0) continue; if ((a[j] / b[j] + (a[j] % b[j] != 0)) > best) { best = a[j] / b[j] + (a[j] % b[j] != 0); bestId = j; } } result += (char)bestId + 'a'; b[bestId]++; } int best = -1, bestId = -1; for (int j = 0; j < 26; j++) { if (a[j] == 0) continue; if ((a[j] / b[j] + (a[j] % b[j] != 0)) > best) { best = a[j] / b[j] + (a[j] % b[j] != 0); bestId = j; } } cout << best << endl << result << 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
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1005; int cnt[maxn], n; char str[maxn]; int main() { scanf("%s%d", str, &n); int m = strlen(str); for (int i = 0; i < m; i++) cnt[str[i] - 'a']++; int num = 0; for (int i = 0; i < 26; i++) if (cnt[i]) num++; if (num > n) printf("-1\n"); else { int ans = maxn; int l = 1, r = maxn; while (l <= r) { int m = (l + r) >> 1; int tmp = 0; for (int i = 0; i < 26; i++) { if (cnt[i] == 0) continue; tmp += cnt[i] / m + (cnt[i] % m > 0); } if (tmp <= n) { ans = m; r = m - 1; } else l = m + 1; } printf("%d\n", ans); int c = 0; for (int i = 0; i < 26; i++) { if (cnt[i] == 0) continue; int tmp = cnt[i] / ans + (cnt[i] % ans > 0); c += tmp; for (int j = 0; j < tmp; j++) printf("%c", i + 'a'); } c = n - c; while (c--) 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
import math s = raw_input().strip() n = int(raw_input().strip()) sl = list(s) sl.sort() letters = [0 for i in range(26)] for i in sl: letters[ord(i)-97] += 1 turns = [(99999 if letters[i] > 0 else -1) for i in range(26)] countinans = [0 for i in range(26)] used = 0 while used < n: x = turns.index(max(turns)) countinans[x] += 1 used += 1 turns[x] = math.ceil(float(letters[x]) / countinans[x]) if 99999 in turns: print -1 else: anss = "" for i in range(len(countinans)): for j in range(countinans[i]): anss += chr(i+97) numturns = 0 for i in turns: if i > numturns: numturns = i print int(numturns) print anss
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; inline int in() { int32_t x; scanf("%d", &x); return x; } inline string get() { char ch[1000010]; scanf("%s", ch); return ch; } const int MAX_LG = 21; const long long maxn = 1e6 + 10; const long long base = 29; const long long mod = 1e9 + 7; const long long INF = 1e9 + 100; bool mark[maxn]; long long cnt; long long maxi; long long c[30]; int32_t main() { string s = get(); long long n = in(); for (long long i = 0; i < s.size(); i++) { c[s[i] - 'a']++; if (!mark[s[i] - 'a']) mark[s[i] - 'a'] = true, cnt++; if (cnt > n) return cout << -1 << "\n", 0; } long long l = 1, r = 1e17, mid, best = -1; while (l <= r) { mid = (l + r) >> 1; long long rem = n; for (long long i = 0; i < 26; i++) { long long need = (c[i] + mid - 1) / mid; rem -= need; if (rem < 0) break; } if (rem >= 0) { best = mid, r = mid - 1; continue; } l = mid + 1; } cout << best << "\n"; string res = ""; for (long long i = 0; i < 26; i++) { long long need = (c[i] + best - 1) / best; for (long long j = 0; j < need; j++) res += char(i + 'a'); } for (long long j = 1; j <= n - (long long)res.size(); j++) cout << "a"; cout << res << "\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
import math from sys import stdin from math import ceil if __name__ == '__main__': s = input() n = int(input()) dictionary = {} for i in s: if i in dictionary: dictionary[i] = dictionary[i] + 1 else: dictionary[i] = 1 if len(dictionary) > n: print(-1) else: if len(s) < n: print(1) newS = s else: lengthS = len(s) // n newLength = len(s) while lengthS < newLength: mid = (lengthS + newLength) // 2 total = 0 for i in dictionary: total = total + ceil(dictionary[i] / mid) if total > n: lengthS = mid + 1 else: newLength = mid print(lengthS) newS = '' for i in dictionary: for j in range(ceil(dictionary[i] / lengthS)): newS = newS + i for i in range(n - len(newS)): newS = newS + s[0] print(newS)
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; int i, j, k, KK, R, NN; string in; map<char, int> MP; int mn; int main() { cin >> in; cin >> NN; mn = 0; for (int i = 0; i < in.size(); i++) { if (MP[in[i]] <= 0) mn += 1; MP[in[i]] += 1; } if (mn > NN) { cout << -1 << endl; return 0; } int low = 1, high = in.size() + 1; int mid = 0; while (low < high) { int mid = (low + high) >> 1; int need = 0; for (char ch = 'a'; ch <= 'z'; ch++) { if (MP[ch] > 0) { need += (MP[ch] + mid - 1) / mid; } } if (need <= NN) high = mid; else low = mid + 1; } mid = high; cout << mid << endl; int need = 0; for (char ch = 'a'; ch <= 'z'; ch++) { if (MP[ch] > 0) { int raod = (MP[ch] + mid - 1) / mid; for (int i = 0; i < raod; i++) { putchar(ch); need++; } } } if (need < NN) { for (int i = need; i < NN; i++) { putchar('c'); } } return 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 n, ans, c[26]; char s[1000]; bool Check(int x) { int ans = 0; for (int i = 0; i <= 25; i++) ans += (c[i] + x - 1) / x; return ans <= n; } void Print(int x) { string res = ""; for (int i = 0; i <= 25; i++) { int l = (c[i] + x - 1) / x; char ch = 'a' + i; for (int j = 1; j <= l; j++) res += ch; } while (res.size() < n) res += 'a'; puts(res.c_str()); } int main() { scanf("%s", s); int len = strlen(s); scanf("%d", &n); for (int i = 0; i <= len - 1; i++) c[s[i] - 'a']++; int left = 1, right = len; ans = -1; while (left <= right) { int mid = (left + right) >> 1; if (Check(mid)) { ans = mid; right = mid - 1; } else left = mid + 1; } printf("%d\n", ans); if (ans == -1) return 0; Print(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
ch = raw_input() N = (int)( raw_input() ) cnt = [] need = [] for _ in range(26) : cnt.append( 0 ) need.append( 0 ) for c in range( len(ch) ) : cnt[ ord(ch[c])- ord('a') ] += 1 def is_ok( a ): all = 0 for c in range( 26 ): need[c] = ( cnt[c] + a - 1 ) / a all += need[c] if all <= N : return True else: return False ans = 1 while (ans<=1000) and (is_ok( ans )==False): ans += 1 if ans > 1000: print "-1" else: print ans chans = "" for c in range(26): for _ in range( need[c] ): chans += chr(c + ord('a')) N -= 1 last = c while N != 0 : chans += chr( last + ord('a') ) N -= 1 print chans
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
from collections import defaultdict s = input() n = int(input()) if len(set(s)) > n: print(-1) else: hist = defaultdict(int) for c in s: hist[c] += 1 for ans in range(1, 2000): cur_need = 0 for c in hist: # ans * t >= hist[c] # t >= hist[c] // ans cur_need += (hist[c] + ans - 1) // ans if cur_need > n: continue print(ans) s = '' for c in hist: s += c * ((hist[c] + ans - 1) // ans) s += (n - len(s)) * 'a' print(s) 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.awt.Point; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class A { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = null; char[] output = br.readLine().trim().toCharArray(); int N = Integer.parseInt(br.readLine().trim()); HashSet<Character> hax = new HashSet<>(); final long[] total = new long[26]; final long[] used = new long[26]; PriorityQueue<Thing> maxHeap = new PriorityQueue<>(1, new Comparator<Thing>() { @Override public int compare(Thing o1, Thing o2) { // Do percentage used instead. return Double.compare(used[o1.x - 'a'] * 1.0 / total[o1.x - 'a'], used[o2.x - 'a'] * 1.0 / total[o2.x - 'a']); } }); for (Character c : output) { int val = c - 'a'; total[val]++; hax.add(c); } if (hax.size() > N) { out.println(-1); } else { for (Character c : hax) { maxHeap.add(new Thing(c, 0)); } StringBuilder ans = new StringBuilder(); while (ans.length() < N) { Thing tmp = maxHeap.poll(); ans.append((char) tmp.x); used[tmp.x - 'a']++; maxHeap.add(new Thing(tmp.x, 0)); } Thing tmp = maxHeap.poll(); out.println((long) Math.ceil(1 * 1.0 / (used[tmp.x - 'a'] * 1.0 / total[tmp.x - 'a']))); out.println(ans.toString()); } br.close(); out.close(); } private static class Thing { char x; long y; public Thing(char x, long y) { this.x = x; this.y = y; } public int hashCode() { return (int) (this.y * 3 + this.x * 5); } public boolean equals(Object o) { return this.toString().equals(o.toString()); } public String toString() { return this.x + ":" + this.y; } } }
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.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class A { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = null; char[] output = br.readLine().trim().toCharArray(); int N = Integer.parseInt(br.readLine().trim()); HashSet<Character> hax = new HashSet<>(); final long[] list = new long[26]; PriorityQueue<Thing> maxHeap = new PriorityQueue<>(1, new Comparator<Thing>() { @Override public int compare(Thing o1, Thing o2) { // Do percentage used instead. return (int) ((list[o1.x - 'a'] - o1.y) * list[o2.x - 'a'] - (list[o2.x - 'a'] - o2.y) * list[o1.x - 'a']); } }); for (Character c : output) { int val = c - 'a'; list[val]++; hax.add(c); Thing tmp = new Thing(c, list[val] - 1); if (maxHeap.contains(tmp)) { maxHeap.remove(tmp); } maxHeap.add(new Thing(c, list[val])); } if (hax.size() > N) { out.println(-1); } else { StringBuilder ans = new StringBuilder(); for (Character c : hax) { ans.append(c); maxHeap.remove(new Thing(c, list[c - 'a'])); maxHeap.add(new Thing(c, list[c - 'a'] - 1)); } while (ans.length() < N) { if (maxHeap.isEmpty()) { ans.append('a'); } else { Thing tmp = maxHeap.poll(); ans.append((char) tmp.x); maxHeap.add(new Thing(tmp.x, tmp.y - 1)); } } long[] list2 = new long[26]; int count = 0; while (true) { for (int i = 0; i < ans.length(); i++) { list2[ans.charAt(i) - 'a']++; } count++; boolean done = true; for (int i = 0; i < 26; i++) { if (list2[i] < list[i]) { done = false; break; } } if (done) break; } out.println(count); out.println(ans.toString()); } br.close(); out.close(); } private static class Thing { char x; long y; public Thing(char x, long y) { this.x = x; this.y = y; } public int hashCode() { return (int) (this.y * 3 + this.x * 5); } public boolean equals(Object o) { return this.toString().equals(o.toString()); } public String toString() { return this.x + ":" + this.y; } } }
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 N = 1e5 + 5; long long n; string s; string ans = ""; map<char, long long> m; bool check(long long x) { ans = ""; set<pair<long long, char> > st; for (auto it : m) { ans += it.first; if (it.second - x > 0) st.insert({it.second - x, it.first}); } while (ans.size() < n) { if (!st.size()) break; auto it = *st.begin(); st.erase(it); ans += it.second; it.first -= x; if (it.first > 0) st.insert({it.first, it.second}); } while (ans.size() < n) ans += 'a'; return (st.size() == 0); } long long binsearch(long long lo, long long hi) { while (lo < hi) { long long mid = (lo + hi) / 2; if (check(mid)) hi = mid; else lo = mid + 1; } check(lo); return lo; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s >> n; for (auto &it : s) m[it]++; if (n < m.size()) { cout << -1; return 0; } long long answer = binsearch(1, 1e5); cout << answer << "\n"; cout << 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; long long countBits(long long n) { long long cnt = 0; while (n) { cnt++; n >>= 1; } return cnt; } long long power(long long a, long long n, long long d) { long long res = 1; while (n) { if (n & 1 != 0) { res = ((res % d) * ((a % d + d) % d)) % d; n--; } else { a = ((((a % d + d) % d)) * (((a % d + d) % d))) % d; n >>= 1; } } return res; } void SieveOfEratosthenes(long long N, vector<long long>& s) { s[0] = 0; s[1] = 0; s[2] = 1; s[3] = 1; bool prime[N + 1]; memset(prime, true, sizeof(prime)); for (long long p = 2; p * p <= N; p++) { if (prime[p] == true) { for (long long i = p * p; i <= N; i += p) { prime[i] = false; s[i] = 0; } } } } struct not_digit { bool operator()(const char c) { return c != ' ' && !std::isdigit(c); } }; void extractNum(string str, vector<long long>& numbers) { not_digit not_a_digit; string::iterator end = std::remove_if(str.begin(), str.end(), not_a_digit); string all_numbers(str.begin(), end); stringstream second(all_numbers); for (long long i = 0; second >> i;) { numbers.push_back(i); } } void getWords(string str, vector<string>& vec, unordered_set<string>& Set) { string word; stringstream iss(str); while (iss >> word) { Set.insert(word); vec.push_back(word); } } bool isPower(long long n) { if (n <= 1) return true; for (long long x = 2; x <= sqrt(n); x++) { unsigned p = x; while (p <= n) { p *= x; if (p == n) return true; } } return false; } string decimalToBinary(long long n) { string s = bitset<64>(n).to_string(); const auto loc1 = s.find('1'); if (loc1 != string::npos) return s.substr(loc1); return "0"; } double slope(long long x1, long long y1, long long x2, long long y2) { double angle; angle = (y2 - y1) / (double)(x2 - x1); return angle; } bool isPrime(long long n) { if (n == 1) return false; else { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } long long Power(long long a, long long n) { long long res = 1; if (n == 0) return 1; while (n) { if (n & 1 != 0) { res *= a; n--; } else { a *= a; n >>= 1; } } return res; } long long Mod(long long x) { return ((x % 1000000007 + 1000000007) % 1000000007); } long long add(long long a, long long b) { return Mod(Mod(a) + Mod(b)); } long long mul(long long a, long long b) { return Mod(Mod(a) * Mod(b)); } const long long N = 2e5 + 2; long long fact[N]; void precalc() { fact[0] = 1; for (long long i = 1; i < N; i++) { fact[i] = mul(fact[i - 1], i); } } long long inv(long long x) { return pow(x, 1000000007 - 2); } long long divide(long long a, long long b) { return mul(a, inv(b)); } long long nCr(long long n, long long r) { return divide(fact[n], mul(fact[r], fact[n - r])); } bool isPalindrome(string s) { long long i, len = s.size(); for (i = 0; i < len / 2; i++) { if (s[i] != s[len - i - 1]) return 0; } return 1; } void primeFactors(long long n, vector<long long>& vec) { while (n % 2 == 0) { vec.push_back(2); n = n / 2; } for (long long i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { vec.push_back(i); n = n / i; } } if (n > 2) vec.push_back(n); } void solve() { long long i, n, q, value, j, copyN, temp; string s; cin >> s; cin >> n; copyN = n; map<char, long long> m; for (i = 0; i < s.size(); i++) { m[s[i]]++; } string ans = ""; if (n >= s.size()) { for (i = 0; i < n; i++) { if (i < s.size()) ans.push_back(s[i]); else ans.push_back('a'); } cout << "1" << endl; cout << ans << endl; return; } if (m.size() > n) { cout << "-1" << endl; return; } temp = 0; ans.clear(); for (i = 1; i <= s.size(); i++) { for (auto it = m.begin(); it != m.end(); it++) { temp += ceil((double)(it->second) / i); } if (temp <= copyN) break; temp = 0; } temp = i; cout << temp << endl; for (auto it = m.begin(); it != m.end(); it++) { long long value = ceil(((double)(it->second) / temp)); for (i = 0; i < value; i++) ans.push_back(it->first); } if (ans.size() < n) ; { while (ans.size() < n) ans.push_back('a'); } cout << ans << endl; return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
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 cnt[35], n; char ch[1010]; bool check(int k) { int ans = 0; for (int i = 0; i < 26; i++) { int t = cnt[i]; if (t) { ans += t / k; t %= k; if (t) ans++; } } if (ans <= n) return true; return false; } int len; int wotamajingranshuiguotoule() { int left = 1, right = len; while (left < right) { int mid = (left + right) >> 1; if (!check(mid)) left = mid + 1; else right = mid; } return left; } char ff[1010]; int main() { cin >> ch >> n; int cc = 0; len = strlen(ch); for (int i = 0; i < len; i++) { int tt = ch[i] - 'a'; if (!cnt[tt]) cc++; cnt[tt]++; } if (cc > n) { puts("-1"); return 0; } int ans = wotamajingranshuiguotoule(); int p = 0; for (int i = 0; i < 26; i++) { while (cnt[i] > 0) { ff[p++] = 'a' + i; cnt[i] -= ans; } } while (p < n) ff[p++] = 'a'; ff[p] = '\0'; printf("%d\n", ans); printf("%s\n", ff); 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[1003]; char res[1003]; int n; int coun[33]; int coun3[33]; int main() { scanf("%s", s); scanf("%d", &n); int i, j; for (i = 0; s[i]; i++) coun[s[i] - 'a']++; int coun2 = 0; for (i = 0; i < 30; i++) if (coun[i] > 0) coun2++; if (coun2 > n) { printf("-1\n"); return 0; } int l, r, re; l = 0; r = 1003; int sum = 0; while (r - l > 1) { sum = 0; re = ((l + r) >> 1); for (j = 0; j < 30; j++) { sum += coun[j] / re; if (coun[j] % re != 0) sum++; } if (sum > n) l = re; else r = re; } for (j = 0; j < 30; j++) { coun3[j] = coun[j] / r; if (coun[j] % r != 0) coun3[j]++; } i = 0; for (j = 0; j < 30; j++) { while (coun3[j] > 0) { res[i++] = j + 'a'; coun3[j]--; } } printf("%d\n", r); while (i < n) res[i++] = 'a'; printf("%s\n", res); }
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 = 2e5 + 5; map<char, int> mp, curmp; vector<pair<int, char> > v, resvec; int main() { int n; string s; cin >> s >> n; for (auto x : s) mp[x]++; if (n < mp.size()) return cout << -1, 0; string ress; int res = -1; if (n >= s.size()) { ress = s; for (int i = s.size(); i < n; i++) ress += 'a'; res = 1; } else { int lo = 1, hi = 1e9; int tempn = n; while (lo <= hi) { int mid = lo + (hi - lo) / 2; for (auto x : mp) v.push_back({(int)ceil(1.0 * x.second / mid), x.first}), tempn -= (int)ceil(1.0 * x.second / mid); if (tempn >= 0) { res = mid; hi = mid - 1; resvec = v; } else lo = mid + 1; tempn = n; v.clear(); } for (auto x : resvec) for (int j = 0; j < x.first; j++) ress += x.second; int rem = n - ress.size(); for (int i = 0; i < rem; i++) ress += 'a'; } cout << res << endl << ress; 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 has[26]; long long check(long long k) { long long ans(0); for (long long i = 0; i < 26; i++) { ans += ceil(double(has[i]) / k); } return ans; } int main() { string s; long long n, ans(-1); cin >> s >> n; if (n >= s.length()) { cout << 1 << endl; long long p = n / s.length(); long long x = n % s.length(); while (p--) cout << s; while (x--) cout << s[0]; return 0; } long long c(0); for (long long i = 0; i < s.length(); i++) { if (has[s[i] - 'a'] == 0) c++; has[s[i] - 'a']++; } if (n < c) return cout << -1, 0; long long low = 1, high = 1000; while (low <= high) { long long mid = (low + high) / 2; if (check(mid) <= n) { ans = mid; high = mid - 1; } else { low = mid + 1; } } cout << ans << endl; if (ans == -1) return 0; long long x(0); for (long long i = 0; i < 26; i++) { if (has[i]) { long long p = ceil(double(has[i]) / ans); x += p; while (p--) { cout << char('a' + i); } } } x = n - x; while (x--) cout << s[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
import java.util.*; public class CF335A { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { char []input = sc.nextLine().toCharArray(); int n = Integer.parseInt(sc.nextLine()); int []inputCharFreq = new int[26]; int uniqueChars = 0; for (int i = 0; i < input.length; i++) { int index = input[i] - 'a'; if (inputCharFreq[index] == 0) uniqueChars++; inputCharFreq[index]++; } if (n < uniqueChars) { System.out.println(-1); return; } char []output = new char[n]; int []outputCharFreq = new int[26]; int i = 0, j = 0; for (; i < uniqueChars; i++) { while (inputCharFreq[j] == 0) j++; output[i] = (char) (j + 'a'); outputCharFreq[j++]++; } if (n >= input.length) { // Fill remaining characters j = 0; for (; i < input.length; i++) { while (j < 26 && inputCharFreq[j] - outputCharFreq[j] <= 0) j++; if (j >= 26) break; output[i] = (char) (j + 'a'); outputCharFreq[j]++; } // Fill in dummy characters for (; i < n; i++) { output[i] = 'a'; outputCharFreq[0]++; } } else { for (; i < n; i++) { double maximumRatio = 0.0; int maxRatioIndex = 0; for (j = 0; j < uniqueChars; j++) { int index = output[j] - 'a'; if (outputCharFreq[index] == 0) continue; double ratio = inputCharFreq[index] / (double) outputCharFreq[index]; if (ratio > maximumRatio) { maximumRatio = ratio; maxRatioIndex = index; } } output[i] = (char) (maxRatioIndex + 'a'); outputCharFreq[maxRatioIndex]++; } } double maxRatio = 0; for (i = 0; i < uniqueChars; i++) { int index = output[i] - 'a'; double ratio = inputCharFreq[index] / (double) outputCharFreq[index]; maxRatio = Math.max(ratio, maxRatio); } System.out.println((int) Math.ceil(maxRatio)); System.out.println(output); } }
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; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const int INF = 1 << 29; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } int N, num[30], cnt; string s; int main() { cin >> s >> N; for (int i = 0; i < (s.size()); i++) { int c = s[i] - 'a'; if (!num[c]) ++cnt; ++num[c]; } if (cnt > N) { cout << -1 << endl; return 0; } int be = 1, en = s.size(), res = s.size(); while (be <= en) { int m = (be + en) / 2; cnt = 0; for (int i = 0; i < (30); i++) cnt += (num[i] + m - 1) / m; if (cnt <= N) { res = m; en = m - 1; } else be = m + 1; } cout << res << endl; string s2; for (int i = 0; i < (30); i++) { char c = 'a' + i; cnt = (num[i] + res - 1) / res; while (cnt--) s2 += c; } while (s2.size() < N) s2 += 'x'; cout << s2 << 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
def main(): from sys import stdin s = stdin.readline()[:-1] n = int(stdin.readline()) d = {} for c in s: if c in d: d[c] += 1 else: d[c] = 1 l = 1 r = max(map(lambda x:x[1],list(d.iteritems()))) + 1 from math import ceil def check(t): res = '' for it in d.iteritems(): res = res + it[0] * int(ceil(1.0 * it[1] / t)) if len(res) > n: return None else: left = n - len(res) return res + 'a'*left while l < r: mid = (l + r) / 2 if check(mid): r = mid else: l = mid + 1 if check(l): print l print check(l) else: print -1 if __name__ == '__main__': main()
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
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class A { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { String goal = nextToken(); int[] count = new int[26]; for(int i = 0; i < goal.length(); i++) { count[goal.charAt(i)-'a']++; } int numD = 0; for(int out: count) { if(out > 0) { numD++; } } int n = readInt(); if(numD > n) { pw.println(-1); } else { int min = 1; int max = 10000; while(min != max) { int mid = (min+max)/2; int use = 0; for(int out: count) { int must = (out+mid-1)/mid; use += must; } if(use <= n) { max = mid; } else { min = mid+1; } } pw.println(min); StringBuilder sb = new StringBuilder(); for(int i = 0; i < count.length; i++) { int use = (count[i] + min-1) / min; while(use-- > 0) { sb.append((char)('a'+i)); } } while(sb.length() < n) { sb.append("z"); } pw.println(sb); } } pw.close(); } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine().trim()); } 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; long long a[28], b[28], n; bool check(long long val) { long long x = 0, y, i; for (i = 0; i < 26; i++) { if (a[i] > 0) { y = (a[i] % val == 0) ? a[i] / val : a[i] / val + 1; x += y; } } if (x > n) return false; else return true; } int main() { string s, s1 = ""; long long i, len, x = 0, y = 0, ans = 0, low, high, mid; cin >> s >> n; len = s.length(); for (i = 0; i < len; i++) a[s[i] - 'a']++; for (i = 0; i < 26; i++) { if (a[i] > 0) x++; } if (n < x) { cout << "-1\n"; return 0; } low = 1; high = 1000; while (high - low >= 10) { mid = (low + high) >> 1; if (check(mid)) { high = mid; } else { low = mid + 1; } } for (i = low; i <= high; i++) { if (check(i)) { ans = i; break; } } cout << ans << "\n"; for (i = 0; i < 26; i++) { if (a[i] > 0) { x = (a[i] % ans == 0) ? a[i] / ans : a[i] / ans + 1; while (x--) s1 += char(i + 'a'); } } while (s1.length() < n) { s1 += 'a'; } cout << s1 << "\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 cnt[1000]; char str[1111]; int len; int isok(int m, int n) { int l = n, te; for (int i = 'a'; i <= 'z'; i++) { if (cnt[i] % m == 0) te = cnt[i] / m; else te = cnt[i] / m + 1; l -= te; if (l < 0) return 0; } return 1; } void put(int m, int n) { int l = n, te; for (int i = 'a'; i <= 'z'; i++) { if (cnt[i] % m == 0) te = cnt[i] / m; else te = cnt[i] / m + 1; l -= te; while (te--) printf("%c", i); } while (l--) printf("a"); cout << endl; } int main(int argc, const char *argv[]) { int n; while (~scanf("%s %d", str, &n)) { memset(cnt, 0, sizeof(cnt)); len = strlen(str); for (int i = 0; i < len; i++) { cnt[str[i]]++; } int l = 1, r = len, ans = -1; while (l <= r) { int m = (l + r) / 2; if (isok(m, n)) r = m - 1, ans = m; else l = m + 1; } cout << ans << endl; if (ans != -1) put(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
#include <bits/stdc++.h> using namespace std; map<char, int> m; string s; int n; string ans; bool ok(int a) { ans.clear(); string k; for (auto p : m) { k += string((p.second + a - 1) / a, p.first); } if (k.size() > n) return false; int b = k.size(); for (int i = 0; i < n - b; i++) k += s[0]; ans = k; return true; } int main() { cin >> s; cin >> n; for (auto p : s) { m[p]++; } int l = 1, r = 1000; int cnt = 400; while (cnt--) { int mid = (l + r) / 2; if (ok(mid)) r = mid; else l = mid + 1; } if (!ok(l)) cout << -1; else { cout << l << "\n"; cout << 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
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010, ALPH = 30; string S; int N; map<char, int> freq; priority_queue<pair<int, char> > pq; int times[ALPH]; int main() { cin >> S >> N; for (int i = 0; i < S.size(); i++) freq[S[i]]++; if (freq.size() > N) { cout << -1 << endl; return 0; } string sheet; for (map<char, int>::iterator it = freq.begin(); it != freq.end(); it++) { sheet += it->first; pq.push(make_pair(it->second, it->first)); times[it->first - 'a']++; } while (sheet.size() < N) { pair<int, char> top = pq.top(); pq.pop(); sheet += top.second; times[top.second - 'a']++; pq.push(make_pair((freq[top.second] + times[top.second - 'a'] - 1) / times[top.second - 'a'], top.second)); } cout << pq.top().first << endl; cout << 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
import java.io.*; import java.util.*; 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); char[] s = sc.next().toCharArray(); int n = sc.nextInt(); int[] f = new int[26]; for(char c: s) f[c - 'a']++; int distinct = 0; for(int x: f) if(x != 0) ++distinct; if(distinct > n) out.println(-1); else { int sheets = 1; while(true) { int chs = 0; for(int x: f) chs += (x + sheets - 1) / sheets; if(chs <= n) break; ++sheets; } StringBuilder sb = new StringBuilder(); for(int i = 0; i < 26; ++i) { int occ = (f[i] + sheets - 1) / sheets; while(occ-->0) sb.append((char) (i + 'a')); } while(sb.length() < n) sb.append("a"); out.println(sheets); out.println(sb); } 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 { return Double.parseDouble(next()); } 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
import java.io.*; import java.util.*; public class Solution { BufferedReader in; StringTokenizer str; PrintWriter out; String SK; String next() throws IOException { while ((str == null) || (!str.hasMoreTokens())) { SK = in.readLine(); if (SK == null) return null; str = new StringTokenizer(SK); } return str.nextToken(); } String nexts()throws IOException { return in.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } char[] charArray() throws IOException{ return next().toCharArray(); } public static void main(String[] args) throws IOException { new Solution().run(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter("output.txt"); solve(); out.close(); } void solve() throws IOException { String st=nexts(); int n=nextInt(); Ch ch[]=new Ch[26]; for (int i = 0; i < 26; i++) { ch[i]=new Ch(i,0,0); } for (int i = 0; i < st.length(); i++) { ch[st.charAt(i)-'a'].c++; } int count=0; for (int i = 0; i < 26; i++) { if(ch[i].c>0)count++; } if(n<count){ out.println(-1); return; } Arrays.sort(ch); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 26; i++) { if(ch[i].c==0){ break; } sb.append((char)(ch[i].ind+'a')); ch[i].used++; } Ch ch2[]=new Ch[26]; for (int i = 0; i < 26; i++) { ch2[i]=new Ch(ch[i].ind,0,0); if(ch[i].c>0)ch2[i]=new Ch(ch[i].ind,ch[i].c-1,ch[i].used); } int l=n-count; int j=0; while(l>0){ double max=0.0; j=0; for (int i = 0; i < 26; i++) { if(ch2[i].used==0)continue; double g=(double)ch[i].c/(double)ch2[i].used; if(max<g){ max=g; j=i; } } ch2[j].used++; l--; sb.append((char)(ch2[j].ind+'a')); } int res=0; for (int i = 0; i < 26; i++) { int g=(int)Math.ceil((double)ch[i].c/ch2[i].used); if(ch2[i].used>0 && res<g){ res=g; } } out.println(res); out.println(sb.toString()); } } class Ch implements Comparable<Ch>{ int c; int ind; int used=0; public Ch(int i, int cc, int u){ c=cc; ind=i; used=u; } public int compareTo(Ch t){ if(c>t.c)return -1; else return 1; } }
JAVA