Nasser and permutation#12348

Nasser and permutation (Editorial)

Problem Author:

Mohammad Nasser (MOAGEZA)

Prepared by:

Mohammad Nasser (MOAGEZA)

Edited by:

Hasan Saleh (MathModel)

Solution

General Idea: To ensure the GCD of adjacent differences is greater than , all differences must share a common divisor of at least .

The simplest strategy is to construct a permutation where numbers of the same parity are grouped together. For example:

[1,3,5,7,\ldots,2,4,6,8,\ldots]

Inside each parity group, every adjacent difference is even, meaning it is divisible by . Therefore, the GCD of these differences is at least .

The longest possible subsequence we can form this way is simply the size of the larger parity group.

— The number of odd integers from to is .

— The number of even integers is .

Thus, the answer is:

\left\lceil \frac{n}{2} \right\rceil

The solution runs in time per test case.

#include <bits/stdc++.h>
using namespace std;
#define int long long 

void solve(){
    int n;
    cin>>n;
    cout<<(n+1)/2;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin>>t;
    while(t--){
        solve();
        cout<<'\n';
    }
}