[Daily Blue Bridge] 32. One-five years provincial Java group real question "base dice"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Drink Redemption

The gambler atm became obsessed with dice in his later years, that is, put the dice on top of the other, not crookedly, they must be square cylinders.

After long-term observation, ATM has discovered the mystery of stable dice, and the faces of some numbers will repel each other;

Let's standardize the dice first, the opposite of 1 is 4, the opposite of 2 is 5, and the opposite of 3 is 6.

Assuming that there are m groups of mutual exclusion, the faces of the two numbers in each group are close to each other, and the dice cannot be stably built up.

Atm wants to calculate how many different possible ways to base the dice.

The two ways to base the dice are the same, if and only if the directions of the corresponding numbers of the dice of the corresponding height in the two ways are the same.

Since there may be too many solutions, please output the result modulo 10^9+7

Don't underestimate the number of dice in ATM

[Input format]

Two integers n and m in the first line

n is the number of dice

In the next m lines, each line contains two integers ab, which means that a and b cannot be close together

[Output format]

One number per line, which means the result of the answer modulo 10^9+7

【Sample input】

2 1

1 2

[Sample output]

544

【data range】

For 30% of the data: n<=5

For 60% of the data: n<=100

For 100% data: 0<=.n<=10^9, m<=36

Resource agreement:

Peak memory consumption (including virtual machines) <256M

CPU consumption <2000ms

Please output in strict accordance with the requirements, and do not print superfluous content like "please enter...".

All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.

Note: Do not use package statement, do not use jdk1.6 and above version features

Note: The name of the main class must be Main, otherwise it will be treated as an invalid code.

Problem-solving ideas:

This question can be solved using the idea of ​​recursion. Perform a deep search and recursion on each side of the six dice, calculate the possibility of each side, and then add all the possibilities to get the final answer

Answer source code:

public class Year2015_Bt9 {

	static long MOD = 1000000007;
	static int n = 0;
	static int[][] rejectArr = new int[7][7];
	static int[] upToDown = new int[7];
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		int m = scanner.nextInt();
		init();//对面骰子数据初始化
		for (int i = 0; i < m; i++) {
			int x = scanner.nextInt();
			int y = scanner.nextInt();
			rejectArr[x][y] = 1;
			rejectArr[y][x] = 1;
		}
		long ans = 0;
		for (int j = 1; j <= 6; j++) {
			ans = (ans+4*f(n-1, j))%MOD;
		}
		System.out.println(ans);
		
	}

	private static long f(int num,int top) {
		
		if (num==0) {			
			return 4;
		}
		long ans=0;
		//枚举骰子上面的数字的值
		for (int j = 1; j <= 6; j++) {
			//如果对应的两个面排斥
			if (rejectArr[top][upToDown[j]]==1) continue;			
			ans = (ans + f(num-1, j))%MOD;
		}
		return ans;
	}
	
	public static void init() {
		upToDown[1] = 4;
		upToDown[2] = 5;
		upToDown[3] = 6;
		upToDown[4] = 1;
		upToDown[5] = 2;
		upToDown[6] = 3;
	}

}

 

Sample output:

 

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

 

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/114944022