DZY Loves Partition

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5646

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

Hint

题意

给你一个数n,你需要把n分成k个不同的数的和,然后使得这k个数的乘积最大。

题解:

首先,肯定是都挨着答案最大,所以公差为1的等差数列最好了。

然后我们二分那个等差数列的起始点的位置就好了。

如果还有数剩下来,我们就给后面的数+1就好了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int mod = 1e9+7;
long long n,k;
long long a[maxn];
void solve()
{
scanf("%lld%lld",&n,&k);
if(1ll*(1+k)*k/2>n)
{
puts("-1");
return;
}
int l = 1,r = n,ans = 1;
while(l<=r)
{
int mid=(l+r)/2;
if(1ll*(mid+mid+k-1)*k/2<=n)l=mid+1,ans=mid;
else r=mid-1;
}
int p = (n-1ll*(ans+ans+k-1)*k/2);
for(int i=1;i<=k;i++)a[i]=ans+(i-1);
for(int i=k;i>k-p;i--)a[i]++;
long long Ans = 1;
for(int i=1;i<=k;i++)
Ans=(Ans*a[i])%mod;
cout<<Ans<<endl;
}
int main()
{
int t;scanf("%d",&t);
while(t--)solve();
}

HDU 5646 DZY Loves Partition 数学 二分的更多相关文章

  1. HDU 5646 DZY Loves Partition

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  2. hdu 5646 DZY Loves Partition 二分+数学分析+递推

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646 题意:将n分成k个正整数之和,要求k个数全部相同:并且这k个数的乘积最大为多少?结果mod 1e^9 ...

  3. HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)

    题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...

  4. hdu-5646 DZY Loves Partition(贪心)

    题目链接: DZY Loves Partition Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/262144 K ( ...

  5. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  6. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  7. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  8. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  9. HDU 5645 DZY Loves Balls 水题

    DZY Loves Balls 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5645 Description DZY loves playing b ...

随机推荐

  1. 自己看之区间DP

    //菜鸡制作,看的时候可能三目运算符略烦;;; 区间DP入门题:Brackets 地址:http://59.77.139.92/Problem.jsp?pid=1463 分析(对区间DP的代码原理进行 ...

  2. Linux mint 18.1 / Ubuntu 16.04 安装steam

    这里以Limit Mint 18.1为例: 安装steam: sudo dpkg -i steam.deb 运行后会有如下错误: 直接运行如下命令修复, 并自动启动steam: LD_PRELOAD= ...

  3. 使用批处理方式从svn 检出DEMO

    Branching in Subversion¶ FROM:https://dev.geogebra.org/trac/wiki/SubversionBranching Some people wan ...

  4. APP版本号记录

    VoLTE版本: VT_BV0800V1.0.0B06 800M版本: NETARTIST_BV0800V1.0.0B01 看详细版本号:9831275#

  5. Power Profiles for Android

    http://source.android.com/devices/tech/power.html Battery usage information is derived from battery ...

  6. ssh使两台机器建立连接

    ssh利用口令建立连接过程: 客户端--> 发送连接请求 --> 远程主机 --> 返回远程主机的公钥 --> 公钥加密客户端私钥+客户端公钥返回远程主机 --> 远程主 ...

  7. TypeError: not all arguments converted during string formatting

    print ("So, you're 5r old, %r tall and %r heavy." % (age, height, weight)) print ("So ...

  8. Ubuntu 下查看已安装的软件

    Ubuntu 下如何查看已安装的软件 1.查看安装的所有软件 dpkg -l 例如: dpkg -l | grep ftp 2.查看软件安装的路径 dpkg -L | grep ftp 也可以用 wh ...

  9. ECharts图表tooltip显示时超出canvas图层解决方法

    我们在做ECharts图表的时候可能会遇到tooltip显示时超出了canvas图层范围,并且被其它z-index较高的div容器遮盖,这是悬浮展示信息就看不全,我们根据官网文档的配置项查询发现con ...

  10. Jmeter----5.1 设置中文

    注意:JMeter5需要Java8 以上,本文环境是Win7 64位 设置永久默认汉化:在Jmeter的安装目录下的bin目录中找到 jmeter.properties这个文件,用文本编辑器打开.在# ...