UVa10527 - Persistent Numbers(数论)

来源:互联网 发布:淘宝同款排除工具 编辑:程序博客网 时间:2024/05/17 22:15

Problem B: Persistent Numbers

 1234567891 1  2  3  4  5  6  7  8  9 2  2  4  6  8 1012141618 3  3  6  9 121518212427 4  4  8 12162024283236 5  5 1015202530354045 6  6 1218243036424854 7  7 1421283542495663 8  8 1624324048566472 9  9 1827364554637281 The multiplicative persistence of a number is defined by Neil Sloane(Neil J.A. Sloane inThe Persistence of a Number publishedin Journal of Recreational Mathematics 6, 1973, pp. 97-98., 1973) asthe number of steps to reach a one-digit number when repeatedlymultiplying the digits. Example:
679 -> 378 -> 168 -> 48 -> 32 -> 6.
That is, the persistence of 679 is 5. The persistence of a singledigit number is 0. At the time of this writing it is known that there are numbers with the persistence of 11. It is not known whether there are numberswith the persistence of 12 but it is known that if they exists then thesmallest of them would have more than 3000 digits.

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

0147184951768-1

Output for sample input

101114172977There is no such number.2688
1、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();}}


  

0 0