UVa11730 - Number Transformation(欧拉筛选法、动态规划)
2014-10-12 12:30
661 查看
You are given an integer number S. Youcan transform any integer number A to another integer number B by adding x to A.This x is an integer number which is a prime factor of A (Please note that 1and A are not being considered as a factor
of A). Now, your task is to find theminimum number of transformations required to transform S to another integernumberT.
Input
For each test case, there will be a line withtwo integers, S (1<=S<=100) &T (1<=T<=1000),as described above. The last test case will be followed by a line with two 0 sdenoting end of output.
This case should not be processed.
Output
Forevery test case except the last one, print a line of the form “Case X: Y” whereX is the serial number of output (starting from 1). Y is the minimum number oftransformations required to transformSto
T. If it is not possible to makeT from S with the given rules, Y shall be -1.
先用欧拉筛选法求出[1,1000]内的素数,然后用动态规划求出从s变换到t的最小变换数
注意,一个数的本身不是它的质因数,如11就不能变换成22
状态转移方程为:dp(i + factor(i)) = min(dp(i + factor(i)), dp(i) + 1),其中factor(i)表示i的质因数
of A). Now, your task is to find theminimum number of transformations required to transform S to another integernumberT.
Input
For each test case, there will be a line withtwo integers, S (1<=S<=100) &T (1<=T<=1000),as described above. The last test case will be followed by a line with two 0 sdenoting end of output.
This case should not be processed.
Output
Forevery test case except the last one, print a line of the form “Case X: Y” whereX is the serial number of output (starting from 1). Y is the minimum number oftransformations required to transformSto
T. If it is not possible to makeT from S with the given rules, Y shall be -1.
Sample Input Output for Sample Input
6 12 6 13 0 0 | Case 1: 2 Case 2: -1 |
先用欧拉筛选法求出[1,1000]内的素数,然后用动态规划求出从s变换到t的最小变换数
注意,一个数的本身不是它的质因数,如11就不能变换成22
状态转移方程为:dp(i + factor(i)) = min(dp(i + factor(i)), dp(i) + 1),其中factor(i)表示i的质因数
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Scanner; import java.util.Arrays; import java.util.ArrayList; public class Main implements Runnable { private static final boolean DEBUG = false; private static final int MAXN = 1001; private static final int INF = 0x7fffffff; private PrintWriter cout; private Scanner cin; private int s, t; private boolean[] vis = new boolean[MAXN]; private int[] dp = new int[MAXN]; private ArrayList<Integer> vPrime = new ArrayList<Integer>(); private int cas = 1; private void init() { try { if (DEBUG) { cin = new Scanner(new BufferedInputStream(new FileInputStream( "d:\\OJ\\uva_in.txt"))); } else { cin = new Scanner(new BufferedInputStream(System.in)); } preprocess(); } catch (Exception e) { e.printStackTrace(); } cout = new PrintWriter(new OutputStreamWriter(System.out)); } private void preprocess() { vis[0] = vis[1] = true; for (int i = 2; i < MAXN; i++) { if (!vis[i]) { vPrime.add(i); } for (int j = 0, size = vPrime.size(); j < size && i * vPrime.get(j) < MAXN; j++) { vis[i * vPrime.get(j)] = true; if (i % vPrime.get(j) == 0) break; } } } private boolean input() { s = cin.nextInt(); t = cin.nextInt(); if (s == 0 && t == 0) return false; return true; } private void solve() { Arrays.fill(dp, INF); dp[s] = 0; for (int i = s; i <= t; i++) { if (dp[i] != INF) { for (int j = 0, size = vPrime.size(); j < size; j++) { if (vPrime.get(j) >= i) break; if (i % vPrime.get(j) == 0) { if (i + vPrime.get(j) > t) break; dp[i + vPrime.get(j)] = Math.min(dp[i + vPrime.get(j)], dp[i] + 1); } } } } if (dp[t] == INF) { cout.println("Case " + cas++ + ": -1"); } else { cout.println("Case " + cas++ + ": " + dp[t]); } cout.flush(); } public void run() { init(); while (input()) { solve(); } } public static void main(String[] args) { new Thread(new Main()).start(); } }
相关文章推荐
- uva103 动态规划
- UVa 11417 GCD (欧拉φ函数)
- [动态规划]UVA 507 - Jill Rides Again
- 【UVA】10285-Longest Run on a Snowboard(动态规划)
- 动态规划 DAG问题uva 437 The Tower of Babylon
- 紫书章六例题16 单词 UVA 10129(欧拉道路)
- UVA - 357 - Let Me Count The Ways (动态规划)
- uva 674 - Coin Change 动态规划
- UVA1630[Folding] 区间动态规划
- UVa 116 - Unidirectional TSP(动态规划)
- UVA 10859 Placing Lampposts (动态规划)
- CJOJ 1071 【Uva】硬币问题(动态规划)
- UVA - 10129 - Play on Words <欧拉道路+并查集>
- 【动态规划】[UVA12563]Jin Ge Jin Qu hao 劲歌金曲
- UVA 10739 String to Palindrome (增删字符将非回文串串变身回文串,动态规划dp )
- [UvaLive 6756] Increasing Shortest Path [动态规划]
- UVA11384-Help is needed for Dexter 动态规划
- uva 116(动态规划起步第三天 DAG)
- uva11400 动态规划
- [动态规划]UVA108 - Maximum Sum