问题描述
DZY喜欢拆分数字。他想知道能否把nn拆成恰好kk个不重复的正整数之和。

思考了一会儿之后他发现这个题太简单,于是他想要最大化这kk个正整数的乘积。你能帮帮他吗?

由于答案可能很大,请模109710​9​​+7输出。
输入描述
第一行tt,表示有tt组数据。

接下来tt组数据。每组数据包含一行两个正整数nkn,k。

(1t502nk1091≤t≤50,2≤n,k≤10​9​​)
输出描述
对于每个数据,如果不存在拆分方案,输出1−1;否则输出最大乘积模109710​9​​+7之后的值。
输入样例
4
3 4
3 2
9 3
666666 2
输出样例
-1
2
24
110888111
Hint
第一组数据没有合法拆分方案。
第二组数据方案为3123=1+2,答案为1221×2=2
第三组数据方案为92349=2+3+4,答案为234242×3×4=24。注意93339=3+3+3是不合法的拆分方案,因为其中包含了重复数字。
第四组数据方案为666666333332333334666666=333332+333334,答案为333332333334111110888888333332×333334=111110888888。注意要对109710​9​​+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的更多相关文章

  1. HDU 5646 DZY Loves Partition 数学 二分

    DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...

  2. hdu-5646 DZY Loves Partition(贪心)

    题目链接: DZY Loves Partition Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/262144 K ( ...

  3. HDU 5646 DZY Loves Partition

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5646 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  4. hdu 5646 DZY Loves Partition 二分+数学分析+递推

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646 题意:将n分成k个正整数之和,要求k个数全部相同:并且这k个数的乘积最大为多少?结果mod 1e^9 ...

  5. hdu 5646DZY Loves Partition(构造)

    DZY Loves Partition  Accepts: 154  Submissions: 843  Time Limit: 4000/2000 MS (Java/Others)  Memory ...

  6. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  8. CodeForces445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  9. BZOJ 3309: DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 761  Solved: 401[Submit][Status ...

随机推荐

  1. 4、Spring+MyBatis增删改查

    0.oracle数据库脚本 create table userinfo (id ), name ), password telephone ), isadmin )); --4.2 用户表序列 cre ...

  2. uhttpd配置文件分析

    文件位于 /etc/config/uhttpd. root@hbg:/etc/config# cat uhttpd config uhttpd 'main'        list listen_ht ...

  3. LeetCode OJ 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. HTML URL编码参考(转载)

    URL编码(URL encoding)的作用是将字符转化为可在因特网上安全传输的格式.URL——统一资源定位符Web浏览器通过URL从Web服务器上请求页面.URL就是网页的地址,如:http://w ...

  5. 网页 HTML表单

    今天,我首先先学习了图片热点和在原来页面嵌入其他页面. 图片热点:<img src="" usemap="#ditu"/> <map name ...

  6. 美化type="file"控件

    对于type="file"而言是一个不大好看的控件.如果不美化他一下,总感觉自己的网站有些缺乏了美感 上代码: <!DOCTYPE html> <html> ...

  7. form表单验证提示语句

    <input id="idcardcode" name="idcardcode" class="form-control"       ...

  8. 学习multiprocessing

    1. multiprocessing.Pool from multiprocessing.pool import Pool def gen_row(): ...return rows def main ...

  9. eclipse 中执行 main 函数如何添加参数

    我们通常执行 main 函数都是直接在类界面 右键 选择 Run As --> Java Application 但是如何 执行时带有参数呢? 右键 --> Run As --> R ...

  10. kloxo面板教程-折腾了一天

    ------------------------------------------------------------------------------- 前一晚安装了掉线,不得不重新来,有点慢, ...