[Luogu p1118] [USACO06FEB] Digital triangle

Face questions

Title Description

FJ and his cows enjoy playing a mental game. They write down the numbers from \(1\) to$ N(1 \le N \le 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=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 \(N\). 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 write \ (1 \) to \ (N \) arranged \ (a_i \) , then each adjacent two numbers together, constitute a new sequence, and then this new sequence operation, a clearly less each time than the length of the sequence consisting of the sequence \ (1 \) , until only one digit position. Here is an example: \ (3,1,2,4 \) \ (4,3,6 \) \ (7,9 \) \ (16 \) finally obtained \ (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 \ (a_i \) , as \ (1 \) to \ (N \) an arrangement. If there are several possible answers, output lexicographically smallest one.

Administrator Note: This title is incorrect description, lexicographical here refers to \ (1,2,3,4,5,6,7,8,9,10,11,12 \) instead of \ (1, 10, 11,12,2,3,4,5,6,7,8,9 \)

Input and output formats

Input Format

Two positive integers \ (n-, SUM \) .

Output Format

The output includes \ (1 \) line, is lexicographically smallest one answer. When no solution, please outputs nothing. (Good wonderful ah)

Sample input and output

Input Sample # 1

4 16

Sample Output # 1

3 1 2 4

Explanation

For \ (40 \% \) data \ (n≤7 \) ;
for \ (80 \% \) data \ (n ≤ 10 \) ;
for \ (100 \% \) data \ (n ≤ 12, sum≤12345 \) .

analysis

This question is obviously searching, but no brain enumeration next_permutationtime complexity of the explosion, so we might as pushing equation to find the law.
If there is a triangle \ (n \) layer, the number of the first row are \ (A_1, A_2, \ ldots, A_N \) , we can launch (\ sum) \ the value of it?
If \ (n-=. 1 \) , then the answer is clearly \ (A_1 \) ;
if \ (n-2 = \) , the answer is that both the layer and, as a natural \ (A_2 A_1 + \) ;
if \ (n-=. 3 \) , draw the triangle:
\ [A_1 \ qquad \ Quad A_2 \ qquad \ \\ A_1 + Quad A_3 A_2 \ \\ Quad A_2 A_3 A_1 + + + 2a_2 A_3 \]
answer \ (a_1 + 2a_2 A_3 + \) .
If \ (n-=. 4 \) : \
[A_1 \ qquad \ qquad A_2 \ qquad \ qquad A_3 \ qquad \ \\ qquad A_4 A_2 A_1 + \ + qquad A_2 A_3 \ \\ A_4 A_1 qquad A_3 + + + 2a_2 A_3 \ quad a_2 + 2a_3 + a_4 \\ a_1
+ 3a_2 + 3a_3 + a_4 \] answer\(a_1+3a_2+3a_3+a_4\)
如果\(n = 5\)
\[ a_1\qquad \qquad \quad a_2\qquad \qquad \quad a_3 \qquad\qquad \quad a_4 \qquad\qquad a_5\\ a_1+a_2 \qquad \quad a_2+a_3 \qquad \quad a_3+a_4 \qquad \quad a_4+a_5\\ a_1+2a_2+a_3 \quad a_2+2a_3+a_4 \quad a_3+2a_4+a_5\\ a_1+3a_2+3a_3+a_4 \quad a_2+3a_3+3a_4+a_5\\ a_1+4a_2+6a_3+4a_4+a_5 \]
答案为\(a_1+4a_2+6a_3+4a_4+a_5\)
列个表:

\(n\) \(sum\)
\(1\) \(a_1\)
\(2\) \(a_1+a_2\)
\(3\) \(a_1+2a_2+a_3\)
\(4\) \(a_1+3a_2+3a_3+a_4\)
\(5\) \(a_1+4a_2+6a_3+4a_4+a_5\)
\(6\) \(a_1+5a_2+10a_3+10a_4+5a_5+a_6\)
\(\ldots\) \(\dots\)

If we throw away the letters do not look only factor, what would be the result?

\(n\) \(\text{coefficient}\)
\(1\) \(1\)
\(2\) \(1,1\)
\(3\) \(1,2,1\)
\(4\) \(1,3,3,1\)
\(5\) \(1,4,6,4,1\)
\(6\) \(1,5,10,10,5,1\)
\(\ldots\) \(\dots\)

Smart you must see it, this is Pascal's Triangle.
And it happens to be the subject of a known \ (SUM \) , find the original formula, and \ (the n-\) , \ (SUM \) and the original array was closely tied to Pascal's triangle. We just need to enumerate the current number by searching multiplied by the corresponding figure Pascal's Triangle, and then see if the sum is equal to \ (SUM \) , if equal to the output end.
Here we can Pascal triangle pretreated with a formula:
\ [C ^ C ^ \\ 0_n K_n. 1 = = \ {n-FRAC. 1-K + K {}} ^ {K-C. 1} _n \]

Code

#include <iostream>
#include <cstdio>

const int maxn = 25;
int n,sum;
bool vis[maxn];
int ans[maxn];
int c[maxn];

bool dfs(int nownum, int nowsum, int step) {
    if (nowsum > sum) return false;//可行性剪枝
    if (step == n) {
        if(nowsum == sum) {
            ans[n] = nownum;
            return true;
        }
        return false;//同样是可行性剪枝,但评测的时候我没写这句也AC了umm
    }

    vis[nownum] = true;
    for (int j = 1; j <= n; j++) {
        if (vis[j]) continue;
        if(dfs(j,nowsum+c[step]*j,step+1)) {
            ans[step] = nownum;
            return true;
        }
    }

    vis[nownum] = false;//回溯
    return false;
}

void pastri() {
    c[0] = c[n-1] = 1;
    if (n == 1) return ; 
    for (int i = 1; i * 2 < n; i++)
        c[i] = c[n-i-1] = (n-i) * c[i-1] / i;
}//用组合数预处理杨辉三角。

int main() {
    scanf("%d%d",&n,&sum);
    pastri();

    if (dfs(0,0,0)) 
        for (int i = 1; i <= n; i++)
            printf("%d ",ans[i]);

    puts("");
    return 0;
}

Record Reviews

AC 100: R30917956

over.

Guess you like

Origin www.cnblogs.com/crab-in-the-northeast/p/luogu-p1118.html