B. Big Vova(暴力+贪心)Codeforces Round #669 (Div. 2)
原题链接: https://codeforces.com/contest/1407/problems
测试样例
input
7
2
2 5
4
1 8 2 3
3
3 8 9
5
64 25 75 100 50
1
42
6
96 128 88 80 52 7
5
2 4 8 16 17
output
5 2
8 2 1 3
9 3 8
100 50 25 75 64
42
128 96 80 88 52 7
17 2 4 8 16
Note
In the first test case of the example, there are only two possible permutations b — [2,5] and [5,2]: for the first one c=[2,1], for the second one c=[5,1].
In the third test case of the example, number 9 should be the first in b, and GCD(9,3)=3, GCD(9,8)=1, so the second number of b should be 3.
In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation b.
题意: 说实话,这个题目我还是不太懂,暂且按我的理解来说吧(有错误请指正),给定一个数组
a
a
a,你可以对该数组进行任意排列,请你找出这样的数组
b
b
b,使得产生的数组
c
c
c是所有排列中产生字典序最大的。其中
c
i
=
g
c
d
(
b
1
⋅
⋅
⋅
⋅
b
i
)
c_i=gcd(b_1····b_i)
ci=gcd(b1⋅⋅⋅⋅bi)。
解题思路: 我们要使得字典序最大,就要尽可能使得
c
i
c_i
ci最大,而
c
i
=
g
c
d
(
b
1
⋅
⋅
⋅
⋅
b
i
)
c_i=gcd(b_1····b_i)
ci=gcd(b1⋅⋅⋅⋅bi),故不管怎样,
i
i
i越大,
c
i
c_i
ci就越小,所以我们必须要让前面的
c
i
c_i
ci都尽可能大。 那么自然可以知道,把具有最大公约数的放前面。(
i
i
i为1的时候自然是放数组中元素最大的),接下来我们就是要以前面的最大公约数为参考去得到接下来的最大公约数,故我们每轮都要保存最大公约数的值。为了之前确定了的不参加计算,所以我们要开一个used数组来判断元素是否已加入
b
b
b数组。OK,我们具体看代码。
AC代码
/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h> //POJ不支持
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e3+5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t,n;
int a[maxn];
bool used[maxn];
int GCD(int n,int m){
int temp;
while(n%m){
temp=n%m;
n=m;
m=temp;
}
return m;
}
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n;
rep(i,0,n-1)
cin>>a[i];
memset(used,false,sizeof(used));
int pos,gcd=0;
//注意0和其他数的最大公约数为其本身。
rep(i,0,n-1){
pos=-1;
rep(j,0,n-1){
if(!used[j]){
if(pos==-1||GCD(gcd,a[j])>GCD(gcd,a[pos])){
//比较得到最大的ci。
pos=j;
}
}
}
//更新最大公约数,去计算接下来的数。
gcd=GCD(gcd,a[pos]);
cout<<a[pos]<<" ";
used[pos]=true;
}
cout<<endl;
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)