You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
typedef long long ll;
|
|
const int mod = 1000000007;
|
|
const int N = 2e5 + 50;
|
|
ll fac[N], ifac[N];
|
|
ll qpow(ll a, ll b)
|
|
{
|
|
ll ans = 1;
|
|
for (; b; b >>= 1, a = a * a % mod)
|
|
if (b & 1)
|
|
ans = ans * a % mod;
|
|
return ans;
|
|
}
|
|
inline ll inv(ll x) { return qpow(x, mod - 2); }
|
|
inline ll C(ll n, ll m) { return (n < 0 || n < m || m < 0) ? 0 : fac[n] * ifac[n - m] * ifac[m] % mod; }
|
|
inline ll F(int x, int y) { return C(x + y - 1, y - 1); }
|
|
int main()
|
|
{
|
|
for (int i = fac[0] = ifac[0] = 1; i < N; i++)
|
|
fac[i] = fac[i - 1] * i % mod, ifac[i] = inv(fac[i]);
|
|
int T, n, m;
|
|
cin >> T;
|
|
while (T--)
|
|
{
|
|
cin >> n >> m;
|
|
if (m == 1 || n == 1)
|
|
{
|
|
puts("1");
|
|
continue;
|
|
}
|
|
ll ans = 0;
|
|
for (int x = 1; x <= n; x++)
|
|
{
|
|
int t = (n - x + m - 2) / (m - 1);
|
|
if (x <= t) continue;
|
|
for (int k = 0; n - (k + 1) * x >= 0; k++)
|
|
{
|
|
ll tmp = C(m - 1, k) * F(n - (k + 1) * x, m - 1) % mod;
|
|
if (k % 2 == 0)
|
|
ans = (ans + tmp) % mod;
|
|
else
|
|
ans = (ans - tmp + mod) % mod;
|
|
}
|
|
}
|
|
cout << ans << endl;
|
|
}
|
|
return 0;
|
|
}
|