[ZOJ2069]Greatest Least Common Multiple

题目大意:

给定一个正整数\(n\),将其分成若干个正整数之和,最大化这些数的LCM。保证答案小于\(10^{25}\)。

思路:

由于答案\(\le10^{25}\),则\(n\le540\)。

可以证明一定存在一种方案使得拆分后各数互质且答案最大。

\(f[i][j]\)表示考虑前\(i\)种质数,组成的和为\(j\)的答案。转移时枚举从\(n\)中拆出这个质数的多少次方。

由于OJ上不支持__int128,所以直接最后打表即可。

打表程序:

#include<cstdio>
#include<cctype>
#include<numeric>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=541,P=N;
typedef long long int64;
typedef __int128 int128;
int128 f[P][N];
int128 gcd(const int128 &a,const int128 &b) {
return b?gcd(b,a%b):a;
}
inline int128 lcm(const int128 &a,const int128 &b) {
return a/gcd(a,b)*b;
}
void print(const int128 &x) {
if(x>9) print(x/10);
putchar(x%10+'0');
}
bool vis[N];
int p[P];
inline void sieve() {
for(register int i=2;i<N;i++) {
if(!vis[i]) p[++p[0]]=i;
for(register int j=1;j<=p[0]&&p[j]*i<N;j++) {
vis[p[j]*i]=true;
if(i%p[j]==0) break;
}
}
}
int main() {
sieve();
std::fill(&f[0][0],&f[0][N],1);
for(register int i=1;i<=p[0];i++) {
f[i][0]=f[i-1][0];
for(register int j=1;j<N;j++) {
f[i][j]=std::max(f[i-1][j],f[i][j-1]);
for(register int q=p[i];q<=j;q*=p[i]) {
f[i][j]=std::max(f[i][j],f[i-1][j-q]*q);
}
}
}
putchar('{');
for(register int i=0;i<N;i++) {
if(i!=0) putchar(',');
putchar('"');print(f[p[0]][i]);putchar('"');
}
puts("};");
return 0;
}

[ZOJ2069]Greatest Least Common Multiple的更多相关文章

  1. [Intermediate Algorithm] - Smallest Common Multiple

    题目 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 例如对 1 和 3 —— 找出能被 1 和 3 和它们之间所有数字整除的最小 ...

  2. K - Least Common Multiple

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

  3. [UCSD白板题] Least Common Multiple

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

  4. hdu1019 Least Common Multiple

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

  5. HDOJ2028Lowest Common Multiple Plus

    Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. hdu 2028 Lowest Common Multiple Plus(最小公倍数)

    Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  7. 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组 ...

  8. HDU-1019 Least Common Multiple

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/ ...

  9. Least Common Multiple

    地址:http://www.codewars.com/kata/5259acb16021e9d8a60010af/train/python 题目: Write a function that calc ...

随机推荐

  1. python2.7 (x64) 调用 java 代码实践

    背景:公司实施接口自动化测试,使用的框架python+unittest:因为开发使用的架构师SpringBoot,在测试过程中难免需要使用到python调用JAVA的一些接口,所以需要用到python ...

  2. RabbitMQ在java中基础使用

    RabbitMQ相关术语:          1.Broker:简单来说就是消息队列服务器实体.          2.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列.      ...

  3. js FileReader 笔记

    以上传图片为例 通过input type='file' 上传完成图片后,获取图片 $('#input').files[0] var reader = new FileReader();    read ...

  4. 论文阅读笔记一:Tutorial on Gabor Filters

    时域下的一维Gabor滤波器: 可以将Gabor滤波器看作是两个输出两个相位的滤波器,分别在实数域和虚数域上. 实数域上滤波器为: 虚数域上滤波器为: 傅里叶变换为频域: 上述两个滤波器对频率敏感,为 ...

  5. .gz解压

    1.今天很神奇我遇到这样的压缩包,啧啧啧,好少见的,记录下 gzip  -d  http_log.gz 这是讲http_log文件解压到当前的路径下

  6. servlet设置cookie

    Cookie cookie =new Cookie("user","黄花菜");//实例化一个Cookie对象 cookie.setMaxAge(7*24*60 ...

  7. mybatis + oracle insert clob,出现ORA-01461:仅能绑定要插入LONG列的LONG值

    在网上查了很久,有可能问题是出现在当从dual中取数据时,会将clob对象的字段转为Long型 最后的解决方法用到了Begin和end语法: 1.用到begin 和end 2.用到insert int ...

  8. Github版本管理以及git使用

    1.git客户端编译安装 同步系统时间服务器 ntpdate cn.ntp.org.cn 安装依赖包: [root@baolin ~]# yum install epel-release -y [ro ...

  9. bzoj4428

    题解: f[i]=f[n/(j+1)向上取整]+p*j+k 然后可以通过枚举每个数的因子来做 时间复杂度nlogn(打表看了一下sigma (i因子个数) 是比较接近nlogn的) 可以有方法优化到n ...

  10. [转]MySQL 数据类型(float)的注意事项

    http://www.cnblogs.com/zhoujinyi/archive/2013/04/26/3043160.html 可能由于版本关系,我的mysql5.7插入数据超过范围时会提示,126 ...