DZY Loves Partition
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1444 Accepted Submission(s): 552
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 .
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 )
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 110888111HintIn 1st testcase, there is no valid partition. In 2nd testcase, the partition is $3=1+2$. Answer is $1\times 2 = 2$. In 3rd testcase, the partition is $9=2+3+4$. Answer is $2\times 3 \times 4 = 24$. Note that $9=3+3+3$ is not a valid partition, because it has repetition. In 4th testcase, the partition is $666666=333332+333334$. Answer is $333332\times 333334= 111110888888$. Remember to output it mudulo $10^9 + 7$, which is $110888111$.
题目大意:给你一个数n,问能否将其拆分为k个不同数的和,使这些数的积最大。
思路:和一定,数字之间越靠近,乘积越大。最差的分类肯定是1+2+3...+k,如果n比这个还小,那肯定不行,这个是最小的情况和,最大的由这个整体上升,多余的余数肯定加在后面的数字上。
另外 hdu有毒,不用long long会超时的,我还以为我的方法不对。原来的方法(注释)应该可以很清楚的看出来算法
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MOD = 1e9 + 7;
int main()
{
long long t, n, k;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n, &k);
/*for(int i = 1; i <= k; i++)
a[i] = i;*/
n -= k*(k+1)/2;
if(n < 0) {printf("-1\n"); continue;}
int u = n / k;
int v = n % k;
/*for(int i = 1; i <= k-v;i++)
a[i] += u + i;
for(int i = k-v+1; i <= k; i++)
a[i] += u+1+i;*/
long long ans = 1;
for(int i = 1; i <= k; i++)
{
if(i <= k-v)
ans = ans * (i+u) % MOD;
else
ans = ans * (i+u+1) % MOD;
}
printf("%lld\n",ans);
}
return 0;
}