hdu 5646DZY Loves Partition(构造)
DZY Loves Partition
DZY喜欢拆分数字。他想知道能否把nn拆成恰好kk个不重复的正整数之和。 思考了一会儿之后他发现这个题太简单,于是他想要最大化这kk个正整数的乘积。你能帮帮他吗? 由于答案可能很大,请模10^9+7109+7输出。
第一行tt,表示有tt组数据。 接下来tt组数据。每组数据包含一行两个正整数n,kn,k。 (1\le t\le 50, 2\le n,k \le 10^91≤t≤50,2≤n,k≤109)
对于每个数据,如果不存在拆分方案,输出-1−1;否则输出最大乘积模10^9 + 7109+7之后的值。
4
3 4
3 2
9 3
666666 2
-1
2
24
110888111
第一组数据没有合法拆分方案。
第二组数据方案为3=1+23=1+2,答案为1\times 2 = 21×2=2
第三组数据方案为9=2+3+49=2+3+4,答案为2\times 3 \times 4 = 242×3×4=24。注意9=3+3+39=3+3+3是不合法的拆分方案,因为其中包含了重复数字。
第四组数据方案为666666=333332+333334666666=333332+333334,答案为333332\times 333334= 111110888888333332×333334=111110888888。注意要对10^9 + 7109+7取模后输出,即110888111110888111。
思路:
首先试了一下: 9拆成3个可以拆分成2+3+4 or 1+3+5,但是很明显前一个的积要大,而且多试了几个都是如此。所以考虑拆成一段连续的数,它们积较大。但是n拆成k个连续数的和后可能有剩余,于是可以考虑吧它们添加到这k个数上面。很明显从前面添加会有重复,所以如果剩余了m,则在倒数m个数上面均加上1.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
#define LL(x) (x<<1)
#define RR(x) (x<<1|1) const ll MOD = 1e9 + 7;
const int maxn = 305; int main()
{
int T;
ll n,k;
ll t;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&n,&k);
if(k%2) t = (ll)(k+1)/2*k;
else t = (ll)k/2*(k+1);
if(t > n)
{
printf("-1\n");
continue ;
}
else
{ ll po = n-t;
ll bei = po/k;
ll els = po%k;
//cout << bei <<" "<<els<<endl;
ll ans = 1;
for(ll i = k; i >= 1; i--)
{
ll cur = i+bei;
if(els)
{
cur++;
els --;
}
ans = (ll)ans*cur%MOD;
}
printf("%I64d\n",ans);
}
}
return 0;
}
hdu 5646DZY 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 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- hdu 5015 233 Matrix(构造矩阵)
http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...
- HDU 5813 Elegant Construction 构造
Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- hdu Dylans loves tree [LCA] (树链剖分)
Dylans loves tree view code#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...
- HDU 4658 Integer Partition(整数拆分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4658 题意:给出n.k.求n的拆分方案数.要求拆分中每个数不超过k. i64 f[N]; void i ...
随机推荐
- 十款不容错过的Swift iOS开源项目及介绍
1.十款不容错过的Swift iOS开源项目. http://www.csdn.net/article/2014-10-16/2822083-swift-ios-open-source-project ...
- Django 视图层
URL映射 1.分布式url映射 include()函数提供分布式url映射功能,使URL映射可以被编写在多个url.py文件中 from django.conf.urls import url fr ...
- 利用python 创建XML文件
#coding=utf-8 from xml.etree import ElementTree import pdb def printNodeInfo(node): #node.tag 标签名称 # ...
- 【iOS】Swift if let 和 if var
if let unwrappedOptional = postDict { print("The optional has a value! It's \(unwrappedOptional ...
- prototype 原型链
// 方法1 var aa=function(){ function bb(){ this.name="1111"; console.log(this.name) }; bb.pr ...
- zookeeper安装及环境变量设置
下载 首先去官网下载(自行选择版本):http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.11/然后执行tar -zxvf解压 对于后台安装 ...
- LeetCode & Q167-Two Sum II - Input array is sorted-Easy
Array Two Pointers Binary Search Description: Given an array of integers that is already sorted in a ...
- 深度学习之 seq2seq 进行 英文到法文的翻译
深度学习之 seq2seq 进行 英文到法文的翻译 import os import torch import random source_path = "data/small_vocab_ ...
- bootstrap 之下拉多选
效果如图: 一.HTML代码 <label class="col-sm-1 control-label text-right" for="ds_host" ...
- CentOS 6.5 Tomcat安装及配置
1.安装jdk,配置jdk环境(此步骤略过) 2.下载安装tomcat 百度网盘链接: https://pan.baidu.com/s/1Ieejo7TQyzRAVPhQft8Phw 密码: dg2v ...