首页 > 其他分享 >[ARC122E] Increasing LCMs

[ARC122E] Increasing LCMs

时间:2023-11-03 20:44:41浏览次数:28  
标签:ARC122E LCM sequence LL Sample cdots Increasing operatorname 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,LCM,sequence,LL,Sample,cdots,Increasing,operatorname,LCMs
From: https://www.cnblogs.com/mekoszc/p/17808470.html

相关文章

  • CF568E Longest Increasing Subsequence 题解
    LongestIncreasingSubsequenceLIS问题有两种主流\(O(n\logn)\)解法,最常见的树状数组法,以及不那么常见的二分法。然后考虑本题,发现一个神奇的思路就是求出LIS后倒序复原出数组。进一步思考后发现,因为本题是LIS(LongestIncreasingSubsequence)而非LNDS(LongestNon-Decr......
  • [ARC130E] Increasing Minimumm
    [ARC130E]IncreasingMinimumm?考虑模拟一下题目中的过程,发现相当于将原序列按照初始值划分为若干个等价类,然后每次都会先合并等价类然后重排等价类中的所有数并加入\(I\)中。这样将\(I\)划分成了若干段。考虑假设我们一共划分出\(T\)个段,那么最终每个数的答案就是\(T\)......
  • E. Increasing Frequency 最大子段和
     题意:给你一个长度为n的数组,再给你一个c,问一次操作后,你最多能让数组中存在多少个c?操作:选择一个区间,对这个区间加上任意整数。做法:那么我们转化一下这个一题,就是要选择一个区间,使得该区间里有一个数,他的数量减去c的数量最大。这个其实就是一个最大子段和,我们数据范围内出现过......
  • [ARC122E] Increasing LCMs
    [ARC122E]IncreasingLCMsAtcoder:[ARC122E]IncreasingLCMs洛谷:[ARC122E]IncreasingLCMsSolution应该意识到这题的核心思想在于构造,想办法将原问题不断划分为子问题。此题策略的证明不算太难,但以我目前的水平肯定不可能靠严密的证明做出这道题。猜,直接把满足条件的数放......
  • Codeforces Round 787 (Div. 3) B. Make It Increasing
    给一个长为\(n\)的数组\(a_1,a_2,\cdots,a_n\quad(0\leqa_i\leq10^9)\)。可以执行以下操作任意次:选择任意一个\(a_i\)并且执行\(a_i=\lfloor\frac{a_i}{2}\rfloor\)。输出最小操作次数,使得数组所有元素变为严格递增。观察:数组一些位置变小,将数组变为严......
  • CF1864A Increasing and Decreasing
    思路首先,给定了一个序列的首项\(a_1\)和末项\(a_n\)以及项数\(n\),要求构造一个严格递增,且差严格递减的序列。因为是构造题,所以可以随便造,考虑差严格递减,所以从后往前构造比较合理。因为严格递增,所以差至少为\(1\),所以\(a_{n-1}\)就构造成\(a_n-1\),\(a_{n-2}\)就构造......
  • [LeetCode][300]longest-increasing-subsequence
    ContentGivenanintegerarraynums,returnthelengthofthelongeststrictlyincreasingsubsequence. Example1:Input:nums=[10,9,2,5,3,7,101,18]Output:4Explanation:Thelongestincreasingsubsequenceis[2,3,7,101],thereforethelengthis4.E......
  • ARC145F Modulo Sum of Increasing Sequences
    为数不多不用多项式科技的单位根反演题。\(A\)不降比较难搞,所以首先令\(B_i=A_i+i-1\),则\(B\)单调递增。转化为对任意的\(k\in[0,\text{MOD}-1]\),求在\([0,N+M-1]\)中选\(N\)个不同的数,总和对\(\text{MOD}\)取模为\(k\)的方案数。记\(p=\text{MOD},n=N+M\)。列出......
  • 950. Reveal Cards In Increasing Order (Medium)
    Description950.RevealCardsInIncreasingOrder(Medium)Youaregivenanintegerarraydeck.Thereisadeckofcardswhereeverycardhasauniqueinteger.Theintegerontheithcardisdeck[i].Youcanorderthedeckinanyorderyouwant.Initially......
  • 【CF1621G】Weighted Increasing Subsequences 题解(优化树状数组)
    CF传送门|LG传送门。优化树状数组+反向处理。Solution发现直接做不好下手。难点主要在求出所有的上升子序列并计算它们分别的贡献。所以需要反向考虑每个单点在什么情况下产生贡献。一个单点会产生多少贡献。一个单点产生贡献的条件很容易得到。一个是在一个上升子序......