QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#104138#6405. Barkleythomas_liWA 3ms3484kbC++171.5kb2023-05-09 03:46:422023-05-09 03:46:45

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-09 03:46:45]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3484kb
  • [2023-05-09 03:46:42]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int MM = 1e5+5,LG = 20;
int n,q; int64_t t[MM][LG]; // store gcd of interval [i,i+2^j)
int64_t query(int a, int b){
    if(a > b) return 0;
    b++;
    int dep = 31-__builtin_clz(b-a);
    return gcd(t[a][dep],t[b-(1<<dep)][dep]);
}
int64_t go(int l, int r, int k){
    if(l > r) return k ? 1 : 0;
    if(k == 0) return query(l,r);
    if(k == r-l+1) return 0;
    // find change points
    int p = l;
    vector<int> ch = {l};
    while(1){
        // find first value where gcd changes  
        int u = p; int64_t st = t[p][0];
        for(int j = LG-1; j >= 0; j--) if(u+(1<<j)-1 <= r){
            if(t[u][j] == st){
                u += (1<<j)-1;
            }
        }
        u++;
        if(u > r) break;
        ch.push_back(u); 
        p = u;
    }
    int64_t bst = 0;
    for(int x : ch){
        bst = max(bst,gcd(query(l,x-1),go(x+1,r,k-1)));
    }
    return bst;
}
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> q;
    for(int i = 1; i <= n; i++) cin >> t[i][0];
    for(int j = 1; j < LG; j++) for(int i = 1; i+(1<<j)-1 <= n; i++) t[i][j] = gcd(t[i][j-1],t[i+(1<<(j-1))][j-1]);
    while(q--){
        int l,r,k; cin >> l >> r >> k;
        cout << go(l,r,k) << "\n";
    }
}
// for k = 1, check all change points
// for k = 2, check first, and then find remaining change points: 60^2 log N
// for k = 3, check first, find remaining, find remaining 60^3 log N

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 3440kb

input:

4 4
3 2 6 4
1 3 1
2 4 1
1 4 2
1 4 3

output:

3
2
3
6

result:

ok 4 number(s): "3 2 3 6"

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3484kb

input:

100 10000
7 25 33 17 13 33 24 29 11 1 3 19 2 20 33 23 14 24 15 12 3 1 5 13 6 35 15 21 10 34 31 19 7 33 17 26 26 1 19 21 31 5 29 20 18 32 19 18 19 31 11 26 6 19 2 26 23 1 4 2 31 21 29 30 1 14 20 23 14 32 4 34 13 29 5 26 24 29 28 5 26 26 21 19 2 33 2 31 30 3 23 24 26 32 36 21 21 11 5 9
56 57 1
90 97 1...

output:

26
1
1
1
1
1
1
1
31
1
1
1
1
1
26
1
1
1
1
1
1
29
1
1
1
1
1
1
4
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
1
1
21
1
1
1
1
1
19
1
1
1
21
1
1
1
1
1
1
1
1
1
1
1
1
1
3
1
1
1
1
1
1
1
1
1
1
4
1
1
1
1
1
3
1
2
1
26
1
1
1
1
1
1
1
7
1
1
1
33
1
1
1
1
1
1
2
1
26
1
1
1
2
1
1
1
1
1
1
26
1
1
1
1
31
1
1
2
1
4
29
1
2
1
1...

result:

wrong answer 7823rd numbers differ - expected: '2', found: '1'