The 11th national competition python test J: blue jump

Blue jump jump


[Problem description]
Xiaolan made a robot named Blue Jump Jump, because this robot basically jumps when it walks.
Blue jump jump can jump around, can also turn around. The distance of each step of the blue jump must be an integer, and the length of each step can not exceed k. Because the balance of the blue jump is not designed well, if two consecutive jumps are made, and the distance between the two jumps is at least p, the blue jump will fall, which is something Xiao Lan does not want to see.
Xiao Lan received a special task to show the blue jump on a stage with a length of L. Xiao Lan wants to control the blue jump to walk from the left to the right of the stage, then turn around, then walk from right to left, then turn around, then walk from left to right, then turn around, then walk from right to left, then turn around, so Back and forth. In order to prevent viewers from being too boring, Xiao Lan decided to let Lan Tiaotiao walk in a different way each time. Xiaolan records the distance of each step of the blue jump, and arranges them in a row in order. Obviously, the number of this row does not exceed k and the sum is L. In this way, a list of numbers will come out after one trip. If the lengths of the two columns are different, or if there is a position value in the two columns, it is considered to be a different plan.
Under the premise of not falling, I would like to ask how many different schemes of blue jumping from one side of the stage to the other.
[Input format]
Input a line containing three integers k, p, L.
[Output format]
Output an integer to indicate the answer. The answer may be large, please output the remainder of the answer divided by 20201114.
[Sample input]
3 2 5
[Sample output]
9
[Sample description] The
blue jump has the following 9 jump methods:
1+1+1+1+1
1+1+1+2
1+1+2 +1
1+2+1+1
2+1+1+1
2+1+2
1+1+3
1+3+1
3+1+1
[Sample input]
5 3 10
[Sample output]
397
Method one
idea :
This kind of problem can be solved by recursive DFS or dynamic programming. You can see that it is impossible to run the data if recursive, so I used dynamic programming to do this problem, we can create a 2-dimensional array dp[i][j], i represents the distance of the stage, j represents the first jump j Grid time.
We first write the known data into the two-dimensional data dp[1][1]=1, and then traverse 2,s+1 and 1,p+1, because when s=0, there is only one method.
Figure one

When we know the first result, we can recursively recurs the following results. ij is a condition when it is 0 or a negative number. When it is 0, there is no second choice, so it is a method by itself. When it is negative, there is no need to go down.

If there are two consecutive jumps, and the distance between the two jumps is at least p, we know that j is the first jumping method, and k is the second selected jumping method, so j>=b is divided into two transition equations, When j>=b, dp[i][j]+=dp[ij][k] otherwise dp[i][j]+=sum(dp[ij]). The final result is sum(dp[s]).
But this non-optimal solution runs at most 80% of the data.

program:

#i 总步数
#j 跳一次的步数
p,b,s=map(int,input().split())
o=0
dp=[[0 for i1 in range(p+1) ] for i in range(s+1)]
dp[1][1]=1
for i in range(2,s+1):			
     for j in range(1,p+1):
        if i-j==0:
             dp[i][j]+=1
        elif j>=b:
            for k in range(1,b):
                 dp[i][j]+=dp[i-j][k]%20201114
        else:
             dp[i][j]+=sum(dp[i-j])%20201114


print(sum(dp[s])%20201114)

Method two
idea:
create a two-layer cyclic array, 0 layer stores the number of ways j<b, and layer 1 stores the number of ways b<p+1. Similar to the above method, it is only stored in a two-layer format, which can reduce the amount of calculation.
We first write the known data into the two-dimensional data dp[1][0]=1, and then traverse 2, s+1 and 1, p+1, because when s=0, there is only one method. Put dp[0][1]=1 or dp[0][0]=1 I just used this as the above method dp[i][j]+=1.
There are also two transfer equations, one of (1,b) and (b,p+1) put j<b in dp[i][0] b<p+1 in dp[i][1].
The transfer equations are dp[i][0]+=dp[ij][1]+dp[ij][0], dp[i][1]+=dp[ij][0].
This way, the data can be run through and the amount of calculation is much less than the previous method.
Figure II

p,b,s=map(int,input().split())
dp=[[0 for i1 in range(2) ] for i in range(s+1)]
dp[0][1]=1
dp[1][0]=1
for i in range(2,s+1):			
    for j in range(1,b):
        if i-j<0:
            break
        dp[i][0]+=((dp[i-j][1]+dp[i-j][0])%20201114)

    for j in range(b,p+1):
        if i-j<0:
            break
        dp[i][1]+=dp[i-j][0]%20201114
        
print(sum(dp[s])%20201114)


Reprinting is prohibited. Only for self-study. No responsibility for program errors.

Guess you like

Origin blog.csdn.net/weixin_46640345/article/details/112400844