結果
| 問題 |
No.1847 Good Sequence
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2022-02-18 22:18:27 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 63 ms / 3,000 ms |
| コード長 | 2,770 bytes |
| コンパイル時間 | 4,855 ms |
| コンパイル使用メモリ | 271,472 KB |
| 最終ジャッジ日時 | 2025-01-28 00:15:52 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 41 |
ソースコード
#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define Inf 1000000001
template <class S,
S (*op0)(S, S),
S (*e0)(),
S (*op1)(S, S),
S (*e1)()
>
struct matrix{
vector<vector<S>> v;
int _h,_w;
matrix(vector<vector<S>> X){
v = X;
_h = X.size();
_w = 0;
if(X.size()>0)_w = X[0].size();
}
matrix(int h,int w){
v.resize(h,vector<S>(w,e0()));
_h = h;
_w = w;
}
void add_element(int from,int to,S x){
v[to][from] = op0(v[to][from],x);
}
matrix e(){
assert(_h==_w);
matrix<S,op0,e0,op1,e1> ret(_h,_w);
for(int i=0;i<_h;i++){
for(int j=0;j<_w;j++){
if(i==j)ret.v[i][j] = e1();
else ret.v[i][j] = e0();
}
}
return ret;
}
matrix &operator*=(const matrix &another){
matrix<S,op0,e0,op1,e1> ret(_h,another._w);
for(int i=0;i<_h;i++){
for(int j=0;j<another._w;j++){
ret.v[i][j] = e0();
for(int k=0;k<_w;k++){
ret.v[i][j] = op0(ret.v[i][j],op1(v[i][k],another.v[k][j]));
}
}
}
v = ret.v;
return (*this);
}
matrix operator*(const matrix &another)const{
return (matrix(*this)*=another);
}
matrix pow(long long cnt){
matrix<S,op0,e0,op1,e1> ret = e();
auto temp = *this;
while(cnt!=0LL){
if((cnt&1)==1){
ret *= temp;
}
temp *= temp;
cnt>>=1;
}
return ret;
}
};
mint op0(mint a,mint b){
return a+b;
}
mint e0(){
return 0;
}
mint op1(mint a,mint b){
return a*b;
}
mint e1(){
return 1;
}
int main(){
long long L,N,m;
cin>>L>>N>>m;
vector<int> K(m);
rep(i,m)cin>>K[i];
map<pair<int,int> ,int> mp;
int t= 0;
rep(i,m){
for(int j=1;j<=K[i]+1;j++){
mp[make_pair(K[i],j)] = t;
t++;
}
}
//cout<<'a'<<endl;
int start = t;
t++;
int goal = t;
t++;
matrix<mint,op0,e0,op1,e1> M(t,t);
//cout<<'a'<<endl;
for(int i=1;i<=N;i++){
if(mp.count(make_pair(i,1))){
M.add_element(start,mp[make_pair(i,1)],1);
}
else{
M.add_element(start,start,1);
}
}
M.add_element(goal,goal,N);
//cout<<'a'<<endl;
rep(i,m){
for(int j=1;j<=K[i]+1;j++){
for(int k=1;k<=N;k++){
int from = mp[make_pair(K[i],j)];
int to;
if(K[i]==k){
to = mp[make_pair(K[i],min(j+1,K[i]+1))];
}
else{
if(j==K[i])to = goal;
else{
if(mp.count(make_pair(k,1)))to = mp[make_pair(k,1)];
else to = start;
}
}
M.add_element(from,to,1);
}
}
}
// cout<<'a'<<endl;
M = M.pow(L);
matrix<mint,op0,e0,op1,e1> temp(t,1);
temp.v[start][0] = 1;
temp = M * temp;
mint ans = 0;
rep(i,m){
ans += temp.v[mp[make_pair(K[i],K[i])]][0];
}
ans += temp.v[goal][0];
cout<<ans.val()<<endl;
return 0;
}
沙耶花