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 ...
随机推荐
- C for阶乘
#include <stdio.h> int main(int argc, char **argv) { //定义三个变量 x n s ,n s的初始值为1; int x; i ...
- OpenMPI源码剖析4:rte.h 头文件的说明信息
上一篇文章中说道,我们在 rte.h 中发现了有价值的说明: 我们一块一块来分析,首先看到第一块,关于 Process name Object: * (a) Process name objects ...
- Leetcode - 461. Hamming Distance n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
- 嵌入式码农的10年Bug调试经验,值得一看
下面这些都是我经历过的会导致难点bug的问题: 1.事件顺序.在处理事件时,提出下列问题会很有成效:事件可以以不同的顺序到达吗?如果我们没有接收到此事件会怎么样?如果此事件接连发生两次会怎么样?哪怕通 ...
- Scala可变对象
Java提供JavaBean作为数据对象的封装, 而对于Scala来说也提供了同样的支持. class Apple { var weight: Float = _ var color: String ...
- 第三周的psp
PSP: 进度条: 累计进度图: 本周PSP饼状图:
- [贪心经典算法]Kruskal算法
Kruskal算法的高效实现需要一种称作并查集的结构.我们在这里不介绍并查集,只介绍Kruskal算法的基本思想和证明,实现留在以后讨论. Kruskal算法的过程: (1) 将全部边按照权值由小到大 ...
- android入门 — ListView点击事件
listView中提供了两种点击事件的处理方法,分别是OnItemClick和OnItemLongClick. OnItemClick提供的是点击操作的处理,OnItemLongClick提供的是长按 ...
- iOS单利创建的方法
我们在使用单例的时候有两种方法@synchronized,GCD,往往人们使用@synchronized,但是推荐使用GCD: 第一种(@synchronized): + (id)sharedInst ...
- TCP系列40—拥塞控制—3、慢启动和拥塞避免概述
本篇中先介绍一下慢启动和拥塞避免的大概过程,下一篇中将会给出多个linux下reno拥塞控制算法的wireshark示例,并详细解释慢启动和拥塞避免的过程. 一.慢启动(slow start) 一个T ...