题目链接:

DZY Loves Partition

Time Limit: 4000/2000 MS (Java/Others)   

 Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 323    Accepted Submission(s): 127

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
4
3 4
3 2
9 3
666666 2
 
Sample Output
-1
2
24
110888111
 
题意:
 
把n拆成k个互不相同的正整数,并使其乘积最大,问这个乘积最大是多少;
思路:首先判断是否能拆分,能拆分的话把这些平分,并使其成递增的数组,再把n/k剩下的n%k分给剩下的数,k为odd是从最大的数往前n%k个数分别+1;k位even时先把k/2个小点数从大到小分别+1,还有多的话在把剩下的大的k/2个数从大到小+1;最后求得的积就是结果;
 
 
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const long long mod=1e9+;
int t;
long long n,k,a[];
int main()
{ scanf("%d",&t);
while(t--)
{
cin>>n>>k;
long long x=n/k;
if(k*(k+)/>n)
{
printf("-1\n");
}
else
{
if(k%)
{
for(int i=k/;i>;i--)
{
a[i]=x-(k/-i+);
}
for(int i=k/+;i<=k;i++)
{
a[i]=x+(i-k/-);
}
int m=n%k;
for(int i=k;m>;m--,i--)
{
a[i]++;
}
}
else
{
for(int i=k/;i>;i--)
{
a[i]=x-(k/-i+);
}
for(int i=k/+;i<=k;i++)
{
a[i]=x+(i-k/);
}
int m=n%k;
for(int i=k/;i&&m;i--,m--)
{
a[i]++;
}
if(m>)
{
for(int i=k;i>k/&&m;i--,m--)
{
a[i]++;
}
}
}
long long ans=a[];
for(int i=;i<=k;i++)
{
ans*=a[i];
ans%=mod;
}
cout<<ans<<"\n";
} }
return ;
}

hdu-5646 DZY Loves Partition(贪心)的更多相关文章

  1. HDU 5646 DZY Loves Partition 数学 二分

    DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...

  2. HDU 5646 DZY Loves Partition

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

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

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

  4. hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)

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

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

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

  6. 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 ...

  7. HDU 5645 DZY Loves Balls 水题

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

  8. CF447B DZY Loves Strings 贪心

    DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...

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

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

随机推荐

  1. CG标准函数库——数学函数(GPU编程与CG语言之阳春白雪下里巴人)

  2. 安装android Studio和运行react native项目(跳坑篇)

    1.需配环境变量,值为sdk的地址. ANDROID_HOME  值:E:\Users\HP\AppData\Local\Android\sdk 2.下载gradle-2.14.1-all.zip 包 ...

  3. 【BZOJ4537】[Hnoi2016]最小公倍数 分块

    [BZOJ4537][Hnoi2016]最小公倍数 Description 给定一张N个顶点M条边的无向图(顶点编号为1,2,…,n),每条边上带有权值.所有权值都可以分解成2^a*3^b的形式.现在 ...

  4. api签名认证

    参数列表: data: { sign, uid或是openId, version, timestamp, param } sign 签名一般情况下,根据如下几项生成,通过md5或是aes加密: 接口 ...

  5. ASP.NET MVC + ADO.NET EF 项目实战(一):应用程序布局设计

    什么叫上下文? 在你设计一个方法的时候,无法直接从方法参数或实例成员(字段或属性)获得的所有信息都是上下文.例如: 当前用户是谁? 刚才提供操作的数据库连接实例从哪里拿到? 这个方法从哪个 View ...

  6. sgu 195 New Year Bonus Grant【简单贪心】

    链接: http://acm.sgu.ru/problem.php?contest=0&problem=195 http://acm.hust.edu.cn/vjudge/contest/vi ...

  7. nginx学习之反向代理篇(六)

    在本节,你将学会: --如何根据不同的协议,将请求转发到后端服务器: --修改发送到后端服务器的请求头: --以及配置是否buffering从后端服务器返回来的响应. 1. 将请求转发给后端服务器 当 ...

  8. Webpack探索【3】--- loader详解

    本文主要说明Webpack的loader相关内容.

  9. eclipse svn 分支合并到主干

    最近公司产品上线,整个系统架构包含有七八个子系统,并且子系统都是集群部署.所以每次升级维护都要确保尽可能不出问题.因为整个系统刚上线不久,意味着新系统不定期有BUG需修复,但新功能模块也在持续的开发中 ...

  10. C#聚合运算方法

    Aggregate 对集合值执行自定义聚合运算 Average 计算集合平均值 Count 对集合的元素惊醒计数,还可以仅对满足某一谓词函数的元素进行计数 LongCount 对大型集合中的元素进行计 ...