CF1858C Yet Another Permutation Problem
CF1858C Yet Another Permutation Problem 题解
题意
给出
思路
先考虑升序的排列:
对于每个
要使其作出不同贡献,
所以,可以先升序填数,判断
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define db double
#define M 100010
#define For(i, j, k) for (int i = j; i <= k; i++)
#define Rep(i, j, k) for (int i = j; i >= k; i--)
#define mem(a, b) memset(a, b, sizeof a)
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define IOS (ios::sync_with_stdio(0), cin.tie(0), cout.tie(0))
int t;
int n;
int ans[M];
bool vis[M];
signed main(){
IOS;
cin >> t;
while(t --){
cin >> n;
int num = 1;
For(i,1,n) vis[i] = false;
For(i,1,n){
while(vis[num]) num ++;//如果已使用,就跳过
ans[i] = num;//初始为最小的可选数
vis[num] = true;
if(i == 1) continue;//注意:如果对1执行操作,只能产生di = 1的贡献,而di = 1所有互质的数都能产生,故不对1进行操作
int j = i;
while(ans[j] * 2 <= n && j < n){//一直往后跳,寻找自己的倍数
j ++;
ans[j] = ans[j - 1] * 2;
vis[ans[j]] = true;
}
num ++,i = j;
}
For(i,1,n) cout << ans[i] << ' ';
cout << endl;
}
return 0;
}