您的位置:首页 > 其它

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.

 

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: