题目链接:

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背包!不是完全背包)

代码

  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<ctime>
  7. #include<vector>
  8. #include<cstdio>
  9. #include<string>
  10. #include<bitset>
  11. #include<cstdlib>
  12. #include<cstring>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<functional>
  16. #include<sstream>
  17. using namespace std;
  18. #define X first
  19. #define Y second
  20. #define mkp make_pair
  21. #define lson (o<<1)
  22. #define rson ((o<<1)|1)
  23. #define mid (l+(r-l)/2)
  24. #define sz() size()
  25. #define pb(v) push_back(v)
  26. #define all(o) (o).begin(),(o).end()
  27. #define clr(a,v) memset(a,v,sizeof(a))
  28. #define bug(a) cout<<#a<<" = "<<a<<endl
  29. #define rep(i,a,b) for(int i=a;i<(b);i++)
  30. #define scf scanf
  31. #define prf printf
  32. typedef long long LL;
  33. typedef vector<int> VI;
  34. typedef pair<int,int> PII;
  35. typedef vector<pair<int,int> > VPII;
  36. const int INF=0x3f3f3f3f;
  37. const LL INFL=0x3f3f3f3f3f3f3f3fLL;
  38. const double eps=1e-8;
  39. const double PI = acos(-1.0);
  40. //start----------------------------------------------------------------------
  41. const int maxn=3333;
  42. double dp[maxn];
  43. LL ans[maxn];
  44. bool pri[maxn];
  45. VI arr;
  46. void pre() {
  47. clr(pri,0);
  48. for(int i=2; i*i<maxn; i++) {
  49. if(!pri[i]) {
  50. for(int j=i*i; j<maxn; j+=i) {
  51. pri[j]=true;
  52. }
  53. }
  54. }
  55. for(int i=2; i<maxn; i++) if(!pri[i]) arr.pb(i);
  56. }
  57. int main() {
  58. pre();
  59. int S,M;
  60. while(scf("%d%d",&S,&M)==2) {
  61. clr(dp,0);
  62. double Ma=-1;
  63. LL last=1;
  64. for(int i=0; i<=S; i++) ans[i]=1;
  65. //01背包
  66. for(int i=0; arr[i]<=S; i++) {
  67. for(int j=S; j>=arr[i]; j--) {
  68. for(int k=arr[i]; k<=j; k*=arr[i]) {
  69. if(dp[j]<dp[j-k]+log10(k)) {
  70. dp[j]=dp[j-k]+log10(k);
  71. ans[j]=ans[j-k]*k%M;
  72. }
  73. }
  74. if(Ma<dp[j]+eps) {
  75. Ma=dp[j];
  76. last=ans[j];
  77. }
  78. }
  79. }
  80. prf("%I64d\n",last);
  81. }
  82. return 0;
  83. }
  84. //end-----------------------------------------------------------------------

HDU 3092 Least common multiple 01背包的更多相关文章

  1. 背包系列练习及总结(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 ...

  2. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

  3. hdu 3092 Least common multiple

    思路: 容易知道,分解成素数的lcm肯定是最大的,因为假设分解成2个合数,设定x为他们的 最大公约数, 那么他们的最小公倍数就要减少x倍了 然后如果是素数之间的最小公倍数,那么就只是他们的乘积,同样的 ...

  4. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  5. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  6. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  7. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  8. HDU 1864 最大报销额 0-1背包

    HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...

  9. hdu 2639 第k大01背包

    求每个状态里的k优解,然后合并 /* HDU 2639 求01背包的第k大解. 合并两个有序序列 */ #include<stdio.h> #include<iostream> ...

随机推荐

  1. JS控制上传文件个数

    <html><body>    <h3>js控制文件上传数量</h3>    <form action="" enctype= ...

  2. CentOS7.3安装mysql数据库

    Mysql数据库安装 1.环境 操作系统:CentOS 7.3 软件:MySQL 5.7 下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5 ...

  3. Linux学习笔记(第九章)

    压缩概念: gzip和zcat: 先进版bzip2,bzcat bzip -d  已压缩文档名 bzip -z 需压缩文档名 bzcat 解压文档打印到屏幕 tar:打包指令 注意:压缩最好拿掉根目录 ...

  4. 克隆虚拟机及配置yum源的步骤及讲解(Hadoop基础)

    1.克隆虚拟机 找一台需要克隆的虚拟机但虚拟机必须在关机下进行,(建议将前期Linux环境 配置完成) 在VMware中右键虚拟机找到克隆的选项.   点击克隆 可以克隆他的快照(提前做快照)或者是克 ...

  5. SSM整合思路

    引自网友: https://zhuanlan.zhihu.com/p/23917781

  6. Oracle中用户解锁

    以hr 用户为例: SQL> alter user hr account unlock; ユーザーが変更されました. SQL> alter user hr identified by hr ...

  7. CF 1138 F. Cooperative Game

    F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...

  8. C++ STL 学习笔记__(6)优先级队列priority_queue基本操作

    10.2.7优先级队列priority_queue v  最大值优先级队列.最小值优先级队列 v  优先级队列适配器 STL priority_queue v  用来开发一些特殊的应用,请对stl的类 ...

  9. CentOS7中使用阿里云镜像

    之前因为下载Docker镜像很慢所以用了一家国内的镜像DaoCloud,今天要用的是阿里云的镜像库. 首先要开通了阿里云开发者帐号,地址 : https://dev.aliyun.com/search ...

  10. C# 通用树形数据结构

    前言 树在图论中是一种重要的图,由于其自身的许多特殊性质,也是一种重要的计算机数据结构,在很多地方都有用.但是这些树大多都是作为其他应用的内部数据结构来使用.我们无法了解这些树的详细信息,而 .Net ...