考虑直接dp会T, 用矩阵优化一下就好了。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 50 + 2; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 2015; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int MN, n, m; struct Matrix { int a[N][N]; Matrix() { for(int i = 0; i < MN; i++) { for(int j = 0; j < MN; j++) { a[i][j] = 0; } } } void init() { for(int i = 0; i < MN; i++) { a[i][i] = 1; } } Matrix operator * (const Matrix &B) { Matrix C; for(int i = 0; i < MN; i++) { for(int j = 0; j < MN; j++) { for(int k = 0; k < MN; k++) { C.a[i][j] = (C.a[i][j] + a[i][k] * B.a[k][j]) % mod; } } } return C; } Matrix operator ^ (int b) { Matrix A = (*this); Matrix C; C.init(); while(b) { if(b & 1) C = C * A; A = A * A; b >>= 1; } return C; } } mat; int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); MN = n + 1; for(int i = 0; i < MN; i++) { for(int j = 0; j < MN; j++) { mat.a[i][j] = (i == MN - 1) ? 1 : 0; } } for(int i = 0; i < n; i++) { int k, x; scanf("%d", &k); while(k--) { scanf("%d", &x); mat.a[i][x - 1]++; } } Matrix ret = mat ^ m; int ans = 0; for(int j = 0; j < MN; j++) { add(ans, ret.a[MN - 1][j]); } printf("%d\n", ans); } return 0; } /* */