Blue Bridge Cup 2016 7th C/C + + provincial competition group A question 6 - winter vacation homework

Title:

Now the math problems in primary school are not so fun.
Look at this winter vacation assignment:
Each box represents a number from 1 to 13, but cannot be repeated.
For example:
 6  + 7 = 13
 9  - 8 = 1
 3  * 4 = 12
 10 / 2 = 5

And:
 7  + 6 = 13
 9  - 8 = 1
 3  * 4 = 12
 10 / 2 = 5

Even two solutions. (different schemes of addition, multiplication and commutative law)
How many options have you found?

Please fill in an integer representing the number of schemes.
Note: what you submit should be an integer. Do not fill in any redundant content or explanatory text.

No idea, look at the problem solution one day:

This is a mathematical solution.

# 6 + 7 = 13 deformation: 7 + 6 = 13 13-7 = 6 13-6 = 7
# 9 - 8 = 1 deformation: 9-1 = 8 1 + 8 = 9 8 + 1 = 9
# 3 * 4 = 12 deformation: 4 * 3 = 12 12 / 4 = 3 12 / 3 = 4
# 10 / 2 = 5 deformation: 10 / 5 = 2 2 * 5 = 10 5 * 2 = 10

# Addition: 6 + 7 = 13 7 + 6 = 13 1 + 8 = 9 8 + 1 = 9
# Subtraction: 9-8 = 1 9-1 = 8 13-7 = 6 13-6 = 7
# Multiplication: 3 * 4 = 12 4 * 3 = 12 2 * 5 = 10 5 * 2 = 10
# Division: 10 / 2 = 5 10 / 5 = 2 12 / 4 = 3 12 / 3 = 4

# 6,7,13 combination: 2 * 2 * 4 * 2 = 32
# Combination of 1, 8 and 9: ibid
#So:
print(64)

After looking for some, I found that this topic was written by deep search and pruning:

It seems that many articles do not have detailed and specific explanations, so I'll explain them in detail.

Code source and blog:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
 
int res=0;
int a[]={1,2,3,4,5,6,7,8,9,10,11,12,13};
void dfs(int start)
{
	if(start>=3 )
	  if(a[0]+a[1]!=a[2]) return ;//Make an equation judgment on the first three numbers determined. If they do not meet the requirements, do not continue to search
	if(start>=6)
	  if(a[3]-a[4]!=a[5]) return ;//Similarly, judge the second equation and prune
	if(start>=9)
	  if(a[6]*a[7]!=a[8]) return ;
	if(start>=12)
	  if(a[11]*a[10]==a[9])
	  {
	  	for(int i=0;i<12;i++)
	  	  cout<<a[i]<<" ";
	  	cout<<endl;
		res++;
		return ;
	  } 
	for(int i=start;i<=12;i++)
	{
		int temp=a[start];
		a[start]=a[i];
		a[i]=temp;
		
		dfs(start+1);
		
		temp=a[start];
		a[start]=a[i];
		a[i]=temp;
	}
}
int main()
{
	dfs(0);
	cout<<res<<endl;
	return 0;
}

The following section explains:

  • The whole trial process is actually the trial of three numbers for every three numbers. From the first number to the last number, every three numbers will be sorted and traversed after themselves, find the three numbers that meet the equation, and then try to buy the next number
  • Start means to judge from the first number, that is, the number of start money to buy is fixed.
  • Among them, the first few in dfs are pruning. For example, if the first three numbers traverse to 1, 2 and 4, you can know that if they are not satisfied, you don't need to enumerate the cases starting with 1, 2 and 4. Judge every three, so add 3 to the if condition of start every time. If return passes, it means that the three numbers are valid in the three places of start~start+3, The following three numbers are enumerated

In this way, you should know what the first if and return mean, and then the key lines of code:

for(int i=start;i<=12;i++)
	{
		int temp=a[start];
		a[start]=a[i];
		a[i]=temp;
		
		dfs(start+1);
		
		temp=a[start];
		a[start]=a[i];
		a[i]=temp;

What is this for, what is it used to achieve, and why it can complete the functions we need. To tell you the truth, I haven't understood what the two swap s are for and why they are used. After continuous debugging, I finally managed to understand how they enumerate. But to tell you the truth, I can't think of it or write it out if I put it in the examination room.

When following the program for the first time, the first 1, 2 and 3 must satisfy 1 + 2 = 3, so start to continue 4, 5 and 6, and you will find that the second formula is not satisfied. Therefore, you need to constantly try to traverse the fourth, fifth and sixth digits in the remaining nine digits, that is, i start from 5 and continue to exchange to 12 to see if the second formula can be satisfied. Obviously, No matter whether bit 6 is exchanged with any of the following numbers, the for loop ends and a picture of the process is cut

Then, after the for loop is completed, start moves forward and changes to 4, that is, the fifth digit. Start from the fifth digit, continue to traverse, exchange a fifth digit, and then start continues to 6, and then cycle.

In fact, it is obvious that every three numbers are continuously divided. There are not three for each start. It is just to traverse in the way of deep search. The next steps are not described in detail here. You should understand it by debugging yourself.

The last question is what is the second swap for?

In fact, it's also easy to understand that the first swap is exchanged for strat and i, then dfs goes back, judges whether the formula is satisfied through if, does not meet the return, and then exchanges the numbers exchanged by the first swap back. In fact, the guarantee is that the traversal is accurate, and every possible situation will be enumerated to.....

Sort out your understanding. Maybe you didn't make it very clear, but you didn't think about how the deep search came out as a whole. Just remember this way first

Added by flhtc on Wed, 26 Jan 2022 05:10:38 +0200