QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#391416#8228. 排序djwj233100 ✓2784ms480744kbC++143.1kb2024-04-16 16:16:002024-04-16 16:16:01

Judging History

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

  • [2024-04-16 16:16:01]
  • 评测
  • 测评结果:100
  • 用时:2784ms
  • 内存:480744kb
  • [2024-04-16 16:16:00]
  • 提交

answer

#include <bits/stdc++.h>
#include <bits/extc++.h>

using namespace std;
using namespace __gnu_pbds;

#define fo(v,a,b) for(int v = a; v <= b; v++)
#define fr(v,a,b) for(int v = a; v >= b; v--)
#define cl(a,v) memset(a, v, sizeof(a))

typedef long long ll;

#pragma GCC optimize("Ofast")

const ll P = 1e9 + 7;
const int N = 35, M = 11;

typedef unsigned int ull;
// typedef unsigned long long ull;
const ull B = 131;

int n, m, d[M];

struct value {  int v; ll cnt;  } ;
inline value operator+(const value &a, const value &b) {
    if(a.v == b.v) return {a.v, a.cnt + b.cnt};
    if(a.v < b.v) return b;
    return a;
}

ull pw[N];
inline ull hsh(vector<int> vec) {
    ull sum = 0;
    for(int i = 0; i < (int)vec.size(); i++)
        sum += (vec[i] + 1) * pw[i];
    return sum;
}

__gnu_pbds::gp_hash_table<ull, int> mp[M];
int arr[N], rb[N][N];
void dfs(int id, int x, vector<int> now) {
    if(x == d[id]) {
        if(id == m) {
            if(now[0]) now.push_back(0), mp[id][hsh(now)] = 0;
            return ;
        }
        fo(i, 0, n - 1) arr[i] = ((i / d[id]) < now[i % d[id]]);
        vector<int> nxt; nxt.resize(d[id + 1]);
        fo(i, 0, n - 1) nxt[i % d[id + 1]] += arr[i];
        fo(i, 0, n - 1) {
            arr[i] ^= 1; if(i >= d[id + 1]) arr[i] += arr[i - d[id + 1]];
        }
        ull vNow = hsh(now), vNxt = hsh(nxt);
        fo(i, 0, d[id] - 1) if(now[i]) {
            int pos = i + (now[i] - 1) * d[id], cur = arr[pos];
            mp[id][vNow + (i + 1) * pw[d[id]]] =
                mp[id + 1][vNxt + (pos % d[id + 1] + 1) * pw[d[id + 1]]] + cur;
        }
        return ;
    }
    fo(i, 0, rb[d[id]][x]) {
        now.push_back(i);
        dfs(id, x + 1, now);
        now.pop_back();
    }
}

__gnu_pbds::gp_hash_table<ull, value> dp; value ans;
void calcDP(int cnt, int x, vector<int> now) {
    if(x == d[1]) {
        auto vv = hsh(now);
        if(cnt == 0) return dp[vv] = {0, 1}, void();
        ull vNow = hsh(now);
        fo(i, 0, d[1] - 1) if(now[i]) {
            vector<int> nxt = now; value cur = dp[vNow - pw[i]];
            // nxt[i] += 1, nxt.push_back(i);
            cur.v += mp[1][vNow + (i + 1) * pw[d[1]]];
            dp[vv] = dp[vv] + cur;
        }
        if(cnt == n) ans = dp[vv];
        return ;
    }
    fo(i, 0, rb[d[1]][x]) {
        now.push_back(i);
        calcDP(cnt + i, x + 1, now);
        now.pop_back();
    }
}

int main()
{
    // n = 30, m = 2;
    // d[1] = 10, d[2] = 1;
    // n = 30, m = 4;
    // d[1] = 10, d[2] = 9, d[3] = 5, d[4] = 1;
    scanf("%d%d", &n, &m);
    fo(i, 1, m) scanf("%d", &d[i]), d[i] = min(d[i], n);

    pw[0] = 1; fo(i, 1, n) pw[i] = pw[i - 1] * B;
    fo(u, 1, n) fo(i, 0, u - 1)
        rb[u][i] = n / u - (i >= (n % u)) + 1;

    fr(i, m, 1) dfs(i, 0, {});
    fo(i, 2, m) __gnu_pbds::gp_hash_table<ull, int>().swap(mp[i]);
    calcDP(0, 0, {});

    fo(x, 0, d[1] - 1) {
        int num = n / d[1] - (x >= (n % d[1])) + 1;
        ans.v += num * (num - 1) / 2;
    }
    printf("%d %lld\n", ans.v, ans.cnt);

    return 0;
}
/*
 
*/

详细

Subtask #1:

score: 18
Accepted

Test #1:

score: 18
Accepted
time: 1ms
memory: 3816kb

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

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

input:

5 4
5 4 2 1

output:

6 4

result:

ok 2 number(s): "6 4"

Test #3:

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

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

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: 1ms
memory: 4004kb

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

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

input:

8 1
1

output:

28 1

result:

ok 2 number(s): "28 1"

Subtask #2:

score: 27
Accepted

Dependency #1:

100%
Accepted

Test #7:

score: 27
Accepted
time: 2ms
memory: 4276kb

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 0
Accepted
time: 23ms
memory: 12544kb

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: 23ms
memory: 12624kb

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: 16ms
memory: 11776kb

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

input:

16 4
7 6 2 1

output:

52 9

result:

ok 2 number(s): "52 9"

Subtask #3:

score: 21
Accepted

Dependency #2:

100%
Accepted

Test #12:

score: 21
Accepted
time: 4ms
memory: 5232kb

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

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

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 0
Accepted
time: 169ms
memory: 46592kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 0
Accepted
time: 135ms
memory: 45116kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 0
Accepted
time: 206ms
memory: 61932kb

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: 212ms
memory: 64664kb

input:

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

output:

109 1

result:

ok 2 number(s): "109 1"

Subtask #4:

score: 10
Accepted

Test #18:

score: 10
Accepted
time: 0ms
memory: 5208kb

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

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

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 0
Accepted
time: 865ms
memory: 183872kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 0
Accepted
time: 672ms
memory: 183804kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 0
Accepted
time: 85ms
memory: 31892kb

input:

25 2
8 1

output:

195 8

result:

ok 2 number(s): "195 8"

Subtask #5:

score: 24
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #23:

score: 24
Accepted
time: 2612ms
memory: 429548kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 0
Accepted
time: 2763ms
memory: 480520kb

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: 2784ms
memory: 471520kb

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: 2443ms
memory: 429392kb

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: 2367ms
memory: 429428kb

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: 2776ms
memory: 480744kb

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: 1961ms
memory: 423028kb

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