Time：2021-2-11

# What number should I guess next ？

The inspiration of this article comes from the recent team building of the Ministry of technology and the famous DP optimization “Eagle Egg”. I remember a month ago, when I discussed with my classmates, I always tried to find a fixed mode to make decisions. However, there are always better solutions. Here, I would like to systematically describe and analyze these decision-making patterns in the context of Tuan Jian’s games.

In other words, I would like to try to introduce:The charm of the fun of decision

The audience in the first half of this article can be those who have not been exposed to oi but have studied high school mathematics, are or want to be exposed to computer programming, or simply find it interesting. I hope this article can arouse your interest in decision-making ideas, dynamic planning, and even analyzing data to find rules. I hope that when you read this article, you don’t rush to turn it down. You can stop and think about what kind of decision you can come up with, or find a classmate. Only when we think together can we show the fun of thinking games.

The second half of the paper is mainly expressed in his own words after understanding Zhu Chenguang’s paper. The audience mainly has a certain DP basis, but they can’t understand the paper or are scared by the rigorous formula proof of the paper.

## 1、 The origin of all evils — the beginning of the deep grass revolution

reminder:The background is pure fiction. If it’s the same, I’ll climb it.

The strange group construction of Ji shubu began. In order to consolidate the dictatorship and strengthen the concentration of thought, Minister jxh ordered all members to sing a song for TA.

At first, the shy little buddies didn’t dare to object, but somehow, a strange ID broke the surface of the peace.

TA revealed in the group that the minister took the money from the red envelope to buy milk tea, which aroused the dissatisfaction of the Department members. With the fermentation of public opinion, the members decided to launch a revolution. Because the mascot of the Ministry is Shencao, it is called Shencao revolution in history.

Seeing that the situation was not good, the cunning Minister decided to fight the members with a game.

“I’ll write one on the paper,” he said$$1$$reach$$1\times 10^{18}$$And then 12 of you can figure it out by guessing the number. Don’t worry, every time I guess, I will tell you the numberIs it smaller than you guessed. But ifIf a person’s guess is greater than or equal to the number I wrote, he is out of the game and can’t guess the next number. If all 12 people are out, you must submit your answers immediately. When you feel that you have determined the number in my hand, you can also submit the answer before you are all out, but,The answer must be in the number you’ve guessed

”If you’re wrong, you’ll sing. If you get the right answer, we willRole reversal You write the numbers and I’ll guess. In order to ensure fairness, after I guess 12 numbers greater than or equal to the answer, I will stop guessing and submit the answer. Of course, like you, I can submit answers before I’m out. Then, weThe number of guessing times to determine the other person’s answerWho is less, who is more. You win if you are as small as me. If you win, I’ll sing for you. “

The honest staff accepted the offer. But the minister was very cunning. He didn’t write the answer on paper. Instead, he didDecide the answer according to the number the members guess. in other words,He calculated in his mind at any time, trying his best to raise the number of guesses to the highest level, even making them unable to guess. It’s just a mistake to write on paperFeintIn fact,The answer is not determined in advance, but constantly adjusted in the process of guessing

Fortunately, there are many big men in the Ministry. They decided to defeat the minister’s fantasy by means of scientific guessing (decision-making).

Because the minister is too strong, even if the Ministry members do not determine the number first, they will adjust as the minister does,The minister can make the least number of guesses with the best decision

Therefore, in order to win over the minister and win the revolution, the members of the Ministry shouldThe best decision must be made to deal with the minister’s absolutely clever adjustment. And also with absolutely smart adjustment, the number of Ministers card to as high as possible.

So the brainstorming started.

However, as the leaders expressed their opinions, the members of the Department split into several factions. The revolutionary army fell into chaos, which historians called the last chaos before dawn.

Next, for the convenience of discussion, I may record the number of department members, such as 12, as K, and the right boundary of the range given by the minister, such as$$1\times 10^{18}$$Let’s denote the number of guesses by N and K.

The following is a simple description of the Eagle Egg problem:
Now that you’ve bought a 1e18 story building, you want to know how many floors an egg will fall from (or it may not fall from head to tail). You only have 12 eggs on hand, and broken eggs can’t be used. Q. in the worst case, how many times does it take to get the answer?
In the following, we will discuss the following points:How to throw itHow efficient is itWhich is the worst case
If you are confused by the “worst” and “least” above, please look back at the background of the story, I believe you can understand the meaning of the “worst” and “least”.

P. Even if you try on the N-1 floor, the eggs are not bad, you still need to throw them from the N-1 floor. This rule is the same as the previous minister’s “your answer must be in the guessed number”Avoid ambiguity

## 2、 From Chaos: conservative decision making

Conservatives believe that the first step in defeating ministers is to ensure that the answer is certain. Therefore, they advocate the safest way.

There is no doubt that guessing one by one can definitely determine the final result, but this kind of operation is easy to be stuck by the cunning minister$$10^{18}$$Times.

The first way they think of isGuess every 11 numbers

Their idea is this: from 12 to 24, 36, 48, and finally to 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999,Even if ministers say “the answer is no less than that” at some point, they can scan the remaining 11 numbers with the remaining 11 people to determine the result

In the end, the worst number of times they guessed (that is, the number of times a cunning minister would get stuck) was$$10^{18}\div 12+11=83,333,333,333,333,344$$. This number has 17 digits, which is obviously outrageous.

But the Conservatives are not discouraged. They observed that there was no need for the remaining 11 people to scan the 11 numbers.As long as you leave one person behind and scan up from the bottom of the narrowed range, you can also determine the number of ministers

How to make the most of the remaining 11 members to narrow down the scope?

They thought of the invincibledichotomy

Select the middle point of the range each time, and then take the next middle point in the narrowed range according to the size prompt given by the minister. So,Every time you guess, you can reduce the range to half of the original

After estimation, dichotomy in any case, just guess$$\log_2 N\approx 60$$. It’s an epic leap.

However, to achieve such a perfect strategy,There are 60 department members, and only 12 department members can’t complete it

Members of the Conservatives know that. So they decidedThe scope is narrowed down by a dichotomy of 11 members, and the remaining member sweeps up from the bottom of the final scope

Obviously, the minister will not tolerate narrowing the scope again and again,He’ll try to get the player who’s trying to score two points out and keep the range as wide as possibleTherefore, the members of the DepartmentYou can only score two points 11 times at most

(there is a decision-making idea from the perspective of the minister mentioned above, which will be discussed in more detail later.)

After estimation, what is the worst number of times they need to guess$$10^{18}\div 2^{11}+11=488,281,250,000,011$$. This number, only 15 digits, compared with the first method,The efficiency is close to 200 times

Just as members of the conservatives were overjoyed and exhausted by the result, a voice came from the side: “why don’t we leave one more person to further narrow the scope?”

(I’m embarrassed by this passage.)

long long guess1(long long N,long long K){
long long l=1,r=N,mid,c=K,cnt=0;
while(l<=r&&c>1){
mid=(l+r)>>1;
cnt++;
if(evil_check(l,r,mid,c)){
c--; r=mid-1;
}else l=mid+1;
}
while(l<=r){
cnt++;
if(evil_check(l,r,l,c)) break;
l++;
}
return cnt;
}

The above code is for reference only, which is convenient for you to understand the decision-making process of this strategy. I use itevil_ Check to show the minister’s judgment on the size relationship. Similar codes in the following are justAs a framework, it is convenient for you to understand the decision-making process and general implementation

In short, I wrote the codeIt’s just for watching and playingIt’s not for running

evil_ Check, that is, how to make the number of cards to the maximum strategy, will be discussed later. But if you think about it, it’s not difficult.

It doesn’t matter if you don’t understand it. Just skip it. I think the expression of the words may be clear..

## 3、 Candlelight in the dark — the rise of Reformists

It was the reformists who made the sound.

The members of the reformists analyzed the two views of the Conservatives and put forward some suggestionsCombine the two waysThe way of thinking.

First, 10 Department members are divided to narrow the scope, and thenChoose the right intervalLet’s guess the number, and finally scan the remaining interval X.

After rough calculation, the remaining undetermined interval is 10 times after two minutes$$SIZE=10^{18}\div2^{10}=976,562,500,000,000$$

At this point, if you directly divide size into two parts, it is the conservative’s second plan. So,What if it’s divided into three parts

It is not difficult to find that after being divided into three parts, the size of each segment is only 325520833333333, although for this reason, they areI guess one more number than two, but the size of each remaining segment is greatly reduced

It can be divided into two parts, which need to be guessed$$1+SIZE\div2=488,281,250,000,001$$second
It can be divided into three parts$$2+SIZE\div3=325,520,833,333,335$$second

P. S. why do I always add “guess” before “guess”“Probably”Or“estimate”In practice, there may be some changes such as rounding down and L = mid-1,This is particularly evident in the following introduction of three points and even k points. For the convenience of discussion, and most of the time, this small change is nothing to the big number of 1e18, so I chose to ignore it. By the way, if there is no special explanation, it will be rounded down by default.

Obviously, dividing into three parts is better than dividing into two parts.

Of course, the reformists did not stop there, they continued to explore:When divided into several parts, the best results can be achieved

If we record the number of shares as X, we want the best result, which is the hope function$$f(X)=SIZE\div X+(X-1)$$Take the minimum.

How could the big guy in the Department of Ji Shu not be able to check the function? So they quickly found the answer.

When$$X=31,250,000$$The optimal solution is 62499999.

Now let me sort out what we have achieved

1. Algorithm using interval 11: 83333344
2. Complete traversal after 11 times: 488281250000011
3. After 10 times in two, it was divided into three parts: 325520833333345
4. Two minutes 10 times later$$\sqrt {SIZE}$$Copies: 62500009

All rise!!! From now on, it’s called Lu.. All right, let’s go

long long guess2(long long N,long long K){
long long l=1,r=N,mid,c=K,cnt=0;
while(l<=r&&c>2){
mid=(l+r)>>1;
cnt++;
if(evil_check(l,r,mid,c)){
c--; r=mid-1;
}else l=mid+1;
}
long long block=sqrt(r-l+1);
while(l+block<=R){
cnt++;
if(evil_check(l,r,l+block,c)){
r=l+block-1;
break;
}
l+=block+1;
}
while(l<=r){
cnt++;
if(evil_check(l,r,l,c)) break;
l++;
}
return cnt;
}

## The attempt of the interval fool

(I’m too lazy to make up the background here)

In the discussion with my classmates, I once thought of a strategy to optimize interval scanningmultiplicationScan strategy.

That is, when scanning,The interval increases gradually from 1, 2, 4 and 8

At that time, my idea was as follows:Since it’s too time-consuming to sweep one by one, I’ll leave one more person and double sweep to narrow down the scope

So, I played a strategy of double K-2 and double K-2, and found thatIn some cases where n is larger and K is smaller, the result is better than that of traversing the whole interval after direct dichotomy k-1

However, in the subsequent thinking, I found this approachIn the worst case, the interval can only be reduced to half of the original, and the number of guessing is higher than that of the dichotomy

So,Why can such a seemingly useless strategy really reduce the number of times?

A few weeks ago, I was only thinking about this issueThe worst case of the binary answer is different from the worst case of the multiplication, so it is neutralizedsuchNaive and wrongIn my mind.

Now, I think I can give a more scientific explanation.

Take the strategy of splitting K-2 first and then doubling as an example

Because the interval size we try to exclude in each multiplication process is 1, 2, 4, 8, and finally 1$$2^n$$

The total size of these intervals is$$\sum2^i=2^{n+1}-1 In the worst case, what is the remaining range\(2^n$$

When$$2^n+nMy strategy will be better than the strategy of traversing after 11 direct dichotomies. In the same way, when n is large enough, the gap will be especially obvious. The following is my violence at that time, for reference only: (it was so ugly that it was deleted) ## 4、 A flash through the air — the pioneering work of the revolutionaries (more and more in two days, my day) Front row statement, this strategy is the students come up with. I’m a good cook, orz Revolutionaries think more deeply: in the final analysis, why do we use two points? First of all, let me introduce what is the trisection. Different from dichotomy, we divide the interval into three parts each time, and divide two equal pointsFrom small to largeGuess in turn. If the small trisection is larger than the answer, prove that the answer is in the first part; If the big trisection is bigger than the answer, prove that the answer is in the middle; If the big trisection is smaller than the answer, prove that the answer is in the third. After that, the same operation is performed on the narrowed interval until the person is exhausted or the interval is narrowed to be judged. If we are guessing from large to small, we need to reduce the interval to one third, but we need to spend two chances to guess large numbers. In the case of enough people, we can compare two points with three points as follows: To determine a number from a range of length N, using dichotomy, in any case, is required\(\log_2 N$$second

Using the trisection method, in the worst case, you need to$$2\times \log_3N$$

$$2\times \log_3N/\log_2N=2\times\log_32=\log_34>1$$

So,In the worst case of sufficient numbers, the three-point method is not better than the two-point method

However, the revolutionaries did not limit their thinking. They realized that the essence of increasing the number of points is to improve the final interval processingIn each interval division, through additional guessing, the scope can be further reduced even if only one person is consumed. In other words, it’s a kind ofGive up the small bureau and do great thingsThe decision-making means of the government.

So, with this idea in mind, the revolutionaries first triedThe strategy of scanning the rest of the interval after three minutes and eleven times

It is estimated that the number of times to guess in the worst case is as follows:$$11\times2+10^{18}\div3^{11}=5,645,029,269,498$$

Compared with 488281250000011, the efficiency is nearly 100 times higher.

It is not difficult to find that the remaining interval is still as high as 13 digits after 11-3 times, so there is a huge space for optimization.

Members of the revolutionary faction think of the optimal conditions for dichotomy$$K\geq\log_2N$$

So why don’t we tryFind a suitable base K so that our K satisfies the above formulaWhat about it?

therefore,K-score methodIt came into being.

For this base K, there should be$$K\geq\log_kN$$In other words, there should be$$k^K\geq N$$

We might as well take it$$k=\sqrt[K-1]{N}=\sqrt{10^{18}}\approx43$$

After the K is determined, theestimateThe number to guess becomes:$$11\times42+10^{18}\div43^{11}\approx463$$

The revolutionaries who worked out this formula looked back at the reformers’ optimization of the final interval, and immediately had mixed feelings.

In fact, the reformers’ optimization is to make the reformers’ optimization more universal,It’s just that this step is so far away, sometimes it’s also so far away, and only those who take the key step will be recorded in history

Here, let’s go over the achievements of the staff,Clap for your patience and the approach of dawn

1. Conservatives: complete traversal after 11 times: 488281250000011

2. Reformers: two points, 10 points$$\sqrt {SIZE}$$Copies: 62500009

3. Reformers: 43 points, 11 times, 463

Yes, you’re right. It’s justnearThe dawn has not yet come

long long guess3(long long N,long long K){
long long l=1,r=N,c=K,cnt=0,block,num,mid;
while(l<=r){
num=floor(pow(r-l+1,1.0/(c-1)));
block=(r-l+1)/num+1; mid=l;
while(mid+block<=r){
mid+=block; cnt++;
if(evil_check(l,r,mid,c)){
c--; r=mid-1; break;
}else l=mid+1;
}
if(mid==l) break;
}
while(l<=r){
cnt++;
if(evil_check(l,r,l,c)) break;
l++;
}
return cnt;
}

Again, the code is only a reference for a decision model. Of course, you can write your own evil_ I’ll check it out, but I’ll compare the dishes. It may come out of the pot

In fact, it’s not difficult to find out whether to round down or up in the above code, and whether to add or subtract after adding blocksmetaphysicsProblems, are likely to make guess the number of timesSubtle changes. Even if the adjustment is made in a certain k-minute, it may have a metaphysical influence on the result. This point,This is especially significant on a large scale such as 1e18. But, ourThere is only one optimal solutionWe want to analyze the optimal results from these many possible adjustments (for example, some add blocks and then subtract 1, some do not). From the point of view of the uniqueness of the solution, I think this algorithm is too metaphysicalThe solution may be very close to the optimal solution, but it should not be the correct solution

What, didn’t you understand the above passage? I don’t know how to explain it. It’s just a simple feeling.

## 5、 Get rid of chaos — mysterious supporter

(I found that I couldn’t find the code that can calculate 1e18 at all. Although I had a screenshot of the problem solution, I couldn’t read and type it out. So I don’t know the specific optimal answer.Maybe in a few daysSo I can only analyze the small data to let you understand its correctness.)

After their respective discussions, the members of the Department have a certain confidence.

However, a powerful minister is an opponent that cannot be underestimated.

We must win, we must ensure the final victory. Everyone is reading it in their heart.

However, the minister’s confident attitude still makes the staff feel uneasy.

anything else? Can the number of guesses be reduced?

When the members of the Department are in agony, the mysterious ID in Q group gives a hint of TA.

“Why should it be limited to some regular segmentation?”

Now, let’s take the role of a member of the Department and think about it together.(in fact, I’m too lazy to change my personal name.)

Each time you need to decide in scope$$[l,r]$$After guessing a certain number x, the situation will change as follows:

1. If x is greater than or equal to the minister’s answer, one member will be out and the remaining k-1 members will continue to be in$$[l,x\big)$$Inner guess
2. If x is less than the answer given by the minister, K members of the Department will continue to answer the question$$\big(x,r]$$Inner guess

As the minister is too strong, he will definitely choose between situation 1 and 2More guessing timesLet’s continue.

Fortunately, although we can’t choose between situation 1 and situation 2, we doFreedom to choose x

So, we just need to figure outEach x corresponds to the maximum number of cases 1 and 2And then choose one of these x’sMax minTo guess the number, you can achieve the optimal number of times.

But how do we get this value?

Let’s record the optimal number of guesses of K people in the range of interval length n as$$f(k,n)$$

That is to say, what we need to solve is$$f(K,N)$$

Suppose in the first guess, the number we guess is the W in the interval.
Then for case 1, the rest of our interval is$$[l,l+W-1\big)$$The corresponding interval length is$$W-1$$Of whom$$K-1$$
For case 2, the remaining interval is$$\big(l+W-1,r]$$The corresponding interval length is$$N-W$$Of whom$$K$$

So we have:

$$f(K,N)=max\{f(K-1,W-1),f(K,N-W)\}+1$$

In other words: we willIn the case of k-person and n-length intervalThe problem of finding the minimum number of guesses in the worst case is transformed intoIn the case of k-1 person and W-1 length intervalandIn the case of K individuals and N-W intervalSolutionTwo smaller subproblems

You may ask, what’s the point of this? Is the problem still unsolved?

It makes sense, of course. Because,As the scale of the subproblem continues to shrink, eventually, we will have the ability to solve the subproblemAnd using the obtained subproblem, we can substitute it into the formula of the big subproblem, solve the big subproblem, and finally solve the problem we requiref(K,N)

In this way, we can decompose the problem from top to bottom until we can calculate the answer, and then calculate the big problem through iterative backtrackingrecursion

So, what kind of small questions can we answer directly?

Obviously, for any interval length N, we have$$f(1,n)=n$$Because in ourWhen there is only one person left, we have to guess from the bottom upIf we first guess the first number and find that it is less than the answer, then we guess the third number. If the third number is greater than or equal to the answer, we can no longer determine whether the second number is the answer. Because of this, in the worst case, we need to guess n times.

Obviously, for any number k, we have$$f(k,0)=0$$When the interval length is 0, there is no need to guess.

Therefore, when we use recursion to solve the problem, we record the number in the interval we take each time in order to get this kind of solution. In this way, we only need to answer according to the pre calculated table to achieve the optimal strategy.

long long f(long long K,long long N){
if(N==0) return 0;
if(K==1) return N;
long long ans=INF;
for(int w=1;w<=N;w++){
long long temp=max( f(K-1,w-1),f(K,N-w) );
if(temp+1

So, in addition to finding out$$f(K,N)$$In addition to the number to be selected each time to get the minimum value, we also find out the optimal number to be selected under various other K and n conditions. Even if the minister makes a decision that does not conform to the worst-case situation, we can make the best choice by looking up the table.

By the way, if we do not use recursive computation, we first preprocess the solution of each small problem, and then expand the scale of the problem step by step, and get the computation from the bottom up, we call it “bottom up”dynamic programmingDP for short.

$f(K,N)= \begin{cases} max\{f(K-1,W-1),f(K,N-W)\}+1 & K>1,N>0\\ 0 & N=0\\ N & K=1 \end{cases}$

The above formula is called dynamic programmingState transfer equation

The code is no longer given, and the writing method of DP is just a casual search. Here I only give the comparison between the k-score method and the positive solution when n = 1000 and K = 3
K-score method (code): there may be evil when running out of 46_ The problem of unreasonable check judgment
Theoretical calculation 31 points method: 30 * 2 + 1000 / (31 ^ 2) = 61
dp：19
I’ll put the code at the end of the article. For reference only.

In this way, the members of the Ministry used their intelligence to come up with a way to defeat the minister.

Just when they turned on the computer and were ready to put the theory into practice, they suddenly found that if they directly enumerated K, N and W in the same way, they needed to loop close$$12\times10^{18}\times10^{18}$$Times. It is almost impossible to describe every situation in 1e18 in a short time. Even computers can’t figure out a proper path before the showdown begins.

What should I do? can onlyOptimize DPIt’s too late.

At this time, someone suddenly found that the strange ID in the group was actually the minister’s trumpet!

It turned out that the minister saw that the atmosphere in the Ministry was dull recently, and everyone lost themselves in the boring study, so he sent out the picture of himself paying for the milk tea for the members of the Ministry with another vest.

The ministers finally understood the minister’s good intentions. They and the minister changed the rules of the game, changed the thinking time of each decision to one second, and banned the use of computers, so the scene became chaotic and happy.

## 6、 DP Optimization — close to God’s field

Warning: this section may not start with a very basic explanation as above.

However, after writing this verse, I didn’t feel close to the realm of God. On the contrary, I felt more and more familiar

The following is a more perceptual interpretation of the content of this paper.

Here we areWithout considering what happened in the decision-making process, we only want to find the optimal number of guesses

first,When the number of people is the same, the longer the interval, the more the number of guessing, not less.According to such a perceptual analysis, we can draw such a conclusionConclusion 1)： $$f(K,N)>=f(K,N-1)$$

(of course, you can also use mathematical induction to prove this conclusion exactly as in the paper.)

Now that we’ve come to this conclusion, let’s go back to our enumeration of W.

After enumeration, we take$$max\{f(K-1,W-1),f(K,N-W)\}$$(the following isEquation 1

According to conclusion 1, it is not difficult to find that with the increase of W, f (k-1, W-1) gradually increases and f (k, N-W) gradually decreases. After we roughly draw the curves of F (k-1, W-1) and f (k, N-W) with W, it is not difficult to find that:
The minimum value of equation 1 is at the intersection of the two curves

So instead of enumerating each W, we use it directlyTwo point search order$$f(K-1,w-1)\geq f(K,N-w)$$Then we compare the best value of selection formula 1 under w = mid and mid-1That’s it.

Besides, it’s not hard to find that,As far as single operation is concerned, its greatest contribution to narrowing the interval is to divide interval one into two(because in the worst case, the minister won’t allow you to get to the cells that are not segmented)

In addition, we mentioned in the description of the k-score method earlier that in all the k-score methods, if there are enough people, the dichotomy has the best performance in the worst case. So,When the number of people is enough for the 2-point method, it is the best for us to conduct the dichotomy directly. At this point, directly record the number of times required for dichotomy, without enumeration or dichotomy w to calculate.

Here, we’ve put the original$$O(KN^2)$$The algorithm is optimized to$$O(KN\log_2N)$$

But the complexity obviously doesn’t allow us to work out data like 1e18. We need further optimization.

(it’s obvious that this kind of immortal thought can’t be thought out by ordinary people like me.)

It can be found that when the number of people is fixed, if the interval length is more than 1, the number of guessing times is either more or unchanged.
Here you canperceptualLet’s have a better understanding: we can pretend that we don’t know the length of the interval is n + 1, and still guess the number according to the strategy that the length of the interval is n, and then we can guess the number again after we get the optimal number in the worst case. In this way, the number of guesses is only one more. No matter what, it won’t be more than two or more.

This is a more scientific and detailed proof

For the formula$$f(K,N)=max\{f(K-1,W-1),f(K,N-W)\}+1$$
Let’s take the case of W = 1 separately. Obviously, f (k-1, W-1) = 0, we have f (k, n) < = f (k, n-1) + 1
On the other hand, we have the conclusion 1: F (k, n) > = f (k, n-1)
All in all, we have$$f(K,N-1)\leq f(K,N)\leq f(K,N-1)+1$$

let me put it another way,F (k, n) is either equal to f (k, n-1) or equal to f (k, n-1) + 1

obviously,When there is a decision w such that Max {f (k-1, W-1), f (k, N-W)} + 1 equals f (k, n-1), we have f (k, n) = f (k, n-1), otherwise we have f (k, n) = f (k, n-1) + 1

In calculating f (k, n), weIn order to realize the state transition of O (1), we use some o (1) method to confirm whether there is a corresponding decision

Now, let’s think about it,When we try to calculate f (k, n), what have we got or can we get in the previous calculation?

I think that DP itself is a process of deriving the solution of a new problem from a known subproblem. Therefore, when we try to optimize DP, it is very important to find out what value or law we can get and what value we can maintain in the process of DP. We should not always focus on the final result or the number of layers of the cycle. Of course, I’m not saying that we’re always staring at the number table. At least, we should not always regard the intermediate data of DP as a black box. Sometimes, only those who are good at observation and analysis, or who are tireless in observing, can find the key to open the treasure box.

Ah, it reminds me of the question that I ran to in Luogu before. I wrote the answer to that problem（

Ah, in the end, the above paragraph is just my mortal idea. Don’t spray if you don’t like it.

Ah, I’m not careful. Let’s go back to the question just now.

In the order of the for loop, when we try to calculate f (k, n), we have calculated all the values from F (k, 0) to f (k, n-1).

For a certain number P of, if it is used in the calculation of F (k, n), the formula should be as follows:

$$f(K,N)=max\{f(K,p),f(K-1,N-p-1)\}+1$$The meaning of P is the size of the remaining range when the eagle egg is not broken after guessing

We know that with the increase of P, the value of F (k, P) will be larger and larger, while the value of F (k-1, n-p-1) will be smaller and smaller.

When p = n-1, f (k, n) = max {f (k, n-1), f (k-1,0)} + 1 = f (k, n-1) + 1

let me put it another way,If we try to find a p such that Max {f (k, P), f (k-1, n-p-1)} + 1 = f (k, n-1), this p must precede n-1

For example, we know that when we calculate f (k, n-1), if f (k, n-1) = f (k, n-2), the corresponding P we find is definitely not n-2.

Furthermore, we hope that f (k, n) = f (k, n-1), the P we choose corresponds toF (k, P) must be less than f (k, n-1)(because there is a + 1 after max)

At the same time, we should not forget that there is also f (k-1, n-p-1) in Max {}

We know that f (k-1, n-p-1) increases with the decrease of P.Therefore, our P can neither be too close to n-1 nor too far away from itOtherwise, f (k-1, n-p-1) becomes larger, which is not good for us.

So the problem is simple.

We take P so that it isThe largest number satisfying f (k, P) < f (k, n-1). Since we know that the biggest difference between adjacent terms of F (k, n) is 1, there must be f (k, P) = f (k, n-1) – 1

If this p cannot make Max {f (k, P), f (k-1, n-p-1)} + 1 = f (k, n-1), thenEven smaller P is not good

It’s obvious,Because f (k, P) = f (k, n-1) – 1, and Max {f (k, P), f (k-1, n-p-1)} + 1 is larger than f (k, n-1), it shows that f (k-1, n-p-1) is larger than f (k, P)Even if we take the smaller P, because f (k-1, n-p-1) increases with the decrease of P, it is impossible to get a better result.

So, weEach time we just need to judge whether Max {f (k, P), f (k-1, n-p-1)} + 1 is equal to f (k, n-1). If f (k, n) is larger than f (k, n-1), then we just need to change p to n-1. Maintenance and query are o (1).

In this way, we optimize DP to o (KN)

Although I still can’t calculate the data of 1e18, in order to understand and express this thing clearly, my brain cells have died

So far, this kind of DP has been optimized quite thoroughly. No more optimization.

what? What do you mean by 1e18? That isAnother way of thinkingIt’s too late.

## 7、 The last dawn

First of all, let’s run the data of k = 12 and N = 1e18 with the following k-score code (of course, we have also theoretically analyzed it before, which is more than 400)

We got 444 results.

Let’s go to the extreme. Let’s run a data with k = 3 and N = 1e18.

We got the result of 149999999.

We know that the k-score method is not the optimal algorithm. In fact, we have found that there is a big gap between the positive solution and the k-score method in the analysis of small data.

For example: the same k = 3, n = 1e8, the k-score method gives 14999, while the positive solution given by DP is 844

Such a gap will obviously enlarge with the increase of n.

What do I want to say? Since we know that when k > 2, the answer will be much smaller than 149999999, we might as well guess boldly:The size of the answer is quite limited. Since there are too many states of F (k, n), let’s adjust our thinking,Instead of calculating the number of guesses f according to K and N, we deduce the size of n according to K and F

In other words, our idea of DP is as follows:

N (k, f) is used to indicate the maximum interval length of the answer which can be determined by f times of guessing (in which K eggs are broken).

We know that when k = 1, we can only guess from small to large once, so n (1, f) is always equal to F

For the derivation of the state transfer equation,The description in the paper is very easy to understandI’ll just give a brief introduction here,By the way, I’d like to explain why some students can’t understand the problem of adding up (if I make it clear)

Let’s assume that the f-th guess is the number X. If this number is smaller than the answer, then we need to use the remaining K eggs (not broken) to guess the upward interval starting from x + 1.So we turn the problem into a subproblem with K eggs to guess F-1 times. Similarly,If the egg is broken, we will turn the problem into a subproblem of guessing F-1 times with K eggs

We have$$n(K,F)=n(K-1,F-1)+1+n(K,F-1)$$

The following explains why the solutions of the two subproblems are added up:
If inIn the worst caseOur f-th guess egg is not broken. We know that it means that we don’t need to pay attention to the interval below x, while the interval up is finite in length n (k, F-1). Therefore, in order to achieve the maximum interval length, we shouldWe need to make the length of the interval below x as large as possible. But that doesn’t mean we can set the interval below x to infinity as we like. We know, we knowAll the discussion is based on the premise that in the worst case. The length of the interval that can be determined by using k-1 eggs to try F-1 is n (k-1, F-1),If we set the interval length longer, then the lower interval will need more guessing times, and the situation of broken eggs will be worse than that of no broken eggs, which is contradictory to the premise of the worst case. Therefore, we have n (k, f) = n (k-1, F-1) + 1 + n (k, F-1)
The same conclusion can be drawn from the analysis of the broken case.

So, we just need to preprocess n (k, f) and then binary search n (k, f) > = n to output the smallest F.

Then the question comes, how does this thing count? How big is it?

The answer is, forget it first
(ah, I’m so serious. What am I doing

There is a clear and rigorous proof of this complexity in the paper, but I am too busy to understand it. Here we useDirect calculationTo determine the scope we need.

When we make a table, we know that the more eggs we have, the larger the range we can determine for the same number of times
So, if we want to know how big the F is, we don’t need to try that big K
It is necessary to open f a little larger, but you should be careful that long long may overflow. You can stop when you find that n can take 1e18.
The code for tabulation is in the following code base.

By the way, I use n (F, K) in some places and n (k, f) in some places. You don’t have to worry about the order. You just need to know that K refers to the number of people and f refers to the number of times.

After tabulating, we still can’t find the corresponding f for n (F, K) > = 1e18 under the condition that we have fixed f to 1E7
And the case of k = 3 is solved successfully, f = 1817121. The size of this figure is still within our acceptable range.
But k = 2 obviously can’t, that is to say,For k = 2 and 1, we need special judgment

Fortunately, both cases are relatively simple.

For the case of k = 2:
n(F,2) = n(F-1,2) + n(F-1,1) + 1 = n(F-1,2) + F
In other words, n (F, 2) = 1 + 2 + 3 +… + F = (1 + F) * f / 2
F*(F+1)=N*2

At this point, the problem has been solved (the code is in the code base later).

Finally, let’s take a look at the result of k = 12 and N = 1e18143

Is this number in line with your expectations?

## A little publicity at the end

It is suggested that you can take a look at the summary of dynamic programming at the end of that paper. In fact, it is more important to summarize the method than to learn a problem and drop it.

As a little Mengxin who hasn’t even won the first-class noipI sincerely hope you can learn something from my blog.

Finally, promote the blog of Technology Department

There will be strange dry goods coming out one after another. You are welcome to pay attention.(though probably not by me)

## Evil check: Minister’s wishful thinking

When members guess the number x, how does the minister decide that x should be less than the answer?

Obviously, if the minister also typed the f (k, n) table, then he only needs to choose a larger one according to the table.

This is also the most scientific and accurate way of decision-making.

Here, we try to give the minister decision-making ideas for the k-score method.

(in fact, I tried to give a more general idea, but failed in the end)

We know that the minister’s goal is to maximize the number of guesses or to determine the interval.

Then, when there is only one member left, if the members of the Department make the mistake of not honestly trying one by one, let them lose.

Let’s try to discuss the case that the number of people is greater than 1

If TA decides that x is not less than the answer, a member of the team will be out.

In the k-score method, the interval is not always divisible by K.

For example, when we divide 11 into three parts, each part is 11 / 3 = 3… 2

The two triangles we need to judge are 15 9 11

The three intervals are [1,4] [6,8] [10,11]

It is not difficult to find that the size of the previous interval is the same except for the last one.

We hope that the k-score method can guess as many times as possible. Naturally, we hope that the k-1 intermediate points generated by k-score can be guessed once.

At the last k-point, obviously, it is better to choose x greater than or equal to the answer. In this way, on the one hand, we get the penultimate interval, which will not be less than the last remaining interval. On the other hand, we lose a chance to throw Eagle eggs (guess a larger number).

typedef long long ll;
bool evil_check(ll l,ll r,ll x,ll c){
if(c==1) return x!=l;
return x-l > r-x;
}

## Sensibility analysis of fanwai 2 to n (F, K)

be careful:This section is not finished yet and needs to be improved.

Scientific and rigorous comparative analysis and complexity calculation, please move the paper.

When I look at the solution of hit, I see such a conclusion
$$n(F,K)=\sum^K_{i=1}{F\choose i}$$

$$F\choose i$$The combination number of F and I

There is only one sentence in front of the solution of the problem: “using mathematical induction, there are:

As we all know, mathematical induction is the most powerful algorithm in OI

I want to interpret this formula in a more perceptual way.
Draw your own Yanghui triangle for easy understanding.
n=0: 1
n=1: 1 1
n=2: 1 2 1
n=3: 1 3 3 1

First of all, n (F, K) is similar to C (n, m), that is, the recurrence formula of the combination number of N selecting M.
Formula 1: n (F, K) = n (F-1, K) + n (F-1, k-1) + 1（K>1)
Formula 2: C (n, m) = C (n-1, m) + C (n-1, m-1)

Leaving aside the original number, when n = 1 counts down the points with k > 1, one more is added, which is caused by the extra numberchange
n=0: 0
n=1: 0 0
n=2: 0 1 1
n=3: 0 1 2 1

There seems to be something wrong（

Forget it. I’ll climb（

## Code base

It should be noted that the K of the problem in real can be up to 1000, rememberThe case of special judgment K is relatively largeReally stupid.
Like the following.

if(k>=log2(n)){ printf("%d\n",log2(n)); continue; }

The following code is generally K < = 63 by default
N, you can choose the appropriate size according to the complexity. (after all, I drive a vector.)

Also, my log2 (n) is written by myself, because I think it’s a bit too metaphysical to calculate directly with functions. It’s better to simulate it by myself. Anyway, the constant is not much different.

The following code is for reference onlyUnderstanding and research. It may be jammed because of the constant of STL. But it’s moreClear and concise
I put all the modified and AC code into another article, which can be referred to if necessary.

Found that the code error can be pointed out in the comments area, but I checked quite a few times, reasonable no problem.

By the way, my input format does not have to be based on the title. If you want to submit, you need to change it. You can get a.

No, no one can’t understand scrolling arrays, can’t they

### Code 1: k-score with evil for k-score_ check

#include
#include
using namespace std;
typedef long long ll;
bool evil_check(ll l,ll r,ll x,ll c){
if(c==1) return x!=l;
return x-l > r-x;
}
ll guess3(ll N,ll K){
ll l=1,r=N,c=K,cnt=0,block,num,mid;
while(l<=r){
num=floor(pow(r-l+1,1.0/(c-1)));
block=(r-l+1)/num+1; mid=l;
while(mid+block<=r){
mid+=block; cnt++;
if(evil_check(l,r,mid,c)){
c--; r=mid-1; break;
}else l=mid+1;
}
if(l==mid) break;
}
while(l<=r){
cnt++;
if(evil_check(l,r,l,c)) break;
l++;
}
//The above while can be directly changed to CNT + = (R-L + 1);
return cnt;
}
int main(){
//Pay attention to the reading order, number first and range second
ll K,N; scanf("%lld%lld",&K,&N);
printf("%lld\n",guess3(N,K));
return 0;
}

### Code 2: non optimized N ^ 3 dp with decision process tabulation

#include
#include
#include
using namespace std;
typedef long long ll;
#define REG register
#define rep(i,a,b) for(REG int i=a;i<=b;i++)
#define Rep(i,a,b) for(REG int i=a;i>=b;i--)
const int INF=1e9;
int N,K;
int main(){
scanf("%d%d",&K,&N);
vector f[K+1],pre[K+1];
//After reset, the element in vector is 0 by default
rep(i,0,K) f[i].resize(N+1),pre[i].resize(N+1);
rep(i,1,N) f[i]=i;
rep(i,2,K){
rep(j,1,N){
f[i][j]=INF;
rep(w,1,j){
int now=max(f[i-1][w-1]+1,f[i][j-w]+1);
if(now route,L;
route.push_back(x*(N+1)+y); L.push_back(l);
while(pre[x][y]){
REG int w=pre[x][y];
if(f[x-1][w-1]>f[x][y-w]) x--,y=w-1;
else y-=w,l+=w;
L.push_back(l);
route.push_back(x*base+y);
}
rep(i,0,route.size()-1){
x=route[i]/base,y=route[i]%base;
if(y==0) break;
printf("[%d,%d] with %d people f[%d][%d]=%d\n",L[i],L[i]+y-1,x,x,y,f[x][y]);
}
return 0;
}

### Code 3: binary optimization + part (I, J) take binary conclusion directly

#include
#include
#include
using namespace std;
inline int log2(int x){
//Direct simulation of the worst case of binary process
int l=1,r=x,c=0;
while(l<=r){
int mid=(l+r)>>1; c++;
if(r-mid>mid-l) l=mid+1;
else r=mid-1;
}
return c;
}
int main(){
int k,n; scanf("%d%d",&k,&n);
vector f,p;
f.resize(n+1); p.resize(n+1);
for(int i=1;i<=n;i++) f[i]=i;
for(int i=2;i<=k;i++){
f.swap(p); f.resize(n+1); f=1;
for(int j=2;j<=n;j++){
//When it's enough to score two, score two directly
if(i>=log2(j)){ f[j]=log2(j); continue; }
//Using binary search to find out the intersection point, determine two possible positions and compare them
int l=1,r=j,mid,ans=r;
while(l<=r){
mid=(l+r)>>1;
if(p[mid-1]>=f[j-mid]) ans=mid,r=mid-1;
else l=mid+1;
}
f[j]=max(p[ans-1],f[j-ans])+1;
if(--ans>=1) f[j]=min(f[j],max(p[ans-1],f[j-ans])+1);
}
}
printf("%d\n",f[n]);
return 0;
}

### Code 4: O (KN) linear fairy optimization

#include
#include
#include
using namespace std;
int main(){
int k,n; scanf("%d%d",&k,&n);
vector f,p; f.resize(n+1);
for(int i=1;i<=n;i++) f[i]=i;
for(int i=2;i<=k;i++){
f.swap(p); f.resize(n+1);
int pre=0; f=1;
for(int j=2;j<=n;j++){
//f[pre]

### Code 5 is tabulated to determine the range of G (k, f) to be preprocessed

#include
typedef long long ll;
const ll F=1e7,K=3,N=1e18;
ll f[F+3][K+1];
int main(){
for(int i=1;i<=F;i++) f[i]=i;
f=f=f=1;
for(int i=2;i<=F;i++){
for(int k=2;k<=K;k++){
f[i][k]=f[i-1][k]+f[i-1][k-1]+1;
}
}
for(int k=2;k<=K;k++){
printf("K=%d:",k);
bool have=0;
for(int i=1;i<=F;i++){
if(f[i][k]>=N){ have=1; printf("%d",i); break; }
}
if(!have) printf("MAX:%lld",f[F][k]);
printf("\n");
}
return 0;
}

### Code 6 another DP idea

In fact, the original title of hit is k < = 64, but the case of k > 60 will be ruled out because log2 (1e18) = 60
However, when I asked my classmates to help me with the test, I was still re. Maybe it’s the OJ problem.
Here is a detail example of hit for reference
353529145519311753 5
The answer is 8426
I’m running 8426, too. Let’s take it as if it’s over

#include
#include
using namespace std;
typedef long long ll;
#define REG register
#define rep(i,a,b) for(REG int i=a;i<=b;i++)
#define Rep(i,a,b) for(REG int i=a;i>=b;i--)
inline char getc(){
static char buf[1<<14],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<14,stdin),p1==p2)?EOF:*p1++;
}
inline ll scan(){
REG ll x=0; REG char ch=0;
while(ch<48) ch=getc();
while(ch>=48) x=x*10+ch-48,ch=getc();
return x;
}
inline ll log2(ll x){
ll l=1,r=x,mid,c=0;
while(l<=r){
mid=(l+r)>>1; c++;
if(mid-l>r-mid) r=mid-1;
else l=mid+1;
}
return c;
}
const ll F=2e6,K=60,N=1e18;
ll f[K+1][F],end[K+1];
inline void prework(){
rep(i,1,F-200) f[i]=i;
rep(i,1,F-200){
rep(j,2,K){
if(end[j]) break;
f[j][i]=f[j][i-1]+f[j-1][i-1]+1;
if(f[j][i]>=N) end[j]=i;
}
}
}
int main(){
REG int T=scan();
prework();
while(T--){
REG ll n=scan(),k=scan(),temp;
if(k==1){ printf("%lld\n",n); continue; }
if(k==2){
ll t=floor(sqrt(n<<1));
if(t*(t+1)=temp){ printf("%lld\n",temp); continue; }
REG int l=1,r=end[k],mid,ans;
while(l<=r){
mid=(l+r)>>1;
if(f[k][mid]>=n) r=mid-1,ans=mid;
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}

# END

## Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]