HDU 5646
DZY Loves Partition
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 815    Accepted Submission(s): 298
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
.
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
)
. Otherwise output the maximum product mudulo 109+7
.
In 1st testcase, there is no valid partition.
In 2nd testcase, the partition is $3=1+2$. Answer is $1\times 2 = 2$.
In 3rd testcase, the partition is $9=2+3+4$. Answer is $2\times 3 \times 4 = 24$. Note that $9=3+3+3$ is not a valid partition, because it has repetition.
In 4th testcase, the partition is $666666=333332+333334$. Answer is $333332\times 333334= 111110888888$. Remember to output it mudulo $10^9 + 7$, which is $110888111$.
记sum(a,k)=a+(a+1)+⋯+(a+k−1)
首先,有解的充要条件是sum(1,k)≤n(如果没取到等号的话把最后一个k扩大就能得到合法解)。
然后观察最优解的性质,它一定是一段连续数字,或者两段连续数字中间只间隔1个数。这是因为1≤a<=b−2时有ab<(a+1)(b−1)如果没有满足上述条件的话,我们总可以把最左边那段的最右一个数字作为a,最右边那段的最左一个数字作为b,调整使得乘积更大。
可以发现这个条件能够唯一确定n的划分,只要用除法算出唯一的a使得sum(a,k)≤n<sum(a+1,k)就可以得到首项了。
理解我yan代码 先排列一个k位的首项为1 的等差数列 将剩下的数 优先靠右分配
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
#define mod 1000000007
#define ll __int64
ll T,n,k,f;
ll ans = ;
int main() {
scanf("%I64d",&T);
while(T--)
{
scanf("%I64d%I64d",&n,&k);
ans = ;
if(k==)
{
cout<<n<<endl;
continue;
}
if(n<k)
cout<<-<<endl;
else
{
n = n-(+k)*k/;//每个只能出现一次
if(n<)
{
cout<<-<<endl;continue;
}
if(n==)
{
for(int i=;i<=k;i++)
ans=(ans*i)%mod;
cout<<ans<<endl;
continue;
}
for(int i=k;i>k-n%k;i--)
ans=(ans*(n/k++i))%mod;
for(int i=k-n%k;i>=;i--)
ans=(ans*(n/k+i))%mod;
cout<<ans<<endl;
}
}
}
HDU 5646的更多相关文章
- hdu 5646 DZY Loves Partition 二分+数学分析+递推
		链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646 题意:将n分成k个正整数之和,要求k个数全部相同:并且这k个数的乘积最大为多少?结果mod 1e^9 ... 
- HDU 5646 DZY Loves Partition
		题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ... 
- HDU 5646 DZY Loves Partition 数学 二分
		DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ... 
- HDOJ 2111. Saving HDU 贪心 结构体排序
		Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ... 
- 【HDU 3037】Saving Beans Lucas定理模板
		http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ... 
- hdu 4859 海岸线 Bestcoder Round 1
		http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ... 
- HDU 4569 Special equations(取模)
		Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ... 
- HDU 4006The kth great number(K大数 +小顶堆)
		The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ... 
- HDU 1796How many integers can you find(容斥原理)
		How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ... 
随机推荐
- 推荐:一个适合于Python新手的入门练手项目
			随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ... 
- Linux 文件的常识
			文件 文件的分类 文件 目录 链接 区分办法,ls -la 查看 十个标志符中的第一个 如:drwxrwxr-x. 2 normal normal 4096 8月 31 23:43 dir 目录是d ... 
- appium关键字:
			## Appium 服务关键字 <expand_table> |关键字|描述|实例||----|-----------|-------||`automationName`|你想使用的自动化 ... 
- 20145214 《Java程序设计》第6周学习总结
			20145214 <Java程序设计>第6周学习总结 教材学习内容总结 串流设计 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 输入串流代表对象为java. ... 
- opencv图像像素值读取
			说到图像像素,肯定要先认识一下图像中的坐标系长什么样. 1. 坐标体系中的零点坐标为图片的左上角,X轴为图像矩形的上面那条水平线:Y轴为图像矩形左边的那条垂直线.该坐标体系在诸如结构体Mat,Rect ... 
- MAC锁屏不断网(快捷键启用屏保)
			第一步:要设定锁定输入密码的设置,进入'系统偏好设置''安全性与隐私',将选项'进入睡眠或开始屏幕保护程序后'打勾,选'立即'. 第二步:到'launchpad'中的'其他'文件夹打开'Automat ... 
- [剑指Offer] 58.对称的二叉树
			题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. [思路]递归,关键是isSame函数中的最后一句 /* struct Tree ... 
- POJ 2785 4 Values whose Sum is 0(折半枚举)
			给出四个长度为n的数列a,b,c,d,求从这四个数列中每个选取一个元素后的和为0的方法数.n<=4000,abs(val)<=2^28. 考虑直接暴力,复杂度O(n^4).显然超时. # ... 
- Tensorflow框架初尝试————搭建卷积神经网络做MNIST问题
			Tensorflow是一个非常好用的deep learning框架 学完了cs231n,大概就可以写一个CNN做一下MNIST了 tensorflow具体原理可以参见它的官方文档 然后CNN的原理可以 ... 
- CentOS 文本操作命令
			1.cat 用于查看纯文本文件,显示行号,加-n参数,适合内容较少的情况 2.more 用于查看纯文本文件,适合内容较多的情况 3.less 用于查看纯文本文件,可以上下翻页 4.head 用于查看纯 ... 
