[ZOJ2069]Greatest Least Common Multiple
[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的更多相关文章
- [Intermediate Algorithm] - Smallest Common Multiple
题目 找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 例如对 1 和 3 —— 找出能被 1 和 3 和它们之间所有数字整除的最小 ...
- K - Least Common Multiple
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- [UCSD白板题] Least Common Multiple
Problem Introduction The least common multiple of two positive integers \(a\) and \(b\) is the least ...
- hdu1019 Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- HDOJ2028Lowest Common Multiple Plus
Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 2028 Lowest Common Multiple Plus(最小公倍数)
Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
题目地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组 ...
- HDU-1019 Least Common Multiple
http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/ ...
- Least Common Multiple
地址:http://www.codewars.com/kata/5259acb16021e9d8a60010af/train/python 题目: Write a function that calc ...
随机推荐
- python操作注册表
#注册表操作 # -*- coding: utf-8 -*- import win32api import win32con #打开注册表:传主键化值,子键值,操作方法(win32con.KEY_AL ...
- python 全栈开发,Day71(模型层-单表操作)
昨日内容回顾 1. {% include '' %} 2. extend base.html: <html> ..... ..... ..... {% block content%} {% ...
- 005-Python字典
Python字典(dict) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号{}中: ...
- tp5.0整合七牛云图片上传
转:https://www.cnblogs.com/adobe-lin/p/7699638.html 这里以上传图片为例 上传其他文件也是大同小异 使用composer安装gmars/tp5-qini ...
- XML使用与总结
xml是一种比较方便的数据储存方式,它适用于小数据的存储.最常见的适用地方莫过于各种web.config与app.config了. 一.创建一个简单的xml路径 public static str ...
- [转]解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
来源:http://www.cnblogs.com/sprinng/p/5141233.html 1.添加M2_HOME的环境变量 2.Preference->Java->Installe ...
- python基础——循环(for,while,break,continue)
for while . break:退出循环 continue:退出本次循环 例子 for i range(0,101,2): print(i) --------------------------- ...
- php通过CURL模拟get提交请求
方式一: $host = "http://jisunews.market.alicloudapi.com"; $path = "/news/get"; $met ...
- POJ1273 USACO 4.2.1 Drainage Ditches CodeVS1993草地排水 网络流 最大流 SAP
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 传送门 - POJ 传送门 - CodeVS 题意概括 给出一个图,告诉你边和容量,起点是1,汇点是n,让你求最大流. 题解 ...
- 057 Java中kafka的Producer程序实现
1.需要启动的服务 这里启动的端口是9092. bin/kafka-console-consumer.sh --topic beifeng --zookeeper linux-hadoop01.ibe ...