結果
問題 | No.214 素数サイコロと合成数サイコロ (3-Medium) |
ユーザー | ぴろず |
提出日時 | 2015-05-25 14:38:13 |
言語 | Java21 (openjdk 21) |
結果 |
MLE
|
実行時間 | - |
コード長 | 4,252 bytes |
コンパイル時間 | 2,371 ms |
コンパイル使用メモリ | 78,888 KB |
実行使用メモリ | 167,848 KB |
最終ジャッジ日時 | 2024-07-06 08:18:30 |
合計ジャッジ時間 | 10,692 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | MLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
ソースコード
package no214; import java.util.Scanner; public class Main { public static int[] d1 = { 2, 3, 5, 7, 11, 13 }; public static int[] d2 = { 4, 6, 8, 9, 10, 12 }; public static long MOD = 1000000007; public static int MAX = 25*50+1; public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); int p = sc.nextInt(); int c = sc.nextInt(); int[] deme1 = deme(d1, p); int[] deme2 = deme(d2, c); int[] deme = new int[MAX]; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX - i; j++) { deme[i + j] += deme1[i] * deme2[j]; } } // System.out.println("hoge"); long[][] a = new long[MAX][MAX]; for (int i = 0; i < MAX - 1; i++) { a[i][i + 1] = 1; } for (int i = 1; i < MAX; i++) { a[i - 1][0] = deme[i]; } MatrixMod A = new MatrixMod(a); // System.out.println(A); long[] x = new long[MAX]; x[0] = 1; MatrixMod X = MatrixMod.vector(x); MatrixMod B = A.fastPow(n).multiply(X); long ans = 0; for (int i = 0; i < MAX; i++) { ans = (ans + B.e[i][0]) % MOD; } System.out.println(ans); } public static int[] deme(int[] d, int num) { int[][] dp = new int[num+1][MAX]; dp[0][0] = 1; for(int i=0;i<6;i++) { for(int j=0;j<num;j++) { for(int k=0;k<MAX-d[i];k++) { dp[j+1][k+d[i]] += dp[j][k]; if (dp[j+1][k+d[i]] >= MOD) { dp[j+1][k+d[i]] -= MOD; } } } } return dp[num]; } } class MatrixMod { public static long MOD = Main.MOD; long[][] e; int n, m; public MatrixMod(long[][] e) { this.e = e; this.n = e.length; this.m = e[0].length; } public static MatrixMod identity(int n) { long[][] e = new long[n][n]; for (int i = 0; i < n; i++) e[i][i] = 1; return new MatrixMod(e); } public static MatrixMod zero(int n, int m) { return new MatrixMod(new long[n][m]); } public static MatrixMod vector(long[] v) { long[][] e = new long[v.length][1]; for (int i = 0; i < v.length; i++) e[i][0] = v[i]; return new MatrixMod(e); } public MatrixMod add(MatrixMod b) { long[][] c = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { c[i][j] = e[i][j] + b.e[i][j]; if (c[i][j] >= MOD) { c[i][j] -= MOD; } } } return new MatrixMod(c); } public MatrixMod multiply(long k) { k = (k % MOD + MOD) % MOD; long[][] c = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { c[i][j] = e[i][j] * k % MOD; } } return new MatrixMod(c); } public MatrixMod multiply(MatrixMod b) { long[][] c = new long[n][b.m]; for (int i = 0; i < n; i++) { for (int j = 0; j < b.m; j++) { for (int k = 0; k < m; k++) { c[i][j] = (c[i][j] + e[i][k] * b.e[k][j]) % MOD; } } } return new MatrixMod(c); } public MatrixMod pow(long exp) { MatrixMod ret = identity(n); MatrixMod x = this; while (exp > 0) { if ((exp & 1) != 0) { ret = ret.multiply(x); } x = x.multiply(x); exp >>>= 1; } return ret; } public long[] mult(long[][] X,long[] a) { int n = a.length; long[] b = new long[n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { b[i] = (b[i] + X[i][j] * a[j]) % MOD; } } return b; } /* * | a 1 0 0 | * | b 0 1 0 | * | c 0 0 1 | * | d 0 0 0 | */ public MatrixMod fastPow(long exp) { long[] u = new long[n]; u[n-1] = 1; long[][] X = this.e; while(exp > 0) { if ((exp & 1) != 0) { u = mult(X,u); } long[] a = new long[n]; for(int i=0;i<n;i++) { a[i] = X[i][n-1]; } a = mult(X,a); X = construct(a); exp >>>= 1; } return new MatrixMod(construct(u)); } private long[][] construct(long[] u) { long[][] X = new long[n][n]; for(int j=n-1;j>=0;j--) { for(int i=0;i<n;i++) { X[i][j] = u[i]; } long[] v = new long[n]; for(int i=0;i<n;i++) { v[i] = (e[i][0] * u[0] + (i == n - 1 ? 0 : u[i+1])) % MOD; } u = v; } return X; } public String toString() { StringBuilder sb = new StringBuilder(); int n2 = Math.min(100, n); int m2 = Math.min(100, m); for (int i = 0; i < n2; i++) { for (int j = 0; j < m2; j++) { if (j > 0) { sb.append(' '); } sb.append(e[i][j]); } sb.append('\n'); } return sb.toString(); } }