Luogu P1490 Problem Solving Report

P1490 buy cake

Topic description

For the wild cat's birthday, of course everyone will give gifts (cough, comrades who haven't given gifts, pay attention!!), because I don't know what to give, and considering other issues such as practicality, everyone decided to buy a birthday for the wild cat. cake. Everyone does not know the exact price of the cake to be bought in the end, but only gives an estimate of the cake, that is, to buy a cake that does not exceed how much money. All OIers take this to play: Can the minimum number of coins be used to make up all the values ​​within the valuation range, so that no matter how much the cake is worth, there is no need to find money...

Now the question arises from this: for a given n, can the least unequal positive integers be used to form all positive integers within n (including n)? If so, how many positive integers are required at least, and how many different composition methods can be used with the minimum number?

Input and output format

Input format:

Only one line contains an integer n (1<=n<=1000).

Output format:

There are two numbers in a line. The first number is the minimum number of numbers required, and the second number is the number of the minimum number to form the program. Separate the two answers with a space.


  • First clarify the first question: what is the smallest positive integer?

Maybe you can check it out, maybe not, but don't worry, we haveseemsreliable way of thinking

Take a look at the example: 6

Possible solutions:

\(1\) \(2\) \(3\);

\(1\) \(2\) \(4\).

We found that for scheme ①, there are two ways (1+2 or 3) when composing 3, while scheme ② has only one. In other words, the use of 3 is wasteful . The plan ② without waste can also be composed of 7.

So, how do we make her (every number) use herself well?

It's easy, just lily

Think about the numbers under the binary digits

\(1\),\(10\),\(11\),\(100\),\(101\),\(110\),\(111\),\(1000\)...

Isn't it, the utilization rate of each number of this \(2^i\) can be high

It can be seen that the number of digits in binary is the smallest positive integer .


  • After you understand the first question, you should give a relative thinking direction for the second question. (Of course not absolutely)

When the utilization of each number is the largest, the largest integer they can make up is their sum, which is beyond doubt.

So what about when utilization is relatively not that big?

We notice that there is already a constraint at this time: the smallest positive integer that exists

Manually simulate it, it is indeed still established. (Actually, I don't know much about it.)

At this time, we put the sum of the numbers used in the participation amount and the largest integer together.

Considering that \(dp[k]\) represents the number of solutions of \(k\) when making up. Let's see what information is going to be added at this time.

obviously, the remaining necessary information is the value of the \(i\) th and \(i\)th number \(j\)

\(dp[i][j][k]\) indicates the number of selected \(i\) , the number of \(i\) is \(j\) , and the sum of the first \(i\) is The number of solutions when \(k\) (the largest integer bit made up \(k\) )

Transition equation \(dp[i+1][l][k+l]+=dp[i][j][k];\)

Where \(l\) is the next fill number of the enumeration

Core code:

    dp[1][1][1]=1;
    for(int i=1;i<ans;i++)
        for(int j=i;j<=(1<<(i-1));j++)
            for(int k=i*(i-1)/2;k<(1<<i);k++)
                for(int l=j+1;l<=k+1;l++)
                    if(l+k<=n)
                        dp[i+1][l][k+l]+=dp[i][j][k];
                    else
                        dp[i+1][l][n]+=dp[i][j][k];

Note that the upper and lower bounds of \(j,k,l\) are all constrained by the first question that has been obtained

Of course, there is no need to run so dead. For example, \(k\) will be faster starting from \(i\) .

As for the judgment of \(if\) and \(else\) , it is a little bit of greed for the convenience of seeking the final result.


2018.5.2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382490&siteId=291194637