题意

给出\(n\)和\(m\),将\(n\)拆成任意个数,求它们的最大的\(lcm\)

分析

1.可以证明\(n=p1^{s1}*p2^{s2}*...*pn^{sn}\)时\(lcm\)最大(其中\(p1,p2...pn\)皆为素数) 证明

2.那么就可以转化为完全背包,模仿公式

\(f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<=v}\)

状态转移方程为\(dp[j]=max(dp[j],dp[j-p]*p)\);

用log代替真实值 \(dp[j]=max(dp[j],dp[j-p]+q*num)\);

来一个三重循环即可,复杂度小于\(O(n^2logn/log2)\)

3.用对数防止取模

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
using namespace std; #define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
#pragma comment(linker, "/STACK:102400000,102400000")
inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}} double dp[3030];
int prime[3030],p[3030],ans[3030],n,m,cnt;
void get_prime()//线性筛
{
p[1]=1;
F(i,2,3000)
{
if(!p[i]) prime[cnt++]=i;
for(int j=0;j<cnt&&i*prime[j]<=3000;++j)
{
p[prime[j]*i]=1;
if(i%prime[j]==0) break;
}
}
}
int main()
{
get_prime();
while(scanf("%d %d",&n,&m)==2)
{
F(i,0,n) { dp[i]=0;ans[i]=1; }
for(int i=0;i<cnt&&prime[i]<=n;++i)
{
for(int j=prime[i];j<=n;j++)
{
double tmp=log(prime[i]*1.0);
for(int p=prime[i],q=1;p<=j;p*=prime[i],q++)
{
if(dp[j-p]+q*tmp>dp[j])
{
dp[j]=dp[j-p]+q*tmp;
ans[j]=ans[j-p]*p%m;
}
}
}
}
printf("%d\n",ans[n]);
}
return 0;
}

HDU3092:Least common multiple(素数筛选+完全背包)的更多相关文章

  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. HDU 3092 Least common multiple 01背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3092 Least common multiple Time Limit: 2000/1000 MS ...

  3. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

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

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

  5. K - Least Common Multiple

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  6. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

  7. [UCSD白板题] Least Common Multiple

    Problem Introduction The least common multiple of two positive integers \(a\) and \(b\) is the least ...

  8. codeforces Soldier and Number Game(dp+素数筛选)

    D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...

  9. hdu1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

随机推荐

  1. I NEED A OFFER!---hdu1203(01背包)

     http://acm.hdu.edu.cn/showproblem.php?pid=1203   Problem Description Speakless很早就想出国,现在他已经考完了所有需要的考 ...

  2. CD-----UVa624(01背包+输出路径)

      CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...

  3. OO第三单元总结——JML

    目录 写在前面 JML理论基础 JML工具链 JMLUnitNG的使用 架构设计 Bug分析 心得体会 写在前面 OO的第三单元学习结束了,本单元我们学习了如何使用JML语言来对我们的程序进行规格化设 ...

  4. freeswitch对媒体的处理的三种方式

    一.默认方式:媒体通过freeswitch, RTP被freeswtich转发, freeswitch控制编码的协商并在协商不一致时提供语音编码转换能力, 支持录音,二次拨号等.   二.代理模式: ...

  5. 弄技术要弄通-公司reis的pub/sub怎么使用的呢?

    Pub/Sub in Redis using PHP Posted on November 14, 2011by xmeng I would like to put an example togeth ...

  6. Visual Studio VS如何卸载Visual assistant

    1 点击工具-扩展管理器   2 选中Visual Assist X,点击卸载即可.                            

  7. Hibernate中的自己定义类型——UserType、CompositeUserType

    一.UserType Hibernate拥有自己定义映射表属性的机制.主要通过实现接口UserType,详细的UserType: import java.sql.PreparedStatement; ...

  8. 获取Windows用户所有的账户名

    /// <summary> /// 设置用户密码 /// </summary> [DllImport("Netapi32.dll")] extern sta ...

  9. [LeetCode][Java] Remove Duplicates from Sorted List II

    题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

  10. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览 总结

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览  总结         SP2013中的OAuth提供了很多新的集成SP On ...