链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646

题意:将n分成k个正整数之和,要求k个数全部相同;并且这k个数的乘积最大为多少?结果mod 1e^9+7;

思路:由于是mod,不能通过模拟进行比较来判断是否为最优解;那么我们就必须直接计算出这个最优解的序列;

由于当a <= b-2时(相等时表示中间空一位),a*b < (a+1)*(b-1);所以最优解序列要不就是一串连续的序列,要不就是中间空一位,分成两段连续的序列;

因为如果存在空格超过1个的两个数,就可以通过加1减1,移到相邻或者只空一个位;

注意:到底空的是哪一位,与平均值并不相关;这需要看n与确定的左边界之后的k个数的和相差的值;(用二分确定左边界,使得sum[a,...a+k) <= n < sum[a...a+k]);

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
typedef pair<int,int> PII;
#define A first
#define B second
#define MK make_pair
typedef __int64 ll;
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
} const int mod = 1e9+;
int add(ll a, ll b) { return (a+b)%mod; }
int sub(ll a, ll b) { return ((a-b)%mod + mod)%mod; }
int mult(ll a, ll b) { return ((a * b))%mod; }
ll n,k;
int s[];
int main()
{
int T;
read1(T);
while(T--){
read2(n,k);
if(n < k*(k+)/){//
puts("-1");
continue;
}
int tmp = n/k,a,l = ,r = tmp;
while(l <= r){
int mid = l+r>>;
if((mid+mid+k-)*k/ <= n) a = mid,l = mid+;
else r = mid - ;
}
int sum = (a+a+k-)*k/;// a即为确定的左边界
ll ans = ;
if(sum == n){
rep0(i,,k) ans= mult(ans,a+i);
}
else{
int t = n - sum;
int cnt = k-t;
rep0(i,,cnt) ans = mult(ans,a+i);
a++;//中间空一位
while(cnt < k)
ans = mult(ans,a+cnt),cnt++;
}
printf("%d\n",ans);
}
return ;
}

hdu 5646 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

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

  3. hdu 5649 DZY Loves Sorting 二分+线段树

    题目链接 给一个序列, 两种操作, 一种是将[l, r]里所有数升序排列, 一种是降序排列. 所有操作完了之后, 问你a[k]等于多少. 真心是涨见识了这题..好厉害. 因为最后只询问一个位置, 所以 ...

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

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

  5. HDU 5649.DZY Loves Sorting-线段树+二分-当前第k个位置的数

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  6. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  7. hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

    传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131 ...

  8. 数据结构(线段树):HDU 5649 DZY Loves Sorting

    DZY Loves Sorting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Oth ...

  9. HDU 5645 DZY Loves Balls 水题

    DZY Loves Balls 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5645 Description DZY loves playing b ...

随机推荐

  1. Html简单demo_html列表中进行编辑操作

    html列表中进行编辑操作 <div class="_sort_box" style="float: left;"><?php echo $v ...

  2. Session Store

    Session Store Configuration Session Usage Flash Data Session Drivers Configuration Since HTTP driven ...

  3. Forms and actions

    Forms and actions Adding new albums We can now code up the functionality to add new albums. There ar ...

  4. iOS9横竖屏设置的处理方法

    在一般的视频类APP播放的时候都会支持横屏,这样做的好处就是便于观看.你的项目中支持横屏吗?我们一起了解一下,在iOS9中横竖屏设置的处理方法吧! 支持横竖屏配置 在iOS6以后,如果APP需要支持横 ...

  5. Java基础知识强化之网络编程笔记21:Android网络通信之 Android常用OAuth登录(获取令牌信息)

    1. 首先我们去下载开发相关SDK(Android): 下载百度使用OAuth的SDK(Android),如下: 下载链接为:http://developer.baidu.com/wiki/index ...

  6. 马上搞定Android平台的Wi-Fi Direct开发

    导语 移动互联网时代,很多用户趋向于将大量的资料保存在移动设备上.但在给用户带来便利的同时引发了一个新的问题——保存在移动设备上的资料该怎样共享出去?到了思考时间,普通青年这样想:折腾什么劲啊,直接用 ...

  7. [转]div里table居中的问题 Div与body顶部间隙

    本文转自:http://www.cnblogs.com/jinhui/archive/2008/09/24/1297729.html 将div的text-align设为center,然后将table的 ...

  8. [转]Oracle Stored Procedures Hello World Examples

    本文转自:http://www.mkyong.com/oracle/oracle-stored-procedures-hello-world-examples/ List of quick examp ...

  9. ant design 自定义表单验证大全

     需求是 账号名可以是手机号也可以是邮箱 要做手机号和邮箱的验证,官网的那个验证规则不匹配  怎么自定义验证规则?  一:组件部分 <Form horizontal> <Row gu ...

  10. Python(2.7.6) 迭代器

    除了对列表.集合和字典等进行迭代,还能对其他对象进行迭代:实现 __iter__ 方法的对象.例如, 文件对象就是可迭代的: >>> dir(file) ['__class__', ...