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. VideoJS 与 Framework7 中 fastclick 冲突问题

    Framework7 由于自动启用  fastclick,会导致在 移动端下使用 video.js,控制条上的 播放和音量按钮 点击的时候会触发两次. 解决办法: 1. 全局禁用 fastclick, ...

  2. python基础===map, reduce, filter的用法

    filter的用法: 这还是一个操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函 ...

  3. 宝塔Linux面板新手安装教程【转】

    一.使用远程连接软件 (如 Putty.XShell) 连接你的Linux服务器,本教程以 Putty 为例. 1.动 Putty.exe 程序,进入 Putty 主界面. 2.在 Host Name ...

  4. 深入解析Mysql 主从同步延迟原理及解决方案

    MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响主服务器服务;③当主服务器出现问题时,可以 ...

  5. C基础 如何让代码只执行一次

    1.0 最简单, 最高效的方式 C 代码运行起点 main 就是个大单例函数. 如果把函数注册在其里面, 那么一定很可以 :) // 某个库需要初始化的函数 void log_init(void) { ...

  6. mac pro上安装docker

    1.进入一下地址进行下载docker https://download.docker.com/mac/stable/Docker.dmg 进入后进行下载后进行安装 2.将其拖动到Appliaction ...

  7. 【严蔚敏】【数据结构题集(C语言版)】1.17 求k阶斐波那契序列的第m项值的函数算法

    已知k阶斐波那契序列的定义为 f(0)=0,f(1)=0,...f(k-2)=0,f(k-1)=1; f(n)=f(n-1)+f(n-2)+...+f(n-k),n=k,k+1,... 试编写求k阶斐 ...

  8. day4 迭代器与生成器解析

    一.迭代器 迭代器是访问集合元素的一种方式.其实迭代器就是一种列表,只是访问集合元素的时候比较特殊,具有一些特定功能,记忆功能,能够记住用户上一次的状态.迭代器是访问集合元素的一种方式.并且,迭代器只 ...

  9. pymongo的一些操作

    参考:http://www.yiibai.com/mongodb/mongodb_drop_collection.html http://www.cnblogs.com/zhouxuchen/p/55 ...

  10. git用法大全

    转载自实验楼,之前有更新过两篇git的文章,毕竟内容太少,而git还有很多更丰富的技能,在实验楼上有一系列全的教程,这里做一下备案.需要时查阅. Git 实战教程 目录 一.实验说明 二.git的初始 ...