QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#744924#9738. Make It DivisiblekuguadawangML 1ms3632kbC++234.6kb2024-11-14 00:25:402024-11-14 00:25:41

Judging History

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

  • [2024-11-27 18:44:44]
  • hack成功,自动添加数据
  • (/hack/1263)
  • [2024-11-14 09:10:13]
  • hack成功,自动添加数据
  • (/hack/1178)
  • [2024-11-14 00:25:41]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:3632kb
  • [2024-11-14 00:25:40]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4 + 10;
ll a[N];
ll b[N];
unordered_map<int, int> q;
int mul(int a, int b, int m)
{
    int r = a * b - m * (int)(1.L / m * a * b);
    return r - m * (r >= m) + m * (r < 0);
}
int mypow(int a, int b, int m)
{
    int res = 1 % m;
    for (; b; b >>= 1, a = mul(a, a, m))
    {
        if (b & 1)
        {
            res = mul(res, a, m);
        }
    }
    return res;
}
int B[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
bool MR(int n)
{
    if (n <= 1)
        return 0;
    for (int p : B)
    {
        if (n == p)
            return 1;
        if (n % p == 0)
            return 0;
    }
    int m = (n - 1) >> __builtin_ctz(n - 1);
    for (int p : B)
    {
        int t = m, a = mypow(p, m, n);
        while (t != n - 1 && a != 1 && a != n - 1)
        {
            a = mul(a, a, n);
            t *= 2;
        }
        if (a != n - 1 && t % 2 == 0)
            return 0;
    }
    return 1;
}
int PR(int n)
{
    for (int p : B)
    {
        if (n % p == 0)
            return p;
    }
    auto f = [&](int x) -> int
    {
        x = mul(x, x, n) + 1;
        return x >= n ? x - n : x;
    };
    int x = 0, y = 0, tot = 0, p = 1, q, g;
    for (int i = 0; (i & 255) || (g = __gcd(p, n)) == 1; i++, x = f(x), y = f(f(y)))
    {
        if (x == y)
        {
            x = tot++;
            y = f(x);
        }
        q = mul(p, abs(x - y), n);
        if (q)
            p = q;
    }
    return g;
}
vector<int> fac(int n)
{
#define pb emplace_back
    if (n == 1)
        return {};
    if (MR(n))
        return {n};
    int d = PR(n);
    auto v1 = fac(d), v2 = fac(n / d);
    auto i1 = v1.begin(), i2 = v2.begin();
    vector<int> ans;
    while (i1 != v1.end() || i2 != v2.end())
    {
        if (i1 == v1.end())
        {
            ans.pb(*i2++);
        }
        else if (i2 == v2.end())
        {
            ans.pb(*i1++);
        }
        else
        {
            if (*i1 < *i2)
            {
                ans.pb(*i1++);
            }
            else
            {
                ans.pb(*i2++);
            }
        }
    }
    return ans;
}
vector<int> s;
vector<int> p;
unordered_map<int, int> f;
void dfs(int now, int sum) {
    if (now == s.size()) {
        p.push_back(sum);
        return ;
    }
    for (int i = 0; i <= f[s[now]]; i++) {
        dfs(now + 1, sum);
        sum = sum * s[now];
    }
}
void solve()
{
    q.clear();
    ll n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    ll ss = 0;
    ll need = n - 1;
    for (int i = 1; i < n; i++)
    {
        ll x = a[i], y = a[i + 1];
        if (x > y)
        {
            swap(x, y);
        }
        else if (x == y)
        {
            need--;
        }
        ll add = y - x;
        f.clear();
        p.clear();
        s = fac(add);
        for (auto x: s) {
            f[x]++;
        }
        s.erase(unique(s.begin(), s.end()), s.end());
        dfs(0, 1);
        for (auto j: p) {
            if (j > x && j + add == y + (j - x) && (j - x) <= k) {
                q[j - x]++;
            }
        }
    }
    if (!need)
    {
        cout << k << ' ' << 1ll * k * (k + 1) / 2 << '\n';
        return;
    }
    ll sum = 0, ans = 0;
    for (auto [x, y] : q)
    {
        if (y != need)
            continue;
        for (int i = 1; i <= n; i++)
        {
            b[i] = a[i] + x;
        }
        bool use = 1;
        deque<int> s;
        for (int i = 1; i <= n; i++)
        {
            while (s.size() && s.back() > b[i])
            {
                ll x = b[i], y = s.back();
                if ((x % y) && (y % x))
                {
                    use = 0;
                    break;
                }
                s.pop_back();
            }
            if (s.size())
            {
                ll x = b[i], y = s.back();
                if ((x % y) && (y % x))
                {
                    use = 0;
                    break;
                }
            }
            s.push_back(b[i]);
        }
        if (use)
        {
            sum = sum + x;
            ans++;
        }
    }
    cout << ans << ' ' << sum << '\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}
/*
1 2
2 3


从小到大
20 10 2 10 20
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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
Memory Limit Exceeded

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:


result: