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 |
---|---|---|---|---|---|
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int PRIME = 999983;
const int MOD = 1000000007;
const int MULTI = 1000000007;
const long double EPS = 1e-10;
inline bool isodd(int x) { return x & 1; }
inline bool isodd(long long x) { return x & 1; }
int main(void) {
int n, k;
scanf("%d%d", &n, &k);
if (n * (n - 1) / 2 <= k)
puts("no solution");
else {
for (int i = 0; i < n; i++) {
printf("%d %d\n", 0, i);
}
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
if (n * (n - 1) / 2 <= k) {
return cout << "no solution", 0;
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < n; j++) {
cout << i << " " << j << endl;
}
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | n, k = map(int, raw_input().split())
if k >= n*(n-1)/2: print 'no solution'
else:
for i in xrange(n):
print '0 {0}'.format(i) | PYTHON |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
private static StringTokenizer tokenizer;
private static BufferedReader bf;
private static PrintWriter out;
private static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
@SuppressWarnings("unused")
private static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private static String nextToken() throws IOException {
while(tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(bf.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt(); int k = nextInt();
if(k < (n*(n-1))/2) {
for(int i = 1; i <= n; i++) {
out.println(0 + " " + i);
}
}
else out.println("no solution");
out.close();
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException,InterruptedException{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),k=sc.nextInt();
pair[] arr=new pair[n];
for (int i = 0; i < n; i++) {
arr[i]=new pair(0,i);
}
Arrays.sort(arr);
int tot=0;
double d=Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
++tot;
if(arr[j].x-arr[i].x>=d) break;
d=Math.min(d,distance(arr[i],arr[j]));
}
}
// pw.println(tot);
if (tot<=k) {
pw.println("no solution");
}else {
for (int i = 0; i < n; i++) {
pw.println(arr[i]);
}
}
pw.close();
}
static double distance(pair a,pair b) {
return Math.sqrt((a.x*a.x-b.x*b.x)+(a.y*a.y-b.y*b.y));
}
static PrintWriter pw=new PrintWriter(System.out);
static class pair implements Comparable<pair> {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair)o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Double(x).hashCode() * 31 + new Double(y).hashCode();
}
public int compareTo(pair other) {
if(this.x==other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if(this.y==other.y) return this.z - other.z;
else return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
static class Scanner{
BufferedReader br=null;
StringTokenizer tk=null;
public Scanner(){
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException{
while(tk==null || !tk.hasMoreTokens())
tk=new StringTokenizer(br.readLine());
return tk.nextToken();
}
public int nextInt() throws NumberFormatException, IOException{
return Integer.valueOf(next());
}
public double nextDouble() throws NumberFormatException, IOException{
return Double.valueOf(next());
}
}
public static void main(String args[]) throws NumberFormatException, IOException{
Scanner sc = new Scanner();
int N = sc.nextInt();
int K = sc.nextInt();
int max = (N*(N - 1))/2;
if (max <= K){
System.out.println("no solution");
return;
}
for(int i = 0; i < N; i++)
System.out.println("0 "+i);
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.io.*;
public class A {
private BufferedReader reader;
public static void main(String[] args) throws IOException
{
new A().run();
}
StreamTokenizer in;
PrintWriter out;
double nextDouble() throws IOException
{
in.nextToken();
return in.nval;
}
int nextInt() throws IOException
{
in.nextToken();
return (int)in.nval;
}
long nextLong() throws IOException
{
in.nextToken();
return (long)in.nval;
}
String nextString() throws IOException
{
in.nextToken();
return (String)in.sval;
}
void run() throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt");
Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt");
this.reader = new BufferedReader(reader);
in = new StreamTokenizer(this.reader);
out = new PrintWriter(writer);
solve();
out.flush();
}
void solve() throws IOException
{
int n = nextInt();
int k = nextInt();
if (k >= ((n*(n-1))/2) || n <= 1 )
out.print("no solution");
else {
for(int i = 0; i < n; i++){
out.println("0 "+i);
}
}
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.util.Scanner;
public class ClosestPair {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
if(k >= n*(n-1)/2) {
System.out.print("no solution");
} else {
for (int i = 0; i <n; i++) {
System.out.println("0 " + i);
}
}
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Sanchit M. Bhatnagar ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
long N = in.nextLong();
long K = in.nextLong();
if (K >= (N * (N - 1)) / 2) {
out.println("no solution");
return;
}
for (int i = 0; i < N; i++) {
out.println("10 " + i);
}
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
int n = nextInt();
int k = nextInt();
int nows = 0;
int nowf = n / 2 - 1;
if((n * (n - 1) / 2) > k){
for(int i = 0; i < n; i ++){
println(0 + " " + (i + 1));
}
} else {
println("no solution");
}
}
private static void getAns(double ab, double cd){
}
private static PrintWriter out = new PrintWriter(System.out);
private static BufferedReader inB = new BufferedReader(new InputStreamReader(System.in));
private static StreamTokenizer in = new StreamTokenizer(inB);
private static void exit(Object o) throws Exception {
out.println(o);
out.flush();
System.exit(0);
}
private static void println(Object o) throws Exception{
out.println(o);
out.flush();
}
private static void print(Object o) throws Exception{
out.print(o);
out.flush();
}
private static long nextLong() throws Exception {
in.nextToken();
return (long)in.nval;
}
private static int nextInt() throws Exception {
in.nextToken();
return (int)in.nval;
}
private static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
} | JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author ayush
*
*/
public class TaskB {
public static void main(String s[]) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(System.out)));
TaskB solver = new TaskB();
solver.solve(in, out);
out.close();
}
void solve(BufferedReader in, PrintWriter out)throws IOException {
StringTokenizer token=new StringTokenizer(in.readLine());
int n=Integer.parseInt(token.nextToken()),k=Integer.parseInt(token.nextToken());
if( n==2|| ( k>=( (n*(n-1))/2) ) )
out.println("no solution");
else{
for(int i=1;i<=n;i++){
out.println(i+" "+(i*500000));
}
}
}
} | JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double PI = 3.141592653589793238463;
const int N = 2e5 + 100;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int x, k;
cin >> x >> k;
if (x * (x - 1) / 2 <= k) {
cout << "no solution\n";
return 0;
}
for (int i = 1e6; i < 1e6 + x; i++) cout << 0 << " " << i << '\n';
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Balanced {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt(), k=sc.nextInt();
if(k>=(n*(n-1)/2))
out.println("no solution");
else {
for(int i=0;i<n;i++)
out.println("0 "+i);
}
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 boolean ready() throws IOException {
return br.ready();
}
}
} | JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
/* Find the maximum number for 'tot', which is just n choose 2 */
if (n*(n-1) <= 2*k)
System.out.println("no solution");
else {
for (int i = 0; i < n; i++) {
System.out.println("0 "+i);
}
}
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class C {
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));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
}
private static void solve() throws IOException {
int n = nextInt();
int k = nextInt();
if (k >= n*(n-1)/2) {
writer.println("no solution");
return;
}
writer.println("0 0");
writer.println("1 1");
for (int i = 2; i < n; i++) {
writer.println("1 " + i);
}
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import sys
inp = sys.stdin.readline() #read line
n, k = map(int, inp.split())
mc = n * ( n - 1 ) / 2
if n == 2:
mc = 1
if mc <= k:
print 'no solution'
else:
for i in xrange(n):
print "%i %i" % (0, i) | PYTHON |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long n, k;
int main() {
long long cnt;
while (cin >> n >> k) {
cnt = n * (n - 1) / 2;
if (cnt > k) {
for (int i = 0; i < n; i++) cout << 0 << " " << i << endl;
} else
cout << "no solution" << endl;
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
int i, j, k, m, n;
int main() {
while (scanf("%d %d", &n, &k) == 2) {
if (k >= n * (n - 1) / 2) {
printf("no solution\n");
continue;
}
for (i = 1; i <= n; i++) printf("0 %d\n", i);
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
if (((n - 1) * n) / 2 <= k) {
printf("no solution\n");
} else {
int ypos = 0;
for (int i = 0; i < n; ++i) {
printf("%d %d\n", i, ypos);
ypos += (n + 1);
}
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
string FILE_NAME = "testcase.C";
string NAME;
string itos(int n) {
stringstream ss;
ss << n;
return ss.str();
}
int main() {
int n, k;
cin >> n >> k;
if (k >= n * (n - 1) / 2) {
cout << "no solution" << endl;
} else {
for (int i = 0; i < (n); i++) {
cout << 0 << ' ' << i << endl;
}
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
if (k >= n * (n - 1) / 2)
cout << "no solution" << endl;
else
for (int i = 1; i <= n; i++) cout << 0 << " " << i << endl;
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | n, k = map(int, input().split())
if k >= n * (n - 1) // 2:
print("no solution")
else:
for i in range(n):
print(i, i * (n + 1))
| PYTHON3 |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
if (k >= n * (n - 1) / 2) {
cout << "no solution";
return 0;
}
for (int i = 1; i <= n; i++) {
cout << 0 << ' ' << i << "\n";
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.util.Scanner;
public class C {
static Scanner scanner = new Scanner(System.in);
static int N, K, tot;
public static void main(String[] args) {
N = scanner.nextInt();
K = scanner.nextInt();
tot = N * (N - 1) / 2;
if (tot <= K) {
System.out.println("no solution");
}
else {
for (int i=1;i<=N;++i) {
System.out.println(0 + " " + i);
}
}
}
}
| JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k, n, m;
while (cin >> n >> k) {
if (n * (n - 1) / 2 <= k) {
puts("no solution");
} else {
for (i = 0; i < n; i++) printf("0 %d\n", i);
}
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
struct A {
int x;
int y;
} p[2000];
double d;
double min(double a, double b) {
if (a > b)
return b;
else
return a;
}
double distance(int ax, int ay, int bx, int by) {
return sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
}
int main() {
int i, j, n, k, tot;
scanf("%d %d", &n, &k);
for (i = 1; i <= n; i++) {
p[i].x = 0;
p[i].y = i;
}
tot = n * (n - 1) / 2;
if (tot > k) {
for (i = 1; i <= n; i++) {
p[i].x = 0;
p[i].y = i;
printf("%d %d\n", p[i].x, p[i].y);
}
} else
printf("no solution");
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
if (k >= (n * n - n) / 2)
puts("no solution");
else {
for (int i = 0; i < n; i++) cout << 0 << " " << i << endl;
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
using pii = pair<long long, long long>;
const long long INF = 1e18;
void dprint(string s) { cout << "\n"; }
void dprint() { cout << "\n"; }
template <class T, class... U>
void dprint(string s, T t, U... u) {
long long w = s.find(',');
cout << "[" << s.substr(0, w) << ": " << t << "] ";
dprint(s.substr(w + 1, (long long)(s).size() - w), u...);
}
const long long N = 100005;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
if (n * (n - 1) / 2 <= k) return cout << "no solution", 0;
long long x = 1, y = 1;
for (long long i = 1; i <= n; i++) {
cout << x << ' ' << i << "\n";
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void OMG() { cout << "OMG>.< I don't know!\n"; }
void W1() { cout << "Freda's\n"; }
void W2() { cout << "Rainbow's\n"; }
bool ff1(string S) {
string A = "";
for (int i = 0; i < 5; i++) A += S[i];
return (A == "miao.");
}
bool ff2(string S) {
string A = "";
for (int i = S.size() - 1 - 4; i < S.size(); i++) A += S[i];
return (A == "lala.");
}
int main() {
long long n, k;
cin >> n >> k;
if ((n * (n - 1)) / 2 <= k) {
cout << "no solution\n";
return 0;
}
long long x = 0, y = 0;
for (int i = 0; i < n; i++) {
cout << x << ' ' << y << '\n';
y++;
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int n=cin.nextInt(),k=cin.nextInt();
if (n*(n-1)/2>k)
while (n>0){
System.out.println("0 "+n);
n--;
}
else System.out.println("no solution");
}
} | JAVA |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxN = 2005;
long long n, k, x[maxN] = {-3000}, y[maxN] = {-3000}, ans;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 1; i < n; i++) {
ans += i;
x[i] = x[i - 1];
y[i] = y[i - 1] - 1;
}
if (ans <= k)
cout << "no solution" << '\n';
else {
for (int i = 0; i < n; i++) {
cout << x[i] << " " << y[i] << '\n';
}
}
return 0;
}
| CPP |
312_C. The Closest Pair | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given n points in the plane, find a pair of points between which the distance is minimized. Distance between (x1, y1) and (x2, y2) is <image>.
The pseudo code of the unexpected code is as follows:
input n
for i from 1 to n
input the i-th point's coordinates into p[i]
sort array p[] by increasing of x coordinate first and increasing of y coordinate second
d=INF //here INF is a number big enough
tot=0
for i from 1 to n
for j from (i+1) to n
++tot
if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be
//out of the loop "for j"
d=min(d,distance(p[i],p[j]))
output d
Here, tot can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, tot should not be more than k in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded?
Input
A single line which contains two space-separated integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ 109).
Output
If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print n lines, and the i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109) representing the coordinates of the i-th point.
The conditions below must be held:
* All the points must be distinct.
* |xi|, |yi| ≤ 109.
* After running the given code, the value of tot should be larger than k.
Examples
Input
4 3
Output
0 0
0 1
1 0
1 1
Input
2 100
Output
no solution | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
static const int INF = 500000000;
template <class T>
void debug(T a, T b) {
for (; a != b; ++a) cerr << *a << ' ';
cerr << endl;
}
int n, k;
int main() {
cin >> n >> k;
int maxi = n * (n - 1) / 2;
if (k >= maxi) {
puts("no solution");
return 0;
}
for (int i = 0; i < n; ++i) printf("%d %d\n", 0, 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 | I=lambda:map(int, raw_input().split())
def main():
d={}
s = raw_input()
l = input()
for c in s:
d[c] = d.get(c,0) + 1
if len(d) > l:
print -1
return
lo = 0
hi = 10000
while lo + 1 < hi:
mid = (lo + hi) / 2
c = 0
for x in d.itervalues():
c += (x + mid - 1) / mid
if c > l:
lo = mid
else:
hi = mid
print hi
ans = []
for x in d.iteritems():
ans.append(x[0] * ((x[1] + hi - 1) / hi))
t = ''.join(ans)
if len(t) < l:
t += 'a' * (l - len(t))
print t
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;
char s[1001];
int co[200];
int len;
struct p {
char c;
int num;
int ap;
bool operator<(const p& other) const {
int t1, t2;
t1 = num / ap;
if (double(num) / ap > t1) {
t1++;
}
t2 = other.num / other.ap;
if (double(other.num) / other.ap > t2) {
t2++;
}
return t1 < t2;
}
};
priority_queue<p> q;
int main(void) {
int i;
int n;
p temp;
int t1;
scanf("%s", s);
scanf("%d", &n);
len = strlen(s);
for (i = 0; i < len; i++) {
co[s[i]]++;
}
for (i = 'a'; i <= 'z'; i++) {
if (co[i] > 0) {
temp.c = i;
temp.ap = 1;
temp.num = co[i];
q.push(temp);
}
}
while (q.size() < n && q.top().num > 1) {
temp.ap = q.top().ap + 1;
temp.num = q.top().num;
temp.c = q.top().c;
q.pop();
q.push(temp);
n--;
}
if (q.size() > n) {
printf("-1\n");
} else {
t1 = q.top().num / q.top().ap;
if (double(q.top().num) / q.top().ap > t1) {
t1++;
}
printf("%d\n", t1);
while (q.size() > 0) {
for (i = 0; i < q.top().ap; i++) {
printf("%c", q.top().c);
}
q.pop();
n--;
}
for (i = 0; i < n; i++) {
printf("a");
}
printf("\n");
}
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
string s;
int tedad[300];
bool possible(int t, int n) {
int len = 0;
for (int i = 'a'; i <= 'z'; i++) {
len += tedad[i] / t;
if (tedad[i] % t != 0) len++;
}
return len <= n;
}
int bs(int first, int second, int n) {
if (first == second) return first;
int d = (first + second) / 2;
if (d == 1 && possible(d, n)) return d;
if (possible(d, n) && !possible(d - 1, n)) return d;
if (!possible(d, n)) return bs(d + 1, second, n);
return bs(first, d - 1, n);
}
int main() {
int n;
cin >> s >> n;
for (int i = 0; i < 300; i++) tedad[i] = 0;
int dif = 0, dif2 = 0;
for (int i = 0; i < s.length(); i++) {
if (tedad[s[i]] == 0) dif++;
tedad[s[i]]++;
}
if (n < dif) {
cout << -1 << endl;
return 0;
}
int ans = bs(1, 1005, n);
string sans = "";
for (int i = 'a'; i <= 'z'; i++) {
int need = tedad[i] / ans;
if (tedad[i] % ans != 0) need++;
for (int j = 0; j < need; j++) sans += char(i);
}
cout << ans << endl;
while (sans.length() < n) sans += 'a';
cout << sans << endl;
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
string a;
int n;
int freq[26] = {0};
cin >> a >> n;
for (int i = 0; i < a.length(); i++) freq[a[i] - 'a']++;
int lo = 1, hi = 1001;
int ans = -1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
int need = 0;
for (int i = 0; i < 26; i++) need += ceil(freq[i] * 1.0 / mid);
if (need <= n) {
ans = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
cout << ans << endl;
int cnt = 0;
for (int i = 0; ans != -1 && i < 26; i++) {
for (int j = 0; j < ceil(freq[i] * 1.0 / ans); j++, cnt++)
cout << char(i + 'a');
}
for (int i = cnt; ans != -1 && 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.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Vadim
*/
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) {
String s = in.readLine();
int n = in.nextInt();
int a[] = new int[26];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
a[c-'a']++;
}
int left = 0;
int right = 1001;
while(left < right) {
int mid = (left + right) >>>1;
if(mid == 0) {
left = 1;
break;
}
int length = 0;
for (int i = 0; i < 26; i++) {
length += a[i]/mid;
if(a[i]%mid != 0) {
length++;
}
}
if(length > n) {
left = mid+1;
}else {
right = mid;
}
}
if(left == 1001) {
System.out.println(-1);
} else {
out.println(left);
StringBuilder sb = new StringBuilder();
int length = 0;
for (int i = 0; i < 26; i++) {
int rep = a[i] / (left);
if (a[i] % (left) != 0) {
rep++;
}
for (int j = 0; j < rep; j++) {
sb.append((char) (i + 'a'));
length++;
}
}
for (int i = 0; i < n - length; i++) {
sb.append(sb.charAt(0));
}
out.println(sb);
}
}
}
class InputReader extends BufferedReader {
public InputReader(InputStream st) {
super(new InputStreamReader(st));
}
public int nextInt() {
try {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new RuntimeException();
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
}
| JAVA |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.io.BufferedReader;
public class Main{
static int[][] grid;
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int[] alphabet = new int[26];
String s = sc.next();
int limit = sc.nextInt();
StringBuilder ans = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
alphabet[s.charAt(i)-'a']++;
if (alphabet[s.charAt(i)-'a'] == 1) {
ans.append(s.charAt(i));
limit--;
}
}
HashMap<Character, Integer> freq = new HashMap<Character, Integer>();
PriorityQueue<pair> max = new PriorityQueue<pair>();
for (int i = 0; i < 26; i++) {
if (alphabet[i] > 1) {
max.add(new pair((char) (i+'a'), alphabet[i]));
freq.put((char) (i+'a'),1);
}
}
while (limit > 0 && !max.isEmpty()) {
pair cur = max.poll();
ans.append(cur.c);
freq.put(cur.c,freq.get(cur.c) +1);
cur.freq = (alphabet[cur.c-'a']/freq.get(cur.c) + ((alphabet[cur.c-'a']%freq.get(cur.c)) != 0?1:0));
if (cur.freq > 1) {
max.add(cur);
}
limit--;
}
if (limit < 0) {
out.println(-1);
}
else {
while (limit > 0) {
ans.append(s.charAt(0));
limit--;
}
if (max.isEmpty()) {
out.println(1);
}
else {
out.println(max.poll().freq);
}
out.println(ans.toString());
}
out.flush();
}
static class pair implements Comparable<pair>{
char c;
int freq;
pair(char c,int freq){
this.c = c;
this.freq = freq;
}
public int compareTo(pair o) {
return o.freq - this.freq;
}
}
}
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() throws InterruptedException {
Thread.sleep(3000);
}
}
| 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
s = sys.stdin.readline().strip()
n = int(sys.stdin.readline())
d = {}
for i in xrange(len(s)):
if s[i] in d:
d[s[i]] += 1
else:
d[s[i]] = 1
num_letters = len(d.keys())
if num_letters > n:
print '-1'
elif num_letters == n:
print max(d.values())
print ''.join(d.keys())
else: # num_letters < n
d1 = d.copy()
stroka = d.keys()
for e in d1.keys():
d1[e] -= 1
if d1[e] <= 0:
del d1[e]
while len(stroka) < n:
if len(d1) == 0:
stroka.append(d.keys()[0])
continue
tups = list(d.iteritems())
tups.sort(key = lambda t: t[1]*1.0/stroka.count(t[0]))
el = tups[-1][0]
d1[el] -= 1
if d1[el] <= 0:
del d1[el]
stroka.append(el)
num = 0
while len(d) > 0:
num +=1
for c in stroka:
if c in d:
d[c] -=1
if d[c] <= 0:
del d[c]
print num
print ''.join(stroka)
| 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;
#pragma comment(linker, "/STACK:64000000")
int j, n, l, i, h, q1, q2, x, y, m, k, t, ans, p1, p2, ans1, ans2, a[1000500],
q, d[1000500], p;
string s, z;
set<char> ss;
void init() {
cin >> s;
n = s.size();
cin >> k;
}
void solve() {
for (j = 1; j <= n; j++) {
ss.insert(s[j - 1]);
a[s[j - 1]]++;
d[s[j - 1]] = 1;
}
if (k < int(ss.size())) {
printf("-1\n");
return;
}
for (set<char>::iterator w = ss.begin(); w != ss.end(); w++) {
z += *w;
}
while (int(z.size()) < k) {
q = 0;
for (j = 'a'; j <= 'z'; j++) {
if (!d[j]) {
continue;
}
p = a[j] / d[j];
if (a[j] % d[j]) {
p++;
}
if (p > q) {
q = p;
t = j;
}
}
z += char(t);
d[t]++;
}
q = 0;
for (j = 'a'; j <= 'z'; j++) {
if (!d[j]) {
continue;
}
p = a[j] / d[j];
if (a[j] % d[j]) {
p++;
}
if (p > q) {
q = p;
t = j;
}
}
printf("%d\n%s", q, z.c_str());
}
void answer() {}
int main() {
init();
solve();
answer();
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 Banana {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = sc.nextInt();
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
for (int i = 0; i < str.length(); i++) {
if (!tm.containsKey(str.charAt(i))) {
tm.put(str.charAt(i), 1);
} else {
tm.put(str.charAt(i), tm.get(str.charAt(i)) + 1);
}
}
if (tm.size() > n) {
System.out.println(-1);
} else {
int left = 1;
int right = 1000;
int result_len = 0;
StringBuilder result_str = new StringBuilder("");
while(left <= right) {
int mid = (left + right)/2;
int len = calculateLength(tm, mid);
if (len > n) {
left = mid + 1;
} else {
result_len = mid;
right = mid - 1;
}
}
for (Map.Entry<Character, Integer> kvp : tm.entrySet() ) {
int times = (int) Math.ceil(kvp.getValue() / (double) result_len);
for(int i = 0; i < times; i++) {
result_str.append(kvp.getKey());
}
}
while (result_str.length() < n) {
result_str.append(result_str.charAt(0));
}
System.out.println(result_len);
System.out.println(result_str);
}
}
public static int calculateLength(TreeMap<Character, Integer> tm, int n) {
int length = 0;
for (Map.Entry<Character, Integer> kvp : tm.entrySet() ) {
length += (int) Math.ceil(kvp.getValue() / (double) n);
}
return length;
}
}
| 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;
string s;
int n;
int cnt[256];
bool check(int x) {
if (x == 0) return false;
int sum = 0;
for (char c = 'a'; c <= 'z'; ++c) {
int d = cnt[c] / x + !!(cnt[c] % x);
sum += d;
}
return sum <= n;
}
string get(int x) {
string t = "";
for (char c = 'a'; c <= 'z'; ++c) {
int d = cnt[c] / x + !!(cnt[c] % x);
while (d--) t += c;
}
while (t.length() < n) t += 'c';
return t;
}
int main() {
cin >> s >> n;
for (int i = 0; i < s.length(); ++i) cnt[s[i]]++;
int l = 1, r = s.length(), m, ans = -1;
l = s.length() / n;
for (m = l; m <= r; ++m) {
if (check(m)) {
ans = m;
break;
}
}
cout << ans << endl;
if (ans != -1) cout << get(ans) << endl;
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Banana {
public static boolean can(int []a,int n,int len)
{
int needed=0;
for(int x:a)
{
if(x!=0)
needed+=((x+n-1)/n);
}
if(len<needed)
return false;
return true;
}
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();int n=sc.nextInt();
int []count =new int[26];
for(char c:s.toCharArray())
count[c-'a']++;
int distinct=0;
for(int x:count)
if(x>0)
distinct++;
if(distinct>n)
{
System.out.println(-1);
return;
}
int lo=1;
int ans=0;
int hi=1000;
while(lo<=hi)
{
int mid=(lo+hi)/2;
if(can(count,mid,n))
{
ans=mid;
hi=mid-1;
}
else
lo=mid+1;
}
int left=n;
System.out.println(ans);
for(int j=0;j<26;j++)
{
int x=count[j];char c=(char)('a'+j);
if(x!=0)
{
int rep=(x+ans-1)/ans;
for(int i=0;i<rep;i++)
{
System.out.print(c);
left--;
}
}
}
while(left-->0)
System.out.print('a');
}
}
class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
} | JAVA |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | import java.util.*;
import java.lang.reflect.Array;
import java.math.*;
import java.io.*;
public class main{
static Scanner scan = new Scanner(new BufferedInputStream(System.in));
public static void main(String[] args){
while(scan.hasNext()){
int[][] a = new int[2][26];
String str = scan.nextLine();
int n = Integer.parseInt(scan.nextLine());
for(int i=0; i<str.length(); i++){
a[0][(int)(str.charAt(i) - 'a')]++;
}
int num = 0;
boolean flag = false;
for(int i=0; i<26; i++)
if(a[0][i] != 0) num++;
for(int i=0; i<26; i++){
a[1][i] = 1;
}
int max = 0;
if(num<=n){
while(num != n){
for(int i=0; i<26; i++){
if(((double)a[0][max] / (double)a[1][max]) >= ((double)a[0][i] / ((double)a[1][i]))){
}else{
max = i;
}
}
n--;
a[1][max]++;
}
int nmax = 0;
for(int i=0; i<26; i++){
if(((double)a[0][nmax] / (double)a[1][nmax]) >= (((double)a[0][i] / (double)a[1][i]))){
}else{
nmax = i;
}
}
if((a[0][nmax])%a[1][nmax] == 0) System.out.println((a[0][nmax])/a[1][nmax]);
else System.out.println(((a[0][nmax])/a[1][nmax])+1);
String strr = "";
for(int i=0; i<26; i++){
while(a[1][i] != 1){
strr += (char)('a'+i);
a[1][i]--;
}
}
for(int i=0; i<26; i++){
if(a[0][i]!=0) strr += (char)('a'+i);
}
System.out.println(strr);
}else{
flag = true;
}
if(flag) System.out.println("-1");
}
}
} | JAVA |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
string s;
cin >> s >> n;
set<char> se(s.begin(), s.end());
if ((int)se.size() > n) {
cout << "-1"
<< "\n";
} else {
string str;
map<char, int> x, y;
for (int i = 0; i < (int)s.size(); i++) {
x[s[i]]++;
y[s[i]] = 1;
if (x[s[i]] == 1) str += s[i];
}
int mx = 0;
char ch;
for (int i = (int)str.size() + 1; i <= n; i++) {
mx = 0;
for (char j = 'a'; j <= 'z'; j++) {
if (x[j] > 0) {
if (ceil(x[j] / (float)y[j]) > mx) {
mx = ceil(x[j] / (float)y[j]);
ch = j;
}
}
}
y[ch]++;
str += ch;
}
mx = 0;
for (char j = 'a'; j <= 'z'; j++) {
if (x[j] > 0) {
if (ceil(x[j] / (float)y[j]) > mx) {
mx = ceil(x[j] / (float)y[j]);
ch = j;
}
}
}
cout << mx << "\n";
cout << str << "\n";
}
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
set<char> st;
int a[26] = {0};
int main() {
int n;
string s;
cin >> s >> n;
for (auto c : s) {
st.insert(c);
a[c - 97]++;
}
if ((int)st.size() > n) {
printf("-1\n");
return 0;
}
for (int i = 1; i <= 1000; ++i) {
int co = 0;
for (int j = 0; j <= 25; ++j) {
co += ceil((double)a[j] / i);
}
if (co <= n) {
printf("%d\n", i);
for (int j = 0; j <= 25; ++j) {
for (int k = 1; k <= ceil((double)a[j] / i); ++k) {
printf("%c", ('a' + j));
}
}
while (co < n) {
printf("a");
co++;
}
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;
template <class T>
void PV(T a, T b) {
while (a != b) cout << *a++ << " ";
cout << endl;
}
template <class T>
inline bool chmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool chmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;
int main() {
ios_base::sync_with_stdio(0);
string s;
cin >> s;
int c[26] = {};
for (int i = 0; i < s.length(); i++) c[s[i] - 'a']++;
int n;
cin >> n;
int diff = 0;
for (int i = 0; i < 26; i++)
if (c[i] > 0) diff++;
if (diff > n) {
cout << -1 << endl;
return 0;
}
int cnt = 1;
while (1) {
int d[26] = {};
int need = 0;
for (int i = 0; i < 26; i++) {
d[i] = (c[i] + cnt - 1) / cnt;
need += d[i];
}
if (need <= n) {
cout << cnt << endl;
for (int i = 0; i < 26; i++)
for (int j = 0; j < d[i]; j++) cout << char(i + 'a');
for (int i = need; i < n; i++) cout << 'a';
cout << endl;
return 0;
}
cnt++;
}
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 cnt[256];
bool check(int x) {
int sum = 0;
for (char c = 'a'; c <= 'z'; ++c) {
int d = cnt[c] / x + !!(cnt[c] % x);
sum += d;
}
return sum <= n;
}
string get(int x) {
string t = "";
for (char c = 'a'; c <= 'z'; ++c) {
int d = cnt[c] / x + !!(cnt[c] % x);
while (d--) t += c;
}
while (t.length() < n) t += 'c';
return t;
}
int main() {
cin >> s >> n;
for (int i = 0; i < s.length(); ++i) cnt[s[i]]++;
int l = 1, r = s.length(), m, ans = -1;
while (l <= r) {
m = (l + r) / 2;
if (check(m)) {
ans = m;
r = m - 1;
} else {
l = m + 1;
}
}
cout << ans << endl;
if (ans != -1) cout << get(ans) << endl;
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
string S;
int N, L;
map<char, int> ch;
void read();
void solve();
bool test(int);
void howdy(int);
int main() {
read();
solve();
return 0;
}
void read() { cin >> S >> N, L = int(S.length()); }
void solve() {
for (int i = 0; i < L; ++i) ++ch[S[i]];
if (int(ch.size()) > N) {
cout << "-1\n";
return;
}
int low = 1, up = 100000, mid;
while (low < up) {
mid = (low + up) / 2;
if (test(mid))
up = mid;
else
low = mid + 1;
}
cout << low << "\n";
howdy(low);
}
bool test(int bnd) {
int q = 0;
for (map<char, int>::iterator itr = ch.begin(); itr != ch.end(); ++itr)
q += (itr->second + bnd - 1) / bnd;
return q <= N;
}
void howdy(int bnd) {
int c = 0;
for (map<char, int>::iterator itr = ch.begin(); itr != ch.end(); ++itr) {
c += (itr->second + bnd - 1) / bnd;
cout << string((itr->second + bnd - 1) / bnd, itr->first);
}
if (c < N) cout << string(N - c, 'a');
cout << "\n";
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | '''
http://codeforces.com/contest/335/problem/A
'''
from collections import Counter
char_counter = dict(Counter(raw_input().strip()))
n = int(raw_input())
result = ""
index = 1
if len(char_counter) > n: print -1
else:
while True:
for key, value in char_counter.iteritems():
if value % index == 0:
result += key * (value//index)
else:
result += key * (value//index + 1)
if len(result) == n:
print index
print result
break
elif len(result) > n:
index += 1
result = ""
else:
print index
print result + ('b' * (n-len(result)))
break
| 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 | //package memsql.r2;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class A {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
char[] s = ns(2000);
int m = ni();
int[] ct = new int[26];
for(int i = 0;i < s.length;i++){
ct[s[i]-'a']++;
}
int nk = 0;
for(int k = 0;k < 26;k++){
if(ct[k] > 0){
nk++;
}
}
if(nk > m){
out.println(-1);
return;
}
int low = 0, high = 2000;
while(high - low > 1){
int x = (high + low) / 2;
long need = 0;
for(int k = 0;k < 26;k++){
if(ct[k] > 0){
need += (ct[k] + x - 1) / x;
}
}
if(need <= m){
high = x;
}else{
low = x;
}
}
out.println(high);
char[] ret = new char[m];
int p = 0;
for(int k = 0;k < 26;k++){
if(ct[k] > 0){
int ne = (ct[k] + high - 1) / high;
for(int i = 0;i < ne;i++){
ret[p++] = (char)('a' + k);
}
}
}
for(;p < m;p++){
ret[p] = 'z';
}
out.println(new String(ret));
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new A().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | s = input()
n = int(input())
symb_cnt = {}
for c in s:
symb_cnt[c] = symb_cnt[c] + 1 if c in symb_cnt else 1
for cnt in range(1, len(s) + 1):
s1 = ""
for c in symb_cnt:
s1 += c * ((symb_cnt[c] + cnt - 1) // cnt)
if len(s1) <= n:
for i in range(n - len(s1)):
s1 += 'a'
print(cnt)
print(s1)
exit(0)
print(-1)
| PYTHON3 |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
string str;
map<char, int> mp;
set<char> st;
bool ok(int k) {
int rem = n - int((st).size());
for (auto it = st.begin(); it != st.end(); it++) {
char c = *it;
int r = mp[c] - k;
if (r <= 0) continue;
int need = (r + k - 1) / k;
if (need > rem) return false;
rem -= need;
}
return true;
}
string getAns(int k) {
string ans;
for (auto it = st.begin(); it != st.end(); it++) {
int cnt = (mp[*it] + k - 1) / k;
for (int i = 0; i < cnt; i++) ans += (*it);
}
while (int((ans).size()) < n) ans += 'a';
return ans;
}
int main() {
cin >> str;
scanf("%d", &n);
for (int i = 0; i < int((str).size()); i++) st.insert(str[i]), mp[str[i]]++;
if (n < int((st).size())) return puts("-1");
int l = 1, r = 10011;
while (l < r) {
int mid = l + (r - l) / 2;
if (ok(mid))
r = mid;
else
l = mid + 1;
}
cout << l << endl;
cout << getAns(l);
}
| 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 Up(int a, int b) { return (a + b - 1) / b; }
class Type {
public:
char ch;
int number;
int k;
int value;
Type() {}
Type(char _ch, int _number) : ch(_ch), number(_number), k(0) {}
friend bool operator<(const Type& a, const Type& b) {
return a.number < b.number;
}
void Change() {
++k;
value = Up(number, k);
}
};
int main() {
string s;
cin >> s;
int n;
cin >> n;
int m[256] = {0};
for (int i = 0; i < (int)s.size(); ++i) {
++m[s[i]];
}
vector<Type> ss;
for (char ch = 'a'; ch <= 'z'; ++ch) {
if (m[ch] != 0) {
ss.push_back(Type(ch, m[ch]));
}
}
sort(ss.rbegin(), ss.rend());
for (int i = 0; i < (int)ss.size(); ++i) {
ss[i].Change();
}
if ((int)ss.size() > n) {
cout << "-1" << endl;
return 0;
}
int cnt = (int)ss.size();
int pos = 0;
while (cnt < n) {
int _maxN = 0;
for (int i = 1; i < (int)ss.size(); ++i) {
if (ss[i].value > ss[_maxN].value) {
_maxN = i;
}
}
ss[_maxN].Change();
++cnt;
}
int ans = 0;
for (int i = 0; i < (int)ss.size(); ++i) {
ans = max(ans, ss[i].value);
}
cout << ans << endl;
for (int i = 0; i < (int)ss.size(); ++i) {
for (int j = 0; j < ss[i].k; ++j) {
cout << ss[i].ch;
}
}
cout << endl;
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
string s;
int c[26];
bool check(int m, string t) {}
bool solve() {
int l = 1, r = 10000;
while (l <= r) {
}
}
bool compare(pair<char, int> a, pair<char, int> b) {
return a.second > b.second;
}
int main() {
cin >> s;
int n;
cin >> n;
unordered_set<char> st;
map<char, int> mp;
for (auto ch : s) st.insert(ch), c[ch - 'a']++, mp[ch]++;
;
vector<pair<char, int>> vp;
for (auto xx : mp) vp.push_back(make_pair(xx.first, xx.second));
sort(vp.begin(), vp.end(), compare);
int mx = vp.front().second;
if (st.size() > n)
return cout << -1 << endl, 0;
else if (st.size() == n) {
cout << mx << endl;
for (auto x : st) cout << x;
cout << endl;
} else {
map<char, int> mc1;
string ans;
for (auto x : st) ans.push_back(x), mc1[x]++;
for (int i = 0; i < n - st.size(); i++) {
double mxx = INT_MIN;
char cc;
for (auto x : mp) {
double tmp = (double)x.second / mc1[x.first];
if (tmp > mxx) {
mxx = tmp;
cc = x.first;
}
}
ans.push_back(cc);
mc1[cc]++;
}
int aa = INT_MIN;
for (auto xx : mp)
aa = max((int)ceil(xx.second / (double)mc1[xx.first]), aa);
cout << aa << endl;
cout << ans << 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;
string s;
int n;
int cnt[256];
bool check(int x) {
if (x == 0) return false;
int sum = 0;
for (char c = 'a'; c <= 'z'; ++c) {
int d = cnt[c] / x + !!(cnt[c] % x);
sum += d;
}
return sum <= n;
}
string get(int x) {
string t = "";
for (char c = 'a'; c <= 'z'; ++c) {
int d = cnt[c] / x + !!(cnt[c] % x);
while (d--) t += c;
}
while (t.length() < n) t += 'c';
return t;
}
int main() {
cin >> s >> n;
for (int i = 0; i < s.length(); ++i) cnt[s[i]]++;
int l = s.length() / n + !!(s.length() % n), r = s.length(), m, ans = -1;
while (l <= r) {
m = (l + r) / 2;
if (check(m)) {
ans = m;
r = m - 1;
} else {
l = m + 1;
}
}
cout << ans << endl;
if (ans != -1) cout << get(ans) << endl;
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int am[27];
int dp[27][1010];
int path[27][1010];
int call(int p, int n) {
if (p == 26) return (n == 0 ? 0 : (1 << 29));
int &ret = dp[p][n];
if (ret != -1) return ret;
int &ppp = path[p][n];
ret = (1 << 29);
if (!am[p]) {
ret = call(p + 1, n);
ppp = 0;
}
for (int i = 1; i <= n; i++) {
int temp = max(call(p + 1, n - i), am[p] / i + bool(am[p] % i));
if (ret > temp) ret = temp, ppp = i;
}
return ret;
}
int main() {
string s;
int n;
cin >> s >> n;
for (__typeof(((int)s.size())) i = 0; i < (((int)s.size())); i++)
am[s[i] - 'a']++;
memset((dp), (-1), sizeof(dp));
;
if (call(0, n) >= (1 << 29)) {
cout << -1 << endl;
return 0;
}
int aa = 0, bb = n;
cout << call(0, n) << endl;
while (aa < 26) {
int am = path[aa][bb];
for (__typeof(am) i = 0; i < (am); i++) cout << char(aa + 'a');
aa++;
bb -= am;
}
cout << endl;
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int a[27], c[27], d[27];
int main() {
int count, i, n, max, co, t, index, k;
char str[1010], ch, b[1010];
ch = getchar();
while (ch != '\n') {
a[ch - 'a' + 1]++;
ch = getchar();
}
count = 0;
for (i = 1; i <= 26; i++)
if (a[i] != 0) count++;
scanf("%d", &n);
if (n < count)
printf("-1\n");
else if (n == count) {
max = 0;
for (i = 1; i <= 26; i++)
if (a[i] > max) max = a[i];
printf("%d\n", max);
for (i = 1; i <= 26; i++)
if (a[i] != 0) printf("%c", i + 'a' - 1);
printf("\n");
} else {
t = 0;
for (i = 1; i <= 26; i++) {
if (a[i] != 0) b[t++] = i + 'a' - 1;
c[i] = 1;
}
copy(a, a + 27, d);
for (k = n - count; k > 0; k--) {
max = 0;
index = 0;
for (i = 1; i <= 26; i++)
if (d[i] > max) {
max = d[i];
index = i;
}
c[index]++;
b[t++] = index + 'a' - 1;
if (a[index] % c[index] != 0)
d[index] = a[index] / c[index] + 1;
else
d[index] = a[index] / c[index];
}
max = 0;
for (i = 1; i <= 26; i++) {
if (d[i] > max) max = d[i];
}
printf("%d\n", max);
b[t] = 0;
puts(b);
}
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.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 Double.compare((list[o1.x - 'a'] - o1.y) * 1.0 / list[o1.x - 'a'], (list[o2.x - 'a'] - o2.y) * 1.0 / list[o2.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 | import java.io.InputStreamReader;
import java.io.IOException;
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
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
char[] s = in.next().toCharArray();
int n = in.nextInt();
int[] a = new int[26];
int numLetters = 0;
for (int i = 0; i < s.length; i++) {
int c = s[i] - 'a';
if (a[c] == 0) {
++numLetters;
}
++a[c];
}
if (numLetters > n) {
out.println(-1);
return;
}
for (int numSheets = 1; numSheets <= s.length; numSheets++) {
int[] b = new int[26];
int sum = 0;
for (int i = 0; i < 26; i++) {
if (a[i] > 0) {
b[i] = (a[i] + numSheets - 1) / numSheets;
sum += b[i];
}
}
if (sum <= n) {
out.println(numSheets);
for (int i = 0; i < 26; i++) {
for (int j = 0; j < b[i]; j++) {
out.print((char)('a' + i));
}
}
for (int i = sum; i < n; i++) {
out.print('z');
}
return;
}
}
}
}
class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.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 | import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
char c[]=br.readLine().toCharArray();
int n=c.length;
int m=Integer.parseInt(br.readLine());
int b[]=new int[26];
for(int i=0;i<n;i++)
b[c[i]-97]++;
int l=1,r=n;
while(r-l>1)
{
int u=0;
int x=(l+r)/2;
for(int i=0;i<26;i++)
u+=(int)Math.ceil((double)b[i]/x);
if(u<=m)
r=x;
else
l=x;
}
int u=0;
int x=l;
for(int i=0;i<26;i++)
u+=(int)Math.ceil((double)b[i]/x);
if(u<=m)
r=l;
u=0;
x=r;
for(int i=0;i<26;i++)
u+=(int)Math.ceil((double)b[i]/x);
boolean bb=false;
if(u<=m)
{ bb=true; }
if(bb)
{
StringBuffer sb=new StringBuffer();
for(int i=0;i<26;i++)
{
u=(int)Math.ceil((double)b[i]/r);
for(int j=0;j<u;j++)
sb.append((char)(i+97));
}
while(sb.length()<m)
sb.append('z');
System.out.println(r);
System.out.println(sb);
}
else
System.out.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.util.Map;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
import java.util.HashMap;
import java.util.Set;
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 RiaD
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Reader in = new Reader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Reader in, OutputWriter out) {
String s = in.nextString();
int n = in.nextInt();
Counter<Character> counter = new Counter<Character>();
for(int i = 0; i < s.length(); ++i) {
counter.add(s.charAt(i));
}
if(counter.size() > n){
out.println(-1);
return;
}
int l = 0;
int r = 1111;
while(r - l > 1) {
int m = (l + r) / 2;
int need = 0;
for (Map.Entry<Character, Long> entry : counter.entrySet()) {
long t = entry.getValue();
need += (t + m - 1) / m;
}
if(need <= n) {
r = m;
}
else
l = m;
}
out.println(r);
int m = r;
int need = 0;
for (Map.Entry<Character, Long> entry : counter.entrySet()) {
long t = entry.getValue();
long cur = (t + m - 1) / m;
need += cur;
for(long i = 0; i < cur; ++i){
out.print(entry.getKey());
}
}
for(int i = 0; i < n - need; ++i) {
out.print('a');
}
}
}
class Reader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public Reader(BufferedReader reader) {
this.reader = reader;
}
public Reader(InputStream stream) {
this(new BufferedReader(new InputStreamReader(stream)));
}
public String nextString() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(readLine());
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
private String readLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
class OutputWriter extends PrintWriter {
public OutputWriter(OutputStream out) {
super(out);
}
public OutputWriter(java.io.Writer writer){
super(writer);
}
}
class Counter<T> extends HashMap<T, Long> {
public void add(T key) {
add(key, 1);
}
public void add(T key, long value) {
put(key, get(key) + value);
}
public Long get(Object key) {
return containsKey(key) ? super.get(key) : 0;
}
}
| 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 ceil(a,b):
return (a+b-1)/b
s = raw_input()
n = input()
cnts = {}
for l in set(s): cnts[l] = s.count(l)
if len(cnts) > n:
print -1
raise SystemExit
for i in range(1,1001):
tot = 0
for (l, c) in cnts.items():
tot += ceil(c,i)
if tot <= n:
print i
out = ''
for (l, c) in cnts.items():
out += l*ceil(c,i)
if len(out) < n:
out += 'a'*(n-len(out))
print out
raise SystemExit
| 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 Counter
from math import ceil
s = raw_input()
c = Counter(s)
n = int(raw_input())
if n < len(c):
print -1
else:
d = {l: 1 for l in c}
for _ in range(n - len(c)):
_, l = max([(float(c[l]) / d[l], l) for l in c])
d[l] += 1
print int(max(ceil(float(c[l]) / d[l]) for l in c))
print ''.join(l * d[l] for l in d)
| 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.PrintWriter;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
char a[]=sc.next().toCharArray();
int fre[]=new int[26];
int n=a.length;
int ans_String_length=sc.nextInt();
if(ans_String_length>=a.length){
out.println(1);
for (int i = 0; i <a.length ; i++) {
out.print(a[i]);
}
for (int i = a.length; i <ans_String_length ; i++) {
out.print('a');
}
out.close();
return;
}
int freAns[]=new int[26];
int max=0;
for (int i = 0; i <n ; i++) {
fre[a[i]-'a']++;
max=Math.max(max,fre[a[i]-'a']);
}
int l=1,r=a.length,ans=a.length;
int y=0;
for (int i = 0; i <26 ; i++) {
if(fre[i]!=0)y++;
}
if(ans_String_length<y){
out.println(-1);
out.close();
return;
}
while (l<=r){
int tempFre[]=new int[26];
int mid=(l+r)>>1;
int sum=0;
for (int i = 0; i <26 ; i++) {
tempFre[i]=(int)Math.ceil(1.0*fre[i]/mid);
sum+=tempFre[i];
}
// out.println(sum+" "+mid+" "+l+" "+r);
if(sum<=ans_String_length){
ans=mid;
freAns=tempFre;
r=mid-1;
}
else{
l=mid+1;
}
}
out.println(ans);
int c=0;
for (int i = 0; i <26 ; i++) {
// out.println(freAns[i]);
for (int j = 0; j <freAns[i]; j++) {
out.print((char)(i+'a'));
c++;
}
}
if(c<ans_String_length){
while (c!=ans_String_length){
out.print('a');c++;
}
}
out.close();
}
} | JAVA |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
template <class F, class T>
T convert(F a, int p = -1) {
stringstream ss;
if (p >= 0) ss << fixed << setprecision(p);
ss << a;
T r;
ss >> r;
return r;
}
template <class T>
void db(T a, int p = -1) {
if (p >= 0) cout << fixed << setprecision(p);
cout << a << " ";
}
template <class T>
T gcd(T a, T b) {
T r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <class T>
T sqr(T x) {
return x * x;
}
template <class T>
T cube(T x) {
return x * x * x;
}
template <class T>
struct Triple {
T x, y, z;
Triple() {}
Triple(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {}
};
template <class T>
Triple<T> euclid(T a, T b) {
if (b == 0) return Triple<T>(1, 0, a);
Triple<T> r = euclid(b, a % b);
return Triple<T>(r.y, r.x - a / b * r.y, r.z);
}
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return s == 0 ? 0 : cntbit(s >> 1) + (s & 1);
}
const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
if (rsz <= 0) {
ptr = 0;
rsz = fread(bf, 1, bfsz, stdin);
if (rsz <= 0) return EOF;
}
--rsz;
return bf[ptr++];
}
void ga(char &c) {
c = EOF;
while (!isalpha(c)) c = gc();
}
int gs(char s[]) {
int l = 0;
char c = gc();
while (isspace(c)) c = gc();
while (c != EOF && !isspace(c)) {
s[l++] = c;
c = gc();
}
s[l] = '\0';
return l;
}
template <class T>
bool gi(T &v) {
v = 0;
char c = gc();
while (c != EOF && c != '-' && !isdigit(c)) c = gc();
if (c == EOF) return false;
bool neg = c == '-';
if (neg) c = gc();
while (isdigit(c)) {
v = v * 10 + c - '0';
c = gc();
}
if (neg) v = -v;
return true;
}
const double PI = 2 * acos(0);
const string months[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
const long long inf = (int)(1e9) + 5;
const long long linf = (long long)1e16 + 5;
const double eps = 1e-9;
const long long mod = 1000000007;
string s;
int n;
int a[33], b[33];
int main() {
memset(a, 0, sizeof(a));
cin >> s >> n;
for (__typeof(((int)(s).size())) i = 0; i < (((int)(s).size())); ++i) {
a[s[i] - 'a']++;
}
for (int i = 1; i <= 1000; i++) {
int num = 0;
for (int c = 0; c <= 'z' - 'a'; c++) {
b[c] = a[c] / i;
if (a[c] % i) b[c]++;
num += b[c];
}
if (num <= n) {
string res = "";
for (int c = 0; c <= 'z' - 'a'; c++) {
for (__typeof(b[c]) run = 0; run < (b[c]); ++run)
res.push_back(c + 'a');
}
while (res.size() < n) res.push_back('a');
cout << i << endl << res << endl;
return 0;
}
}
cout << -1;
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 |
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.InputStream;
import java.util.ArrayList;
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);
A solver = new A();
int testCount = 1;
for (int i = 1; i <= testCount; i++) {
solver.solve(i, in, out);
}
out.close();
}
}
class A {
public void solve(int testNumber, InputReader in, OutputWriter out) {
String s = in.readString(); int n = in.readInt();
int[] f = new int[26];
for( int i = 0 ; i < s.length(); i++){
f[s.charAt(i) - 'a']++;
}
for( int i = 1; i <= 1000; i++){
int req = 0;
for( int j = 0; j < 26; j++){
if(f[j] != 0)
req += (int)Math.ceil((double)f[j]/i);
}
if( req <= n){
out.printLine(i);
StringBuilder sb = new StringBuilder();
for( int k = 0; k < 26; k++){
for( int l = 0; l < (int)Math.ceil((double)f[k]/i); l++){
sb.append((char)(k+'a'));
}
}
int o = sb.length();
for( int h = o; h < n; h++){
sb.append('a');
}
out.printLine(sb.toString());
return;
}
}
out.printLine(-1);
}
}
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 String next() {
return readString();
}
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 IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.readInt();
}
return array;
}
}
| 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 ceil_div(int a, int b) { return (a + b - 1) / b; }
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
string s;
cin >> s;
int n;
cin >> n;
set<char> st(s.begin(), s.end());
if (n < st.size()) {
cout << -1 << endl;
return 0;
}
map<char, int> weight, ct;
int used = 0;
for (auto ch : st) weight[ch] = 1, used++;
for (auto ch : s) ct[ch]++;
while (used < n) {
char up = *st.begin();
for (auto ch : st)
if (ct[ch] * weight[up] > ct[up] * weight[ch]) up = ch;
weight[up]++;
used++;
}
int val = 0;
string ret;
for (auto it : weight)
for (int i = 0; i < it.second; i++) {
ret += it.first;
val = max(val, ceil_div(ct[it.first], it.second));
}
cout << val << endl << ret << 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 math
def onetry(p, tr):
m = 0
for k in p.keys():
nm = int(math.ceil((p[k]+0.)/tr[k]))
if nm > m:
m = nm
mk = k
return (m, mk)
if __name__=="__main__":
s = raw_input()
n = int(raw_input())
p = {}
for i in range(len(s)):
try:
p[s[i]] += 1
except:
p[s[i]] = 1
if len(p) > n:
print -1
exit()
tr = {}
for k in p.keys():
tr[k]=1
for i in range(n-len(tr)):
tr[onetry(p, tr)[1]]+=1
rs=""
for k in tr.keys():
rs+=k*tr[k]
print onetry(p,tr)[0]
print rs | PYTHON |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
struct lett {
char let;
int nb;
int at;
};
int main() {
int values[26], offset = static_cast<int>('a'), total;
lett letts[26];
string str;
cin >> str;
for (int i = 0; i < 26; i++) {
values[i] = 0;
}
for (int i = 0; i < str.length(); i++) {
values[static_cast<int>(str.at(i) - offset)]++;
}
int total_nb = 0;
for (int i = 0; i < 26; i++) {
if (values[i] > 0) {
total_nb++;
letts[total_nb - 1].let = static_cast<char>(offset + i);
letts[total_nb - 1].nb = values[i];
letts[total_nb - 1].at = 1;
}
}
string final = "";
cin >> total;
if (total < total_nb) {
cout << "-1";
return 0;
}
for (int i = 0; i < total_nb; i++) {
final += letts[i].let;
total--;
}
while (total > 0) {
int id = 0;
for (int i = 0; i < total_nb; i++) {
if (letts[i].nb / (letts[i].at + 0.0) >
letts[id].nb / (letts[id].at + 0.0))
id = i;
}
final += letts[id].let;
letts[id].at += 1;
total--;
}
int fin_id = 0;
for (int i = 0; i < total_nb; i++) {
if (letts[i].nb / (letts[i].at + 0.0) >
letts[fin_id].nb / (letts[fin_id].at + 0.0))
fin_id = i;
}
if ((letts[fin_id].nb) % letts[fin_id].at == 0)
cout << (letts[fin_id].nb) / letts[fin_id].at;
else
cout << (letts[fin_id].nb) / letts[fin_id].at + 1;
cout << endl;
cout << final;
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
from fractions import Decimal
S=input()
N=int(input())
Srep={}
ansrep={}
for item in "abcdefghijklmnopqrstuvwxyz":
Srep[item]=0
ansrep[item]=0
for item in S:
Srep[item]+=1
ansrep[item]+=1
Q=list(set(S))
if(len(Q)>N):
print(-1)
else:
n=len(Q)
ans=list(S)
num=1
req=1
n=len(ans)
while(len(ans)>N):
n=len(ans)
minn=req+1005
removal=ans[0]
k=True
for item in ans:
if(ansrep[item]==1):
continue
if(math.ceil(Srep[item]/(ansrep[item]-1))>req):
if(minn>math.ceil(Srep[item]/(ansrep[item]-1))):
minn=math.ceil(Srep[item]/(ansrep[item]-1))
removal=str(item)
continue
else:
ansrep[item]-=1
ans.remove(item)
k=False
break
if(k):
ansrep[removal]-=1
req=math.ceil(Srep[removal]/ansrep[removal])
ans.remove(removal)
g=""
if(len(ans)<N):
g=S[0]*(N-len(ans))
print(req)
for item in ans:
print(item,end="")
print(g)
| 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;
const double eps = 1e-8;
int dblcmp(double d) {
if (fabs(d) < eps) return 0;
return d > eps ? 1 : -1;
}
const double PI = 3.1415926535897932384626433832795;
const double pi = acos(-1.0);
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
long long pov(long long a, long long b, long long mod) {
long long res = 1;
while (b > 0) {
if (b % 2) {
res *= a;
b--;
} else {
b /= 2;
a *= a;
;
}
res %= mod;
}
return res;
}
int main(int argc, char* argv[]) {
string s;
int n;
cin >> s >> n;
int N = 30;
vector<int> larr(N, 0);
map<char, int> lett;
map<int, char> revl;
for (int i = 0; i < s.size(); i++) {
lett[s[i]]++;
larr[s[i] - 'a']++;
}
int k = s.size();
int m = lett.size();
if (m > n) {
cout << -1;
return 0;
}
int maxC = 0;
for (int j = 0; j < larr.size(); j++) {
if (larr[j] > maxC) {
maxC = larr[j];
}
}
int minC = k / n;
if (k % n) {
minC++;
}
int total = 0;
vector<int> res(N, 0);
int C = minC;
for (; C <= maxC; C++) {
res.assign(N, 0);
total = 0;
for (int j = 0; j < larr.size(); j++) {
int l = larr[j];
if (l == 0) {
continue;
}
int x = 1;
if (l > 1) {
x = l / C;
if (l % C) {
x++;
}
}
res[j] = x;
total += x;
}
if (total <= n) {
break;
}
}
string ans;
for (int i = 0; i < res.size(); i++) {
while (res[i] > 0) {
res[i]--;
ans.push_back(i + 'a');
}
}
while (ans.size() < n) {
ans.push_back('a');
}
cout << C << endl;
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;
const int mod = 1e9 + 9;
long long gcd(long long a, long long b) {
if (b == 0) return b;
return gcd(b, a % b) + a / b;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n;
cin >> n;
vector<pair<int, int>> a(26);
for (int i = 0; i < 26; i++) a[i] = make_pair(0, i);
for (int i = 0; i < s.size(); i++) a[s[i] - 'a'].first++;
sort(a.begin(), a.end());
if (n < 1 || (n < 26 && a[25 - n].first > 0)) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i < 1001; i++) {
int k = 0;
for (int j = 0; j < 26; j++) {
k += a[j].first / i + (a[j].first % i != 0);
}
if (k <= n) {
cout << i << endl;
string t;
k = n;
for (int h = 0; h < 26; h++)
for (int j = 0; j < a[h].first / i + (a[h].first % i != 0); j++) {
t = t + (char)('a' + a[h].second);
k--;
}
while (k--) t = t + (char)('a' + a.back().second);
cout << t << endl;
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 | #include <bits/stdc++.h>
using namespace std;
int i, j, n, m, k, v[30];
string s;
int main() {
cin >> s >> n;
for (i = 0; i < s.size(); i++) v[s[i] - 'a']++;
for (k = 1; k <= s.size(); k++) {
m = 0;
for (i = 0; i < 26; i++) m += ceil(v[i] / (double)k);
if (m <= n) {
cout << k << "\n";
for (i = 0; i < 26; i++)
for (j = 1; j <= ceil(v[i] / (double)k); j++) cout << (char)('a' + i);
for (i = m; i < n; i++) cout << 'a';
break;
}
}
if (k > s.size()) cout << -1;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
namespace p335ABanana {
void solve(int test_num) {
int cnt[26] = {0};
string str;
int n;
cin >> str >> n;
for (int i = 0; i < (int)str.size(); i++) cnt[str[i] - 'a']++;
for (int res = 1; res <= 1000; res++) {
int need = 0;
for (int c = 0; c < 26; c++) need += (cnt[c] + res - 1) / res;
if (need <= n) {
cout << res << endl;
for (int c = 0; c < 26; c++) {
int am = (cnt[c] + res - 1) / res;
for (int i = 0; i < am; i++) cout << char(c + 'a');
n -= am;
}
while (n--) cout << 'z';
cout << endl;
return;
}
}
cout << -1 << endl;
}
void solve() { solve(1); }
} // namespace p335ABanana
int main() {
p335ABanana::solve();
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 v[10001];
int main() {
string s;
int n, i, j, k, m, o, p;
cin >> s >> n;
o = s.length();
for (i = 0; i < o; i++) {
v[s[i] - 'a']++;
}
for (i = 1; i <= 1000; i++) {
string ans = "";
for (j = 0; j < 26; j++) {
if (v[j]) {
int y = (v[j] + i - 1) / i;
ans += string(y, 'a' + j);
}
}
int t = ans.length();
if (t <= n) {
while (t < n) ans += 'a', t++;
cout << i << "\n" << ans;
return 0;
}
}
cout << -1;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Alex
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out){
String s = in.next();
int n = in.ri();
int[] count = new int[26];
for(char c : s.toCharArray()) count[c-'a']++;
int min = 1, max = 1003;
while(min < max){
int middle = (min + max) / 2;
if(works(middle, count, n)) max = middle;
else min = middle + 1;
}
for(int i = 0; i < 26; i++) count[i] = IntegerUtils.roundUp(count[i], min);
StringBuilder res = new StringBuilder();
for(int i = 0; i < 26; i++) while(count[i] > 0) {res.append((char) (i+'a')); count[i]--;}
while(res.length() < n) res.append('a');
if (min == 1003) {out.printLine(-1); return;}
out.printLine(min);
out.printLine(res.toString());
}
private boolean works(int middle, int[] count, int n){
int length = 0;
for(int i = 0; i < 26; i++) length += IntegerUtils.roundUp(count[i], middle);
return length <= n;
}
}
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 ri(){
return readInt();
}
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 {
if (Character.isValidCodePoint(c))
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 String next() {
return readString();
}
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 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();
}
public void printLine(int i) {
writer.println(i);
}
}
class IntegerUtils {
public static int roundUp(int a, int b) {
assert(b > 0);
int d = a % b;
if (d < 0) d += b;
if (d != 0) a += b - d;
return a / b;
}
}
| 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;
string s;
int coos;
map<char, int> cnt;
string getcoos(int nos) {
string p = "";
for (auto y : cnt) {
int t = (y.second + nos - 1) / nos;
for (int j = 0; j < t; j++) p += (char)(y.first);
}
return p;
}
int main() {
cin >> s;
cin >> coos;
int n = s.size();
for (int i = 0; i < n; i++) cnt[s[i]]++;
int lo = 1, hi = 1e6;
int ans = -1;
while (lo <= hi) {
int nos = lo + (hi - lo) / 2;
if (getcoos(nos).size() > coos) {
lo = nos + 1;
} else {
ans = nos;
hi = nos - 1;
}
}
cout << ans << endl;
if (ans != -1) {
string p = getcoos(ans);
cout << p;
for (int i = p.size(); i < coos; i++) cout << 'a';
}
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
long long int n;
cin >> n;
long long int size = s.length();
vector<long long int> freq(26, 0);
for (long long int i = 0; i < size; i++) {
freq[s[i] - 'a']++;
}
long long int diff = 0;
for (long long int i = 0; i < 26; i++) {
if (freq[i] != 0) diff++;
}
if (n < diff)
cout << "-1\n";
else {
long long int minsheet = (n + size - 1) / n;
long long int sum = 0;
for (long long int i = 0; i < 26; i++) {
if (freq[i] != 0) {
sum += (freq[i] + minsheet - 1) / minsheet;
}
}
while (sum > n) {
minsheet++;
sum = 0;
for (long long int i = 0; i < 26; i++) {
if (freq[i] != 0) {
sum += (freq[i] + minsheet - 1) / minsheet;
}
}
}
cout << minsheet << endl;
string s2;
for (long long int i = 0; i < 26; i++) {
if (freq[i] != 0)
for (long long int j = 0; j < (freq[i] + minsheet - 1) / minsheet;
j++) {
s2.push_back('a' + i);
}
}
cout << s2;
for (long long int j = 0; j < n - s2.size(); j++) 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 | #include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
string s, t;
long long n, i;
cin >> s;
cin >> n;
set<pair<double, char>> m;
map<char, long long> freq, c;
for (i = 0; i < s.length(); ++i) {
freq[s[i]]++;
}
for (char a = 'a'; a <= 'z'; ++a) {
if (freq[a] > 0) {
m.insert({freq[a], a});
t += a;
c[a]++;
}
}
if (n < m.size())
cout << -1;
else {
for (i = 0; i < n - m.size(); ++i) {
auto it = m.end();
it--;
long long pos = it->second;
t += pos;
c[pos]++;
m.erase(it);
m.insert({(double)freq[pos] / c[pos], pos});
}
auto it = m.end();
--it;
if (freq[it->second] / c[it->second] == 0)
cout << 1 << '\n';
else if (freq[it->second] % c[it->second] == 0)
cout << freq[it->second] / c[it->second] << '\n';
else
cout << freq[it->second] / c[it->second] + 1 << '\n';
cout << 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;
char s[2000];
int u[30], n, i, ans, l, r, c, j;
int main() {
scanf("%s%d", &s, &n);
for (i = 0; s[i]; i++) u[s[i] - 'a']++;
l = 1;
r = 414141;
while (l < r) {
c = (l + r) / 2;
ans = 0;
for (i = 0; i < 26; i++) ans += (u[i] + c - 1) / c;
if (ans > n)
l = c + 1;
else
r = c;
}
if (l == 414141) {
puts("-1");
return 0;
}
printf("%d\n", l);
for (i = 0; i < 26; i++)
for (j = 0; j < (u[i] + l - 1) / l; j++) {
printf("%c", (char)(i + 'a'));
n--;
}
while (n-- > 0) printf("z");
puts("");
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
char s[1010];
int n;
int cnt[26];
inline bool check(int m) {
int all = 0;
for (int i = 0; i < 26; ++i) all += (int)ceil(1.0 * cnt[i] / m);
return all <= n;
}
int main() {
scanf("%s%d", s, &n);
int len = strlen(s);
for (int i = 0; i < len; ++i) cnt[s[i] - 'a']++;
int num = 0;
for (int i = 0; i < 26; ++i)
if (cnt[i]) num++;
if (num > n) {
puts("-1");
return 0;
}
int L = 1, R = 2000;
while (L < R) {
int mid = (L + R) >> 1;
if (check(mid))
R = mid;
else
L = mid + 1;
}
printf("%d\n", L);
for (int i = 0; i < 26; ++i)
if (cnt[i]) {
int t = (int)ceil(1.0 * cnt[i] / L);
n -= t;
for (int j = 0; j < t; ++j) putchar((char)(i + 'a'));
}
for (int i = 0; i < n; ++i) putchar('a');
}
| 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.Arrays;
import java.util.Scanner;
public class A {
static int[] freq = new int[26];
static int[][] dp;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
int n = in.nextInt();
for (int i = 0; i < s.length(); i++)
freq[s.charAt(i) - 'a']++;
dp = new int[26][n + 1];
for(int i = 0; i < dp.length; i++)
Arrays.fill(dp[i], -1);
int ans = solve(0, n);
if (ans == Integer.MAX_VALUE)
System.out.println(-1);
else {
String res = find(0, n, ans);
System.out.println(ans);
System.out.println(res);
}
}
private static String find(int idx, int n, int res) {
if (idx == freq.length) {
String s = "";
while (n-- != 0)
s += 'a';
return s;
}
if (freq[idx] == 0)
return find(idx + 1, n, res);
String s = "";
for (int i = 1; i <= Math.min(n, freq[idx]); i++) {
s += (char) (idx + 'a');
int d = (freq[idx] / i) + (freq[idx] % i == 0 ? 0 : 1);
int x = solve(idx + 1, n - i);
if (Math.max(d, x) == res) {
return s + find(idx + 1, n - i, x);
}
}
return s;
}
private static int solve(int idx, int n) {
if (idx == freq.length) {
return 0;
}
if(dp[idx][n] != -1)
return dp[idx][n];
if (freq[idx] == 0)
return solve(idx + 1, n);
if (n == 0)
return Integer.MAX_VALUE;
int ans = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++) {
int d = (freq[idx] / i) + (freq[idx] % i == 0 ? 0 : 1);
ans = Math.min(ans, Math.max(d, solve(idx + 1, n - i)));
}
return dp[idx][n] = 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;
const double PI = 2.0 * acos(0.0);
const double EPS = 1e-6;
int main() {
char s[1005];
scanf("%s", s);
int n;
scanf("%d", &n);
int len = strlen(s);
int cnt[26] = {0};
for (int i = 0; i < len; i++) cnt[s[i] - 'a']++;
bool found = false;
for (int res = 1; res <= len; res++) {
int need = 0;
int res_cnt[26] = {0};
for (int k = 0; k < 26; k++)
need += res_cnt[k] = (int)ceil((double)cnt[k] / res);
if (need <= n) {
found = true;
printf("%d\n", res);
for (int k = 0; k < 26; k++)
for (int j = 0; j < res_cnt[k]; j++) putchar(k + 'a');
for (int i = 0; i < n - need; i++) putchar('z');
puts("");
break;
}
}
if (!found) puts("-1");
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | s = input()
n = int(input())
d = {}
r = 0
for a in s:
d.setdefault(a, 0)
d[a] += 1
if(d[a] > r):
r = d[a]
if (len(d) > n):
print(-1)
else:
l = 0
while r - l > 1:
k = (l + r) // 2
cur = 0
for x in d.values():
cur += (x+k-1) // k
if cur > n:
l = k
else:
r = k
print(r)
s = ''
for a in d.keys():
s += a * ((d[a] + r - 1) // r)
l=len(s)
s += 'a' * (n-len(s))
print(s) | 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>
int main() {
std::string s;
int n;
std::cin >> s >> n;
std::vector<int> v(26, 0);
for (auto &c : s) {
v[c - 'a']++;
}
for (std::size_t i = 1; i <= s.size(); i++) {
int need = 0;
for (auto &m : v) {
need += (m + i - 1) / i;
}
if (need <= n) {
std::string out;
for (std::size_t j = 0; j < v.size(); j++) {
for (std::size_t k = 0; k < (v[j] + i - 1) / i; k++, n--) {
out += 'a' + j;
}
}
while (n--) {
out += 'z';
}
std::cout << i << '\n' << out << '\n';
return 0;
}
}
std::cout << -1 << '\n';
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class Banana {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int n = Integer.parseInt(br.readLine());
int[] freq = new int[26];
int sol = 0;
StringBuilder sb = new StringBuilder();
if(n >= s.length())
{
sb.append(s);
for (int i = s.length(); i < n; i++) {
sb.append(s.charAt(0));
}
System.out.println(1);
System.out.println(sb);
return;
}
for (int i = 0; i < s.length(); i++)
{
char curr = s.charAt(i);
if(freq[curr - 'a'] == 0)
sol++;
freq[curr - 'a']++;
}
if(sol > n)
{
System.out.println(-1);
return;
}
boolean ans = false;
int k = 1;
while(!ans)
{
int len = 0;
for (int i = 0; i < freq.length; i++) {
if(freq[i] == 0)
continue;
len += Math.ceil(freq[i]*1.0/k);
}
if(len <= n)
ans = true;
else
k++;
}
if(ans)
{
for (int i = 0; i < freq.length; i++) {
if(freq[i] == 0)
continue;
int num = (int) Math.ceil(freq[i]*1.0/k);
while(num --> 0)
sb.append((char)(i + 'a'));
}
while(sb.length() < n)
sb.append('a');
System.out.println(k);
System.out.println(sb);
}
else{
System.out.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 | s = input()
n = int(input())
freq = [0 for i in range(0, 300)]
raport = [0 for i in range(0, 300)]
differentLetters = 0
tickets = 0
sol = ''
for c in s: freq[ord(c)] += 1
for i in freq:
if i > 0: differentLetters += 1
if differentLetters > n:
print('-1')
exit()
for i in 'abcdefghijklmnopqrstuvwxyz':
if freq[ord(i)] == 0: continue
sol += i
freq[ord(i)] -= 1
raport[ord(i)] = freq[ord(i)]
for i in range(differentLetters, n):
#pun litera cu cea mai mare frecventa
maxRaport = raport[ord('z')]
chosenLetter = 'z'
for j in 'abcdefghijklmnopqrstuvwxyz':
if raport[ord(j)] > maxRaport:
maxRaport = raport[ord(j)]
chosenLetter = j
sol += chosenLetter
raport[ord(chosenLetter)] = freq[ord(chosenLetter)] / sol.count(chosenLetter)
for i in sol:
a = s.count(i)
b = sol.count(i)
if a%b == 0: tickets = max(tickets, int(a//b))
else: tickets = max(tickets, int(a//b) + 1)
print(tickets)
print(sol)
| 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 cn['z' + 1], ans, n;
string s, res;
int main() {
cin >> s;
scanf("%ld", &n);
ans = -1;
for (int i = 0; i <= s.length() - 1; i++) cn[s[i]]++;
for (int i = 1; i <= s.length(); i++) {
res = "";
for (int j = 'a'; j <= 'z'; j++)
if (cn[j]) {
int l = cn[j] / i;
if (l * i < cn[j]) l++;
while (l--) res += char(j);
};
if (res.length() <= n) {
ans = i;
break;
}
}
printf("%ld\n", ans);
if (ans != -1) {
while (res.length() < n) res += 'a';
cout << 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;
int n;
int freq[26], filled[26];
vector<pair<int, int> > vfreq;
bool good(int x) {
int sfilled = 0;
for (int i = 0; i < 26; i++) {
filled[i] = (freq[i] + x - 1) / x;
sfilled += filled[i];
}
if (sfilled <= n) return true;
return false;
}
int main() {
string s;
cin >> s;
cin >> n;
memset(freq, 0, sizeof freq);
for (int i = 0; i < s.size(); i++) {
freq[s[i] - 'a']++;
}
for (int i = 0; i < 26; i++) vfreq.push_back(make_pair(freq[i], i));
sort(vfreq.rbegin(), vfreq.rend());
memset(filled, 0, sizeof filled);
int lo = 1, hi = 1111;
while (lo != hi) {
int mid = (lo + hi) / 2;
if (!good(mid)) {
lo = mid + 1;
} else {
hi = mid;
}
}
if (lo == 1111) {
cout << -1 << endl;
return 0;
} else {
cout << lo << endl;
good(lo);
int pcnt = 0;
for (int i = 0; i < 26; i++) {
for (int j = 0; j < filled[i]; j++) {
cout << (char)('a' + i);
pcnt++;
}
}
for (int i = pcnt; 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 | #include <bits/stdc++.h>
using namespace std;
int n;
string str;
map<char, int> num;
void print(char x, int y) {
for (int i = 0; i < y; i++) {
cout << x;
}
}
bool check(double x) {
int s = 0;
for (auto tmp : num) {
s += ceil(tmp.second / x);
}
if (s <= n) {
return true;
}
return false;
}
void solve() {
for (int i = 0; i < str.size(); i++) {
num[str[i]]++;
}
int r = 1010, l = 0;
while (l + 1 < r) {
int m = (r + l) / 2;
if (check(m)) {
r = m;
} else {
l = m;
}
}
if (r == 1010) {
cout << -1;
} else {
cout << r << endl;
int cnt = 0;
for (auto tmp : num) {
int x = ceil(tmp.second / (double)r);
print(tmp.first, x);
cnt += x;
}
if (cnt < n) {
print('a', n - cnt);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> str;
cin >> n;
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 n, m;
char s[1001];
int main() {
scanf("%s%d", s, &n);
m = strlen(s);
vector<int> c(26, 0);
for (int i = 0; i < m; ++i) {
c[s[i] - 'a'] += 1;
}
m = 0;
for (int i = 0; i < 26; ++i) {
if (c[i] > 0) {
m += 1;
}
}
if (m > n) {
printf("-1\n");
return 0;
}
int pl = 0, pr = 10000;
while (pl + 1 < pr) {
int pm = pl + (pr - pl) / 2;
int count = 0;
for (auto i : c) {
count += (i + pm - 1) / pm;
}
(count <= n ? pr : pl) = pm;
}
string res = "";
for (int i = 0; i < 26; ++i) {
int count = (c[i] + pr - 1) / pr;
res += string(count, 'a' + i);
}
while ((int)res.length() < n) {
res += 'z';
}
printf("%d\n", pr);
printf("%s\n", res.c_str());
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | import java.util.Scanner;
import java.util.Map;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author BSRK Aditya
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
String s = in.next();
int n = in.nextInt();
Map<Character, Integer> ms = new HashMap<Character, Integer>();
for(int i = 0; i < s.length(); ++i) {
Character c = s.charAt(i);
ms.put(c, (ms.containsKey(c)?ms.get(c):0) + 1);
}
//for(Map.Entry e : ms.entrySet()) {
// out.println(e.getKey() + " " + e.getValue());
//}
if(ms.size() > n) {
out.println(-1);
} else {
int left = n - ms.size();
Map<Character, Integer> ans = new HashMap<Character, Integer>();
for(Character c : ms.keySet())
ans.put(c, 1);
while(left-- > 0) {
int max_turns = 0;
char maxc = '\0';
for(Map.Entry<Character,Integer> e : ans.entrySet()) {
char c = e.getKey(); int i = e.getValue();
if(ceil(ms.get(c), i) > max_turns) {
max_turns = ceil(ms.get(c), i);
maxc = c;
}
}
//out.println(maxc + " will be improved");
ans.put(maxc, ans.get(maxc)+1);
}
int max_turns = 0;
StringBuilder sb = new StringBuilder();
for(Map.Entry<Character, Integer> e : ans.entrySet()) {
char c = e.getKey();
int i = ceil(ms.get(c), e.getValue());
if(max_turns < i) max_turns = i;
i = e.getValue();
while(i-- > 0) sb.append(c);
}
out.println(max_turns);
out.println(sb.toString());
}
}
public static int ceil(int x, int y) {
return x/y + (x%y==0?0:1);
}
}
| JAVA |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int freq[30];
int mx, n;
bool check(int x) {
int cnt = 0;
for (int i = 0; i < 30; i++) {
cnt += ceil((double)freq[i] / x);
}
if (cnt <= n) return true;
return false;
}
int main() {
string s;
cin >> s;
int len = s.size();
int cnt = 0;
for (int i = 0; i < len; i++) {
freq[s[i] - 'a']++;
if (freq[s[i] - 'a'] == 1) cnt++;
mx = max(freq[s[i] - 'a'], mx);
}
cin >> n;
if (n < cnt) {
cout << -1;
return 0;
}
if (n == cnt) {
cout << mx << endl;
for (int i = 0; i < 26; i++)
if (freq[i] > 0) cout << (char)(i + 'a');
return 0;
}
int st = 1;
int en = 1000000;
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 counter = 0;
for (int i = 0; i < 26; i++) {
int temp = ceil(freq[i] / (double)ans);
while (temp > 0) {
cout << (char)(i + 'a');
temp--;
counter++;
}
}
int temp = n;
while (temp > counter) {
cout << 'a';
temp--;
}
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>
int main() {
const long N = 26;
std::string s;
std::cin >> s;
std::vector<long> a(N, 0);
long unique(0);
for (long p = 0; p < s.size(); p++) {
if (!a[s[p] - 'a']) {
++unique;
}
++a[s[p] - 'a'];
}
long n;
std::cin >> n;
if (n < unique) {
puts("-1");
return 0;
}
std::string sheet;
long num(0);
for (long u = 1; u <= s.size(); u++) {
if (num > 0) {
break;
}
std::string cand("");
for (long p = 0; p < a.size(); p++) {
long rep = a[p] / u + ((a[p] % u) > 0);
while (rep--) {
cand += ('a' + p);
}
}
if (cand.size() <= n) {
num = u;
sheet = cand;
break;
}
}
while (sheet.size() < n) {
sheet += 'a';
}
std::cout << num << std::endl << sheet << std::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.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class Main{
static Scanner scan = new Scanner(new BufferedInputStream(System.in));
public static void main(String[] args){
while(scan.hasNext()){
int[][] a = new int[2][26];
String str = scan.nextLine();
int n = Integer.parseInt(scan.nextLine());
for(int i=0; i<str.length(); i++){
a[0][(int)(str.charAt(i) - 'a')]++;
}
int num = 0;
boolean flag = false;
for(int i=0; i<26; i++)
if(a[0][i] != 0) num++;
for(int i=0; i<26; i++){
a[1][i] = 1;
}
int max = 0;
if(num<=n){
while(num != n){
for(int i=0; i<26; i++){
if(((double)a[0][max] / (double)a[1][max]) >= ((double)a[0][i] / ((double)a[1][i]))){
}else{
max = i;
}
}
n--;
a[1][max]++;
}
int nmax = 0;
for(int i=0; i<26; i++){
if(((double)a[0][nmax] / (double)a[1][nmax]) >= (((double)a[0][i] / (double)a[1][i]))){
}else{
nmax = i;
}
}
if((a[0][nmax])%a[1][nmax] == 0) System.out.println((a[0][nmax])/a[1][nmax]);
else System.out.println(((a[0][nmax])/a[1][nmax])+1);
String strr = "";
for(int i=0; i<26; i++){
while(a[1][i] != 1){
strr += (char)('a'+i);
a[1][i]--;
}
}
for(int i=0; i<26; i++){
if(a[0][i]!=0) strr += (char)('a'+i);
}
System.out.println(strr);
}else{
flag = true;
}
if(flag) System.out.println("-1");
}
}
} | JAVA |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
void data() {
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
}
char s[1100];
int su[27];
int k;
int jd(int n) {
int cnt = 0;
for (int i = 0; i < (26); ++i) {
cnt += (su[i] + n - 1) / n;
}
if (cnt <= k)
return 1;
else
return 0;
}
void pt(int n) {
cout << n << endl;
string ss = "";
int cnt = 0;
for (int i = 0; i < (26); ++i) {
for (int j = 0; j < ((su[i] + n - 1) / n); ++j) {
ss += (i + 'a');
cnt++;
}
}
cout << ss;
for (int i = 0; i < (k - cnt); ++i) cout << 'a';
cout << endl;
}
void bit(int l, int r) {
while (l < r) {
int mid = (l + r) / 2;
int kk = jd(mid);
if (kk > 0)
r = mid;
else
l = mid + 1;
}
pt(l);
}
int main() {
while (cin >> s >> k) {
for (int i = 0; i < (26); ++i) su[i] = 0;
for (int i = 0; i < (strlen(s)); ++i) su[s[i] - 'a']++;
int ma = 0, cnt = 0;
for (int i = 0; i < (26); ++i)
if (su[i]) {
cnt++;
ma = max(ma, su[i]);
}
if (k < cnt)
cout << -1 << endl;
else {
for (int i = 1; i <= (strlen(s)); ++i) {
if (jd(i)) {
pt(i);
break;
}
}
}
}
return 0;
}
| CPP |
335_A. Banana | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
Input
The first line contains string s (1 ≤ |s| ≤ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print instead a single line with the number -1.
Examples
Input
banana
4
Output
2
baan
Input
banana
3
Output
3
nab
Input
banana
2
Output
-1
Note
In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
/**
* @param args
*/
static char str[];
static int count[] = new int[26];
static int length;
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer read( ){
try {
return new StringTokenizer(read.readLine());
} catch (IOException e) {
}
return null;
}
public static boolean isPo( int k, int n ){
int length = n;
for( int i = 0; i < 26; i++ ){
int need = count[i]/k;
if( need*k < count[i] ) need++;
length -= need;
if( length < 0 ) return false;
}
return true;
}
public static void main(String[] args) {
StringTokenizer token = read();
str = token.nextToken().toCharArray();
length = str.length;
int difChar = 0;
for( int i = 0; i < length; i++ ){
if( count[ (int)(str[i]-'a') ] == 0 ) difChar++;
count[ (int)(str[i]-'a') ]++;
}
int N = Integer.parseInt(read().nextToken());
if( N < difChar ){ System.out.println(-1); return; }
int l = 1, r = length;
while( l < r ){
int mid = (l+r)/2;
if( isPo(mid, N) ) r = mid;
else l = mid+1;
}
System.out.println(l);
int prt = 0;
for( int i = 0; i < 26; i++ ){
if( count[i] == 0 ) continue;
int tot = count[i]/l;
if( tot*l < count[i] ) tot++;
prt += tot;
for( int j = 0; j < tot; j++ ) System.out.print( (char)( (int)'a'+i) );
}
if( prt < N ){
prt = N-prt;
for( int i = 0; i < 26; i++ ){
if( count[i] > 0 ){
for( int j = 0; j < prt; j++ ) System.out.print( (char)( (int)'a'+i) );
break;
}
}
}
System.out.println();
}
}
/*
*
10
71 59 88 55 18 98 38 73 53 58
20
1 5 93
1 7 69
2 3
1 1 20
2 10
1 6 74
1 7 100
1 9 14
2 3
2 4
2 7
1 3 31
2 4
1 6 64
2 2
2 2
1 3 54
2 9
2 1
1 6 86
0 0 0 0 0 0 0 0 0 0
*/
| 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
s = raw_input()
n = input()
d = [0] * 40
for i in s:
d[ord(i) - 97] += 1
sn = len(filter(lambda x: x > 0, d))
if n < sn:
print -1
else:
for ans in xrange(1, len(s) + 1):
tmp = 0
for i in xrange(40):
tmp += (d[i] + ans - 1) / ans
if tmp <= n:
cnt = 0
print ans
for i in xrange(40):
for j in xrange ((d[i] + ans - 1) / ans):
cnt += 1
sys.stdout.write(chr(i + 97))
while cnt < n:
sys.stdout.write('a')
cnt += 1
break
| 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;
char s[1005];
int n;
char ans[1004];
int cnt[40];
bool check(int num) {
int c = 0;
for (int i = 0; i < 40; i++)
if (cnt[i] > 0) c += cnt[i] / num + (cnt[i] % num != 0);
return c <= n;
}
int main() {
scanf("%s", s);
scanf("%d", &n);
int len = strlen(s);
for (int i = 0; i < len; i++) cnt[s[i] - 'a']++;
int sz = 0;
for (int i = 0; i < 40; i++)
if (cnt[i] > 0) ans[sz++] = 'a' + i;
if (sz > n) {
printf("-1\n");
return 0;
}
int l = 1, r = 1000000;
while (l < r) {
int mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid + 1;
}
while (!check(l)) l++;
sz = 0;
for (int i = 0; i < 40; i++) {
int c = cnt[i] / l + (cnt[i] % l != 0);
for (int j = 0; j < c; j++) ans[sz++] = 'a' + i;
}
for (int i = sz; i < n; i++) ans[i] = 'a';
printf("%d\n%s\n", l, ans);
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.