题目链接:

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. 用代码构造PreferenceScreen

    在PreferenceFregment中构造界面,简单省事的方法就是使用findPreference然后在xml里把UI写好.在代码中动态的添加UI内容也是需要的.核心代码是: PreferenceS ...

  2. GT背靠背onsite

    Google: 因为暑假在G家实习过,所以仅仅是简单面了2轮. 后来跟曾经的intern host吃饭得知,他和还有一个reviewer对我的实习工作都给了不错的评价,所以面试的时候面试官都放水了.题 ...

  3. 登录shell与非登录shell读取文件过程

    登录shell与非登录shell读取文件过程登录:/etc/profile→/etc/profile.d/*.sh        ~/.bash_profile非登录:~/.bash_profile→ ...

  4. ASP.NET动态网站制作(17)-- C#(1)

    前言:用C#也有一年多了,基本上都是边用边学的,现在可以跟着老师系统的学习一下,感觉应该挺好的. 内容: 1.网站部署的相关内容:  (1)想要做一个网站,首先得去买一个域名,老师的域名是在美橙上买的 ...

  5. 承载(Host)通用语言执行时

    承载(Host)通用语言执行时(CLR) 还有一种使用COM 的方法是是把须要集成的 F# 代码与已有的 C/C++ 应用程序集成到一起.开成自己定义的承载通用语言执行时.通用语言执行时就是 C++ ...

  6. maven;tomcat配置

    [说明]今天呀,上午刚刚打算写javaweb项目,服务器就出现了问题,就花了点时间搞定了:下午听老大说了任务的大致内容和意义,收获颇多:晚上去服务器上部署了maven,听说可以实现热部署 一:今天完成 ...

  7. SpringMVC结合REST

    一.什么是REST REST即 Representational State Transfer,也就是(资源)表现层状态转化.资源是指网络上的一个实体或者说是网络上的一个具体信息. 每种资源对应一个特 ...

  8. 电路分析三------KCL,KVL,VCR方程

    1.2b方程 2.举例 举例2

  9. elasticsearch从入门到出门-08-Elasticsearch容错机制:master选举,replica容错,数据恢复

    假如: 9 shard,3 node Elasticsearch容错机制:master选举,replica容错,数据恢复 最佳分配情况: 这样分配之后,不管其中哪个node 宕机这个es 依然可以提供 ...

  10. linux c编程:popen

    我们在执行shell命令比如cat /etc/group | grep root的时候,通过管道的机制将cat /etc/group的结果传递给grep root,然后将结果显示出来 linux中提供 ...