Blue Bridge Cup: Sequence Evaluation (Dynamic Programming)

Problem Description

Given a sequence of 1, 1, 1, 3, 5, 9, 17, …, starting from item 4, each item is the sum of the first three items. Find the last 4 digits of item 20190324.

Answer submission
This is a question that fills in the blanks with the result. You only need to calculate the result and submit it. The result of this question is a 4-digit integer (hint: the thousand digit of the answer is not 0). When submitting the answer, only fill in this integer, and fill in the extra content will not be scored.

答案
4659

answer:

For typical dynamic programming problems, we can obviously find the dynamic transition equation, but note that the term is too large, so this number is too large and causing errors, and because we only need four integers, we only need to The obtained sum can be calculated as the remainder of 10000, because the high bit of addition will not affect the low bit.
Why does the addition high bit not affect the low bit?
For example, in the formation of a four-digit number, the highest digit is formed first, and then hundreds, tens, and ones digits are formed, that is, the high digits are formed first, and the low digits are formed later.
Therefore, what we first formed here is the high position, that is, the position that breaks the limit of 10000 first is the high position. Therefore, the high position that we perform the remainder operation and discards is also the high position, which has no effect on the following calculations. What we want is the low position.
And note that the fourth bit cannot be 0.

Code:

#include <bits/stdc++.h>
#define N 20190324
using namespace std;
int dp[30000000];

int main()
{
    
    
	dp[1] = 1;
	dp[2] = 1;
	dp[3] = 1;
	for(int i=4;i<=N;i++)
	{
    
    
		dp[i] = dp[i-1]%10000+dp[i-2]%10000+dp[i-3]%10000;
	}
	int temp = dp[N]%10000;
	
	cout<<temp;
	return 0;
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/115280357