题目:武汉工程大学第八届ACM新生赛 D
一个矩阵的k次方后,a[i][j]表示从i到j长度为k的条数
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128=__int128;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using db = double;
using ldb = long double;
#define F first
#define S second
#define int ll
const int MOD=998244353;
ll ksm(ll a,ll x,ll mod=MOD){
a%=mod;
if(a==0) return 0;
ll ans=1;
while(x){
if(x&1) ans=(ans*a)%mod;
a=(a*a)%mod;
x>>=1;
}
return ans;
}
struct matrix{
vector<vector<ll>> a;
int n;
matrix(int x,bool _=false){
n=x;
a.assign(n+1,vector<ll>(n+1,0));
if(_){
for(int i=1;i<=n;i++) a[i][i]=1;
}
}
matrix operator*(const matrix &b)const{
matrix res(n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
res.a[i][j]=(res.a[i][j]+a[i][k]*b.a[k][j]%(MOD))%(MOD);
}
}
}
return res;
}
static matrix ksm(matrix x,ll y){
matrix res(x.n,true);
while(y){
if(y&1) res=res*x;
x=x*x;
y>>=1;
}
return res;
}
};
void solve(){
ll n;cin>>n;
if(n==1){
cout<<"5\n";
return;
}
matrix base(5);
for(int i=1;i<=5;i++){
int k;cin>>k;
for(int j=1;j<=k;j++){
int x;cin>>x;
base.a[i][x]=1;
}
}
matrix ans=matrix::ksm(base,n-1);
ll res=0;
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
res=(res+ans.a[i][j])%MOD;
}
}
cout<<res<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int t=1;
// cin>>t;
while(t--) solve();
return 0;
}

京公网安备 11010502036488号