結果
| 問題 |
No.572 妖精の演奏
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-11-12 13:03:47 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 115 ms / 2,000 ms |
| コード長 | 2,270 bytes |
| コンパイル時間 | 2,665 ms |
| コンパイル使用メモリ | 77,588 KB |
| 実行使用メモリ | 52,620 KB |
| 最終ジャッジ日時 | 2024-11-24 22:40:45 |
| 合計ジャッジ時間 | 4,949 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
long n = sc.nextLong() - 1;
int m = sc.nextInt();
long[][][] dp = new long[33][m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
dp[0][i][j] = sc.nextInt();
}
}
for (int i = 1; i < 33; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
for (int l = 0; l < m; l++) {
dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][l] + dp[i - 1][l][k]);
}
}
}
}
long[][] current = new long[m][m];
for (int i = 32; i >= 0; i--) {
if (n < (1L << i)) {
continue;
}
n -= (1L << i);
long[][] next = new long[m][m];
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
for (int l = 0; l < m; l++) {
next[j][k] = Math.max(next[j][k], current[j][l] + dp[i][l][k]);
}
}
}
current = next;
}
long max = 0;
for (long[] arr : current) {
for (long x : arr) {
max = Math.max(max, x);
}
}
System.out.println(max);
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public String nextLine() throws Exception {
return br.readLine();
}
public String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten