题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646
DZY Loves Partition
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 887 Accepted Submission(s): 327
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
题意:将n个数拆分成k个不同的正整数相加,求这n个正整数的最大乘积
方法:sum(1,k)为k个数相加的最小值,如果n小于这个数,则n无法拆分成k个数, 若n > sum(1, k) 则存在k个连续的数或相差为2的连续上升的数列, 令n = n - sum(1,k) , c = n / k , d = n % k, 如果d!=0 则存在相差为2数, 所以此时将数列分为两部分,前一部分为连续的数列,后一部分为相差为2的数列,将他们累乘。
#include <bits/stdc++.h>
#define Mod 1000000007
using namespace std;
int main()
{
int T;
long long n, k;
scanf("%d",&T);
while(T-- && scanf("%lld %lld",&n,&k))
{
long long sum = (k + 1) * k / 2;
if(sum > n)
{
printf("-1\n");
continue;
}
n -= sum;
long long c = n / k;
long long d = n % k;
long long ans = 1;
for(int i=c+1; i<=k+c; i++)
{
if(i <= k-d+c) ans = (ans * i) % Mod;
else ans = (ans * (i+1)) % Mod;
}
printf("%lld\n", ans%Mod);
}
return 0;
}