QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358736#8285. Shell SortPPP#AC ✓1956ms11352kbC++175.0kb2024-03-19 23:20:172024-03-19 23:20:17

Judging History

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

  • [2024-03-19 23:20:17]
  • 评测
  • 测评结果:AC
  • 用时:1956ms
  • 内存:11352kb
  • [2024-03-19 23:20:17]
  • 提交

answer

//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
typedef long double ld;
const int maxN = 35;
const int maxM = 15;
int n, m;
int d[maxM];
const int mod = (int)1e9 + 7;
int sum(int a, int b) {
    int s = a + b;
    if (s >= mod) s -= mod;
    return s;
}
int sub(int a, int b) {
    int s = a - b;
    if (s < 0) s += mod;
    return s;
}
int mult(int a, int b) {
    return (1LL * a * b) % mod;
}
int ini_array[maxN];
int cnt_chain[15];
int after[maxM][maxN];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> d[i];
    }
    vector<vector<int>> chains(d[0]);
    for (int z = 0; z < d[0]; z++) {
        for (int x = z; x < n; x += d[0]) {
            chains[z].emplace_back(x);
        }
    }
    int add = 0;
    for (int x = 0; x < d[0]; x++) {
        add += (chains[x].size() * (chains[x].size() - 1)) / 2;
    }
//    cout << add << " ????? " << endl;
    int states = 1;
    for (int x = 0; x < d[0]; x++) {
        states *= (chains[x].size() + 1);
    }
//    cout << states << " ??? " << endl;
    vector<pair<int,int>> P(states, make_pair(-1, 0));
    vector<int> suf_prod(d[0]);
    suf_prod[0] = 1;
    for (int i = 1; i < d[0]; i++) {
        suf_prod[i] = suf_prod[i - 1] * (chains[i - 1].size() + 1);
    }
    P[0] = {add, 1};
//    cout << " gg " << endl;
    for (int i = 0; i < states; i++) {
        int cur = i;
        for (int x = 0; x < d[0]; x++) {
            cnt_chain[x] = (cur % (chains[x].size() + 1));
            cur /= (chains[x].size() + 1);
        }
        for (int u = 0; u < d[0]; u++) {
            for (int g = 0; g < cnt_chain[u]; g++) {
//                ini_array[chains[u][g]] = 0;
                after[0][chains[u][g]] = 0;
            }
            for (int g = cnt_chain[u]; g < (int)chains[u].size(); g++) {
//                ini_array[chains[u][g]] = 2;
                after[0][chains[u][g]] = 2;
            }
        }
        for (int u = 1; u < m; u++) {
            int f = d[u];
            for (int z = 0; z < n; z++) after[u][z] = after[u - 1][z];
            for (int x = 0; x < f; x++) {
                for (int Y = x; Y < n; Y += f) {
                    for (int Z = Y + f; Z < n; Z += f) {
                        if (after[u][Y] > after[u][Z]) {
                            swap(after[u][Y], after[u][Z]);
                        }
                    }
                }
            }
        }
        for (int x = 0; x < d[0]; x++) {
            if (cnt_chain[x] < (int)chains[x].size()) {
                /*for (int u = 0; u < d[0]; u++) {
                    for (int g = 0; g < cnt_chain[u]; g++) {
                        ini_array[chains[u][g]] = 0;
                    }
                    for (int g = cnt_chain[u]; g < (int)chains[u].size(); g++) {
                        ini_array[chains[u][g]] = 2;
                    }
                }*/
//                ini_array[chains[x][cnt_chain[x]]] = 1;
                int new_state = suf_prod[x] + i;
                int T = 0;
                int WHERE = chains[x][cnt_chain[x]];
                for (int u = 1; u < m; u++) {
                    int f = d[u];
                    /*for (int p = 0; p < f; p++) {
                        for (int X = p; X < n; X += f) {
                            for (int Y = X + f; Y < n; Y += f) {
                                if (ini_array[X] > ini_array[Y]) {
                                    if (ini_array[X] == 1 && ini_array[Y] == 0) {
                                        T += 1;
                                    }
                                    swap(ini_array[X], ini_array[Y]);
                                }
                            }
                        }
                    }*/

                    for (int p = WHERE + f; p < n; p += f) {
                        if (after[u - 1][p] == 0) T += 1;
                    }

                    int S = WHERE % f;
                    int qq = 0;
                    for (int X = S; X < n; X += f) {
                        if (after[u][X] == 0) qq += 1;
                    }
                    WHERE = qq * f + S;
//                    for (int z = S; z < n; z += f) {
//                        cout << after[u][z] << " ";
//                    }
//                    cout << endl;
//                    assert(WHERE < n);
                }
                if (P[new_state].first < P[i].first + T) {
                    P[new_state] = {P[i].first + T, P[i].second};
                }
                else if (P[new_state].first == P[i].first + T) {
                    P[new_state].second = sum(P[new_state].second, P[i].second);
                }
            }
        }
    }
    cout << P.back().first << " " << P.back().second << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

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

input:

5 4
5 4 2 1

output:

6 4

result:

ok 2 number(s): "6 4"

Test #3:

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

input:

8 4
6 3 2 1

output:

15 4

result:

ok 2 number(s): "15 4"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3472kb

input:

8 6
8 7 5 4 2 1

output:

14 2

result:

ok 2 number(s): "14 2"

Test #5:

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

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

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

input:

8 1
1

output:

28 1

result:

ok 2 number(s): "28 1"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3540kb

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 0
Accepted
time: 9ms
memory: 3680kb

input:

16 8
10 9 8 7 6 5 4 1

output:

57 5

result:

ok 2 number(s): "57 5"

Test #9:

score: 0
Accepted
time: 9ms
memory: 3556kb

input:

16 10
10 9 8 7 6 5 4 3 2 1

output:

57 3

result:

ok 2 number(s): "57 3"

Test #10:

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

input:

16 7
10 9 8 6 5 4 1

output:

49 1

result:

ok 2 number(s): "49 1"

Test #11:

score: 0
Accepted
time: 2ms
memory: 3516kb

input:

16 4
7 6 2 1

output:

52 9

result:

ok 2 number(s): "52 9"

Test #12:

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

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

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

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 0
Accepted
time: 70ms
memory: 3964kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 0
Accepted
time: 82ms
memory: 3820kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 0
Accepted
time: 82ms
memory: 3988kb

input:

22 6
10 9 8 7 3 1

output:

97 1

result:

ok 2 number(s): "97 1"

Test #17:

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

input:

22 10
10 9 8 7 6 5 4 3 2 1

output:

109 1

result:

ok 2 number(s): "109 1"

Test #18:

score: 0
Accepted
time: 2ms
memory: 3632kb

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

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

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 0
Accepted
time: 228ms
memory: 7012kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 0
Accepted
time: 174ms
memory: 6372kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 0
Accepted
time: 31ms
memory: 3952kb

input:

25 2
8 1

output:

195 8

result:

ok 2 number(s): "195 8"

Test #23:

score: 0
Accepted
time: 807ms
memory: 11296kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 0
Accepted
time: 1652ms
memory: 11352kb

input:

30 9
10 9 8 7 6 5 4 3 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #25:

score: 0
Accepted
time: 1641ms
memory: 11292kb

input:

30 8
10 9 8 7 4 3 2 1

output:

154 1

result:

ok 2 number(s): "154 1"

Test #26:

score: 0
Accepted
time: 1591ms
memory: 11292kb

input:

30 8
10 8 7 6 5 4 3 1

output:

155 1

result:

ok 2 number(s): "155 1"

Test #27:

score: 0
Accepted
time: 1285ms
memory: 11288kb

input:

30 6
10 8 6 4 3 1

output:

150 4

result:

ok 2 number(s): "150 4"

Test #28:

score: 0
Accepted
time: 1956ms
memory: 11352kb

input:

30 10
10 9 8 7 6 5 4 3 2 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #29:

score: 0
Accepted
time: 872ms
memory: 9244kb

input:

29 6
10 9 7 5 3 1

output:

129 200

result:

ok 2 number(s): "129 200"

Extra Test:

score: 0
Extra Test Passed