【BestCoder Round #76 (div.2)】HDU5646DZY Loves Partition
来源:互联网 发布:中等收入陷阱知乎 编辑:程序博客网 时间:2024/04/30 23:37
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646
问题描述
DZY喜欢拆分数字。他想知道能否把n拆成恰好k个不重复的正整数之和。思考了一会儿之后他发现这个题太简单,于是他想要最大化这k个正整数的乘积。你能帮帮他吗?由于答案可能很大,请模109+7输出。
输入描述
第一行t,表示有t组数据。接下来t组数据。每组数据包含一行两个正整数n,k。(1≤t≤50,2≤n,k≤109)
输出描述
对于每个数据,如果不存在拆分方案,输出−1;否则输出最大乘积模109+7之后的值。
输入样例
43 43 29 3666666 2
输出样例
-1224110888111
Hint
第一组数据没有合法拆分方案。第二组数据方案为3=1+2,答案为1×2=2第三组数据方案为9=2+3+4,答案为2×3×4=24。注意9=3+3+3是不合法的拆分方案,因为其中包含了重复数字。第四组数据方案为666666=333332+333334,答案为333332×333334=111110888888。注意要对109+7取模后输出,即110888111。
代码:
#include<iostream>#include<cstdio>#include<string>#include<cstring>#define LL long long#define mod 1000000007using namespace std;int main(){ int t; LL n,k; cin>>t; while(t--){ cin>>n>>k; LL sum=(1+k)*k/2; // sum(1...k); if(n<sum){ cout<<"-1"<<endl; continue; } n-=sum; // 减去基本的sum(1..k); LL x=n/k,y=n%k; // x表示对应的每个数(1..k)还差x;y表示每个数加上x之后的总和还差y; LL ans=1; int i; if(y==0){ for(i=1;i<=k;i++) ans=(ans*(i+x))%mod; }else{ for(i=1;i<=k-y;i++) ans=(ans*(i+x))%mod; for(;i<=k;i++) // 因为总和还差y,所以我们可以在后面的y个数,每个都加1,这样就可以造成乘积最大了; ans=(ans*(i+x+1))%mod; } cout<<ans<<endl; } return 0;}
0 0
- 【BestCoder Round #76 (div.2)】HDU5646DZY Loves Partition
- 【BestCoder Round #76 (div.2)】HDU5646DZY Loves Balls
- BestCoder Round #76 (div.2)-DZY Loves Partition(模拟)
- 【BestCoder Round #76 (div.2)】DZY Loves Partition(数学分析)
- hdu5646DZY Loves Partition
- BestCoder Round #76 (div.2) DZY Loves Balls
- CA Loves Stick(BestCoder Round #78 (div.2))
- BestCoder Round #78 (div.2)1002 CA Loves GCD
- BestCoder Round #78 (div.2)_A_ CA Loves Stick
- BestCoder Round #78 (div.2)_B_ CA Loves GCD
- 【BestCoder Round #78 (div.2)】HDU5655CA Loves Stick
- 【BestCoder Round #82 (div.2)】HDU5675ztr loves math
- BestCoder Round #76 (div.2)
- BestCoder Round #76 (div.2)
- BestCoder Round #76 (div.2)A
- HDU 5675 ztr loves math——BestCoder Round #82(div.2)
- BestCoder Round #82 (div.2) -ztr loves lucky numbers(BFS)
- BestCoder Round #78 (div.1) CA Loves GCD
- android 系统获取通话状态的方法
- 类与对象(5)抽象类
- ListView不能自适应高度全屏的解决方法(可能是最完美的解决方案)
- 网络:文件下载
- Unity3D Terrain Settings 彻底搞懂 地形详细设置 全实验与解析 (一)Pixel Error
- 【BestCoder Round #76 (div.2)】HDU5646DZY Loves Partition
- 网络:HEAD 的使用
- 详解location.href几种用法的区别【JS跳转】
- C语言中的找特殊数字问题
- 魅族usb调试
- HDU 5371 Manacher
- 网络:NSFIleHandle的使用
- CG 知识摘要
- 网络:NSOutputStream的使用