[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 ...
随机推荐
- Three.js中实现碰撞检测
1. 引言 碰撞检测是三维场景中常见的需求,Three.js是常用的前端三维JavaScript库,本文就如何在Three.js中进行碰撞检测进行记述 主要使用到的方法有: 射线法Raycaster ...
- Minio 安装部署
minio 入门 简介 简介 自己看官网 https://min.io/docs/minio/kubernetes/upstream/ 安装部署 (1)Linux服务器安装 minio 可以采用以下几 ...
- Row Major
Smiling & Weeping ----昨天, 别人在我身旁大声说出你的名字, 这对于我, 像从敞开的窗口扔进了一朵玫瑰花. 思路:不客气地说,这是一道令人费解的题目,要求构造一个字符串, ...
- 使用极速全景图下载大师下载720yun全景图片(一键下载建E、720云全景原图)
VR全景图片下载 软件简介 极速全景图下载大师下载地址: 点击进入下载页面 极速全景图下载大师(VR全景图下载器)软件官网: 点击进入官网 极速全景图下载大师如何下载720yun全景图片 1.首先,在 ...
- MindSpore简要性能分析
技术背景 在之前的一篇博客中,我们介绍过MindInsight的安装与使用.不过在前面的文章中,我们主要介绍的是MindInsight与SummaryCollector的配合使用,更多的是用于对结果进 ...
- 我找回了我喜欢的Github Old Feed
前言 这周Github更新了个人主页Feed(指的是用户的活动源或动态源),作为GitHub重度爱好者而言New Feed完全不是我之前所喜欢的效果.虽然说New Feed添加了允许用户可以自定义配置 ...
- Mysql优化篇-索引优化与查询优化
1.索引失败案列 如果查询时没有使用索引,查询语句就会扫描表中所有记录,在数据量大的情况下,查询会很慢. (1)全值匹配 (2)最佳左前缀法则 mysql可以为多个字段创建索引,一个索引可以包括16个 ...
- MySQL实战实战系列 04 深入浅出索引(上)
提到数据库索引,我想你并不陌生,在日常工作中会经常接触到.比如某一个 SQL 查询比较慢,分析完原因之后,你可能就会说"给某个字段加个索引吧"之类的解决方案.但到底什么是索引,索引 ...
- 使用Java统计gitlab代码行数
一.背景: 需要对当前公司所有的项目进行代码行数的统计 二. 可实现方式 1.脚本:通过git脚本将所有的项目拉下来并然后通过进行代码行数的统计 样例: echo 创建项目对应的文件夹 mkdir 项 ...
- vue2.0组件之间传递数据
vue2.0组件之间传递数据 一,父向子 当父组件向子组件传数据的时候用这种方法比较简单.步骤为: 1,在子组件中声明props 2,在父组件中使用子组件时传入数据 二,组件之间 在组件之间如果两个组 ...