洛谷-2652
key
有个mod k的想法很好,然后就是对于一遍一遍的询问进行前缀和优化,但有个问题就是算出来的s矩阵最开始是个下三角矩阵,但是根据前缀和公式来看,s[i][j]上方的s[i - 1][j]就是空的,而不是我们需要的值,所以要在第i行结束时,对s[i][i + 1]进行特别赋值。
// #include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <cctype>
#include <list>
#include <bitset>
#define dbg(x) cout << #x << " = " << x << "\n"
using namespace std;
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void solve();
int main() {solve();return 0;}
using ll = long long;
const int maxn = 2010;
int c[maxn][maxn];
ll s[maxn][maxn];
inline void solve() {
int t, k;
cin >> t >> k;
c[0][0] = 1;
for (int i = 1;i <= 2000;i++) {
c[i][0] = 1;
}
for (int i = 1;i <= 2000;i++) {
for (int j = 1;j <= i;j++) {
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % k;
if (c[i][j] == 0) s[i][j] = 1;
}
}
for (int i = 1;i <= 2000;i++) {
for (int j = 1;j <= i;j++) {
s[i][j] += s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];
}
s[i][i + 1] = s[i][i];
}
while (t -- ) {
int n, m;
cin >> n >> m;
if (m > n) cout << s[n][n] << "\n";
else cout << s[n][m] << "\n";
}
}
标签:Case,洛谷,cout,int,include,2822
From: https://www.cnblogs.com/FanWQ/p/17205525.html