結果
| 問題 |
No.3202 Periodic Alternating Subsequence
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-07-03 10:24:23 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,460 bytes |
| コンパイル時間 | 956 ms |
| コンパイル使用メモリ | 93,540 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2025-07-03 21:35:04 |
| 合計ジャッジ時間 | 1,766 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 2 |
| other | WA * 23 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
using namespace std;
using ll = long long;
using Matrix = vector<vector<ll>>;
const int MOD = 1e9 + 7;
const int DIM = 7;
// 行列積
Matrix multiply(const Matrix& A, const Matrix& B) {
Matrix C(DIM, vector<ll>(DIM, 0));
for (int i = 0; i < DIM; ++i) {
for (int j = 0; j < DIM; ++j) {
for (int k = 0; k < DIM; ++k) {
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
}
}
}
return C;
}
// 行列の累乗
Matrix power(Matrix A, ll k) {
Matrix res(DIM, vector<ll>(DIM, 0));
for (int i = 0; i < DIM; ++i) res[i][i] = 1; // 単位行列で初期化
while (k > 0) {
if (k & 1) res = multiply(res, A);
A = multiply(A, A);
k >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll K;
cin >> K;
string T;
cin >> T;
Matrix M0(DIM, vector<ll>(DIM, 0));
// V_new[0] (C0) = V_old[0] + (1 + V_old[3]) = C0 + C1 + 1
M0[0][0] = 1; M0[0][3] = 1; M0[0][6] = 1;
// V_new[1] (L0) = V_old[1] + (1 + V_old[4] + V_old[3]) = L0 + C1 + L1 + 1
M0[1][1] = 1; M0[1][3] = 1; M0[1][4] = 1; M0[1][6] = 1;
// V_new[2] (Q0) = V_old[2] + (1 + V_old[5] + 2*V_old[4] + V_old[3]) = Q0 + C1 + 2L1 + Q1 + 1
M0[2][2] = 1; M0[2][3] = 1; M0[2][4] = 2; M0[2][5] = 1; M0[2][6] = 1;
M0[3][3] = 1; M0[4][4] = 1; M0[5][5] = 1; M0[6][6] = 1;
Matrix M1(DIM, vector<ll>(DIM, 0));
M1[0][0] = 1; M1[1][1] = 1; M1[2][2] = 1;
// V_new[3] (C1) = V_old[3] + (1 + V_old[0])
M1[3][0] = 1; M1[3][3] = 1; M1[3][6] = 1;
// V_new[4] (L1) = V_old[4] + (1 + V_old[1] + V_old[0])
M1[4][0] = 1; M1[4][1] = 1; M1[4][4] = 1; M1[4][6] = 1;
// V_new[5] (Q1) = V_old[5] + (1 + V_old[2] + 2*V_old[1] + V_old[0])
M1[5][0] = 1; M1[5][1] = 2; M1[5][2] = 1; M1[5][5] = 1; M1[5][6] = 1;
M1[6][6] = 1;
Matrix MT(DIM, vector<ll>(DIM, 0));
for(int i = 0; i < DIM; ++i) MT[i][i] = 1; // 単位行列
for (char c : T) {
if (c == '0') {
MT = multiply(M0, MT);
} else {
MT = multiply(M1, MT);
}
}
Matrix M_final = power(MT, K);
// 初期ベクトル [0,0,0,0,0,0,1]^T との積を計算すると、結果は最終列に現れる
ll final_Q0 = M_final[2][6];
ll final_Q1 = M_final[5][6];
cout << (final_Q0 + final_Q1) % MOD << endl;
return 0;
}