HDU-3092 Least common multiple---数论+分组背包
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3092
题目大意:
有一个数字n,现在要把它分解成几个数字相加!然后这几个数字有最小公倍数,题目目的是求出最大的最小公倍数。我们知道所有的素数或者其指数方相加可以表示其它的数字,而把n分解之后求其公倍数自然是互质的数字直接相乘最大,所以目的就变成了求n能分解之后由素数或者其指数数,只要他们之间相互互质就行。
解题思路:
将n分解成不同素数之和,这样就是两两互质,求出的lcm是最大的,而且不只是素数之和,可以分解成素数的k次方,这样不同的素数的k次方之间仍然是互质的。
这样变成了分组背包,每一个素数就是一组,这一组中只能选一个,而背包容量为S。求出最大的价值即可。
还有一个问题,由于价值过大,需要取模,但是随便取模的话就不好判断大小,所以采用取对数的方法,一个数组记录取对数的值,一个数据记录答案,每次按照取对数的数组的大小判断是否需要更新,需要更新的话直接更新该数组和答案数组。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m;
const int maxn = + ;
bool is_prime[maxn];
ll prime[maxn], tot, ans[maxn];
double dp[maxn];
void init(int n)
{
for(int i = ; i <= n; i++)is_prime[i] = ;
for(int i = ; i <= n; i++)
if(is_prime[i])
{
prime[tot++] = i;
for(int j = i * ; j <= n; j += i)is_prime[j] = ;
}
//for(int i = 0; i < tot; i++)cout<<prime[i]<<endl;
}
int main()
{
init();
while(cin >> n >> m)
{
for(int i = ; i <= n; i++)dp[i] = , ans[i] = ;
for(int i = ; i < tot && prime[i] <= n; i++)
{
double tmp = log(prime[i]);
for(int j = n; j >= ; j--)
{
ll k = prime[i], cnt = ;
while(k <= j)
{
if(dp[j] < dp[j - k] + tmp * cnt)
{
dp[j] = dp[j - k] + tmp * cnt;
ans[j] = ans[j - k] * k % m;
}
cnt++;
k *= prime[i];
}
}
//for(int i = 1; i <= n; i++)cout<<dp[i]<<endl;
}
cout<<ans[n]<<endl;
}
return ;
}
HDU-3092 Least common multiple---数论+分组背包的更多相关文章
- 背包系列练习及总结(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 ...
- HDU 3092 Least common multiple 01背包
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3092 Least common multiple Time Limit: 2000/1000 MS ...
- 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 1712 ACboy needs your help (分组背包模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 有n门课,和m天时间.每门课上不同的天数有不同的价值,但是上过这门课后不能再上了,求m天里的最大 ...
- HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU 4913 Least common multiple
题目:Least common multiple 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4913 题意:有一个集合s,包含x1,x2,...,xn, ...
- hdu 2028 Lowest Common Multiple Plus(最小公倍数)
Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- ACM hdu 1019 Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
随机推荐
- shell 操作环境
一.路径与命令查找顺序 命令的运行程序可以这样看: 1.以相对/绝对的路径执行命令,例“/bin/ls”或“ls” 2.由alias乍到该命令来执行 3.由bash内置的(builtin)命令来执行 ...
- Knime读取Jason数据
Knime ETL 工具 Jason数据解析到DB 1. 下面例子是一段Jason代码 [{,,},{,,},{,,}] 2. 用文本文件存储上面代码. test_jason.txt 3. 用File ...
- fixed
之前我们见的fixed就是固定在低端.后来我遇到一个问题 如果 这样写: div{ position: fixed; left: 0; bottom: 0; } 内容吧div撑开,有多少算多少 di ...
- [转]MVC系列——MVC源码学习:打造自己的MVC框架(一:核心原理)
本文转自:http://www.cnblogs.com/landeanfen/p/5989092.html 阅读目录 一.MVC原理解析 1.MVC原理 二.HttpHandler 1.HttpHan ...
- git本地分支关联远程分支
问题描述: 从远程master克隆下来以后, 在本地创建wf_dev分支, 此时执行git pull 操作出现图中问题. 这是因为:本地的wf_dev分支还没有和远程的wf_dev进行关联. 执行: ...
- Bash 终端快捷键
在终端下输大量的命令,有些快捷键还是挺有帮助的. 跳跃 跳到最后 ctrl + e (end) 跳到最前 ctrl + a (abcde....) 向后跳一个单词 alt + 右方向键 向前跳一个单词 ...
- jQuery plugin: Tablesorter 2.0
http://mottie.github.io/tablesorter/docs/example-pager.htmlhttp://tablesorter.com/docs/example-pager ...
- 【数据库】9.0 MySQL入门学习(九)——获得数据库和表的信息、日期计算、查询、选择特殊列
1.0 SELECT语句用来从数据表中检索信息. SELECT what_to_select FROM which_table WHERE conditions_to_satisfy; what_to ...
- 移动端点击a链接出现蓝色背景问题解决
a:link, a:active, a:visited, a:hover { background: none; -webkit-tap-highlight-color: rgba(0,0,0,0); ...
- 为mongodb添加账号
进入切换到某一个数据库,我这里是位每个模块分配一个DataBase use 0 执行添加账号命令 db.createUser( { user: "*****", pwd: &quo ...