HDU 4671 Partition(定理题)

题目链接

这题,明显考察搜索能力。。。在中文版的维基百科中找到了公式。

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <string>

 4 #include <algorithm>

 5 using namespace std;

 6 #define MOD 1000000007

 7 #define LL __int64

 8 LL dp[200001];

 9 int que[1001],flag[1001];

10 int main()

11 {

12     int i,n,t,j,num;

13     LL x;

14     num = 0;

15     for(i = 0;;i ++)

16     {

17         t = (3*i*i - i)/2;

18         if(t > 300000) break;

19         flag[num] = i;

20         que[num++] = t;

21         t = (3*i*i + i)/2;

22         flag[num] = i;

23         que[num++] = t;

24     }

25     dp[1] = 1;dp[2] = 2;

26     dp[0] = 1;

27     for(i = 3;i <= 100000;i ++)

28     {

29         for(j = 1;j <= 895;j ++)

30         {

31             if(que[j] > i)

32             break;

33             else

34             x = dp[i-que[j]];

35             if(flag[j]%2 == 1)

36             {

37                 dp[i] = (dp[i] + x)%MOD;

38             }

39             else

40             {

41                 dp[i] = (dp[i] - x)%MOD;

42                 if(dp[i] < 0)

43                 dp[i] += MOD;

44             }

45         }

46     }

47     scanf("%d",&t);

48     while(t--)

49     {

50         scanf("%d",&n);

51         printf("%I64d\n",dp[n]);

52     }

53     return 0;

54 }

 

你可能感兴趣的:(partition)