# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
657786 | iomoon191 | Permutation (APIO22_perm) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "perm.h"
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(),(a).end()
const int N=1e6+5;
//const ll mod=1e9+7;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
vector<ll> sub_1(ll k)
{
vector<ll> vt(k-1);
iota(all(vt),0ll);
reverse(all(vt));
return vt;
}
vector<ll> construct_permutation(ll k)
{
// if (k<=90)
// return sub_1(k);
if (k==1ll)
return {};
if (k==2ll)
return {0ll};
if (k==3ll)
return {1ll,0ll};
vector<ll> vt=construct_permutation(k/4ll);
ll sz=(ll)vt.size();
if (k%4==0)
{
vt.pb(sz);
vt.pb(sz+1);
return vt;
}
if (k%4==1)
{
vt.pb(sz);
vt.pb(sz+1);
vt.pb(-1ll);
for (auto &to:vt)
++to;
return vt;
}
if (k%4==2)
{
vt.pb(sz);
vt.pb(-1ll);
vt.pb(sz+1);
for (auto &to:vt)
++to;
//for (auto &o:vt) cout<<o<<' ';
return vt;
}
vector<ll> vT=vt;
vt.pb(sz);
vt.pb(sz+1);
for (auto &to:vt)
{
if (to>1)
to++;
}
vt.pb(2);
vector<ll> f((ll)vt.size());
ll sum=1;
for (ll i=0; i<(ll)vt.size(); i++)
{
f[i]=1;
for (ll j=0; j<i; j++)
{
if (vt[j]<vt[i])
{
f[i]+=f[j];
f[i]=min(f[i],LLONG_MAX);
}
}
sum+=f[i];
sum=min(sum,LLONG_MAX);
//cout<<sum<<endl;
}
if (sum==k)
return vt;
for (auto &to:vT)
{
//cout<<to<<' ';
//cout<<endl;
to+=2;
}
vT.pb(sz+2);
vT.pb(1ll);
vT.pb(sz+3);
vT.pb(0ll);
return vT;
}
/*
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen("t.inp","r"))
{
freopen("teddy.inp","r",stdin);
freopen("teddy.out","w",stdout);
}
int tt;
cin>>tt;
while (tt--)
{
ll k;
cin>>k;
for (auto &to:construct_permutation(k))
cout<<to<<' ';
cout<<endl;
}
}
*/