HDU 4658 Integer Partition

来源:互联网 发布:数控编程人员工资待遇 编辑:程序博客网 时间:2024/05/02 00:48
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
44 24 34 44 5
 

Sample Output
2445

生成函数+五边形数定理+分割函数

#include<iostream>#include<cmath>#include<map>#include<vector>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 200005;const int base = 1000000007;int f[maxn], p[maxn], n, T, k;void init(){int n = 1000;for (int i = f[0] = 1; i <= n; i++){f[i + i - 1] = (i * (i + i + i - 1) >> 1) % base;f[i + i] = (i * (i + i + i + 1) >> 1) % base;}for (int i = p[0] = 1; i < maxn; i++){p[i] = 0;for (int j = 1, k = -1; i >= f[j]; j++){if (j & 1) k *= -1;(p[i] += k * p[i - f[j]]) %= base;}p[i] = (p[i] + base) % base;}}int work(int n, int k){int ans = p[n];for (int i = 1, j = 1; f[i] * k <= n; i++){if (i & 1) j *= -1;(ans += j * p[n - f[i] * k]) %= base;}return (ans + base) % base;}int main(){init();scanf("%d", &T);while (T--) scanf("%d%d", &n, &k), printf("%d\n", work(n, k));return 0;}


0 0
原创粉丝点击