HDU 4658 Integer Partition (2013多校6 1004题)

Integer Partition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22    Accepted Submission(s): 15


Problem Description
Given n, k, calculate the number of different (unordered) partitions of n such that no part is repeated k or more times.
 

 

Input
First line, number of test cases, T.
Following are T lines. Each line contains two numbers, n and k.

1<=n,k,T<=105
 

 

Output
T lines, each line contains answer to the responding test case.
Since the numbers can be very large, you should output them modulo 109+7.
 

 

Sample Input
4 4 2 4 3 4 4 4 5
 

 

Sample Output
2 4 4 5
 

 

Source
 

 

Recommend
zhuyuanchen520
 
 
 
跟上次多校求数的划分很类似。
 
 
所谓的五边形数定理还没有搞懂。
 
 
先贴个代码先,胡搞弄过去的
 
 
 1 /*
 2  * Author:  kuangbin
 3  * Created Time:  2013/8/8 11:53:35
 4  * File Name: 1004.cpp
 5  */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstdlib>
 9 #include <cstring>
10 #include <cmath>
11 #include <algorithm>
12 #include <string>
13 #include <vector>
14 #include <stack>
15 #include <queue>
16 #include <set>
17 #include <time.h>
18 using namespace std;
19 const int MOD = 1e9+7;
20 int dp[100010];
21 void init()
22 {
23     memset(dp,0,sizeof(dp));
24     dp[0] = 1;
25     for(int i = 1;i <= 100000;i++)
26     {
27         for(int j = 1, r = 1; i - (3 * j * j - j) / 2 >= 0; j++, r *= -1)
28         {
29             dp[i] += dp[i -(3 * j * j - j) / 2] * r;
30             dp[i] %= MOD;
31             dp[i] = (dp[i]+MOD)%MOD;
32             if( i - (3 * j * j + j) / 2 >= 0 )
33             {
34                 dp[i] += dp[i - (3 * j * j + j) / 2] * r;
35                 dp[i] %= MOD;
36                 dp[i] = (dp[i]+MOD)%MOD;
37             }
38 
39         }
40 
41 
42 
43     }
44 }
45 
46 int solve(int n,int k)
47 {
48     int ans = dp[n];
49     for(int j = 1, r = -1; n - k*(3 * j * j - j) / 2 >= 0; j++, r *= -1)
50     {
51         ans += dp[n -k*(3 * j * j - j) / 2] * r;
52         ans %= MOD;
53         ans = (ans+MOD)%MOD;
54         if( n - k*(3 * j * j + j) / 2 >= 0 )
55         {
56             ans += dp[n - k*(3 * j * j + j) / 2] * r;
57             ans %= MOD;
58             ans = (ans+MOD)%MOD;
59         }
60 
61     }
62     return ans;
63 }
64 
65 int main()
66 {
67     init();
68     int T;
69     int n,k;
70     scanf("%d",&T);
71     while(T--)
72     {
73         scanf("%d%d",&n,&k);
74         printf("%d\n",solve(n,k));
75     }
76     return 0;
77 }

 

 
 
 
 
 
 

 

 

 

 

posted on 2013-08-08 19:44  kuangbin  阅读(1521)  评论(1编辑  收藏  举报

导航

JAVASCRIPT: