hdu5646 DZY Loves Partition 数学
来源:互联网 发布:mac如何复制粘贴图片 编辑:程序博客网 时间:2024/05/01 02:58
ZY Loves Partition
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 365 Accepted Submission(s): 138
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 thesek numbers. Can you help him?
The answer may be large. Please output it modulo109+7 .
After some thinking he finds this problem is Too Simple. So he decides to maximize the product of these
The answer may be large. Please output it modulo
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 .
解:若sum(1,k)已经大于n则必无解,否则可通过对最大数增加一定能得到解。
当有解时:由基本不等式的(a1+a2+...+an)*1/n>=(a1*a2...*an)^1/n.挡a1,a2...相等时取等号,要使乘积尽可能大,那么需要这n个数尽可能接近,于是求连续最大和sum(begin,begin+k-1)<=n,然后把n-sum(begin.begin+k-1)剩下的从大到小分配即可。
AC代码:
//************************************************************************////*Author : Handsome How *////************************************************************************////#pragma comment(linker, "/STA CK:1024000000,1024000000")#pragma warning(disable:4996) #include <vector>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <algorithm>#include <sstream>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <cstring>#include <ctime> #include <cassert>#if defined(_MSC_VER) || __cplusplus > 199711L#define aut(r,v) auto r = (v)#else#define aut(r,v) __typeof(v) r = (v)#endif#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)#define fur(i,a,b) for(int i=(a);i<=(b);i++)#define furr(i,a,b) for(int i=(a);i>=(b);i--)#define cl(a) memset((a),0,sizeof(a))using namespace std;typedef long long LL;//----------------------------------------------------const LL mod = 1e9 + 7;int main(){ //freopen("E:\\data.in", "r", stdin); ios :: sync_with_stdio(false); int T; scanf("%d", &T); while (T--) { LL n, k; scanf("%I64d%I64d", &n, &k); LL t; t = (1 + k)*k / 2; if(t>n) { printf("-1\n"); continue; } LL begin; begin = (2 * n / k + 1 - k) / 2; LL res =n - (begin + begin + k - 1)*k / 2; LL real = 1; LL end = begin + k - 1; while (res) { real *= (end + 1); real %= mod; res--; end--; } for (LL i = begin; i <= end; i++) { real *= i; real %= mod; } printf("%I64d\n",real); } return 0;}
0 0
- hdu5646 DZY Loves Partition 数学
- hdu5646(DZY Loves Partition) 划分
- hdu5645 DZY Loves Balls && hdu5646 DZY Loves Partition
- HDOJ 5646 DZY Loves Partition(数学)
- HDU 5646 DZY Loves Partition (数学)
- HDOJ 5646-DZY Loves Partition【数学】
- hdoj 5646 DZY Loves Partition(数学)
- HDU 5646 DZY Loves Partition(数学)
- 【HDU5646 BestCoder Round 76 (div1)A】【贪心】DZY Loves Partition n个数拆分k个最大乘积
- DZY Loves Partition
- hdu_5646 DZY Loves Partition
- 【HDU 5646】DZY Loves Partition
- HDOJ 5646 DZY Loves Partition
- HDU 5646-DZY Loves Partition
- hdu 5646 DZY Loves Partition
- HDU 5646 DZY Loves Partition
- HDU5645 DZY Loves Balls 数学
- hdu5646(数学)
- 根据字符返回位置(重点)
- EL表达式中${sessionScope['oper_key'].userName}的含义
- oracle中exp,imp的使用详解
- Python学习笔记_02 标识符与关键字
- RPI-设置-调试-文件传输
- hdu5646 DZY Loves Partition 数学
- python 基本数据类型的使用
- 项目总结: MyBatis-paginator实现分页
- U盘安装Ubuntu12.04遇到的问题汇总
- 查看win10 内核数据结构
- iOS description方法的使用介绍
- session过期后自动跳转到登陆页
- uwsgi和nginx的启动/停止方法
- linux查找命令之locate