hdu 5646 DZY Loves Partition(整数拆分)

2 篇文章 0 订阅
1 篇文章 0 订阅
Problem Description
DZY loves partitioning numbers. He wants to know whether it is possible to partition n into the sum of exactly k distinct positive integers.


After some thinking he finds this problem is Too Simple. So he decides to maximize the product of these k numbers. Can you help him?


The answer may be large. Please output it modulo 109+7.
 


Input
First line contains t denoting the number of testcases.


t testcases follow. Each testcase contains two positive integers n,k in a line.


(1≤t≤50,2≤n,k≤109)
 


Output
For each testcase, if such partition does not exist, please output −1. Otherwise output the maximum product mudulo 109+7.
 


Sample Input
4
3 4
3 2
9 3
666666 2
 


Sample Output
-1
2
24

110888111


题意:

给定n,问能否把n拆成恰好k个不重复的正整数之和。可以的话求这k个正整数的乘积的最大值

solution:

然后观察最优解的性质,它一定是一段连续数字,或者两段连续数字中间只间隔1个数。这是因为1<= a<=b-2  1≤a<=b−2时 有ab<(a+1)(b-1)ab<(a+1)(b−1)

用sum代表1~k的和,有解的情况是sum<=n,现在看是连续数字还是两段数字间差一个数,设u=(n-sum)/k,v=(n-sum)%k.如果v==0的话,那么答案就为1~k每个数都加上u,若v!=0,那么1~k前k-v加u,剩下的加u+1即可。

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll n, k;
int main()
{
	int t;;
	scanf("%d", &t);
	while (t--)
	{
		cin >> n >> k;
		ll sum = k*(k + 1) / 2;
		if (sum > n)printf("-1\n");
		else {
			ll u = (n-sum)/ k, v = (n-sum)%k;
			long long ans = 1;
			for (ll i = 1; i <= k; i++)
				if (i <= k - v)ans = (ans*(i + u)) % mod;
				else ans = (ans*(i + u + 1)) % mod;
				cout << ans << endl;
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值