QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#640694#9426. Relearn through ReviewCu_OH_2TL 0ms3544kbC++203.1kb2024-10-14 15:20:302024-10-14 15:20:32

Judging History

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

  • [2024-10-14 15:20:32]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3544kb
  • [2024-10-14 15:20:30]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define int128 __int128_t
const int128 tag = 1;

int prime[12] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
ll qpow(ll a, ll b, ll mod)
{
    ll ans = 1;
    a %= mod;
    for (; b; b >>= 1)
    {
        if (b & 1) ans = (tag * ans * a) % mod;
        a = (tag * a * a) % mod;
    }
    return ans % mod;
}

bool check(ll a, ll p)
{
    ll d = p - 1;
    ll get = qpow(a, d, p);
    if (get != 1) return 1;
    while ((d & 1) ^ 1)
    {
        d >>= 1;
        if ((get = qpow(a, d, p)) == p - 1) return 0;
        else if (get != 1) return 1;
    }

    return 0;
}

bool Miller_Rabin(ll x)
{
    if (x > 40)
    {
        for (int i = 0; i < 12; i++)
            if (check(prime[i], x)) return 0;
        return 1;
    }

    for (int i = 0; i < 12; i++)
        if (x == prime[i]) return 1;
    return 0;
}

vector<ll> factor;
ll pollard_rho(ll n, ll c)
{
    if (n == 4) return 2;
    ll x = rand() % (n - 1) + 1;
    ll y = x;
    x = (tag * x * x + c) % n;
    y = (tag * y * y + c) % n;
    y = (tag * y * y + c) % n;
    for (int lim = 1; x != y; lim = min(lim << 1, 128))
    {
        ll cnt = 1;
        for (int i = 0; i < lim; i++)
        {
            cnt = tag * cnt * abs(x - y) % n;
            if (!cnt) break;
            x = (tag * x * x + c) % n;
            y = (tag * y * y + c) % n;
            y = (tag * y * y + c) % n;
        }
        ll d = __gcd(cnt, n);
        if (d != 1) return d;
    }

    return n;
}

void findFac(ll n)
{
    if (Miller_Rabin(n))
    {
        factor.push_back(n);
        return;
    }
    ll p = n;
    while (p >= n) p = pollard_rho(p, rand() % (n - 1) + 1);
    findFac(p);
    findFac(n / p);
}

void solve()
{
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) cin >> a[i];

    set<ll> s;

    findFac(a.front());
    for (int i = 0; i < (1 << factor.size()); ++i)
    {
        ll res = 1;
        for (int j = 0; j < factor.size(); ++j)
        {
            if (i >> j & 1) res *= factor[j];
        }
        s.insert(res);
    }
    factor.clear();

    findFac(a.back());
    for (int i = 0; i < (1 << factor.size()); ++i)
    {
        ll res = 1;
        for (int j = 0; j < factor.size(); ++j)
        {
            if (i >> j & 1) res *= factor[j];
        }
        s.insert(res);
    }
    factor.clear();

    ll ans = 1;
    for (auto e : s)
    {
        vector<int> v;
        vector<ll> t(a);
        bool f = 1;
        for (int i = 0; i < n; ++i)
        {
            if (a[i] % e && (a[i] + k) % e == 0) v.push_back(i);
        }
        if (v.size())
        {
            int lef = *min_element(v.begin(), v.end());
            int rig = *max_element(v.begin(), v.end());
            for (int i = lef; i <= rig; ++i) t[i] += k;
        }
        for (int i = 0; i < n; ++i)
        {
            if (t[i] % e) f = 0;
        }
        if (f) ans = e;
    }
    ll g = 0;
    for (int i = 0; i < n; ++i) g = __gcd(g, a[i] + k);
    cout << max(g, ans) << '\n';
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

詳細信息

Test #1:

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

input:

2
6 2
5 3 13 8 10 555
3 0
3 6 9

output:

5
3

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

100000
1 608611451460421713
33155506392034032
1 743116173559300609
6138108577573005
7 364454564010802125
657035115675878115 657035115675878115 657035115675878115 657035115675878115 657035115675878115 292580551665075990 657035115675878115
4 316648374341335221
365788422120542814 182894211060271407 731...

output:

641766957852455745
749254282136873614
657035115675878115
182894211060271407
880411769063535667
560553564512176618
183698346865682381
962990836390050009
616597869896951268
878097339332572161
188820994675344528
997057718507559252
949074379610491450
37337367838628559
632093288650732211
3771217139073309...

result: