/*@author xht*/ #include<cassert> #include<cctype> #include<climits> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<iomanip> #include<iostream> #include<list> #include<map> #include<queue> #include<set> #include<stack> #include<string> #include<vector> usingstd::cin; usingstd::cout; char ch; bool iosig; template<classT> inlinevoidread(T &x) { iosig = 0, x = 0; do { ch = getchar(); if (ch == '-') iosig = 1; } while (!isdigit(ch)); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar(); if (iosig) x = -x; } typedeflonglong ll; int t, k; int f[2010][2010]; int tree[2010][2010]; #define lowbit(k) k & -k inlinevoidmodify(int k, int n, int cur, int v = 1){ for (; k <= n; k += lowbit(k)) tree[cur][k] += v; } inlineintquery(int k, int cur){ int ans = 0; for (; k; k -= lowbit(k)) ans += tree[cur][k]; return ans; } inlinevoidinit(){ for (int i = 0; i <= 2000; i++) f[i][0] = 1; for (int i = 1; i <= 2000; i++) for (int j = 1; j <= i; j++) f[i][j] = (f[i - 1][j - 1] + f[i - 1][j]) % k; for (int i = 0; i <= 2000; i++) for (int j = 0; j <= i; j++) if (!f[i][j]) modify(j + 1, 2001, i + 1);
} inlinevoidsolve(){ int n, m; read(n), read(m); int ans = 0; for (int i = 0; i <= n; i++) ans += query(std::min(i, m) + 1, i + 1); cout << ans << "\n"; } intmain(){ read(t), read(k); init(); while (t--) solve(); return0; }