[title] sequence counting – Blue Bridge Cup

Time:2021-3-4

Title: sequence counting

[problem description]
Xiao Ming wants to know the number of positive integer sequences that satisfy the following conditions:
1. The first term is n;
2. The second term does not exceed n;
3. Starting from the third term, the absolute value of the difference between each term and the first two.
How many sequences are there for a given n.

[input format]
The input line contains an integer n.
[output format]
Output an integer to indicate the answer. The answer may be large, please output the remainder of the answer divided by 10000.

[sample input]
4
[sample output]
7
[example description]
The following is a sequence that satisfies the conditions:
4 1
4 1 1
4 1 2
4 2
4 2 1
4 3
4 4
[evaluation case size and convention]
For 20% of the evaluation cases, 1 < = n < = 5;
For 50% of the evaluation cases, 1 < = n < = 10;
For 80% of the evaluation cases, 1 < = n < = 100;
For all evaluation cases, 1 < = n < = 1000.


Idea: memory recursion

From the third point of the title, we can get the recursive formula:

F (old, now) means an old is the previous element and now is the sum of the current elements.

  f(old,now) = f(now, 1) + …. + f(now,|old-now|-1) + 1;

  

According to the condition of 2,3, we conclude that the sequence sum of F (old, now) = 1 + F (old, now – 1) + F (now, | old – now | – 1);

 

Thus, the code (c) is obtained

  

#include 
#include 
int hx[1001][1001] = {0};

long long int dfs( int old, int now ) {
    if( now <= 0 ) return 0;
    if( hx[old][now] != 0 ) return hx[old][now]; 
//Starting from the third term, the absolute value of the difference between each term and the first two terms is from 1 to Fabs (old now) - 1
//The sequence starting with old and now includes: self + now < old + the third item that meets the conditions 
    hx[old][now] = ( 1+dfs(old,now-1)+dfs(now,fabs(old-now)-1) )%10000;
    return hx[old][now];
}

int main(){
    int num;
    scanf("%d",&num);
    printf("%I64d",dfs(num,num));
    return 0;
}

//Starting from the third term, it is recursive: 
//f(old,now) = f(now, 1) + .... + f(now,|old-now|-1) + 1;
//From 1 to the absolute value of old and now - 1 plus yourself

 

2020-03-25-20:57:01