目錄java
1 問題描述數組
2 解決方案spa
下面代碼參考自文末參考資料1,具體講解請見參考資料1~.net
具體代碼以下:code
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); } }
參考資料:blog