[ARC122E] Increasing LCMs
Problem Statement
We have a sequence of $N$ positive integers: $A_1,A_2,\cdots,A_N$.
You are to rearrange these integers into another sequence $x_1,x_2,\cdots,x_N$, where $x$ must satisfy the following condition:
- Let us define $y_i=\operatorname{LCM}(x_1,x_2,\cdots,x_i)$, where the function $\operatorname{LCM}$ returns the least common multiple of the given integers. Then, $y$ is strictly increasing. In other words, $y_1<y_2<\cdots<y_N$ holds.
Determine whether it is possible to form a sequence $x$ satisfying the condition, and show one such sequence if it is possible.
Constraints
- $1 \leq N \leq 100$
- $2 \leq A_1 < A_2 \cdots < A_N \leq 10^{18}$
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$
$A_1$ $A_2$ $\cdots$ $A_N$
Output
If it is possible to form a sequence $x$ satisfying the condition, print your answer in the following format:
Yes
$x_1$ $x_2$ $\cdots$ $x_N$
If it is impossible, print No.
Sample Input 1
3
3 4 6
Sample Output 1
Yes
3 6 4
For $x=(3,6,4)$, we have:
- $y_1=\operatorname{LCM}(3)=3$
- $y_2=\operatorname{LCM}(3,6)=6$
- $y_3=\operatorname{LCM}(3,6,4)=12$
Here, $y_1<y_2<y_3$ holds.
Sample Input 2
3
2 3 6
Sample Output 2
No
No permutation of $A$ would satisfy the condition.
Sample Input 3
10
922513 346046618969 3247317977078471 4638516664311857 18332844097865861 81706734998806133 116282391418772039 134115264093375553 156087536381939527 255595307440611247
Sample Output 3
Yes
922513 346046618969 116282391418772039 81706734998806133 255595307440611247 156087536381939527 134115264093375553 18332844097865861 3247317977078471 4638516664311857
巧妙地,考虑倒着构造整个序列。
想一下如何选出一个可以排在最后的数,当且仅当他存在某一个质因子的次数是严格最大的。
可以先用 Pollard-Pho 分解出来判断。
每次选一个可以放在最后的元素,不会使本来可以放的数变成不能放。
但是真的要 Pollard-Pho 吗?
枚举 \(10^6\) 以内的数进行分解,那么还没分解出来的要不是两个大质数相乘,要不是一个质数。
对于两个大质数的情况,枚举其他的数,取gcd,如果取出来不是 1 我们就分解出来了,否则可以把这个数当成一个数,不影响性质。
#include<bits/stdc++.h>
using namespace std;
const int N=105,M=N*30;
typedef long long LL;
int p[N][M],c,n,vs[N],st[N];
LL a[N],to[M],b[N];
map<LL,LL>v;
multiset<int>s[M];
LL gcd(LL x,LL y)
{
if(!y)
return x;
return gcd(y,x%y);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",a+i),b[i]=a[i];
for(int j=2;j<=1000000;j++)
{
if(a[i]%j==0)
{
if(!v[j])
to[++c]=j,v[j]=c;
while(a[i]%j==0)
a[i]/=j,p[i][v[j]]++;
}
}
}
for(int i=1;i<=n;i++)
{
if(a[i]==1)
continue;
int fl=0;
for(int j=1;j<=n;j++)
{
LL d=gcd(a[i],a[j]);
if(d^a[i]&&d^1)
{
if(!v[d])
v[d]=++c;
if(!v[a[i]/d])
to[++c]=a[i]/d,v[a[i]/d]=c;
++p[i][v[d]],++p[i][v[a[i]/d]];
fl=1,j=n;
}
}
if(!fl)
{
if(!v[a[i]])
to[++c]=a[i],v[a[i]]=c;
p[i][v[a[i]]]++;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=c;j++)
s[j].insert(p[i][j]);
// for(int i=1;i<=n;i++)
// {
// printf("%lld ",b[i]);
// for(int j=1;j<=c;j++)
// printf("%lld %d\n",to[j],p[i][j]);
// puts("");
//
// }
for(int i=1;i<=n;i++)
{
int pf=0;
for(int j=1;j<=n;j++)
{
if(vs[j])
continue;
int fl=0;
for(int k=1;k<=c;k++)
if(s[k].size()==1||(*(--s[k].end())==p[j][k]&&(*--s[k].end())^(*(--(--s[k].end())))))
fl=1,vs[j]=1,st[i]=j,k=c,pf=1;
if(fl)
{
for(int k=1;k<=c;k++)
s[k].erase(s[k].lower_bound(p[j][k]));
j=n;
}
}
if(!pf)
return puts("No"),0;
}
puts("Yes");
for(int i=n;i>=1;i--)
printf("%lld ",b[st[i]]);
}
[ARC122E] Increasing LCMs的更多相关文章
- ARC 122 简要题解
ARC 122 简要题解 传送门 A - Many Formulae 考虑对于每个数分别算其贡献. 通过枚举该数前面的符号,借助一个非常简单的 \(\mathrm{DP}\)(\(f_{i,0/1}\ ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- git error: unable to rewind rpc post data - try increasing http.postBuffer
error: unable to rewind rpc post data - try increasing http.postBuffererror: RPC failed; curl 56 Rec ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- LintCode-Longest Increasing Subsequence
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Longest Increasing Path in a Matrix -- LeetCode 329
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
随机推荐
- KRPano动态热点专用素材图50多个,加动态热点使用方法
KRPano动态热点专用素材是一种特定形式的序列图,该序列图要求帧的水平和垂直的具体位置必须准确,否则图的动作将会出现错乱,KRPano不支持动态图.目前网上比较匮乏动态热点素材,在此亲手整理制作了5 ...
- Kruskal重构树 学习笔记
Kruskal 重构树 最大生成树将部分内容倒置即可 回顾:Kruskal 基本信息 求解最小生成树 时间复杂度:\(O(m \log m)\) 更适合稀疏图 算法思想 按照边权从小到大排序 依次枚举 ...
- 【遥遥领先】Eolink IDEA 插件:零代码入侵,自动生成接口
省流版: Eolink 有 IDEA 插件吗? 有,而且遥遥领先!我们在一年半之前就发布了,而且功能更丰富! IDEA 插件市场搜索"Eolink Apikit"即可安装使用. 使 ...
- EQ 均衡器
EQ 的全称是 Equalizer,EQ 是 Equalizer 的前两个字母,中文名字叫做"均衡器".最早是用来提升电话信号在长距离的传输中损失的高频,由此得到一个各频带相对平衡 ...
- 服务链路追踪 —— SpringCloud Sleuth
Sleuth 简介 随着业务的发展,系统规模变得越来越大,微服务拆分越来越细,各微服务间的调用关系也越来越复杂.客户端请求在后端系统中会经过多个不同的微服务调用来协同产生最后的请求结果,几平每一个请求 ...
- LUSH & LUXURIOUS
明亮色系Punchy & Bright 明亮.有着强烈对比的颜色更引人注目. 这种大胆的色彩组合要谨慎地利用,所以在明亮色系中的调和色一般用中性色. 其中不同的色彩饱和度,表现出不同的氛围和意 ...
- 第七单元《中国传统文化与管理》单元测试 mooc
第七单元<中国传统文化与管理>单元测试 返回 本次得分为:8.00/10.00, 本次测试的提交时间为:2020-08-30, 如果你认为本次测试成绩不理想,你可以选择 再做一次 . 1 ...
- 在线问诊 Python、FastAPI、Neo4j — 问题咨询
目录 查出节点 拼接节点属性 测试结果 问答演示 通过节点关系,找出对应的节点,获取节点属性值,并拼接成想要的结果. 接上节生成的CQL # 输入 question_class = {'args': ...
- Groovy初学者指南
本文已收录至GitHub,推荐阅读 Java随想录 微信公众号:Java随想录 原创不易,注重版权.转载请注明原作者和原文链接 目录 Groovy & Java Groovy语法 动态类型 元 ...
- Ansible与Ansible部署
Ansible与Ansible部署 Ansible简介: Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩.它融合了众多老牌运维工具的优点,Pubbet ...