CodeForces 1858C J - Yet Another Permutation Problem

  1. 如何构造才能使其最多

我们只需要将 a , a ∗ 2 a,a*2 a,a2,放在一起就能让结果变成最多

我们只需要枚举当前n以内的所有奇数,然后乘上2的次幂次方,就能使的结果不重不漏

由于gcd的特性,d最大值不能超过 n 2 \frac{n}{2} 2n, 该构造方式最大就是 n 2 \frac{n}{2} 2n,达到了我们的理论最大值。

#include 
using namespace std;
typedef long long ll;
void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i += 2)
    {
        for (int j = 1; j * i <= n; j *= 2)
        {
            cout << i * j << " ";
        }
    }
    cout << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

你可能感兴趣的:(算法)