分类: 数论

  • 洛谷1414 又是毕业季II

    沉迷做水题了qwq..

    很简单的一道数学题。

    首先把所有的数的约数处理出来,然后统计一下每个约数$d$的出现次数$count(d)$.

    然后,对于一个询问$k$,我们只要在出现次数大于等于$k$的约数中输出最大的即可.

    证明:假设一个数$d$作为约数出现了$k$次,那我一定可以选出$k$个数使得他们的公约数是$d$.

    然后最大化这个约数即可。

    求约数直接上的$O(\sqrt N)$的暴力。

    细节见代码。

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using int_t = long long int;
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 1000000;
    
    int_t times[LARGE + 1];
    int_t n;
    void factors(int_t x)
    {
        int_t i;
        for (i = 1; i * i <= x; i++)
        {
            if (x % i == 0)
            {
                times[i]++;
                if (x / i != i)
                    times[x / i]++;
            }
        }
    }
    struct Pair
    {
        int_t count;
        int_t number;
        Pair(int_t count, int_t number)
        {
            this->count = count;
            this->number = number;
        }
        operator int_t()
        {
            return count;
        }
        bool operator<(const Pair &x)
        {
            return count < x.count;
        }
    };
    std::vector<Pair> divisors;
    int main()
    {
        cin >> n;
        int_t max = 0;
        for (int_t i = 1; i <= n; i++)
        {
            int_t x;
            cin >> x;
            factors(x);
            max = std::max(x, max);
        }
        //出现次数为i的约数最大是多少
        static int_t counter[LARGE + 1];
        std::fill(counter + 1, counter + 1 + n, -1);
        for (int_t i = 1; i <= max; i++)
        {
            if (times[i] != 0)
            {
                counter[times[i]] = std::max(i, counter[times[i]]);
            }
        }
        for (int_t i = 1; i <= n; i++)
        {
            if (counter[i] != -1)
            {
                divisors.push_back(Pair(i, counter[i]));
            }
        }
        std::sort(divisors.begin(), divisors.end());
    
        for (int_t i = divisors.size() - 2; i >= 0; i--)
        {
            auto &x = divisors[i];
            x.number = std::max(x.number, divisors[i + 1].number);
        }
        for (int_t i = 1; i <= n; i++)
        {
            auto &curr = *std::lower_bound(divisors.begin(), divisors.end(), i);
            cout << curr.number << endl;
        }
        return 0;
    }

     

  • 清华集训2012 模积和

    真的是有史以来做过的最简单的luogu标签为省选/NOI-难度的题目..

    $$ a\ mod\ b=a-\lfloor \frac{b}{a} \rfloor a \\ \text{设}f\left( n \right) =\sum_{1\le i\le n}{\left( n-\lfloor \frac{n}{i} \rfloor i \right)}=n^2-\sum_{1\le i\le n}{\lfloor \frac{n}{i} \rfloor i} \\ \text{可以数论分块}O\left( \sqrt{n} \right) \text{计算} \\ \\ \text{设}g\left( n,m \right) =\sum_{1\le i\le \min \left( n,m \right)}{\left( n-\lfloor \frac{n}{i} \rfloor i \right) \left( m-\lfloor \frac{m}{i} \rfloor i \right)} \\ \sum_{1\le i\le \min \left( n,m \right)}{nm-n\lfloor \frac{m}{i} \rfloor i-m\lfloor \frac{n}{i} \rfloor i+\lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{i} \rfloor i^2} \\ \text{仍然数论分块即可}. \\ \text{答案是}f\left( n \right) \times f\left( m \right) -g\left( n,m \right) \\
    $$

    #include <iostream>
    #include <algorithm>
    
    using int_t = long long int;
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t mod = 19940417;
    const int_t inv2 = 9970209;
    const int_t inv3 = 6646806;
    int_t S(int_t x)
    {
        return (x * (x + 1) % mod * inv2 % mod) % mod;
    }
    int_t S2(int_t x)
    {
        return x * (x + 1) % mod * (2 * x + 1) % mod * inv2 % mod * inv3 % mod;
    }
    int_t Sab(int_t a, int_t b)
    {
        return (S(b) - S(a - 1)) % mod;
    }
    int_t S2ab(int_t a, int_t b)
    {
        return ((S2(b) - S2(a - 1)) % mod + mod) % mod;
    }
    int_t f(int_t n)
    {
        int_t result = n * n % mod;
        int_t i = 1;
        while (i <= n)
        {
            int_t next = n / (n / i);
            result = (result - (n / i) * Sab(i, next) % mod + mod) % mod;
            i = next + 1;
        }
        return result;
    }
    int_t g(int_t n, int_t m)
    {
        int_t i = 1;
        int_t result = n * n % mod * m % mod;
        while (i <= n)
        {
            int_t next = std::min(n / (n / i), m / (m / i));
            result = (result - n * ((m / i) * Sab(i, next) % mod) % mod + mod) % mod;
            result = (result - m * ((n / i) * Sab(i, next) % mod) % mod + mod) % mod;
            result = (result + (m / i) * (n / i) % mod * (S2ab(i, next)) % mod) % mod;
            i = next + 1;
        }
        return result;
    }
    int main()
    {
        int_t n, m;
        cin >> n >> m;
        if (n > m)
            std::swap(n, m);
        cout << ((f(n) * f(m) % mod - g(n, m)) % mod + mod) % mod << endl;
        return 0;
    }

     

  • SDOI2011 计算器

    快速幂+exgcd+BSGS的模板.

    关于BSGS:

    $$\text{已知}a,b,p\ \text{求}x\text{使得}a^x\equiv b\left( mod\ p \right) \\\text{设}sqr=\sqrt{p}\\x=i\times sqr+j\\a^x=a^{i\times sqr}\times a^j=\left( a^{sqr} \right) ^i\times a^j=b\\\left( a^{sqr} \right) ^i=b\times a^{-j}\\\text{枚举}j\text{从0到}sqr,\text{把}b\times a^{-j}\text{存在哈希表里}\\\text{然后枚举}i,\text{查询是否存在}b\times a^{-j}\text{使得}\left( a^{sqr} \right) ^i=b\times a^{-j}\text{成立即可}\\$$

    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <unordered_map>
    #include <utility>
    #include <cmath>
    #include <assert.h>
    using int_t = long long int;
    using pair_t = std::pair<int_t, int_t>;
    using std::cin;
    using std::cout;
    using std::endl;
    
    const char *ERROR = "Orz, I cannot find x!";
    
    int_t power(int_t base, int_t index, int_t mod)
    {
        int_t result = 1;
        if (index < 0)
        {
            index *= -1;
            base = power(base, mod - 2, mod);
        }
        while (index)
        {
            if (index & 1)
            {
                result = (result * base) % mod;
            }
            index >>= 1;
            base = (base * base) % mod;
        }
        return result;
    }
    pair_t exgcd(int_t a, int_t b)
    {
        if (b == 0)
        {
            return pair_t(1, 0);
        }
        auto temp = exgcd(b, a % b);
        return pair_t(temp.second, temp.first - a / b * temp.second);
    }
    int_t gcd(int_t a, int_t b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
    //寻找x,满足a^x=b mod p
    int_t log_mod(int_t a, int_t b, int_t p)
    {
        a %= p;
        b %= p;
        if (a == 0)
            return -1;
        int_t sqr = sqrt(p - 1) + 3;
        assert(sqr * sqr >= p);
        std::unordered_map<int_t, int_t> memory;
        for (int_t i = 0; i <= sqr; i++)
        {
            int_t curr = b * power(a, -i, p) % p;
            if (memory.count(curr))
            {
                memory[curr] = std::min(memory[curr], i);
            }
            else
            {
                memory[curr] = i;
            }
        }
        // curr = 1;
        for (int_t i = 0; i <= sqr; i++)
        {
            int_t curr = power(a, sqr * i, p);
            if (memory.count(curr))
            {
                return memory[curr] + sqr * i;
            }
        }
        return -1;
    }
    int_t solve(int_t y, int_t z, int_t p)
    {
        /*
        Sy+Tp=z
        */
        int_t A = y % p;
        int_t B = p;
        int_t C = z % p;
        int_t gcd = ::gcd(A, B);
        if (z % gcd != 0)
            return -1;
        int_t x0 = exgcd(A, B).first;
        x0 = (x0 % p + p) % p;
        return x0 * C % p;
    }
    int main()
    {
        int_t T, k;
        cin >> T >> k;
        for (int_t i = 1; i <= T; i++)
        {
            int_t y, z, p;
            cin >> y >> z >> p;
            if (k == 1)
            {
                cout << power(y, z, p) << endl;
            }
            else if (k == 2)
            {
                int_t result = solve(y, z, p);
                if (result == -1)
                    cout << ERROR << endl;
                else
                    cout << result << endl;
            }
            else if (k == 3)
            {
                int_t result = log_mod(y, z, p);
                if (result == -1)
                    cout << ERROR << endl;
                else
                    cout << result << endl;
            }
        }
        return 0;
    }

     

  • 第二类斯特林数的一些小东西…

    考生物时闲得无聊推的qwq..

    $$\left( \text{考生物时无聊推的}.. \right) \\\text{设}S_2\left( n,k \right) \text{表示把}n\text{个元素划分成}k\text{个集合的方案数}\\\text{则有}\\m^n=\sum_{0\le i\le m}{\left( \begin{array}{c} m\\ i\\\end{array} \right) S_2\left( n,i \right) i!}\\\text{解释:枚举不为空的集合的数量,乘上把}n\text{个元素划分成}i\text{个集合的方案数,再乘上集合的数量(因为集合有序)}\\\text{设}f\left( m \right) =m^n,g\left( i \right) =S_2\left( n,i \right) i!\\\text{则有}f\left( m \right) =\sum_{0\le i\le m}{\left( \begin{array}{c} m\\ i\\\end{array} \right) g\left( i \right)}\\\text{二项式反演可得}\\g\left( m \right) =\sum_{0\le i\le m}{\left( \begin{array}{c} m\\ i\\\end{array} \right) \left( -1 \right) ^{m-i}f\left( i \right)}\\\text{代入}\\S_2\left( n,m \right) \times m!=\sum_{0\le i\le m}{\left( \begin{array}{c} m\\ i\\\end{array} \right) \times \left( -1 \right) ^{m-i}\times i^n}\\\text{整理一下}\\S_2\left( n,m \right) =\frac{1}{m!}\sum_{0\le i\le m}{\left( \begin{array}{c} m\\ i\\\end{array} \right) \times \left( -1 \right) ^{m-i}\times i^n}\\\text{这是斯特林数的通项}\\\text{再整理一下,展开}\left( \begin{array}{c} m\\ i\\\end{array} \right) \\S_2\left( n,m \right) =\frac{1}{m!}\sum_{0\le i\le m}{\frac{m!}{i!\times \left( m-i \right) !}\times \left( -1 \right) ^{m-i}\times i^n}\\=\sum_{0\le i\le m}{\frac{i^n\times \left( -1 \right) ^{m-i}}{i!\times \left( m-i \right) !}}\\\text{然后发现这个式子可以卷积}…\\\text{设}A\left( x \right) =\sum_{0\le i\le m}{x^iS_2\left( n,i \right) =\sum_{0\le i\le m}{x^i\sum_{0\le j\le i}{\frac{j^n}{j!}\times \frac{\left( -1 \right) ^{i-j}}{\left( i-j \right) !}}}}\\\text{构造两个}m\text{次多项式}\\B\left( x \right) =\sum_{0\le i\le m}{\frac{i^n}{i!}x^i}\\C\left( x \right) =\sum_{0\le i\le m}{\frac{\left( -1 \right) ^i}{i!}x^i}\\B\left( x \right) C\left( x \right) \text{的}i\text{次项前的系数即为}S_2\left( n,i \right) $$

  • 洛谷4549 裴蜀定理

    https://www.luogu.org/problemnew/show/P4549

    这都是些什么破题啊..

    $$\text{设}S=\sum_{1\le i\le n}{A_ix_i}\text{,其中}x_i\in Z\text{,要求最小化}S\text{使得}S>0\\\text{设}d=gcd\left( A_1,A_2…..,A_n \right) \\\text{设}A_i=dk_i\\\text{则}S=d\sum_{1\le i\le n}{k_ix_i}\\\text{显然,当}\sum_{1\le i\le n}{k_ix_i}=\text{1时}S\text{最小}\\\text{假设}n\ge \text{2,那么则令所有}x_i=0\left( i>2 \right) \\\text{设}a=k_1,b=k_2\\\text{则等价于解方程}ax_1+bx_2=1\\\text{因为}a\text{与}b\text{互质,所以方程}ax_1+bx_2=\text{1一定有整数解}\\\text{所以}S\text{最小值为}d$$

    // luogu-judger-enable-o2
    #include <iostream>
    
    using int_t = long long int;
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 20;
    int_t seq[LARGE + 1];
    int_t n;
    
    int_t gcd(int_t a, int_t b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
    
    int main()
    {
        cin >> n;
        for (int_t i = 1; i <= n; i++)
        {
            cin >> seq[i];
            if (seq[i] < 0)
                seq[i] *= -1;
        }
        if (n == 1)
            cout << seq[1] << endl;
        else
        {
            int_t gcd = ::gcd(seq[1], seq[2]);
            for (int_t i = 3; i <= n; i++)
            {
                gcd = ::gcd(gcd, seq[i]);
            }
            cout << gcd << endl;
        }
        return 0;
    }

     

  • 洛谷1072 Hankson的趣味题

    一道水题..

    $$pk=\frac{b_1}{a_1}\\k\bot \frac{a_0}{a_1}\text{且}p_{}\bot \frac{b_1}{b_0}\\\text{的}p\text{,}k\text{的对数}\\\\\text{设}\\x=k_1a_1\\a_0=k_2a_1\\\text{可知}k_1\bot k_2,k_2=\frac{a_0}{a_1}\\\text{设}\\b_1=p_1x\\b_1=p_2b_0\\\text{可知}p_1,p_2\text{互质,且}p_2=\frac{b_1}{b_0}\\\because x=k_1a_1\\\therefore b_1=p_1k_1a_1\\p_1k_1=\frac{b_1}{a_1}\\\text{只需要算出}\frac{b_1}{a_1}\text{的所有因子}d\\\text{设}e=\frac{b_1}{a_1d}\\\text{如果满足}d\bot \frac{a_0}{a_1}\text{且}e_{}\bot \frac{b_1}{b_0}\text{则答案}+1\\\text{如果}b_1\text{不是}a_1\text{的倍数则无解}\\$$

    // luogu-judger-enable-o2
    #include <iostream>
    #include <algorithm>
    using int_t = long long int;
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 1000000;
    int_t factor[LARGE + 1];
    
    int_t gcd(int_t a, int_t b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    int_t process(int_t a0, int_t a1, int_t b0, int_t b1)
    {
        if (b1 % a1 != 0)
            return 0;
        int_t x = b1 / a1;
        int_t result = 0;
        for (int_t i = 1; i * i <= x; i++)
        {
            if (x % i == 0)
            {
                int_t e = x / i;
                if (gcd(i, a0 / a1) == 1 && gcd(e, b1 / b0) == 1)
                    result += 1;
                if (i != e)
                {
                    if (gcd(e, a0 / a1) == 1 && gcd(i, b1 / b0) == 1)
                        result += 1;
                }
            }
        }
        return result;
    }
    
    int main()
    {
        // for (int_t i = 1; i <= LARGE; i++)
        //     factor[i] = i;
        // for (int_t i = 2; i <= LARGE; i++)
        // {
        //     if (factor[i] == i)
        //     {
        //         for (int_t j = i * 2; j <= LARGE; j += i)
        //         {
        //             factor[j] = i;
        //         }
        //     }
        // }
        int_t T;
        cin >> T;
        for (int_t i = 1; i <= T; i++)
        {
            int_t a, b, c, d;
            cin >> a >> b >> c >> d;
            cout << process(a, b, c, d) << endl;
        }
        return 0;
    }

     

  • Crash的数字表格 / JZPTAB

    https://www.luogu.org/problemnew/show/P1829

    $$ =\sum_{1\le d\le n}{\sum_{1\le i\le n}{i\sum_{1\le j\le m}{j\frac{1}{d}\left[ gcd\left( i,j \right) ==1 \right]}}} \\ =\sum_{1\le d\le n}{d\sum_{1\le i\le \frac{n}{d}}{i\sum_{1\le j\le \frac{m}{d}}{j\left[ gcd\left( i,j \right) ==1 \right]}}} \\ =\sum_{1\le d\le n}{d\sum_{1\le i\le \frac{n}{d}}{i\sum_{1\le j\le \frac{m}{d}}{j\sum_{k|gcd\left( i,j \right)}{\mu \left( k \right)}}}} \\ =\sum_{1\le k\le n}{\mu \left( k \right)}\sum_{1\le d\le n}{d\sum_{1\le i\le \frac{n}{d}}{i\sum_{1\le j\le \frac{m}{d}}{j\left[ k|gcd\left( i,j \right) \right]}}} \\ =\sum_{1\le k\le n}{\mu \left( k \right)}\sum_{1\le d\le n}{d\sum_{1\le i\le \frac{n}{dk}}{ik\sum_{1\le j\le \frac{m}{dk}}{jk}}} \\ =\sum_{1\le k\le n}{k^2\mu \left( k \right)}\sum_{1\le d\le n}{dS\left( \lfloor \frac{n}{kd} \rfloor \right) S\left( \lfloor \frac{m}{kd} \rfloor \right)}\ \ \ \ \ \\ \text{其中}S\left( x \right) =\sum_{1\le i\le x}{i} \\ =\sum_{1\le k\le n}{k^2\mu \left( k \right)}\sum_{1\le d\le n}{dS\left( \lfloor \frac{n}{kd} \rfloor \right) S\left( \lfloor \frac{m}{kd} \rfloor \right)}\ \ \\ =\sum_{1\le T\le n}{S\left( \lfloor \frac{n}{T} \rfloor \right) S\left( \lfloor \frac{m}{T} \rfloor \right) T}\sum_{k|T}{k\mu \left( k \right)} \\ $$

    然后线性筛出第二个sigma的函数即可

    // luogu-judger-enable-o2
    #include <iostream>
    #include <stdint.h>
    #include <algorithm>
    #include <cstring>
    #include <ctime>
    #include <vector>
    using std::cin;
    using std::endl;
    using std::cout;
    
    typedef int32_t int_t;
    const int_t LARGE=10000000;
    const int_t mod=20101009;
    
    int8_t mu[LARGE+1];
    int_t f0[LARGE+1];
    bool isPrime[LARGE+1];
    void process() {
        memset(isPrime,1,sizeof(isPrime));
        isPrime[1]=0;
        for(int_t i=2; i*i<=LARGE; i++) {
            if(isPrime[i]) {
                for(int_t j=i*i; j<=LARGE; j+=i) {
                    isPrime[j]=false;
                }
            }
        }
        mu[1]=1;
        f0[1]=1;
        std::vector<int_t> primes;
        for(int_t i=2; i<=LARGE; i++) {
            if(isPrime[i]) {
                primes.push_back(i);
                mu[i]=-1;
                f0[i]=1-i;
            }
            for(int_t j=0; j<primes.size()&&primes[j]*i<=LARGE; j++) {
                int_t x=i*primes[j];
                if(i%(primes[j])==0) {
                    mu[x]=0;
                    f0[x]=f0[i];
                } else {
                    mu[x]=mu[i]*(-1);
                    f0[x]=f0[i]*(1-primes[j]);
                }
            }
        }
        for(int_t i=1; i<=LARGE; i++) {
            f0[i]=(f0[i]%mod+mod)%mod;
        }
    }
    
    
    int main() {
        process();
        int_t n,m;
        cin>>n>>m;
        if(n>m) std::swap(n,m);
        static int32_t S[LARGE+1];
        for(int_t i=1;i<=LARGE;i++) S[i]=(S[i-1]+i)%mod;
        int64_t result=0;
        for(int_t i=1; i<=n; i++) {
            result=((result+(((int64_t)S[n/i]*S[m/i]%mod)*((int64_t)i*f0[i]%mod))%mod)%mod+mod)%mod;
        }
        cout<<result<<endl;
        return 0;
    }

     

  • f(x)=(\phi*1)的证明

    $$ \text{设}f\left( x \right) =x,g\left( x \right) =1 \\ \\ \text{求证} \\ f\left( n \right) =\left( \varphi *g \right) \left( n \right) \\ n=\sum_{d|n}{\sum_{1\le j\le n}{\left[ \text{gcd}\left( j,n \right) ==d \right]}}\ \left( \left( \text{可以理解为,枚举}1-n\text{之间的每一个数与}n\text{的}gcd \right) \right) \\ =\sum_{d|n}{\sum_{1\le jd\le n}{\left[ \text{gcd}\left( jd,n \right) ==d \right]}} \\ =\sum_{d|n}{\sum_{1\le j\le \frac{n}{d}}{\left[ \text{gcd}\left( j,\frac{n}{d} \right) ==1 \right]}} \\ =\sum_{d|n}{\varphi \left( \frac{n}{d} \right)} \\ $$

  • 卢卡斯定理

    $$ \text{设模数为}p,\text{要计算}\left( \begin{array}{c} n\\ m\\ \end{array} \right) \%p \\ \text{设}s=\lfloor \frac{n}{p} \rfloor ,t=n\%p \\ \text{则}n=p\times s+t \\ \left( 1+x \right) ^n=\left( 1+x \right) ^{ps+t}=\left( \left( 1+x \right) ^p \right) ^s\left( 1+x \right) ^t \\ \text{首先,有一个性质,设}u\text{是非负整数,}\left( \begin{array}{c} p\\ u\\ \end{array} \right) \equiv 0\left( mod\ p \right) \text{(显然有因子}p\text{,所以模}p\text{为0)} \\ \text{定义多项式}A\text{对整数}m\text{取模为}A\text{的每一项系数依次对}m\text{取模} \\ \text{则}\left( 1+x \right) ^p\equiv \left( 1+x^p \right) \left( \text{除了}\left( \begin{array}{c} p\\ 0\\ \end{array} \right) ,\left( \begin{array}{c} p\\ p\\ \end{array} \right) \text{以外的系数模}p\text{都为}0 \right) \\ \text{则有} \\ \left( 1+x \right) ^n=\left( \left( 1+x \right) ^p \right) ^s\left( 1+x \right) ^t=\left( 1+x^p \right) ^s\left( 1+x \right) ^t=\sum_{0\le i\le s}{\left( \begin{array}{c} s\\ i\\ \end{array} \right) x^{pi}\sum_{0\le j\le t}{\left( \begin{array}{c} t\\ j\\ \end{array} \right) x^j}} \\ \sum_{0\le i\le ps+t}{\left( \begin{array}{c} ps+t\\ i\\ \end{array} \right) x^i}=\sum_{0\le i\le s}{\left( \begin{array}{c} s\\ i\\ \end{array} \right) x^{pi}\sum_{0\le j\le t}{\left( \begin{array}{c} t\\ j\\ \end{array} \right) x^j}} \\ \text{等式右边是两个多项式的卷积,其中第一个多项式的次数均为}p\text{的倍数,第二个多项式的次数均小于}p \\ \ \text{则等号右边的多项式乘积展开后,所有}x\text{的次数构成}0-n\text{的所有数} \\ \text{则对于一个}p\text{进制数}n=ps+t\text{,}x^s\text{一定存在于第一个多项式中,}x^t\text{一定存在于第二个多项式中} \\ \therefore \left( \begin{array}{c} n\\ m\\ \end{array} \right) =\left( \begin{array}{c} ps+t\\ pi+j\\ \end{array} \right) =\left( \begin{array}{c} s\\ i\\ \end{array} \right) \left( \begin{array}{c} t\\ j\\ \end{array} \right) =\left( \begin{array}{c} n/p\\ m/p\\ \end{array} \right) \left( \begin{array}{c} n\%p\\ m\%p\\ \end{array} \right) \\ \text{对于}\left( \begin{array}{c} n\%p\\ m\%p\\ \end{array} \right) \text{,直接返回结果,对于}\left( \begin{array}{c} n/p\\ m/p\\ \end{array} \right) \text{,递归计算} \\ \\ $$

    https://www.luogu.org/problemnew/show/P3807

    // luogu-judger-enable-o2
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <vector>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::vector;
    
    using int_t=long long int;
    int_t p;
    const int_t LARGE=1000000;
    int_t facts[LARGE+1]={1};
    int_t fastPower(int_t base,int_t index){
        int_t result=1;
        while(index){
            if(index&1){
                result=(result*base)%p;
            }
            base=(base*base)%p;
            index>>=1;
        }
        return result;
    }
    int_t inv(int_t x){
        return fastPower(x,p-2);
    }
    int_t C(int_t n,int_t m){
        if(m<n) return 0;
        return facts[m]*inv(facts[n]*facts[m-n]%p)%p;
    }
    int_t Lucas(int_t n,int_t m){
        if(n==0) return 1;
        return Lucas(n/p,m/p)*C(n%p,m%p)%p;
    } 
    void process(){
        int_t n,m;
        cin>>n>>m>>p;
        m+=n;
        for(int_t i=1;i<=LARGE;i++){
            facts[i]=(facts[i-1]*i%p);
        }
        cout<<Lucas(n,m)<<endl;
    }
    int main(){
    //	p=998244353;
    //	for(int_t i=1;i<=LARGE;i++){
    //		facts[i]=(facts[i-1]*i%p);
    //	}
    //	while(true){
    //		int_t n,m;
    //		cin>>n>>m;
    //		cout<<C(n,m)<<endl;
    //	} 
        int_t T;
        cin>>T;
        for(int_t i=1;i<=T;i++) process();
        return 0;
    }

     

  • 扩展中国剩余定理

    $$ \text{解方程组}\begin{cases} x_{}\equiv a_1\left( mod\ x_1 \right)\\ x_{}\equiv a_2\left( mod\ x_2 \right)\\ …\\ x_{}\equiv a_i\left( mod\ m_i \right)\\ \end{cases} \\ \text{我们先求解前}i\text{个方程式,然后根据前}i\text{个方程式的通解求出前}i+\text{1个方程式的通解} \\ \text{设}M_i=\prod_{1\le j\le i}{m_j} \\ \text{设前}i\text{个方程的解为}X_i \\ \text{首先,求解第一个方程} \\ \text{显然解是}a_1 \\ \text{我们现在知道了前}i-\text{1个方程的解}X_{i-1} \\ \text{显然,}X_{i-1}+t\times M_{i-1}\left( t\text{为任意非负数} \right) \text{也是前}i-\text{1个方程的解}\left( \text{因为}M_{i-1}\%m_j\left( 1\le j\le i \right) =0 \right) \\ \text{现在,我们需要求一个}t\text{,让}X_{i-1}+t\times M_{i-1}\text{既是前}i-\text{1个方程的解,又是第}i\text{个方程的解,即解方程} \\ X_{i-1}+t\times M_{i-1}\equiv a_i\left( mod\ m_i \right) \\ \text{这个方程等价于} \\ X_{i-1}+t\times M_{i-1}+q\times m_i=a_i \\ \text{其中}q\text{为未知数} \\ \text{移项得} \\ t\times M_{i-1}+q\times m_i=a_i-x \\ \text{注意,如果}a_i-X_{i-1}<\text{0,那么我们要把}a_i-X_{i-1}\text{对}m_i\text{取模使之成为正值方便计算} \\ \text{这个方程可以使用扩展欧几里得算法求解} \\ \text{设}A=M_{i-1},B=m_i,C=a_i-X_{i-1} \\ \text{使用扩展欧几里得算法求出} \\ At+Bq=gcd\left( A,B \right) \text{的一组解}\left( t,q \right) \\ \text{首先把这组解变成}At+Bq=C\text{的解} \\ t=t\times \frac{C}{gcd\left( A,B \right)} \\ \text{然后,我们让}t\text{成为最小符合条件的非负整数解} \\ \text{设这个解是}t’ \\ \text{因为}t+s\times \frac{B}{gcd\left( A,B \right)}\left( s\text{为任意整数} \right) \text{都是原方程的解} \\ \text{所以可以认为} \\ t’\equiv t\left( mod\ \frac{B}{gcd\left( A,B \right)} \right) \\ \text{所以,计算}\left( t\%\left( \frac{B}{gcd\left( A,B \right)} \right) +\frac{B}{gcd\left( A,B \right)} \right) \%\frac{B}{gcd\left( A,B \right)}\text{得到的就是最小的符合条件的非负整数}t’ \\ \text{所以,前}i\text{个方程的一个解为} \\ X_i=X_{i-1}+t’M_{i-1} \\ \text{前}i\text{个方程的通解为}X_i+sM_i\left( s\text{为任意整数} \right) \left( \text{也就是说这个解可以对}M_i\text{取模} \right) \\ \text{然后我们只要一直算下去,就可以求出整个方程组的通解} \\ \text{注意:无解的判定,如果任何一次扩展欧几里得算法无解,那么整个方程组无解} \\ \\ \\ $$

    完整代码:

    https://www.luogu.org/problemnew/show/P4777

    // luogu-judger-enable-o2
    #include <iostream>
    #include <utility>
    #include <inttypes.h>
    #include <assert.h>
    #define debug(x) std::cout << #x << " = " << x << std::endl;
    
    using int_t = __int128;
    using pair_t = std::pair<int_t, int_t>;
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    const int_t LARGE = 100000;
    
    int_t coefs[LARGE + 1];
    int_t mods[LARGE + 1];
    int n;
    
    pair_t exgcd(int_t a, int_t b)
    {
        if (b == 0)
        {
            return std::make_pair(1, 0);
        }
        auto temp = exgcd(b, a % b);
        return std::make_pair(temp.second, temp.first - (a / b) * temp.second);
    }
    int_t gcd(int_t a, int_t b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
    template <class T>
    void write(T x)
    {
        if (x > 9)
            write(x / 10);
        putchar('0' + x % 10);
    }
    int main()
    {
    
        cin >> n;
        for (int_t i = 1; i <= n; i++)
        {
            uint64_t mod, coef;
            cin >> mod >> coef;
            mods[i] = mod;
            coefs[i] = (coef % mod + mod) % mod;
        }
        int_t result = coefs[1];
        int_t M = mods[1];
        for (int i = 2; i <= n; i++)
        {
            int_t A = M;
            int_t B = mods[i];
            int_t C = coefs[i] - result;
            C = (C % B + B) % B;
            int_t d = gcd(A, B);
            int_t x = exgcd(A, B).first * (C / d);
            x = (x % (B / d) + B / d) % (B / d);
            result = (result + x * M);
            M = M * B / d;
        }
        write(result);
        return 0;
    }