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的更多相关文章

  1. ARC 122 简要题解

    ARC 122 简要题解 传送门 A - Many Formulae 考虑对于每个数分别算其贡献. 通过枚举该数前面的符号,借助一个非常简单的 \(\mathrm{DP}\)(\(f_{i,0/1}\ ...

  2. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  3. [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 ...

  4. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. 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 ...

  6. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  7. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  8. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  9. LintCode-Longest Increasing Subsequence

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  10. 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 ...

随机推荐

  1. 10、Spring之AOP概述

    10.1.概念 AOP(Aspect Oriented Programming)是一种设计思想,是软件设计领域中的面向切面编程 AOP是面向对象编程(OOP)的一种补充和完善,OOP是纵向继承机制,A ...

  2. C++算法之旅、04 基础篇 | 第一章

    常用代码模板1--基础算法 - AcWing ios::sync_with_stdio(false) 提高 cin 读取速度,副作用是不能使用 scanf 数据输入规模大于一百万建议用scanf 快速 ...

  3. [EasyExcel] 导出合并单元格

    前言 使用spring boot 对excel 进行操作在平时项目中要经常使用.常见通过jxl和poi 的方式进行操作.但他们都存在一个严重的问题就是非常的耗内存.这里介绍一种 Easy Excel ...

  4. Electron创建项目并打包生成exe

    安装nodejs 访问这个网站去下载 http://nodejs.cn/download/ 创建项目 创建项目 git clone https://github.com/electron/electr ...

  5. ionic4请求skynet服务器的资源跨域问题

    最近在做一个后台接口, 顺便用ionic4写了个简单的管理后台, 本来skynet管理后台监听的端口是6666, 但是发现chrome默认对一些接口不友善, 虽然可以通过设置启动参数来解决, 但是还是 ...

  6. 在 Net7.0环境下通过反射创建泛型实例和调用泛型方法

    一.介绍 最近没事干,就用闲暇时间写点东西,也记录一下温习历程.老人说的好,好记性,不如烂笔头.时间一长,当时记忆的再清楚,都会变得模糊,索性就写博客记录下来,如果下次需要,直接打开博客就找到了,不用 ...

  7. 宏观上理解blazor中的表单验证

    概述 表单验证的最终效果大家都懂,这里不阐述了,主要从宏观角度说说blazor中表单验证框架涉及到的类,以及它们是如何协作的,看完这个,再看官方文档也许能更轻松点. blazor中的验证框架分为两部分 ...

  8. Blazor前后端框架Known-V1.2.16

    V1.2.16 Known是基于C#和Blazor开发的前后端分离快速开发框架,开箱即用,跨平台,一处代码,多处运行. Gitee: https://gitee.com/known/Known Git ...

  9. 高效数据管理:Java助力实现Excel数据验证

    摘要:本文由葡萄城技术团队原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 在Java中,开发者可以使用一些开源的库(如Apache POI ...

  10. Spring Boot中发送邮件时,如何让发件人显示别名

    之前,我们通过一系列文章,介绍了如何在Spring Boot中发送邮件: 发送邮件 添加附件 引用静态资源 邮件模版 已经包含了大部分的应用场景.但最近DD在做YouTube中文配音的时候,碰到一个问 ...