3 条题解

  • 1
    @ 2023-4-26 23:01:16

    P3868 [TJOI2009] 猜数字

    题意:

    求最小的 nNn\in \mathbb{N},满足对于 i[1,k]\forall i\in [1,k],有 bi(nai)b_i | (n-a_i)

    思路:

    由于bi(nai)b_i | (n-a_i) 因此nn modmod bib_i == aia_i 因此这是一道exCRT的模板题 当然因为这个题保证bib_i的数两两互质 CRT也可以做

    参考代码(exCRT封装):

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define endl '\n'
    #define ll __int128
    const int N = 15;
    int k, n, t;
    int _a[N], _b[N];
    namespace exCRT
    {
        ll a, b, A, B, x, y;
        void exGCD(ll a, ll b, ll &x, ll &y)
        {
            if (b == 0)
            {
                x = 1, y = 0;
                return;
            }
            exGCD(b, a % b, x, y);
            t = x, x = y, y = t - a / b * y;
        }
        ll gcd(ll x, ll y)
        {
            return y == 0 ? x : gcd(y, x % y);
        }
        ll lcm(ll x, ll y)
        {
            return x / gcd(x, y) * y;
        }
        inline void solve()
        {
            exGCD(a, A, x, y);
            ll del = B - b, g = gcd(a, A);
            x = (x * del / g + (A / g)) % (A / g);
            ll mod = lcm(a, A);
            b = (a * x + b + mod) % mod;
            a = mod;
        }
        void main()
        {
            for (int i = 1; i <= n; i++)
            {
                A = _a[i], B = _b[i];
                if (i > 1)
                    solve();
                else
                    a = A, b = B;
            }
            cout << (int)(b % a) << endl;
        }
    }
    signed main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> _b[i];
        for (int i = 1; i <= n; i++)
            cin >> _a[i];
        exCRT::main();
    }
    

    信息

    ID
    1261
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    15
    已通过
    13
    上传者