QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#674782#7778. Turning PermutationCalculatelove#WA 0ms3948kbC++142.3kb2024-10-25 17:26:502024-10-25 17:26:50

Judging History

你现在查看的是最新测评结果

  • [2024-10-25 17:26:50]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3948kb
  • [2024-10-25 17:26:50]
  • 提交

answer

#include <bits/stdc++.h>

typedef long long s64;

const int N = 55;
const s64 inf = 1000000000000000000;

s64 add(s64 x, s64 y) {
    x += y;
    return x >= inf ? inf : x;
}

int n;
s64 k;

bool exist[N];

int ans[N];

int t, val[N];

s64 f[N][N][2];
s64 pre[N], suf[N];

bool solve(int now) {
    if (now == n + 1) return 1;
    for (int num = 1; num <= n; num ++) {
        if (exist[num]) continue;

        if (num == 1 || num == n || (!exist[num - 1] && !exist[num + 1]) || (exist[num - 1] && exist[num + 1])) {
            exist[num] = 1, ans[now] = num;

            t = 0;
            for (int i = 1; i <= n; i ++)
                if (!exist[i]) val[++ t] = i;
            
            s64 cnt = 0;

            if (t == 0) {
                return solve(now + 1);
            } else if (t == 1) {
                cnt = 1;
            } else {
                int st = 2;

                if (val[1] == 1 && val[2] == 2)
                    st = 3, f[2][1][1] = 1, f[2][2][0] = 1;
                else
                    f[1][1][0] = 1;

                for (int i = st; i <= t; i ++) {
                    int v = val[i];

                    for (int j = 1; j <= i; j ++) f[i][j][0] = f[i][j][1] = 0;

                    pre[0] = 0;
                    for (int j = 1; j <= i - 1; j ++) pre[j] = add(pre[j - 1], f[i - 1][j][1]);

                    suf[i] = 0;
                    for (int j = i - 1; j >= 1; j --) suf[j] = add(suf[j + 1], f[i - 1][j][0]);

                    if (exist[v - 1]) {
                        for (int j = 1; j <= i; j ++)
                            f[i][j][0] = add(pre[i - 1], suf[1]);
                    } else {
                        for (int j = 1; j <= i; j ++)
                            f[i][j][0] = pre[j - 1], f[i][j][1] = suf[j];
                    }
                }

                for (int j = 1; j <= t; j ++) cnt = add(cnt, f[t][j][0] + f[t][j][1]);
            }

            if (k > cnt)
                k -= cnt, exist[num] = 0;
            else
                return solve(now + 1);
        }
    }

    return 0;
}

int main() {
    scanf("%d%lld", &n, &k);

    if (!solve(1)) {
        puts("-1");
    } else {
        for (int i = 1; i <= n; i ++)
            printf("%d ", ans[i]);
        puts("");
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3888kb

input:

3 2

output:

2 1 3 

result:

ok 3 number(s): "2 1 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

3 5

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3756kb

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: 3748kb

input:

4 11

output:

-1

result:

ok 1 number(s): "-1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3948kb

input:

3 1

output:

1 3 2 

result:

ok 3 number(s): "1 3 2"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3688kb

input:

3 10

output:

-1

result:

ok 1 number(s): "-1"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3672kb

input:

3 52

output:

-1

result:

ok 1 number(s): "-1"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

3 756

output:

-1

result:

ok 1 number(s): "-1"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

3 7721

output:

-1

result:

ok 1 number(s): "-1"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3864kb

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: 0ms
memory: 3792kb

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: 0ms
memory: 3748kb

input:

5 85

output:

-1

result:

ok 1 number(s): "-1"

Test #13:

score: 0
Accepted
time: 0ms
memory: 3824kb

input:

5 846

output:

-1

result:

ok 1 number(s): "-1"

Test #14:

score: 0
Accepted
time: 0ms
memory: 3688kb

input:

5 6957

output:

-1

result:

ok 1 number(s): "-1"

Test #15:

score: 0
Accepted
time: 0ms
memory: 3920kb

input:

8 1

output:

1 3 2 5 4 7 6 8 

result:

ok 8 numbers

Test #16:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

8 7

output:

1 3 2 5 7 8 4 6 

result:

ok 8 numbers

Test #17:

score: -100
Wrong Answer
time: 0ms
memory: 3608kb

input:

8 71

output:

-1

result:

wrong answer 1st numbers differ - expected: '1', found: '-1'