目錄
1 問題描述
問題描述
觀察這個數列:
1 3 0 2 -1 1 -2 ...
這個數列中后一項總是比前一項增加2或者減少3。
棟棟對這種數列很好奇,他想知道長度為 n 和為 s 而且后一項總是比前一項增加a或者減少b的整數數列可能有多少種呢?
1 3 0 2 -1 1 -2 ...
這個數列中后一項總是比前一項增加2或者減少3。
棟棟對這種數列很好奇,他想知道長度為 n 和為 s 而且后一項總是比前一項增加a或者減少b的整數數列可能有多少種呢?
輸入格式
輸入的第一行包含四個整數 n s a b,含義如前面說述。
輸出格式
輸出一行,包含一個整數,表示滿足條件的方案數。由於這個數很大,請輸出方案數除以100000007的余數。
樣例輸入
4 10 2 3
樣例輸出
2
樣例說明
這兩個數列分別是2 4 1 3和7 4 1 -2。
數據規模和約定
對於10%的數據,1<=n<=5,0<=s<=5,1<=a,b<=5;
對於30%的數據,1<=n<=30,0<=s<=30,1<=a,b<=30;
對於50%的數據,1<=n<=50,0<=s<=50,1<=a,b<=50;
對於70%的數據,1<=n<=100,0<=s<=500,1<=a, b<=50;
對於100%的數據,1<=n<=1000,-1,000,000,000<=s<=1,000,000,000,1<=a, b<=1,000,000。
對於30%的數據,1<=n<=30,0<=s<=30,1<=a,b<=30;
對於50%的數據,1<=n<=50,0<=s<=50,1<=a,b<=50;
對於70%的數據,1<=n<=100,0<=s<=500,1<=a, b<=50;
對於100%的數據,1<=n<=1000,-1,000,000,000<=s<=1,000,000,000,1<=a, b<=1,000,000。
2 解決方案
下面代碼參考自文末參考資料1,具體講解請見參考資料1~
具體代碼如下:
import java.util.Scanner; public class Main { public static long n, s, a, b; public static long result = 0L; public static int e = 0; public static long[][] dp;; public void getDP() { dp = new long[2][1000005]; dp[e][0] = 1; for(int i = 1;i < n;i++) { e = 1 -e; for(int j = 0;j <= i * (i + 1) / 2;j++) { if(i > j) dp[e][j] = dp[1 - e][j]; else dp[e][j] = (dp[1 - e][j] + dp[1 - e][j - i]) % 100000007; } } } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); n = in.nextLong(); s = in.nextLong(); a = in.nextLong(); b = in.nextLong(); test.getDP(); for(long i = 0;i <= n * (n - 1) / 2;i++) { long t = s - i * a + (n*(n-1)/2-i) * b; if(t % n == 0) result = (result + dp[e][(int) i]) % 100000007; } System.out.println(result); } }
參考資料: