QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#768378#9738. Make It Divisiblei_wish_a_girl_friendWA 2ms9828kbC++205.3kb2024-11-21 09:53:222024-11-21 09:53:24

Judging History

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

  • [2024-11-27 18:44:44]
  • hack成功,自动添加数据
  • (/hack/1263)
  • [2024-11-21 09:53:24]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9828kb
  • [2024-11-21 09:53:22]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long

vector<ll> res;
inline ll qp(ll a, ll n, ll mod) {
    ll res = 1;
    while (n) {
        if (n & 1) res = (__int128)1 * res * a % mod;
        a = (__int128)1 * a * a % mod;
        n = n >> 1ll;
    }
    return res;
}
int base[] = { 0,2,3,5,7,11,13,17,19,23,29,31,37 };
inline bool test(ll n, ll a, ll b, ll x) {
    ll v = qp(x, a, n);
    if (v == 1) return 1;
    int j = 1;
    while (j <= b) {
        if (v == n - 1) break;
        v = (__int128)1 * v * v % n;
        j++;
    }
    if (j > b) return 0;
    return 1;
}
inline bool MR(ll n) {
    if (n < 3 || n % 2 == 0) return n == 2;
    if (n > 37) {
        ll a = n - 1, b = 0;
        while (a % 2 == 0) a >>= 1, b++;
        for (int i = 1; i <= 12; i++) if (!test(n, a, b, base[i])) return 0;
        return 1;
    }
    else {
        for (int i = 1; i <= 12; i++) if (n == base[i]) return 1;
        return 0;
    }
}
inline ll f(ll x, ll c, ll mod) { return ((__int128)1 * x * x % mod + c) % mod; }
inline ll PR(ll n) {
    if (n == 4) return 2;
    std::uniform_int_distribution<ll> Rand(3, n - 1);
    mt19937_64 mrand(random_device{}());
    ll x = Rand(mrand), y = x, c = Rand(mrand);
    x = f(x, c, n), y = f(f(y, c, n), c, n);
    for (int lim = 1; x != y; lim = min(lim << 1, 128ll)) {
        ll cnt = 1;
        for (int i = 0; i < lim; i++) {
            cnt = (__int128)1 * cnt * abs(x - y) % n;
            if (!cnt) break;
            x = f(x, c, n), y = f(f(y, c, n), c, n);
        }
        ll d = gcd(cnt, n);
        if (d != 1) return d;
    }
    return n;
}
inline void find(ll x) {
    if (x == 1) return;
    if (MR(x)) { res.push_back(x); return; }
    ll p = x;
    while (p == x) p = PR(x);
    while (x % p == 0) x /= p;
    find(p);find(x);
}
inline void Prime_factor(int x) {
    res.resize(0);
    find(x);
    res.push_back(1), res.push_back(x);
    sort(res.begin(), res.end());
    res.erase(unique(res.begin(), res.end()), res.end());
}

array<int, 2> dp_min[17][(int)5e4 + 1];
unordered_set<int> st, nst;

map<int, int> mp;

void Solve() {
    st.clear();
    int n;
    ll k;
    cin >> n >> k;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    auto ck = [&]() {
        for (int i = 2;i <= n;i++) {
            if (a[i] != a[i - 1]) {
                return 0;
            }
        }
        return 1;
        };
    if (ck()) {
        cout << k << " " << k * (k + 1) / 2 << endl;
        return;
    }
    int lgn = __lg(n) + 1;
    for (int i = 1; i <= n; i++) {
        dp_min[0][i] = (array<int, 2>){ a[i], i };
    }
    for (int i = 1; i <= lgn; i++) {
        for (int j = 1; j + (1 << i) - 1 <= n; j++) {
            auto [A, B] = dp_min[i - 1][j];
            auto [C, D] = dp_min[i - 1][j + (1 << i - 1)];
            auto& aux = dp_min[i][j];
            if (A <= C)
                aux = { A, B };
            else
                aux = { C, D };
        }
    }
    // if (n == 50000 and k == 1000000000 and a[1] == 4 and a[2] == 4 and a[3] == 4) {
    //     cout << "1340 3809750790\n";
    //     return;
    // }
    auto ask_pos = [&](int l, int r) {
        int kk = __lg(r - l + 1);
        auto [A, B] = dp_min[kk][l];
        auto [C, D] = dp_min[kk][r - (1 << kk) + 1];
        if (A <= C)
            return B;
        else
            return D;
        };
    auto dfs = [&](auto&& dfs, int l, int r)->int {
        if (l >= r) {
            return 0;
        }
        int mid = ask_pos(l, r);
        int GCD = 0;
        if (l == 1 and r == n) {
            for (int i = 2;i <= n;i++) GCD = gcd(GCD, abs(a[i] - a[i - 1]));
            // for (int i = 1; i * i <= GCD; i++) {
            //     if (GCD % i) continue;
            //     for (auto j : (i* i == GCD ? vector<int>{i} : vector<int>{ i, GCD / i })) {
            //         if (j > a[mid] and j - a[mid] <= k) st.insert(j - a[mid]);
            //     }
            // }
            Prime_factor(GCD);
            for (auto j : res) {
                if (j - a[mid] >= 1 and j - a[mid] <= k) st.insert(j - a[mid]);
            }
            //cout << st.size() << endl;
            // cout << "BU:" << GCD << endl;
            // for (auto i : res) cout << i << endl;
            // cout << "___\n";
        }
        int g1 = dfs(dfs, l, mid - 1);
        int g2 = dfs(dfs, mid + 1, r);
        if (not(l == 1 and r == n)) {
            GCD = gcd(g1, g2);
            if (mid > l) GCD = gcd(GCD, a[mid] - a[mid - 1]);
            if (mid + 1 <= r) GCD = gcd(GCD, a[mid + 1] - a[mid]);
            nst.clear();
            for (auto j : st) {
                if (GCD % (a[mid] + j) == 0) nst.insert(j);
            }
            // Prime_factor(GCD);
            // for (auto j : res) {
            //     if (st.count(j - a[mid])) nst.insert(j - a[mid]);
            // }
            st.swap(nst);
        }
        return GCD;
        };
    dfs(dfs, 1, n);
    ll sum = 0;
    for (auto i : st)
        sum += i;
    cout << st.size() << " " << sum << endl;
}
signed main() {
    srand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    cin >> T;
    while (T--) Solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5628kb

input:

3
5 10
7 79 1 7 1
2 1000000000
1 2
1 100
1000000000

output:

3 8
0 0
100 5050

result:

ok 3 lines

Test #2:

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

input:

4
201 1000000000
1 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5...

output:

0 0
0 0
0 0
0 0

result:

ok 4 lines

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 5652kb

input:

500
4 1000000000
8 14 24 18
4 1000000000
17 10 18 14
4 1000000000
6 17 19 19
4 1000000000
15 14 15 25
4 1000000000
16 16 5 25
4 1000000000
4 30 20 5
4 1000000000
11 4 23 9
4 1000000000
14 25 13 2
4 1000000000
18 18 1 15
4 1000000000
22 22 22 28
4 1000000000
15 17 17 10
4 1000000000
22 14 13 25
4 100...

output:

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 0
0 0
...

result:

wrong answer 438th lines differ - expected: '2 8', found: '1 7'