题目链接:

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. volatile的含义及使用场景

    volatile保证线程间的数据是可见的(共享的),但不保证数据同步 volatile相当于synchronized的弱实现,也就是说volatile实现了类似synchronized的语义,却又没有 ...

  2. Rancher探秘一:初识Rancher

    前言:最近公司需要导入k8s管理,看了一些rancher相关内容,在此做一记录,rancher系列会根据进展不定期更新. Rancher是什么? Rancher是一个开源的企业级容器管理平台.通过Ra ...

  3. vue实践---vue不依赖外部资源实现简单多语

    vue使用多语,最常见的就是 vue-i18n, 但是如果开发中的多语很少,比如就不到10个多语,这样就没必要引入vue-i18n了, 引入了反正导致代码体积大了,这时候单纯用vue实现多语就是比较好 ...

  4. html5小趣味知识点系列(一)autofocus

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Sql注入_类型

    1.sql注入 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 2.sql注入类型 按照注入点类型来分类 (1)数字型注入点 在 Web ...

  6. 九度OJ 1194:八进制 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3521 解决:2058 题目描述: 输入一个整数,将其转换成八进制数输出. 输入: 输入包括一个整数N(0<=N<=100000 ...

  7. php字符串操作: 去掉UTF-16的空格

    $s = json_encode($s); $s = str_replace('\u00a0','',$s); $s = str_replace('\u3000','',$s); $s = str_r ...

  8. windows下php升级到7.2

    1: 官网下载:https://windows.php.net/download#php-7.2

  9. 使用阿里云的PyPI源

    方法1:(1)创建pip.conf文件 (2).编辑如下内容 [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] ...

  10. crontab定时任务(待补充)

    cron是一个ubuntu下的后台进程,用来定期的执行一些任务 想让cron执行你指定的任务,首先就要编辑crontab文件.crontab是一个文本文件,用来存放你要运行的命令 第一种 vim /e ...