QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446310#8527. Power Divisionsucup-team3924WA 1204ms93064kbC++203.6kb2024-06-17 07:20:242024-06-17 07:20:25

Judging History

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

  • [2024-06-17 07:20:25]
  • 评测
  • 测评结果:WA
  • 用时:1204ms
  • 内存:93064kb
  • [2024-06-17 07:20:24]
  • 提交

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 + MOD;
}

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 a, int b) {
    return {a, b};
}

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;
    }

    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);

        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);

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

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());

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

input:

5
2 0 0 1 1

output:

6

result:

ok 1 number(s): "6"

Test #2:

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

input:

1
0

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 4ms
memory: 14944kb

input:

2
1 1

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 4ms
memory: 14960kb

input:

3
2 1 1

output:

3

result:

ok 1 number(s): "3"

Test #5:

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

input:

4
3 2 2 3

output:

4

result:

ok 1 number(s): "4"

Test #6:

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

input:

5
3 4 4 2 4

output:

2

result:

ok 1 number(s): "2"

Test #7:

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

input:

7
3 4 3 5 6 3 4

output:

6

result:

ok 1 number(s): "6"

Test #8:

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

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: 4ms
memory: 15004kb

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: 5ms
memory: 14936kb

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: 3ms
memory: 14936kb

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: 22ms
memory: 15984kb

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: 0
Accepted
time: 113ms
memory: 20404kb

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:

592751350

result:

ok 1 number(s): "592751350"

Test #14:

score: 0
Accepted
time: 704ms
memory: 43212kb

input:

300000
0 5 1 5 5 4 5 3 0 5 0 5 1 4 1 2 2 2 3 0 1 5 4 0 3 1 4 5 2 1 0 3 2 1 2 5 0 2 4 5 0 1 2 1 1 0 0 5 3 0 0 3 4 5 0 2 1 1 1 2 5 1 4 3 1 0 2 0 0 4 3 3 2 5 3 3 1 5 2 0 2 4 3 1 0 3 4 1 3 3 1 0 0 1 1 1 3 1 2 3 5 3 3 2 0 3 0 0 5 5 0 0 0 0 1 4 3 3 4 3 4 5 3 3 5 1 1 4 2 2 1 3 2 1 1 0 0 5 5 0 0 3 2 4 5 5 2...

output:

842503795

result:

ok 1 number(s): "842503795"

Test #15:

score: 0
Accepted
time: 1204ms
memory: 93064kb

input:

300000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

432100269

result:

ok 1 number(s): "432100269"

Test #16:

score: -100
Wrong Answer
time: 1045ms
memory: 59632kb

input:

300000
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 10000...

output:

871462811

result:

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