hdu 5646 DZY Loves Partition(整数拆分)
来源:互联网 发布:如何提高物理成绩知乎 编辑:程序博客网 时间:2024/04/30 23:19
Problem Description
DZY loves partitioning numbers. He wants to know whether it is possible to partition n 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.
Input
First line contains t 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)
Output
For each testcase, if such partition does not exist, please output −1. Otherwise output the maximum product mudulo 109+7.
Sample Input
4
3 4
3 2
9 3
666666 2
Sample Output
-1
2
24
DZY loves partitioning numbers. He wants to know whether it is possible to partition n 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.
Input
First line contains t 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)
Output
For each testcase, if such partition does not exist, please output −1. Otherwise output the maximum product mudulo 109+7.
Sample Input
4
3 4
3 2
9 3
666666 2
Sample Output
-1
2
24
110888111
题意:
给定n,问能否把n拆成恰好k个不重复的正整数之和。可以的话求这k个正整数的乘积的最大值
solution:
然后观察最优解的性质,它一定是一段连续数字,或者两段连续数字中间只间隔1个数。这是因为1<= a<=b-2 1≤a<=b−2时 有ab<(a+1)(b-1)ab<(a+1)(b−1)
用sum代表1~k的和,有解的情况是sum<=n,现在看是连续数字还是两段数字间差一个数,设u=(n-sum)/k,v=(n-sum)%k.如果v==0的话,那么答案就为1~k每个数都加上u,若v!=0,那么1~k前k-v加u,剩下的加u+1即可。
#include<cstdio>#include<iostream>using namespace std;typedef long long ll;const int mod = 1e9 + 7;ll n, k;int main(){int t;;scanf("%d", &t);while (t--){cin >> n >> k;ll sum = k*(k + 1) / 2;if (sum > n)printf("-1\n");else {ll u = (n-sum)/ k, v = (n-sum)%k;long long ans = 1;for (ll i = 1; i <= k; i++)if (i <= k - v)ans = (ans*(i + u)) % mod;else ans = (ans*(i + u + 1)) % mod;cout << ans << endl;}}return 0;}
0 0
- hdu 5646 DZY Loves Partition(整数拆分)
- 【HDU 5646】DZY Loves Partition
- HDU 5646-DZY Loves Partition
- hdu 5646 DZY Loves Partition
- HDU 5646 DZY Loves Partition
- HDU 5646 DZY Loves Partition(二分法)
- HDU 5646 DZY Loves Partition 数列
- hdu 5646DZY Loves Partition(构造)
- HDU 5646 DZY Loves Partition (数学)
- HDU 5646 DZY Loves Partition(BC)
- HDU 5646 DZY Loves Partition(数学)
- HDOJ 5646 DZY Loves Partition
- HDOJ 5646 DZY Loves Partition(数学)
- 杭电5646 DZY Loves Partition
- HDOJ 5646-DZY Loves Partition【数学】
- hdoj 5646 DZY Loves Partition(数学)
- DZY Loves Partition
- hdu_5646 DZY Loves Partition
- Python笔记3
- 泰坦尼克号
- J2EE --- forward VS redirect
- Criteria 的用法 (转)
- 图像的稀疏表示——ScSPM和LLC的总结
- hdu 5646 DZY Loves Partition(整数拆分)
- J2EE --- JDBC获取数据库元数据信息
- Opencv2简洁代码提取连通区域
- Spring发送邮件案例
- AtomicLongMap的使用
- 进程间通信-管道(PIPE)和有名管道(FIFO)
- Jeecg-P3 1.0版本发布,JAVA插件开发框架
- 项目10-太乐了
- 第四周项目4:程序分析(1)