题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1398
母函数题目。
构造生成函数:(1+x+x^2+x^3+...+x^n)*(1+x^4+x^8+...)*(1+x^9+x^18+...)*...*(1+x^n)
求取其中x^n的系数即可。
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define __ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
int c1[maxn], c2[maxn];
int main() {
__;
int n;
while (cin >> n) {
if (n == 0)break;
for (int i = 0; i <= n; ++i) {
c1[i] = 1;
c2[i] = 0;
}
for (int i = 2; i <= n; ++i) {
int t = i * i;
for (int j = 0; j <= n; ++j) {
for (int k = 0; k + j <= n; k += t) {
c2[j + k] += c1[j];
}
}
for (int j = 0; j <= n; ++j) {
c1[j] = c2[j];
c2[j] = 0;
}
}
cout << c1[n] << endl;
}
return 0;
}