forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabelmaker.java
More file actions
55 lines (48 loc) · 1.62 KB
/
labelmaker.java
File metadata and controls
55 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
import java.io.*;
import java.math.*;
public class labelmaker
{
public static void main (String[] argv) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int kase = Integer.parseInt(st.nextToken());
for (int k = 1; k <= kase; ++k) {
System.out.print(String.format("Case #%s: ", k));
String letters;
BigInteger n;
st = new StringTokenizer(in.readLine());
letters = st.nextToken();
n = BigInteger.valueOf(Long.parseLong(st.nextToken()));
int len = letters.length();
if (len == 1) {
for (Long i = 0L; i < n.longValue(); ++i) {
System.out.print(letters);
}
System.out.println();
continue;
}
int sz = 0;
BigInteger tmp = BigInteger.ONE;
BigInteger bLen = BigInteger.valueOf(len);
while (true) {
++sz;
tmp = tmp.multiply(bLen);
if (n.compareTo(tmp) <= 0) {
tmp = tmp.divide(bLen);
break;
}
n = n.subtract(tmp);
}
n = n.subtract(BigInteger.ONE);
StringBuilder ret = new StringBuilder();
for (int i = 0; i < sz; ++i) {
ret.append(letters.charAt(n.divide(tmp).intValue()));
n = n.mod(tmp);
tmp = tmp.divide(bLen);
}
System.out.println(ret.toString());
}
}
}