QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#280230 | #7778. Turning Permutation | ucup-team1631# | WA | 1ms | 3540kb | C++20 | 3.3kb | 2023-12-09 14:32:40 | 2023-12-09 14:32:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
ll gcd(ll(a), ll(b)) {
if (a == 0)return b;
if (b == 0)return a;
ll c = a;
while (a % b != 0) {
c = a % b;
a = b;
b = c;
}
return b;
}
vll F = { 1,1,2,4,10,32,122,544,2770,15872,101042,707584,
5405530,44736512,398721962,3807514624,38783024290,
419730685952,4809759350882,58177770225664,
740742376475050,9902996106248192,
138697748786275802,2030847773013704704 };
vector<bool> US;
ll multinom(vll& A) {
ll N = A.size();
ll S = 0;
rep(i, N)S += A[i];
ll res = 1;
rep(i, N) {
rep(j, A[i]) {
ll g = gcd(S, j + 1);
ll b = S / g;
ll u = (j + 1) / g;
res /= u;
if (res > (1e18 + b - 1) / b + 10)return 1e18;
res *= b;
if (res > 1e18)return 1e18 + 1;
S--;
}
}
return res;
}
ll cnt() {
vll A;
ll k = 0;
ll res = 1;
ll N = US.size();
rep(i, N) {
if (!US[i])k++;
else if (k != 0) {
A.push_back(k);
ll f = (F[k] + 1) / 2;
if (res > (1e18 + f - 1) / f + 10)return 1e18 + 1;
res *= f;
k = 0;
}
}
if (k != 0) {
A.push_back(k);
ll f = (F[k] + 1) / 2;
if (res > (1e18 + f - 1) / f + 10)return 1e18 + 1;
res *= f;
k = 0;
}
ll pl = multinom(A);
if (pl > 1e18)return 1e18 + 1;
if (res > (1e18 + pl - 1) / pl + 10)return 1e18 + 1;
res *= pl;
return res;
}
void solve(ll N, ll K) {
US.assign(N, false);
vll AN;
rep(i, N) {
ll res = 0;
vector<bool> nextOK(N, 1);
vll pr(N, -1);
vll af(N, N);
rep(j, N) {
if (j != 0)pr[j] = pr[j - 1];
if (US[j])pr[j] = j;
}
for (ll j = N - 1; j >= 0; j--) {
if (j != N - 1)af[j] = af[j + 1];
if (US[j])af[j] = j;
}
rep(j, N) {
if (US[j]) {
nextOK[j] = 0;
}
else {
if (i == 0)continue;
if (pr[j] == -1 && af[j] == N)continue;
if ((pr[j]==-1||(j - pr[j]) % 2 == 0) && ((j - af[j] % 2 == 0)||af[j]==N))continue;
if ((j == 0 || US[j - 1]) && (j == N - 1 || US[j + 1]))continue;
nextOK[j] = 0;
}
}
bool E = 0;
rep(j, N) {
if (US[j])continue;
if (!nextOK[j])continue;
US[j] = 1;
res = cnt();
if (res >= K) {
AN.push_back(j);
E = 1;
break;
}
else {
K -= res;
}
US[j] = 0;
}
if (!E) {
cout << -1 << endl;
return;
}
}
rep(i, N)cout << AN[i] + 1 << " \n"[i == N - 1];
}
int main() {
ll N, K;
cin >> N >> K;
solve(N, K);
// for (ll k = 1; k <= K; k++)cout << k << endl, solve(N, k);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3500kb
input:
3 2
output:
2 1 3
result:
ok 3 number(s): "2 1 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3480kb
input:
3 5
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
4 6
output:
3 1 2 4
result:
ok 4 number(s): "3 1 2 4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
4 11
output:
-1
result:
ok 1 number(s): "-1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
3 1
output:
1 3 2
result:
ok 3 number(s): "1 3 2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3436kb
input:
3 10
output:
-1
result:
ok 1 number(s): "-1"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3424kb
input:
3 52
output:
-1
result:
ok 1 number(s): "-1"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3424kb
input:
3 756
output:
-1
result:
ok 1 number(s): "-1"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
3 7721
output:
-1
result:
ok 1 number(s): "-1"
Test #10:
score: 0
Accepted
time: 1ms
memory: 3476kb
input:
5 1
output:
1 3 2 5 4
result:
ok 5 number(s): "1 3 2 5 4"
Test #11:
score: 0
Accepted
time: 1ms
memory: 3504kb
input:
5 8
output:
2 4 1 3 5
result:
ok 5 number(s): "2 4 1 3 5"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3416kb
input:
5 85
output:
-1
result:
ok 1 number(s): "-1"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
5 846
output:
-1
result:
ok 1 number(s): "-1"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
5 6957
output:
-1
result:
ok 1 number(s): "-1"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
8 1
output:
1 3 2 5 4 7 6 8
result:
ok 8 numbers
Test #16:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
8 7
output:
1 3 2 5 7 8 4 6
result:
ok 8 numbers
Test #17:
score: -100
Wrong Answer
time: 1ms
memory: 3476kb
input:
8 71
output:
-1
result:
wrong answer 1st numbers differ - expected: '1', found: '-1'