DZY Loves Partition
DZY喜欢拆分数字。他想知道能否把nn拆成恰好kk个不重复的正整数之和。 思考了一会儿之后他发现这个题太简单,于是他想要最大化这kk个正整数的乘积。你能帮帮他吗? 由于答案可能很大,请模1097109+7输出。
第一行tt,表示有tt组数据。 接下来tt组数据。每组数据包含一行两个正整数nkn,k。 (1t502nk1091≤t≤50,2≤n,k≤109)
对于每个数据,如果不存在拆分方案,输出1−1;否则输出最大乘积模1097109+7之后的值。
4
3 4
3 2
9 3
666666 2
-1
2
24
110888111
第一组数据没有合法拆分方案。
第二组数据方案为3123=1+2,答案为1221×2=2
第三组数据方案为92349=2+3+4,答案为234242×3×4=24。注意93339=3+3+3是不合法的拆分方案,因为其中包含了重复数字。
第四组数据方案为666666333332333334666666=333332+333334,答案为333332333334111110888888333332×333334=111110888888。注意要对1097109+7取模后输出,即110888111记sumakaa1ak1sum(a,k)=a+(a+1)+⋯+(a+k−1)。 首先,有解的充要条件是sum1knsum(1,k)≤n(如果没取到等号的话把最后一个kk扩大就能得到合法解)。 然后观察最优解的性质,它一定是一段连续数字,或者两段连续数字中间只间隔1个数。这是因为1ab21≤a<=b−2时有aba1b1ab<(a+1)(b−1),如果没有满足上述条件的话,我们总可以把最左边那段的最右一个数字作为aa,最右边那段的最左一个数字作为bb,调整使得乘积更大。 可以发现这个条件能够唯一确定nn的划分,只要用除法算出唯一的aa使得sumaknsuma1ksum(a,k)≤n<sum(a+1,k)就可以得到首项了。 时间复杂度OnO(√n),这是暴力把每项乘起来的复杂度。110888111
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
int a[100005];
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, k;
scanf("%d %d", &n, &k);
LL tmp = (LL)k * (k + 1) / 2;
if (tmp > n)
{
puts("-1");
continue;
}
for (int i = 1; i <= k; i++) a[i] = i;
n -= tmp;
for (int i = 1; i <= k; i++) a[i] += n / k;
n -= n / k * k;
for (int i = k; i >= 1 && n > 0; i--, n--) a[i]++;
LL ans = 1;
for (int i = 1; i <= k; i++)
{
ans *= a[i];
ans %= 1000000007;
}
printf("%lld\n", ans);
}
return 0;
}
DZY Loves Partition的更多相关文章
- HDU 5646 DZY Loves Partition 数学 二分
DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...
- hdu-5646 DZY Loves Partition(贪心)
题目链接: DZY Loves Partition Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ( ...
- 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 二分+数学分析+递推
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646 题意:将n分成k个正整数之和,要求k个数全部相同:并且这k个数的乘积最大为多少?结果mod 1e^9 ...
- hdu 5646DZY Loves Partition(构造)
DZY Loves Partition Accepts: 154 Submissions: 843 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
- CodeForces445A DZY Loves Chessboard
A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...
- BZOJ 3309: DZY Loves Math
3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 761 Solved: 401[Submit][Status ...
随机推荐
- Xshell连接服务器密钥改成密码登录
密钥登陆 直接点本地shell打开之后直接ssh ip连接 需要centos用户登录,选择机器密钥. 登录后 sudo su - root 修改root密码 passwd root 修改文件 vi / ...
- web小技巧
如内容超出单元格,则隐藏style="TABLE-LAYOUT: fixed" 让弹出窗口总是在最上面: <body onblur="this.focus();&q ...
- chapter8_4 错误处理
在Lua中,对于大多数程序都不用作任何错误处理,应用程序本身会负责这类问题. 所有的Lua活动都是由应用程序的一次调用开始的,这类调用要求Lua执行一个程序块. 执行过程中发生了错误,此调用会返回一个 ...
- iOS之文件解析
JSON JSON – OC 转换对照表 JSON OC {} NSDictonary [] NSArray "" NSString 数字 10,10.5 NSNumber NSJ ...
- c题 Registration system
Description A new e-mail service "Berlandesk" is going to be opened in Berland in the near ...
- POJ1410 Intersection 计算几何
题目大意:给出一个线段的两端,和矩形两端(不一定是左上和右下),问线段是否与矩形相交(若线段在矩形内也算相交).这题蒸鹅心-- 题目思路:判断所有情况:线段是否在矩形内,线段内一点是否在矩形内,线段是 ...
- 让NetBeans的Web应用热部署可用
MyEclipse最大的优点是,开发web应用时,可以轻松地做到:修改Java类后,在浏览器中刷新页面立刻就可以看到修改的效果,不用手动重启Web Server. 但是MyEclipse除此之外其他方 ...
- android 复制、粘贴文字
Android的剪切板(ClipboardManager) 注意:导包的时候 API 11之前: android.text.ClipboardManagerAPI 11之后: android.cont ...
- iosUISegmentedControl的基本设置
//创建segmentControl 分段控件 UISegmentedControl *segC = [[UISegmentedControl alloc]initWithFrame:CGRectMa ...
- VBS操作JS网页元素实例
'=========================================================================='' VBScript Source File - ...