HDU 4658 2013 多校联合赛第6场 Integer Partition

5 篇文章 0 订阅

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
 

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.
 

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值