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 ...
随机推荐
- spark操作数据库的几种方法
一.使用jdbcRDD的接口: SparkConf conf = new SparkConf(); conf.setAppName("Simple Application").se ...
- 由一个hash字符串生成多个子hash字符串
通过存储一个head hash,然后把子hash放到网络中 当然,也可以像默克尔树那样的,生成多级的子hash ,可以通过规则配置不同的hash 生成方式.倒置的默克尔树 我有一个文件,然后我把她分隔 ...
- 七:Web Application Proxy
yarn自带了web接口,默认是和RM一起的(8088端口).但是为了减少从web接口受到的攻击,可以把Web接口单独放在别的机器上. 设置下web代理就行了 Configurations Confi ...
- Code obfuscatio (翻译!)
Description Kostya likes Codeforces contests very much. However, he is very disappointed that his so ...
- jQuery对象和DOM对象使用说明
1.jQuery对象和DOM对象第一次学习jQuery,经常分辨不清哪些是jQuery对象,哪些是 DOM对象,因此需要重点了解jQuery对象和DOM对象以及它们之间的关系.DOM对象,即是我们用传 ...
- 百度安卓sdk开发
一 key问题 1 在百度地图api控制台申请key的流程主要用到了app包,开发工具的开发sha1和发布sha1值,这2个值的获取就非常关键了. 一般来说我们都是在windows上开发安卓,使用an ...
- Ansys Workbench热流固耦合仿真配置
1.Fluent-Thermal-Structural瞬态分析 此模块连接在fluent已实现流体和固体的热流耦合,传递至thermal实际上只是将流体表面温度作为热载荷施加在固体的液体通道表面,极大 ...
- AppDomain.CurrentDomain.AssemblyResolve
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); 参 ...
- 【WCF】WCF 附录 高级主题 配置服务配额设置
微软产品自带一个“默认安全”方案.这也包括了WCF,意味着WCF中的多种配置可以设置来阻止诸如DOS(拒绝服务访问)攻击.微软为很多基于一个单一计算机的开发环境选择这样的设置.这也意味着默认设置中的一 ...
- BZOJ 1834 网络扩容(最大流+费用流)
对于第一问,直接求最大流. 对于第二问,建源点s和汇点t,s连1容量为INF,费用为0的边,n连t容量为最大流+k,费用为0的边.这样就把最大流限制为最多增加k了. 限制需要求扩充的最小费用,原图的边 ...