voidwork(){ int n, k, p; cin >> n >> k >> p; vector<vector<int>> dp(k + 1, vector<int>(k + 1, 0)); dp[0][0] = 1; for (int i = 1; i <= n + 2; i++) { // n + 2, 0, 0 will be the ans. vector<vector<int>> sum(k + 1, vector<int>(k + 1, 0)); for (int b = 0; b <= k; b++) { sum[b][0] = dp[0][b]; for (int a = 1; a <= k; a++) sum[b][a] = (dp[a][b] + sum[b][a - 1]) % p; } for (int b = 0; b <= k; b++) { for (int c = 0; c <= k; c++) { if (b - c > 0) dp[b][c] = (sum[b][k] - sum[b][b - c - 1] + p) % p; // a > b - c is ok else dp[b][c] = sum[b][k]; // any a is ok } } } cout << dp[0][0] << '\n'; }
intmain(){ ios::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while (T--) work(); return0; }