Problem B: Persistent Numbers
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 |
3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 |
4 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 |
5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |
6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 |
7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 |
8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 |
9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 |
679 -> 378 -> 168 -> 48 -> 32 -> 6.
The problem that you are to solve here is: what is the smallest numbersuch that the first step of computing its persistence results in thegiven number?
For each test case there is a single line of input containing a decimal number with up to 1000 digits. A line containing -1 follows thelast test case.For each test case you are to output one line containing one integernumber satisfying the condition stated above or a statement saying thatthere is no such number in the format shown below.
Sample input
0 1 4 7 18 49 51 768 -1
Output for sample input
10 11 14 17 29 77 There is no such number. 26881、s位数只有1位时,直接输出1s
2、将s表示成pow(2,n1) * pow(3, n2) * pow(5, n3) * pow(7, n4),然后分别除以9, 8,7,6,5,4,3,2
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.HashMap;
public class Main implements Runnable
{
private static final boolean DEBUG = false;
private static final int[] prime = {2, 3, 5, 7};
private BufferedReader cin;
private PrintWriter cout;
private StreamTokenizer tokenizer;
String s;
private void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
tokenizer = new StreamTokenizer(cin);
tokenizer.resetSyntax();
tokenizer.wordChars('0', '9');
tokenizer.wordChars('a', 'z');
tokenizer.wordChars('A', 'Z');
tokenizer.wordChars('-', '-');
tokenizer.wordChars(128 + 32, 255);
tokenizer.whitespaceChars(0, ' ');
tokenizer.quoteChar('\'');
tokenizer.quoteChar('"');
tokenizer.commentChar('/');
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
private String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tokenizer.nval);
} else return tokenizer.sval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private boolean input()
{
s = next();
if ("-1".compareTo(s) == 0) return false;
return true;
}
private void solve()
{
if (s.length() <= 1) {
cout.println("1" + s);
cout.flush();
return;
}
BigInteger bi = new BigInteger(s);
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for (int i = 0; i < prime.length; i++) {
if (bi.remainder(BigInteger.valueOf(prime[i])).compareTo(BigInteger.ZERO) == 0) {
int cnt = 0;
while (bi.remainder(BigInteger.valueOf(prime[i])).compareTo(BigInteger.ZERO) == 0) {
cnt++;
bi = bi.divide(BigInteger.valueOf(prime[i]));
}
hm.put(prime[i], cnt);
}
}
if (bi.compareTo(BigInteger.ONE) != 0) {
cout.println("There is no such number.");
cout.flush();
return;
}
StringBuilder sb = new StringBuilder();
while (hm.containsKey(3) && hm.get(3) >= 2) {
int val = hm.get(3);
val -= 2;
hm.put(3, val);
sb.append("9");
}
while (hm.containsKey(2) && hm.get(2) >= 3) {
int val = hm.get(2);
val -= 3;
hm.put(2, val);
sb.append("8");
}
while (hm.containsKey(7) && hm.get(7) > 0) {
int val = hm.get(7);
val -= 1;
hm.put(7, val);
sb.append("7");
}
while (hm.containsKey(2) && hm.containsKey(3) && hm.get(2) > 0 && hm.get(3) > 0) {
int val = hm.get(2);
val -= 1;
hm.put(2, val);
val = hm.get(3);
val -= 1;
hm.put(3, val);
sb.append("6");
}
while (hm.containsKey(5) && hm.get(5) > 0) {
int val = hm.get(5);
val -= 1;
hm.put(5, val);
sb.append("5");
}
while (hm.containsKey(2) && hm.get(2) >= 2) {
int val = hm.get(2);
val -= 2;
hm.put(2, val);
sb.append("4");
}
while (hm.containsKey(3) && hm.get(3) > 0) {
int val = hm.get(3);
val -= 1;
hm.put(3, val);
sb.append("3");
}
while (hm.containsKey(2) && hm.get(2) > 0) {
int val = hm.get(2);
val -= 1;
hm.put(2, val);
sb.append("2");
}
cout.println(sb.reverse().toString());
cout.flush();
}
public void run()
{
init();
while (input()) {
solve();
}
}
public static void main(String[] args)
{
new Thread(new Main()).start();
}
}