题解:CF2158F1 Distinct GCDs (Easy Version)
MaxBlazeResFire · · 题解
不难发现,如果能选出
问题转化为构造
复杂度
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 105
int f[MAXN],n,m,edg[MAXN][MAXN],now[MAXN],p2[MAXN],p3[MAXN];
int st[MAXN * MAXN],cnt = 0,ans[MAXN * MAXN],vis[MAXN];
void dfs( int x ){
for( int i = now[x] ; i <= m ; i ++ ){
now[x] = i + 1;
if( edg[x][i] ){
edg[x][i] = edg[i][x] = 0;
dfs( i );
}
}
st[++cnt] = x;
}
inline void solve(){
scanf("%lld",&n);
m = 1; while( f[m] < n ) m ++;
for( int i = 1 ; i <= m ; i ++ )
for( int j = 1 ; j <= m ; j ++ )
if( i != j ) edg[i][j] = 1;
if( m % 2 == 0 ){
for( int i = 1 ; i <= m ; i += 2 )
edg[i][i + 1] = edg[i + 1][i] = 0;
}
for( int i = 1 ; i <= m ; i ++ ) now[i] = 1;
if( m % 2 == 0 ) st[++cnt] = 2;
dfs( 1 );
for( int i = 1 ; i <= m ; i ++ )
for( int j = 1 ; j <= m ; j ++ ) edg[i][j] = now[i] = 0;
int C = 0;
for( int i = 1 ; i <= m ; i ++ ) vis[i] = 0;
for( int i = cnt ; i >= 1 ; i -- ){
int x = st[i];
int v = p2[x - 1] * p3[m - x];
ans[++C] = v; if( !vis[x] ) ans[++C] = v,vis[x] = 1;
}
for( int i = 1 ; i <= n ; i ++ ) printf("%lld ",ans[i]);
for( int i = 1 ; i <= m ; i ++ ) vis[i] = 0;
puts("");
cnt = 0;
}
signed main(){
p2[0] = p3[0] = 1;
for( int i = 1 ; i <= 40 ; i ++ ) p2[i] = p2[i - 1] * 2,p3[i] = p3[i - 1] * 3;
f[1] = 2;
for( int i = 2 ; i <= 80 ; i ++ ){
if( i & 1 ) f[i] = i * ( i - 1 ) / 2 + i + 1;
else f[i] = i * ( i - 1 ) / 2 - i / 2 + 1 + i + 1;
}
int testcase; scanf("%lld",&testcase);
while( testcase -- ) solve();
return 0;
}