Integer Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 201 Accepted Submission(s): 97
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<=10 5
Following are T lines. Each line contains two numbers, n and k.
1<=n,k,T<=10 5
Output
T lines, each line contains answer to the responding test case.
Since the numbers can be very large, you should output them modulo 10 9+7.
Since the numbers can be very large, you should output them modulo 10 9+7.
Sample Input
4 4 2 4 3 4 4 4 5
Sample Output
2 4 4 5
Source
题意 将一个整数分拆成几个整数相加,其中相同的因子不能重复出现k次,问共有多少种不同的分法。注意此处不区分数的排列位置。
思路 整数分拆的升级版,只要理解五边形数定理就不难了,直接将前一次写的代码改一下就好了。
AC代码:
/*
* File :tmp.cpp
* Author :Kevin Tan
* Source :ZJNU
*
* 2013年8月7日,下午7:16:41
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<climits>
#include<utility>
#include<cctype>
#include<iomanip>
#include<string>
#include<map>
#include<deque>
#include<queue>
#include<set>
#include<vector>
#include<iterator>
using namespace std;
#define MAX 100005
#define MOD 1000000007
int q[MAX], p[MAX];
int solve(int n,int k){
int res = 0;
for (int j = 0; q[j]*k <= n; j++) {
if (((j + 1) >> 1) & 1) res = (res - p[n - k*q[j]]) % MOD;//此处j从0开始,j-1改成j+1
else res = (res + p[n - k*q[j]]) % MOD;
if (res < 0) res += MOD;
}
return res;
}
int main(int argc, char **argv) {
q[0] = 0;
int k = 1;
for (int i = 1; q[k - 1] <= MAX; i++) {
q[k++] = (3 * i * i - i) / 2;
q[k++] = (3 * i * i + i) / 2;
}
p[0] = 1;
for (int i = 1; i <= MAX; i++) {
p[i] = 0;
for (int j = 1; q[j] <= i; j++) {
if (((j - 1) >> 1) & 1) p[i] = (p[i] - p[i - q[j]]) % MOD;//(j-1)>>1 &1是符号两位两位的变
else p[i] = (p[i] + p[i - q[j]]) % MOD;
if (p[i] < 0) p[i] += MOD;
}
}
//for(int i=1;i<=100;i++)cout<<q[i]<<' ';cout<<endl;
int T;
scanf("%d", &T);
while (T--) {
int n,t;
scanf("%d%d", &n,&t);
printf("%d\n", solve(n,t));
}
return 0;
}