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.
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.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();}}
0 0
- UVa10527 - Persistent Numbers(数论)
- Persistent Numbers
- POJ 2325 Persistent Numbers
- poj 2325 Persistent Numbers
- poj 2325 Persistent Numbers
- poj 2325 Persistent Numbers
- POJ 2325 Persistent Numbers .
- Persistent Numbers(贪心+高精度)
- POJ 2325 Persistent Numbers 笔记
- POJ2325 Persistent Numbers(大数+贪心)
- POJ - 2325 Persistent Numbers【贪心、高精度】
- 【POJ2325】Persistent Numbers 贪心+高精度/低精度
- poj 2325 Persistent Numbers 贪心+高精度除法
- 数论-Special Numbers
- 数论 Special Numbers
- HDU1058 Humble Numbers 【数论】
- 数论-Special Numbers
- Raising Modulo Numbers(数论)
- Java获取数据库表列信息、索引信息、存储过程等信息 .
- 多备份:带领站长冲上“云端”
- 下推栈的数组实现
- hdu 2159 FATE
- ucos-ii学习笔记——消息邮箱的原理及使用
- UVa10527 - Persistent Numbers(数论)
- Mysql 批量更新操作的时候出错 “Multi-statement transaction required more than 'max_binlog_cache_size' bytes of s
- 为什么Java byte 类型的…
- 短信远程开机
- sizeof用法总结(个人感觉非常好的…
- 逻辑运算符<<和>>的详细分析
- C符号之逻辑运算符、左移与右移、…
- 利用union判断系统的大小端
- 易错的优先级