Java實現藍橋杯模擬正整數序列的數量

問題描述

  小明想知道,滿足以下條件的正整數序列的數量:

  1. 第一項為 n;

  2. 第二項不超過 n;

  3. 從第三項開始,每一項小於前兩項的差的絕對值。

  請計算,對於給定的 n,有多少種滿足條件的序列。

輸入格式

  輸入一行包含一個整數 n。

輸出格式

  輸出一個整數,表示答案。答案可能很大,請輸出答案除以10000的餘數。

樣例輸入

4

樣例輸出

7

樣例說明

  以下是滿足條件的序列:

  4 1

  4 1 1

  4 1 2

  4 2

  4 2 1

  4 3

  4 4

評測用例規模與約定

  對於 20% 的評測用例,1 <= n <= 5;

  對於 50% 的評測用例,1 <= n <= 10;

  對於 80% 的評測用例,1 <= n <= 100;

  對於所有評測用例,1 <= n <= 1000。

package 第十三次模擬;

import java.util.Scanner;

public class Demo8序列 {

public static int n=0,count=0;

public static int [] []map ;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

n =sc.nextInt();

sc.close();

map = new int [n+1][n+1];

for (int i = 1; i <=n; i++) {

map[i][i]=1;

map[i][0]=1;

map[0][i]=1;

}

for (int i = 1; i <=n; i++) {

count+=f(n,i);

count%=10000;

//System.out.println(count);

}

System.out.println(count);

// System.out.println(f(4,2));

}

public static int f(int x,int y){

if(map[x][y]!=0){

return map[x][y];

}

for (int i = Math.abs(x-y)-1; i>=0; i--) {

map[x][y]+=f(y,i);

}

map[y][x]=map[x][y];

return map[x][y];

}

}

————————————————

原文鏈接:https://blog.csdn.net/a1439775520/article/details/104750318


分享到:


相關文章: