知识点:母函数(其实更建议去看《组合数学》),五边形数定理(百度总结的也不错)。
#include<bits/stdc++.h> #define ll long long #define P pair<int,int> #define pb push_back #define lson root << 1 #define INF (int)2e9 + 7 #define maxn (int)1e5 + 7 #define rson root << 1 | 1 #define LINF (unsigned long long int)1e18 #define mem(arry, in) memset(arry, in, sizeof(arry)) using namespace std; const int mod = (int)1e9 + 7; int T, n, k; int dp[maxn]; void Inite(){ mem(dp, 0); dp[0] = 1; for(int i = 1; i <= 100000; i++){ for(int j = 1, r = 1; i - (3 * j * j - j) / 2 >= 0; j++, r *= -1){ dp[i] += dp[i - (3 * j * j - j) / 2] * r; dp[i] %= mod; dp[i] =(dp[i] + mod) % mod; if(i - (3 * j * j + j) / 2 >= 0){ dp[i] += dp[i - (3 * j * j + j) / 2] * r; dp[i] %= mod; dp[i] =(dp[i] + mod) % mod; } } } } int main() { Inite(); cin >> T; while(T--){ cin >> n >> k; cout << (dp[n] - dp[n - k] + mod) % mod << endl; } return 0; }