UVa10527 - Persistent Numbers(数论)

Problem B: Persistent Numbers

 123456789
1 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 in The 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

0
1
4
7
18
49
51
768
-1

Output for sample input

10
11
14
17
29
77
There 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();
	}
}


  

iptables-persistent是一个工具,用于在Linux系统上保存和恢复iptables防火墙规则。它可以通过调用iptables-save和iptables-restore命令来保存和加载防火墙规则。当系统重启或防火墙服务重新加载时,iptables-persistent会自动恢复之前保存的规则,确保防火墙规则持久有效。使用iptables-persistent可以简化管理防火墙规则的过程,并确保规则在系统重启后仍然有效。 你可以使用以下命令来保存和加载防火墙规则: 1. 保存规则:sudo /etc/init.d/iptables-persistent save 2. 重新加载规则:sudo /etc/init.d/iptables-persistent reload 另外,还有一些额外的脚本工具,如docker-iptables-save.sh和docker-iptables-restore.sh,可以用于制作NAT表和FILTER表的备份,并将它们放置在/etc/iptables目录中,带有基本时间戳记。这些工具可以帮助你更方便地管理防火墙规则的备份和恢复。 总之,iptables-persistent是一个实用的工具,可以确保在Linux系统上防火墙规则持久有效,并提供了一些辅助脚本工具来简化规则的备份和恢复过程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [使用iptables-persistent持久化iptables规则](https://blog.csdn.net/hehaibo2008/article/details/70768955)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [docker-iptables-restore:将iptables推送到docker主机时,可能会破坏docker创建的现有规则。 备份并稍后...](https://download.csdn.net/download/weixin_42177768/18715322)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值