HDU 3092 Least common multiple 01背包
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3092
Least common multiple
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers , then what is the largest LCM of these numbers? partychen thought this problems for a long time but with no result, so he turned to you for help!
> Since the answer can very big,you should give the answer modulo M.
#### 输入
> There are many groups of test case.On each test case only two integers S( 0 Output the largest LCM modulo M of given S.
####样例输入
> 6 23
样例输出
6
题意
给你一个n,求把n拆成若干个数的和,要求这些数的最小公倍数最大。最后输出%M.
题解
由于最大公倍数会爆,无法直接维护,所以我们可以通过取对数的方式,得到大小,确定转移方向,然后利用这个去维护最优解。
这道题需要用到两个贪心策略:
1、只选质数(或质数的幂)(22*3=12,还不如22+3=7。)
2、每个质数只选一个(如果你拆出39=3+9+27,明显,只有27是有用的。。)然后就可以转换成01背包求解了(贪心策略2决定了这是01背包!不是完全背包)
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=3333;
double dp[maxn];
LL ans[maxn];
bool pri[maxn];
VI arr;
void pre() {
clr(pri,0);
for(int i=2; i*i<maxn; i++) {
if(!pri[i]) {
for(int j=i*i; j<maxn; j+=i) {
pri[j]=true;
}
}
}
for(int i=2; i<maxn; i++) if(!pri[i]) arr.pb(i);
}
int main() {
pre();
int S,M;
while(scf("%d%d",&S,&M)==2) {
clr(dp,0);
double Ma=-1;
LL last=1;
for(int i=0; i<=S; i++) ans[i]=1;
//01背包
for(int i=0; arr[i]<=S; i++) {
for(int j=S; j>=arr[i]; j--) {
for(int k=arr[i]; k<=j; k*=arr[i]) {
if(dp[j]<dp[j-k]+log10(k)) {
dp[j]=dp[j-k]+log10(k);
ans[j]=ans[j-k]*k%M;
}
}
if(Ma<dp[j]+eps) {
Ma=dp[j];
last=ans[j];
}
}
}
prf("%I64d\n",last);
}
return 0;
}
//end-----------------------------------------------------------------------
HDU 3092 Least common multiple 01背包的更多相关文章
- 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)
作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...
- ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)
Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...
- hdu 3092 Least common multiple
思路: 容易知道,分解成素数的lcm肯定是最大的,因为假设分解成2个合数,设定x为他们的 最大公约数, 那么他们的最小公倍数就要减少x倍了 然后如果是素数之间的最小公倍数,那么就只是他们的乘积,同样的 ...
- HDU 5234 Happy birthday --- 三维01背包
HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- HDU 1864 最大报销额 0-1背包
HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...
- hdu 2639 第k大01背包
求每个状态里的k优解,然后合并 /* HDU 2639 求01背包的第k大解. 合并两个有序序列 */ #include<stdio.h> #include<iostream> ...
随机推荐
- macOS 开启 VNC 远程桌面和 SSH 服务
macOS 开启 VNC 远程桌面和 SSH 服务 准备用 macOS 来做为服务器,既然是服务器,那不可缺少的是远程管理,实际上 macOS 自带 VNC 远程桌面和 SSH 服务,只是默认没有开启 ...
- 使用MATLAB工具deploytool打包不成功的解决办法
下面解决方法是来自百度贴吧一抹伊静的解决办法,很好用,非常感谢,现记录下来以便后续查看: 这个问题是2014a 需要破解:下载一个破解文件,原因是matlab未完全破解,2014a的破解办法:http ...
- 2017-2018-2 《网络对抗技术》20155322 Exp6 信息搜集与漏洞扫描
[-= 博客目录 =-] 1-实践目标 1.1-实践介绍 1.2-实践内容 1.3-实践要求 2-实践过程 2.1-Google hacking & ZoomEye 2.2-DNS.IP信息收 ...
- WPF-学习笔记 动态修改控件Margin的值
原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...
- MySQL下建立表
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackgao@gmail. ...
- Noip前的大抱佛脚----考场配置
(global-linum-mode t) (global-set-key (kbd "RET") 'newline-and-indent) (setq default-tab-w ...
- SQL Server 中SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...
- jzoj5377 开拓
哇好火的dp啊. 开始根本想不出 这里如果顺着dp肯定是不行的,没办法记录钻头能力值 显然能力值的变化会影响后面收入 而具体影响就是:全部乘了1-0.01k或1+0.01c. 可以倒着dp(orz) ...
- 三剑客之sed&grep
第1章 练习题 1.1 第1题 取得/etc/hosts 文件的权限 如何取得/etc/hosts 文件的权限对应的数字内容,如-rw-r--r-- 为 644,要求使用命令取得644 这样的数字. ...
- SpringBoot日记——按钮的高亮和添加篇
场景如下: 我们点击主页,主页那个按钮就高亮: 我们点击员工,员工那个按钮就高亮: 高亮的处理 直接来看代码如何编写: 1.先看一下官方文档如何编写关于参数配置的,等下我们来解释为何这么写: 所以,我 ...