QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446316#8527. Power Divisionsucup-team3924TL 316ms15952kbC++204.0kb2024-06-17 07:46:522024-06-17 07:46:53

Judging History

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

  • [2024-06-17 07:46:53]
  • 评测
  • 测评结果:TL
  • 用时:316ms
  • 内存:15952kb
  • [2024-06-17 07:46:52]
  • 提交

answer

#include <bits/stdc++.h>

const int MAX_VAL = 1000000;
const int MAX_LG  = 20;

const int MOD = 1000000007;

int add(int a, int b, int m = MOD) {
    if (a + b >= m) return a + b - m;
    return a + b;
}

int sub(int a, int b, int m = MOD) {
    if (a >= b) return a - b;
    return a - b + m;
}

void increment(std::vector<int>& v, int pos, int& msb) {
    v[pos]++;
    while (v[pos] == 2) {
        v[pos] = 0;
        v[pos + 1]++;
        pos++;
    }

    msb = std::max(pos, msb);
}

void clear_bit(std::vector<int>& v, int pos) {
    for (int i = 0; i < MAX_LG; i++)
        v[pos + i] = 0;
}

const int M1 = 269696969;
const int M2 = 769696969;

std::pair<int, int> hash(int v) {
    return {v % M1, v % M2};
}

std::pair<int, int> add_hash(std::pair<int, int> a, std::pair<int, int> b) {
    return {add(a.first, b.first, M1), add(a.second, b.second, M2)};
}

std::pair<int, int> sub_hash(std::pair<int, int> a, std::pair<int, int> b) {
    return {sub(a.first, b.first, M1), sub(a.second, b.second, M2)};
}

std::vector<std::pair<int, int>> phash;

void divide(int l, int r, 
        std::vector<int>& v, 
        std::vector<int>& aux,
        std::vector<int>& msb_table,
        std::vector<std::pair<int, int>>& ranges) {

    if (l == r) {
        ranges.push_back({l, r});
        return;
    }


    int mid = (l + r) / 2;

    divide(l, mid, v, aux, msb_table, ranges);
    divide(mid + 1, r, v, aux, msb_table, ranges);

    auto h = hash(0);
    int msb = 0;

    std::map<std::pair<int, int>, int> left, right;

    // code repetition goes brrr
    for (int i = mid + 1; i <= r; i++) {
        increment(aux, v[i], msb);
        msb_table[i] = msb;
        
        h = add_hash(h, phash[v[i]]);
        right[h] = i;
    }

    for (int i = mid + 1; i <= r; i++) {
        clear_bit(aux, v[i]);
    }

    msb = 0;
    h = hash(0);
    for (int i = mid; i >= l; i--) {
        increment(aux, v[i], msb);
        msb_table[i] = msb;
    
        h = add_hash(h, phash[v[i]]);
        left[h] = i;
    }
    
    std::cerr << "divide: " << l << " / " << mid << " / " << r << "\nmsb_table: ";
    for (int i = l; i <= r; i++)
        std::cerr << msb_table[i] << " ";
    std::cerr << "\n";

    h = hash(0);
    for (int i = mid + 1; i <= r; i++) {
        h = add_hash(h, phash[v[i]]);
        auto adv = sub_hash(phash[msb_table[i] + 1], h);

        std::cerr << "    " << i << " -> " << h.first << " " << h.second << "\n";

        if (left.find(adv) != left.end())
            ranges.push_back({left[adv], i});
    }

    h = hash(0);
    for (int i = mid; i >= l; i--) {
        clear_bit(aux, v[i]);

        h = add_hash(h, phash[v[i]]);
        auto adv = sub_hash(phash[msb_table[i] + 1], h);
        
        std::cerr << "    " << i << " -> " << h.first << " " << h.second << "\n";

        if (right.find(adv) != right.end())
            ranges.push_back({i, right[adv]});
    }

    std::cerr << "\n";
}

int main() {
    std::cin.tie(NULL);
    std::iostream::sync_with_stdio(false);

    int n;
    std::cin >> n;

    std::vector<int> v(1 + n);

    for (int i = 1; i <= n; i++)
        std::cin >> v[i];

    std::vector<std::pair<int, int>> ranges;

    std::vector<int> aux(1 + MAX_VAL + MAX_LG);
    
    phash.resize(1 + MAX_VAL + MAX_LG);

    phash[0] = hash(1);
    for (int i = 1; i < phash.size(); i++)
        phash[i] = add_hash(phash[i - 1], phash[i - 1]);

    std::vector<int> msb_table(1 + n);

    divide(1, n, v, aux, msb_table, ranges);

    std::sort(ranges.begin(), ranges.end());
    ranges.resize(std::unique(ranges.begin(), ranges.end()) - ranges.begin());
    
    for (auto it: ranges)
        std::cerr << it.first << " " << it.second << "\n";

    std::vector<int> dp(1 + n);
    dp[0] = 1;

    for (auto it: ranges) {
        dp[it.second] = add(dp[it.second], dp[it.first - 1]);
    }

    std::cout << dp[n];

    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
2 0 0 1 1

output:

6

result:

ok 1 number(s): "6"

Test #2:

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

input:

1
0

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

2
1 1

output:

2

result:

ok 1 number(s): "2"

Test #4:

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

input:

3
2 1 1

output:

3

result:

ok 1 number(s): "3"

Test #5:

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

input:

4
3 2 2 3

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 5ms
memory: 15072kb

input:

5
3 4 4 2 4

output:

2

result:

ok 1 number(s): "2"

Test #7:

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

input:

7
3 4 3 5 6 3 4

output:

6

result:

ok 1 number(s): "6"

Test #8:

score: 0
Accepted
time: 5ms
memory: 14920kb

input:

10
8 6 5 6 7 8 6 8 9 9

output:

4

result:

ok 1 number(s): "4"

Test #9:

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

input:

96
5 1 0 2 5 5 2 4 2 4 4 2 3 4 0 2 1 4 3 1 2 0 2 2 3 2 4 5 3 5 2 0 2 2 5 3 0 4 5 3 5 4 4 3 1 2 0 5 4 5 0 2 3 2 4 0 0 4 2 0 2 5 3 3 1 5 5 1 1 1 0 5 0 3 0 2 1 1 0 5 0 3 3 4 4 5 3 0 2 2 0 5 4 5 0 5

output:

11332014

result:

ok 1 number(s): "11332014"

Test #10:

score: 0
Accepted
time: 3ms
memory: 14992kb

input:

480
2 0 4 4 1 0 0 3 1 1 4 2 5 5 4 2 1 2 4 4 1 3 4 3 0 5 2 0 2 5 1 0 5 0 0 5 5 0 2 5 2 2 3 1 4 3 5 4 5 2 4 4 4 4 1 4 0 3 4 3 4 1 0 4 3 4 5 4 3 5 0 2 2 0 1 5 4 4 2 0 3 3 3 4 3 0 5 5 3 1 5 1 0 1 0 4 3 0 5 1 4 1 4 3 0 1 3 5 0 3 3 1 0 4 1 1 2 0 1 2 0 3 5 2 0 5 5 5 5 3 5 1 0 2 5 2 2 0 2 0 2 3 5 1 2 1 5 4 ...

output:

506782981

result:

ok 1 number(s): "506782981"

Test #11:

score: 0
Accepted
time: 40ms
memory: 14932kb

input:

2400
0 2 2 0 5 4 3 2 3 2 5 4 5 4 4 5 2 2 4 2 2 0 1 0 5 0 4 4 0 0 5 0 4 0 1 3 4 5 0 3 1 0 4 0 2 5 0 3 3 3 3 1 0 5 5 3 1 3 5 2 4 0 5 0 4 5 4 2 2 1 5 2 2 4 1 0 5 1 5 0 1 2 0 0 3 5 4 0 0 1 1 1 4 2 0 5 1 3 3 5 0 4 4 1 5 5 3 4 4 4 0 2 4 0 5 1 3 1 5 0 5 5 1 3 0 3 1 2 0 1 1 3 5 2 3 4 0 3 0 5 4 0 4 3 5 0 5 2...

output:

586570528

result:

ok 1 number(s): "586570528"

Test #12:

score: 0
Accepted
time: 316ms
memory: 15952kb

input:

12000
2 2 1 2 0 2 5 3 2 0 1 3 2 5 4 0 0 5 3 2 0 2 3 4 3 2 1 4 3 0 3 5 4 1 0 2 4 1 3 2 3 5 0 3 0 0 4 0 4 5 1 0 4 1 1 1 5 4 3 0 3 5 4 5 2 5 0 1 2 3 5 5 2 5 4 2 0 4 4 3 0 0 2 5 0 3 4 2 5 4 2 1 4 5 1 1 2 3 0 3 3 3 3 4 0 5 3 4 0 3 0 2 0 0 2 0 3 4 2 2 0 1 0 5 3 0 2 0 2 2 1 0 5 3 5 4 5 5 0 4 0 4 1 4 4 3 2 ...

output:

201653965

result:

ok 1 number(s): "201653965"

Test #13:

score: -100
Time Limit Exceeded

input:

60000
2 5 0 3 2 3 5 3 5 5 4 1 1 5 3 0 1 1 2 5 5 5 0 3 2 0 3 2 3 3 0 0 1 4 3 1 4 2 3 3 0 5 1 0 1 1 5 5 4 0 5 4 1 3 1 3 5 3 2 4 4 4 5 4 3 2 3 2 4 5 2 0 4 5 1 2 0 4 0 5 1 3 4 1 2 4 1 1 3 3 0 1 1 3 0 0 2 3 3 2 1 4 1 2 4 3 3 5 2 5 3 4 3 0 2 1 1 1 5 1 2 4 2 3 1 2 1 0 2 0 1 1 5 5 3 4 2 5 2 4 5 3 0 5 1 4 2 ...

output:


result: