結果
問題 | No.1513 simple 門松列 problem |
ユーザー | koba-e964 |
提出日時 | 2021-12-11 11:13:33 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 210 ms / 3,000 ms |
コード長 | 3,027 bytes |
コンパイル時間 | 14,560 ms |
コンパイル使用メモリ | 379,108 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-19 11:35:02 |
合計ジャッジ時間 | 15,694 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 210 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 1 ms
5,376 KB |
testcase_11 | AC | 1 ms
5,376 KB |
testcase_12 | AC | 1 ms
5,376 KB |
testcase_13 | AC | 1 ms
5,376 KB |
testcase_14 | AC | 190 ms
5,376 KB |
testcase_15 | AC | 187 ms
5,376 KB |
testcase_16 | AC | 206 ms
5,376 KB |
testcase_17 | AC | 122 ms
5,376 KB |
testcase_18 | AC | 40 ms
5,376 KB |
testcase_19 | AC | 3 ms
5,376 KB |
testcase_20 | AC | 21 ms
5,376 KB |
ソースコード
use std::io::Read; fn get_word() -> String { let stdin = std::io::stdin(); let mut stdin=stdin.lock(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec<u8> = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = String::from_utf8(buf).unwrap(); return ret; } } } #[allow(dead_code)] fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() } const MOD: i64 = 998_244_353; // https://yukicoder.me/problems/no/1513 (2.5) // https://yukicoder.me/problems/no/1516 (3.5) // O(K^6 log N) or O(K^2 N) (累積和で加速)。 // dpgt[i+1][a][b] += dplt[i][b][c] (a > b, c != a) // dplt[i+1][a][b] += dpgt[i][b][c] (a < b, c != a) // epgt[i+1][a][b] += eplt[i][b][c] + a * dplt[b][c] (a > b, c != a) // eplt[i+1][a][b] += epgt[i][b][c] + a * dpgt[b][c] (a < b, c != a) fn main() { let n: usize = get(); let k: usize = get(); let mut dp = vec![vec![0; k]; k]; let mut dpacc = vec![vec![0; k + 1]; k]; let mut ep = vec![vec![0; k]; k]; let mut epacc = vec![vec![0; k + 1]; k]; for a in 0..k { for b in 0..k { if a != b { dp[a][b] += 1; ep[a][b] += (a + b) as i64; } } } for a in 0..k { for b in 0..k { dpacc[a][b + 1] = dpacc[a][b] + dp[a][b]; epacc[a][b + 1] = epacc[a][b] + ep[a][b]; } } for _ in 0..n - 2 { let mut ndp = vec![vec![0; k]; k]; let mut nep = vec![vec![0; k]; k]; for a in 0..k { for b in 0..k { if a == b { continue; } if a < b { ndp[a][b] = (ndp[a][b] + dpacc[b][b] - dp[b][a] + MOD) % MOD; nep[a][b] = (nep[a][b] + epacc[b][b] - ep[b][a] + MOD) % MOD; nep[a][b] = (nep[a][b] + (dpacc[b][b] - dp[b][a] + MOD) * a as i64) % MOD; } else { ndp[a][b] = (ndp[a][b] + dpacc[b][k] - dpacc[b][b + 1] - dp[b][a] + 2 * MOD) % MOD; nep[a][b] = (nep[a][b] + epacc[b][k] - epacc[b][b + 1] - ep[b][a] + 2 * MOD) % MOD; nep[a][b] = (nep[a][b] + (dpacc[b][k] - dpacc[b][b + 1] - dp[b][a] + 2 * MOD) * a as i64) % MOD; } } } dp = ndp; ep = nep; for a in 0..k { for b in 0..k { dpacc[a][b + 1] = dpacc[a][b] + dp[a][b]; epacc[a][b + 1] = epacc[a][b] + ep[a][b]; } } } let mut ans1 = 0; let mut ans2 = 0; for c in 0..k { for d in 0..k { if c != d { ans1 = (ans1 + dp[c][d]) % MOD; ans2 = (ans2 + ep[c][d]) % MOD; } } } println!("{} {}", ans1, ans2); }