hdu 4651 - Partition(五边形数定理)
定理详见维基百科。。。。http://zh.wikipedia.org/wiki/%E4%BA%94%E9%82%8A%E5%BD%A2%E6%95%B8%E5%AE%9A%E7%90%86
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #define LL long long #define INF 0x7fffffff #define MOD 1000000007 #define M 100005 using namespace std; int p[M]; LL ans[M]; void presolve() { int c = 0; for(int i = 1; ; i++) { p[c++] = (3*i*i-i)/2; p[c++] = (3*i*i+i)/2; if((3*i*i-i)/2>M) break; } } LL solve(int n) { if(ans[n]) return ans[n]; LL temp = 0; for(int i = 0; p[i] <= n; ++i) { if((i/2)&1) temp -= solve(n-p[i]); else temp += solve(n-p[i]); } if(temp<0) temp+=(-temp/MOD+1)*MOD; return ans[n] = temp%MOD; } int main () { int t, n; scanf("%d",&t); presolve(); memset(ans,0,sizeof(ans)); while(t--) { scanf("%d",&n); ans[0] = 1; ans[1] = 1; ans[2] = 2; for(int i = 3; i <= n; ++i) solve(i); printf("%I64d\n",solve(n)); } return 0; }