Description
求 n n 的本质不同拆分数
Input
第一行一整数表示用例组数,每组用例输入一整数 n(1≤T≤100,1≤n≤105) n ( 1 ≤ T ≤ 100 , 1 ≤ n ≤ 10 5 )
Output
对于每组用例,输出 n n 的本质不同拆分数,结果模
Sample Input
4
5
11
15
19
Sample Output
7
56
176
490
Solution
设 n n 的拆分数为,由五边形数定理有 p(n)=p(n−1)+p(n−2)−p(n−5)−p(n−7)+... p ( n ) = p ( n − 1 ) + p ( n − 2 ) − p ( n − 5 ) − p ( n − 7 ) + . . .
O(nn−−√) O ( n n ) 即可筛出 p(1) p ( 1 ) ~ p(n) p ( n )
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
#define mod 1000000007
void inc(int &x,int y)
{
x=x+y>=mod?x+y-mod:x+y;
}
void dec(int &x,int y)
{
x=x-y<0?x-y+mod:x-y;
}
int T,n,f[maxn];
int main()
{
f[0]=1;
for(int i=1;i<=100000;i++)
{
for(int j=1,w=1;w<=i;w+=3*j+1,j++)
if(j&1)inc(f[i],f[i-w]);
else dec(f[i],f[i-w]);
for(int j=1,w=2;w<=i;w+=3*j+2,j++)
if(j&1)inc(f[i],f[i-w]);
else dec(f[i],f[i-w]);
}
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("%d\n",f[n]);
}
return 0;
}