結果

問題 No.1847 Good Sequence
ユーザー SSRSSSRS
提出日時 2022-02-18 22:42:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 3,000 ms
コード長 2,137 bytes
コンパイル時間 2,450 ms
コンパイル使用メモリ 208,360 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 19:43:26
合計ジャッジ時間 4,308 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 23 ms
4,384 KB
testcase_22 AC 5 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 8 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 6 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 10 ms
4,384 KB
testcase_32 AC 8 ms
4,384 KB
testcase_33 AC 24 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 22 ms
4,384 KB
testcase_37 AC 17 ms
4,384 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 AC 45 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long modpow(long long a, long long b){
	long long ans = 1;
	while (b > 0){
		if (b % 2 == 1){
			ans *= a;
			ans %= MOD;
		}
		a *= a;
		a %= MOD;
		b /= 2;
	}
	return ans;
}
vector<vector<long long>> matmul(vector<vector<long long>> A, vector<vector<long long>> B){
	int N = A.size();
	vector<vector<long long>> ans(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++){
		for (int j = 0; j < N; j++){
			for (int k = 0; k < N; k++){
				ans[i][k] += A[i][j] * B[j][k];
				ans[i][k] %= MOD;
			}
		}
	}
	return ans;
}
vector<vector<long long>> matexp(vector<vector<long long>> A, long long b){
	int N = A.size();
	vector<vector<long long>> ans(N, vector<long long>(N, 0));
	for (int i = 0; i < N; i++){
		ans[i][i] = 1;
	}
	while (b > 0){
		if (b % 2 == 1){
			ans = matmul(ans, A);
		}
		A = matmul(A, A);
		b /= 2;
	}
	return ans;
}
int main(){
  long long L;
  int N, M;
  cin >> L >> N >> M;
  vector<int> K(M);
  for (int i = 0; i < M; i++){
    cin >> K[i];
  }
  long long ans = modpow(N, L);
  vector<int> id(N + 1, -1);
  for (int i = 0; i < M; i++){
    id[K[i]] = i;
  }
  vector<int> S(M + 1);
  S[0] = 1;
  for (int i = 0; i < M; i++){
    S[i + 1] = S[i] + K[i] + 1;
  }
  vector<vector<long long>> A(S[M], vector<long long>(S[M], 0));
  A[0][0] += N - M;
  for (int i = 0; i < M; i++){
    A[0][S[i]]++;
  }
  for (int i = 0; i < M; i++){
    for (int j = 1; j <= K[i] + 1; j++){
      for (int k = 0; k < N; k++){
        int p = S[i] + j - 1;
        if (K[i] == k + 1){
          if (j <= K[i]){
            A[p][p + 1]++;
          } else {
            A[p][p]++;
          }
        } else if (id[k + 1] != -1){
          if (j != K[i]){
            A[p][S[id[k + 1]]]++;
          }
        } else {
          if (j != K[i]){
            A[p][0]++;
          }
        }
      }
    }
  }
  A = matexp(A, L);
  ans += MOD - A[0][0];
  for (int i = 0; i < M; i++){
    for (int j = 1; j <= K[i] + 1; j++){
      if (j != K[i]){
        ans += MOD - A[0][S[i] + j - 1];
      }
    }
  }
  ans %= MOD;
  cout << ans << endl;
}
0