Submission #730908

#TimeUsernameProblemLanguageResultExecution timeMemory
730908Jarif_Rahman순열 (APIO22_perm)C++17
91.33 / 100
3 ms340 KiB
#include "perm.h"
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const vector<int> primes = {3, 5, 7, 11, 13, 17, 19};

vector<int> gen(ll k){
    k--;

    vector<int> sth;
    int n = 0;

    int P = 0;
    while((1LL<<P)-1 <= k) P++;
    P--;
    k-=(1LL<<P)-1;
    n+=P;

    vector<int> s(P);
    ll ss = 0;

    for(int i = P-1; i > 0; i--){
        int p = 1;
        while((1LL<<p)-1 + ((1LL<<p)-1)*((1LL<<i)-1) <= k) p++;
        p--;
        k-=(1LL<<p)-1 + ((1LL<<p)-1)*((1LL<<i)-1);
        s[i] = p;
        n+=p; 
    }

    while(k > 0){
        int p = 0;
        while((1LL<<p)-1 <= k) p++;
        p--;
        k-=(1LL<<p)-1;
        sth.pb(p);
        n+=p;
    }

    vector<int> ans;
    for(int x: sth){
        for(int i = n-x; i < n; i++) ans.pb(i);
        n-=x;
    }

    vector<int> anss, pp;
    for(int i = n-P; i < n; i++) pp.pb(i);
    n-=P;
    if(!pp.empty()) anss.pb(pp[0]);
    for(int i = 1; i < P; i++){
        while(s[P-i]--) anss.pb(n-1), n--;
        anss.pb(pp[i]);
    }

    ans.insert(ans.end(), anss.begin(), anss.end());

    return ans;
}


vector<int> construct_permutation(ll k){
    auto ans = gen(k);
    if(ans.size() <= 90) return ans;
    int mn = ans.size(), pp = -1;
    for(int p: primes){
        if((k-1)%p != 0) continue;
        k = (k-1)/p+1;
        auto s = gen(k);
        if(s.size()*p < mn) mn = s.size()*p, pp = p;
    }
    
    if(mn == ans.size()) return ans;
    ans.clear();
    k = (k-1)/pp+1;
    auto s = gen(k);
    for(int i = 0; i < pp; i++) for(int j = int(s.size())-1; j >= 0; j--) ans.pb(s[j]+i*s.size());
    reverse(ans.begin(), ans.end());
    return ans;
}

Compilation message (stderr)

perm.cpp: In function 'std::vector<int> gen(ll)':
perm.cpp:25:8: warning: unused variable 'ss' [-Wunused-variable]
   25 |     ll ss = 0;
      |        ^~
perm.cpp: In function 'std::vector<int> construct_permutation(ll)':
perm.cpp:74:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   74 |         if(s.size()*p < mn) mn = s.size()*p, pp = p;
      |            ~~~~~~~~~~~^~~~
perm.cpp:77:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     if(mn == ans.size()) return ans;
      |        ~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...