HDU - 5976 Detachment 贪心

☆HDU 5976 Detachment 详细题解(贪心+逆元+前缀和,积) Detachment   解题思路: 【题意】 给定一个自然数x,让你给出一种拆分方式x=a1+a2+...(ai≠aj),使得每个小部分的乘积s=a1*a2*...最大 【类型】 贪心构造 【分析】 此题关键在于得出如何能使乘积s最大 按照以往经验,必然是取一段连续自然数能够使得乘积最大,而这段连续自然数可从2开始(为 阅读详情

Detachment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 895    Accepted Submission(s): 258


Problem Description
In a highly developed alien society, the habitats are almost infinite dimensional space.
In the history of this planet,there is an old puzzle.
You have a line segment with x units’ length representing one dimension.The line segment can be split into a number of small line segments:  a1,a2 , … (x=  a1+a2 +…) assigned to different dimensions. And then, the multidimensional space has been established. Now there are two requirements for this space: 
1.Two different small line segments cannot be equal (  ai≠aj  when i≠j).
2.Make this multidimensional space size s as large as possible (s=  a1∗a2 *...).Note that it allows to keep one dimension.That's to say, the number of ai can be only one.
Now can you solve this question and find the maximum size of the space?(For the final number is too large,your answer will be modulo 10^9+7)
 

Input
The first line is an integer T,meaning the number of test cases.
Then T lines follow. Each line contains one integer x.
1≤T≤10^6, 1≤x≤10^9
 

Output
Maximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.
 

Sample Input
  
1 4
 

Sample Output
  
4
 

Source


刚开始读错题意了。。。。

好吧,读对也过不了=.=

贪心

构造2,3,4,5,6一直到总和不超过x

举个例更好说

x=30

那么就是2,3,4,5,6,5

最后一个5重复了,是用前面的数去补他还是,用他去补前面的数,举了几个例子是往前补更优,证明不详。。。。。。

所以就这样贪心的构造,

求乘积时可以先处理出来阶乘,然后取模除一除

#include<bits/stdc++.h>
using namespace std;
long long mo=1e9+7;
long long f[1000005];
long long poww(long long a,long long x)
{
	long long ans=1,t=a;
	while(x)
	{
		if(x&1)
		ans*=t;
		t*=t;
		ans%=mo;
		t%=mo;
		x>>=1;
	}
	return ans;
}
void init()
{
	f[0]=1;
	for(int i=1;i<=1000000;i++)
	{
		f[i]=(f[i-1]*i)%mo;
	}
	return ;
}
long long solve(long long x,long long y)
{
	return (x*poww(y,mo-2))%mo;
}
int main()
{
	init();
	int T;
	long long x;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld",&x);
		if(x==1)
		{
			printf("1\n");
			continue;
		}
		long long le=0,ri=x,mid,res,sum,tt;
		while(le<=ri)
		{
			mid=(le+ri)>>1;
			if((mid+2)*(mid-1)/2<=x)
			{
				res=mid;
				le=mid+1;
			}
			else
			ri=mid-1;
		}
	//	cout<<res<<endl;
		sum=(res+2)*(res-1)/2;
		tt=x-sum;
		if(tt==res)
		{
			printf("%lld\n",(solve(f[res],2)*(res+2))%mo);
		}
		else
		{
		//	cout<<res<<" "<<tt<<endl;
			printf("%lld\n",(solve(f[res+1],res-tt+1)%mo));
		}
	}
	return 0;
}


HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站) Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 570    Accepted Submission(s): 192 Problem Description In a highly 阅读详情

相关推荐

【故障排查】telnet连接失败:端口23不可达的深度分析与解决方案

本文深度剖析了telnet连接失败,特别是端口23不可达的常见原因与系统性解决方案。文章从客户端本地配置、服务器端服务与防火墙、中间网络路径三个层面,提供了详细的排查步骤与命令,并强调了Telnet协议的安全风险,建议使用SSH等更安全的替代方案进行远程管理与端口测试。

weixin_29306011的博客 917

HDU 5976 && 2016ICPC大连 F: Detachment(找规律)

题意: 将n拆成a1+a2+a3+…+ax的形式(ai≠aj),让a1*a2*a3*…*ax的值最大,求这个最大值 找规律 n=35:2+3+4+5+6+7+8;      max:2*3*4*5*6*7*8 n=36:2+3+4+5+6+7+8+1;  max:2*3*4*5*6*7*9 n=37:2+3+4+5+6+7+8+2;  max:2*3*4*5*6*8*9 n

加载中... 598

白话详细解读(七)----- CBAM:Convolutional Block Attention Module

一、CBAM简介 论文链接:https://x.sci-hub.org.cn/scholar?hl=zh-TW&as_sdt=0%2C5&q=CBAM%3A+Convolutional+Block+Attention+Module&btnG=&oq=CB CBAM这篇论文是在2018年提出的,自提出之后,已经被引用上千次,可见其影响力。CBAM是一种专门为了卷积神经网络而设计的注意力模块,传统的卷积存在只关注局部信息,而往往忽略全局信息的弊端,以3*3卷积为例,滤波器有9个

奋斗の博客 1万+

HDU 5976 Detachment (逆元)

Problem Description In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment wi

小坏蛋_千千 1128

Nature、science、cell旗下刊物

Nature\science\cell子刊说明

计算机视觉 5617

完整复现YOLOv8:包括训练、测试、评估、预测阶段【本文源码已开源,地址在文章末尾】

(这里主要是在colab.research.google.com实现的,本地的配置也是类似的方法)YOLOv8是一种用于目标检测的深度学习算法。主要参考:https://github.com/ultralytics/ultralytics/blob/main/README.zh-CN.md。yolov8的权重:https://github.com/ultralytics/assets/releases。yolov8的文档:https://v8docs.ultralytics.com/

源代码杀手的博客 1万+

HDU 5976 逆元

Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description In a highly developed alien society, the habitats are almost infinite dime

black_miracle的博客 1784

Qt代码-软件商店

ui->statusBar->showMessage(QString("%1 已卸载").arg(app["name"].toString()));ui->statusBar->showMessage(QString("开始下载 %1...").arg(app["name"].toString()));QString("%1 已下载完成,是否立即安装?ui->statusBar->showMessage(QString("%1 下载完成").arg(app["name"].toString()));

寒寒豆豆软件公司的博客 117

HDU - 5976 Detachment(逆元+贪心+前缀和)

In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x units’ length representi...

>*,< 392

ANSYS——“There is at least 1 small equation solver pivot term”问题的解决办法

ANSYS——“There is at least 1 small equation solver pivot term”问题的解决办法

python_AI_fans的博客 1万+

CAN轴控制流【6041,6040】

【代码】CAN轴控制流【6041,6040】

cfqq1989的博客 1791

Detachment HDU - 5976(染色法)

In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x units’ length representing...

码农的一亩三分地 389

目标检测基础

目标检测是计算机视觉的核心任务,需同时对图像中的物体进行定位与识别。传统方法存在速度慢、精度低等问题,而深度学习显著推动了该领域发展,使实时高精度检测成为可能。检测的核心是为每个物体预测边界框坐标和类别标签。为实现高效检测,引入了锚框机制,通过预设多尺度、多宽高比的候选区域,缩小预测偏移量,提升定位精度。训练时,通过计算锚框与真实边界框的交并比 (IoU) 确定正负样本,并基于此生成类别标签和坐标偏移量作为监督信号。该方法提升了检测器对多尺度目标和复杂场景的鲁棒性。

盼小辉丶的博客 2919

hdu-5976 Detachment 解题报告(乘法逆元、贪心?)

目录题目描述思路分析完整代码 题目描述 题目:https://vjudge.net/problem/HDU-5976 好像有翻译来着,我就不翻译了。 思路分析 这道题初看起来就让我们联想到高中数学的基本不等式:和一定,分解为相同的数时,他们的积最大,即:aa+bb>=2ab,当且仅当a==b时,ab最大。 但是这道题有个要求:互不相等,所以,我们取个较近的情况:他们是阶乘的情况最大,这时这道...

nagisa2019的博客 447

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

HDU 5976 Detachment——贪心

很容易想到最优策略是2*3*4一直乘到前缀和小于n的最靠后的位置,设这个位置为p 当segma(2,p)==n时明显答案就是mul(2,p) 但是当segma(2,p) != n时会有一个余项,设为need,need=n-segma(2,p),显然现在项数不会再增加了,我们只能把need按照一定的策略分配给前面的元素才能使结果更大,按照直觉我们应该将need平摊给前面所有的元素,但是要注意ne...

hao_zong_yin的博客 493

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

HDU 5976 Detachment 【数论+贪心+逆元+前缀和、积】

传送门:HDU 5976 Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description In a highly developed alien society, the habitats are almost infinit...

Waiting For You 530

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Detachment HDU - 5976 前缀+逆元+贪心

In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x units’ length representi...

liluoyu_1016的博客 305

HDU - 5976 Detachment(贪心思维)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5976 题意:把一个数拆成若干个不相同的数之和,问这些数的乘积最大是多少。 思路:一般要使乘积最大,那么先拆成两个不同数时,这两个数一定是接近n/2,然后每一个数又可以拆成两个数。但要这些数都不相同,所以最好的情况肯定是n等于从2开始一直到某个数k的连续和,如9=2+3+4,这样肯定是最优的,那么n...

LLL_yx的博客 704

【数论】【逆元】【贪心】HDU5976 Detachment

题目链接题意:给出一个数x,存在∑ni=1ai=x\sum_{i=1}^n a_i= x,ai!=aja_i != a_j 使得∏ni=1\prod_{i=1}^n 最大,求∏ni=1\prod_{i=1}^n 的最大值。解题思路:容易发现要使得积最大,要把x分得尽可能小。所以把x分成2,3,4,5,…,i的形式。从2开始是因为分成1并不会使积更大。如果x能直接分成这个形式,就可以直接得出答案。

Dizzz的博客 474

HDU5976-Detachment(乘法逆元+贪心)

题目链接 Problem Description In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x...

QAQ 602

HDU 5976 Detachment

  这道题目是向这位博主学习的,链接贴出来:☆HDU 5976 Detachment 详细题解(贪心+逆元+前缀和,积),讲的很细致,很感谢; 思路: 1.题意是将数x分成若干个正整数和,使得这些整数的乘积最大,并求出这个最大乘积除以1e9+7的余数; 2.要使得乘积最大,那么我们需要尽可能的分出更多的数(1除外),又因为分出的数需要各不相同,由贪心思想,我们可以从最小的2开始往后分...

Yuhan的博客 313

HDU 5976 Detachment 题解(贪心+逆元+前缀和,积)

个人体会:无脑取模,最为致命!因为把存放前缀和的数组取了模,导致一些本来不等的元素变成了相同的元素,二分搜索出错。。。而且C++会T,G++就ac了。。 http://acm.hdu.edu.cn/showproblem.php?pid=5976 分解出的几个数越接近,乘积越大,如果可以相等,则分解成3的乘积,不能相等,只能分成阶乘则最大,不能分成阶乘时,把多出来的数从后往前均摊,因为如果从前往后均摊会造成存在相等的数,然后均摊完多数相乘复杂度太高,需要有除法,于是根据费马小定理把除法改成乘法逆元,需要预处

tongjingqi_的博客 735

hdu5976 Detachment思维数学(贪心+逆元)

这个题,比赛的时候分析出来了,但是最后时间不够了,就没有做出来,GG了,最后补题的时候有因为自己地粗心开始不断debug,不过也发现问题了。 先来说一下思路,将这个数分解后使其乘积最大,且因子各不相同,我就想到了阶乘,但是一个数不一定可以分解的彻底,所以可以将其从后向前均分到每一个数上,可以想一下,前面的数每增加1其实就是相比于原乘积增加一倍的后面数的乘积。然后分析一下时间复杂度,发现必须直接计算...

AC_jie的博客 391

Detachment HDU - 5976(贪心+逆元+二分)

In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x units’ length representing...

码农的一亩三分地 325

大连2016 F - Detachment HDU - 5976(贪心,数论,猜结论)

In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x units’ length representing...

tomjobs的博客 549
上一篇: HDU - 5974 A Simple Math Problem 解方程
下一篇: FatMouse and Cheese hdu-1078
jerans
博客等级 码龄10年 54粉丝 255原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值