DZY Loves PartitionTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1414 Accepted Submission(s): 536
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
Sample Output
Source
|
分析:
一个正整数n拆分成k个正整数相加,求出k个正整数相乘的最大值。
要取得k个正整数相乘有最大值,即n去和n/k最相近的k个满足要求的数。
先将n/k个整数分配,再分配n%k剩下的数字,即将这些数字补到最大的几个数字上,每个数字加1,如果补到小的数字上会出现与后面重复的情况。
代码如下:
#include <stdio.h>
typedef long long LL;
int a[100005];
int main()
{
int T;
int n,k,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&k);
LL tmp = (LL)k*(k+1)/2;
if(tmp>n)
{//如何最小的k个和大于n,则输出-1
printf("-1\n");
continue;
}
//将最小的1~k分配到这k个数上
for(i=1;i<=k;i++)
a[i]=i;
n-=tmp; //减去分配的数字
for(i=1;i<=k;i++)
a[i]+=n/k;
n=n%k;
//来实现将n%k的数补到后面数的几个数上,每个数加一
for(i=k;n--;i--)
a[i]++;
LL ans=1;
for(i=1;i<=k;i++)
ans=(ans*a[i])%1000000007;
printf("%I64d\n",ans);
}
return 0;
}