Java implementation of the Blue Bridge Cup VIP number generation algorithms to improve

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Improve the algorithm produces several
time limit: 1.0s Memory Limit: 256.0MB

Problem Description

A given integer n (n <10 ^ 30), and the conversion rule of k (k <= 15).

rule:

Digit convertible into another digit:

Right portion of a rule can not be zero.

For example: n = 234. Rules (k = 2):

2-> 5

3-> 6

Integer Integer above 234 after the conversion is possible (including the original number):

234

534

264

564

4 different number generating

problem:

A given n and k integer rules.

Calculated:

After transformation of any number of times (zero or more), you can produce a number of different integers.

Only the number of required output.
  Input format:
  NK
  X1 Y1
  X2 Y2
  ... ...
  Xn Yn
  output formats:
  an integer (number satisfying the condition):
Sample Input
234 2
2. 5
. 3. 6
Sample Output
4

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Vector;


public class 产生数 {
	static int[] v=new int[10];
	static Vector<Vector<Integer>> gz=new Vector<Vector<Integer>>();
	static int geti(int i){
		int a=1;
		v[i]=1;
		for (int j = 0; j <gz.get(i).size(); j++) {
			int k=gz.get(i).get(j);
			if(v[k]==1)continue;
			a+=geti(k);
		}
		return a;
	}
	public static void main(String[] args) throws IOException {
		for (int i = 0; i <10; i++) {
			gz.add(new Vector<Integer>());
		}
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String[] a1=bf.readLine().split(" ");
		int k=Integer.parseInt(a1[1]);
		int[] s=new int[a1[0].length()];
		for (int i = 0; i < a1[0].length(); i++) 
			s[i]=a1[0].charAt(i)-'0';
		for (int i = 0; i < k; i++) {
			a1=bf.readLine().split(" ");
			gz.get(Integer.parseInt(a1[0])).add(Integer.parseInt(a1[1]));
		}
		BigInteger num=new BigInteger("1");
		for (int i = 0; i < s.length; i++) {
			num=num.multiply(BigInteger.valueOf(geti(s[i])));
			v=new int[10];
		}
		//System.out.println(BigInteger.valueOf(2).pow(20));
		System.out.println(num);
	}

}

Guess you like

Origin blog.csdn.net/a1439775520/article/details/93298431