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 ...
随机推荐
- js提交form表单
<form action="/Enterprise/member" id="sendinviteid" method="post"&g ...
- C#实现文件的压缩和解压
这里主要解决文件夹包含文件夹的解压缩问题.1)下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download ...
- [ An Ac a Day ^_^ ] hrbust 2291 Help C5 分形
开博客这么久从来没写过自己学校oj的题解 今天写一篇吧 嘿嘿 原题链接:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProble ...
- 日志输出--C#
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Openjudge-计算概论(A)-短信计费
描述: 用手机发短信,一条短信资费为0.1元,但限定一条短信的内容在70个字以内(包括70个字).如果你一次所发送的短信超过了70个字,则会按照每70个字一条短信的限制把它分割成多条短信发送.假设已经 ...
- There was a problem parsing the package(android)
android phone when you install the application there will inevitably be "a problem parsing the ...
- redis数据类型:hashes
redis hash 是一个string类型的field和value的映射表. 它的添加.删除操作都是O(1)(平均),hash特别适合用于存储对象 将一个对象存储在hash类型总会占用更少的内存,并 ...
- ural 1073.Square Country(动态规划)
1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square ...
- 安装appuim
一.作为一名软件测试人员,对工作习惯和品质的有要求,要有对问题的敏感度,遇到问题就不能轻易放过,提前保存一切可能对分析解决问题有帮助的资料,不怕麻烦,尽可能的全面详细,不漏重点.若神经大条,嫌麻烦,不 ...
- 获取Camera 支持视频的尺寸
<uses-permission android:name="android.permission.CAMERA" > </uses-permission> ...