[Digital] USACO triangles `Backward Digit ...

[Digital] USACO triangles `Backward Digit ...

topic

Title Description

FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \le 10)N(1≤N≤10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4N=4) might go like this:

3   1   2   4
  4   3   6
    7   9
     16

Behind FJ’s back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number NN. Unfortunately, the game is a bit above FJ’s mental arithmetic capabilities.

Write a program to help FJ play the game and keep up with the cows.

There is such a game:

A_i arranged to write a 1 to N, and each adjacent two numbers together, constitute a new sequence, and then the new sequence of such operations, apparently constituting each sequence less than a sequence length 1, until only one digit position. Below is an example:

3,1,2,4

4,3,6

7,9

16

Finally, with 16 such a figure.

Backwards now want to play such a game, if you know N, know the size of the resulting digital sum, you find the original sequence ai, a permutation 1 to N. If there are several possible answers, output lexicographically smallest one.

Administrator Note: This topic describes error, here lexicographical it refers 1,2,3,4,5,6,7,8,9,10,11,12

Instead 1,10,11,12,2,3,4,5,6,7,8,9

Input Format

Two positive integers n, sum.

Output Format

The output includes one line, the minimum order is dictionary that answer.

When no solution, please outputs nothing. (Good wonderful ah)

Sample input and output

Copy Input # 1
416
output copy # 1
3124
Description / prompt
for 40% of the data, n≤7;

For 80% of the data, n≤10;

To 100% of the data, n≤12, sum≤12345.

analysis

First, look at the value of the sample law:
3 1 2 4
4 3 6 3 + 1 = 4 //; 3 = 1 + 2; 4 + 2 = 6;
79 @ 7 = 3 + 1 + 1 + 2; 9 = 1 + 2 + 2 + 4;
16 // 16 = 3 + 2 + 1 + 1 + 1 + 2 + 4 + 2;
3 times with the first row 1, three times with 1, 2 with the 3, 4 with the 1 times. : 16 = 3 1 + 1 3 + 2 3 + 4 . 1;
is (1,3,3,1) , which is not the fourth row do Pascal triangle.

Pascal's Triangle: (The following two numerical values equal to right and left and upper)
. 1
. 1. 1
. 1 2. 1
. 1. 1. 3. 3

So this question, we first construct the value of Pascal's Triangle save up, and then do a deep search. Specifically look at the code and comments.

Code

#include<iostream>

using namespace std;

int n,sum;
int y[20][20];    //杨辉三角
bool vis[20];	 //是否用过 
int a[20];		 //存放数据

bool f;
void dfs(int k,int s){
	if(s > sum || f ){		//如果累加的s超过了sum,就返回 
		return ;
	}
	if(k > n){				//如果k等于n, 
		if(s == sum){			//s等于sum,就输出a【】 
			cout<<a[1];
			for(int i=2;i<=n;i++){
				cout<<" "<<a[i];
				f = 1;
			}
		}
		return ;			 
	}
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			vis[i] = true;				//标记成用过
			a[k] = i;					//保存第k个取的数
			dfs(k+1,s+ y[n][k]*i);	//要乘上杨辉三角对应的数值。
			
			vis[i] = false;
		}
	}
} 
int main(){
	cin>>n>>sum;
		
	//生成杨辉三角
	y[1][1]=1;
	for(int i=2;i<=n;i++){
		for(int j=1;j<=i;j++){
			y[i][j] = y[i-1][j-1] + y[i-1][j];
		}
	}
	
	//1表示 1到n
	//0表示 累加,到sum时 
	dfs(1,0);
	
	return 0;
}
Published 75 original articles · won praise 1 · views 3641

Guess you like

Origin blog.csdn.net/A793488316/article/details/104725066