Luo Gu P1593 factor - the number of Small chowder

Luo Gu P1593 factor and

Topic links: Luo Gu P1593 factor and

Algorithms 数论Tags: 费马小定理, 欧拉定理, ,快速幂

topic

Title Description

Enter two positive integers a and b, find \ (a ^ b \) factors and. Result is too large, as long as the remainder of its 9901 output.

Input Format

Only one row, two positive integers \ (A \) and \ (B \) \ ((0≤a, b≤50000000) \) .

Output Format

\ (a ^ b \) factors and the remainder of 9901.

Sample input and output

Input # 1

2 3

Output # 1

15

answer:

This question can probably be understood as Simple hodgepodge of number theory Very simple integration of several topics, simply about the number involved in several aspects of the theory of knowledge:

  1. The only integer decomposition theorem

    For any positive integer, and has only one way to write the expression product of its prime factors.

    \ (A = (P_1 k_1 ^ {^} * {P_2 P_3 K_2} * {K_3} ^ * \ ^ {K_n P_n ldots *}) \) [wherein \ (P_i \) are the prime number]

  2. About the number and formula

    For it has been decomposed in accordance with a unique integer integer decomposition Theorem

    \(A = (p_1 ^ {k_1}*p_2 ^ {k_2}*p_3 ^ {k_3} *\ldots *p_n ^ {k_n})\)

    There \ (A \) is the sum of all factors as:

\[ S = (1+p_1+p_1^2+p_1^3+\ldots+p_1^{k_1})*(1+p_2+p_2^2+p_2^3+\ldots+p_2^{k_2})*(1+p_3+p_3^2+p_3^3+\ldots+p_3^{k_3})*\ldots*(1+p_n+p_n^2+p_n^3+\ldots+p_n^{k_n}) \]

Number divisor formula:

\(N = (1+k_1)*(1+k_2)*(1+k_3)*\ldots*(1+k_n)\)

  1. Congruence mold formula

    \((a+b) \% m = (a\%m + b\%m)\%m\)

    \((a*b)\%m = (a\%m + b\%m)\%m\)

  2. Geometric series summation formula

    \ (SUM = \ {A_1 FRAC (K-Q ^. 1)} {(. 1-Q)} \) [wherein \ (A_1 \) led key, \ (Q \) as a common ratio, \ (K \) of several]

After this knowledge can Pleasant manner This question is analyzed, and the general idea is this question:

Quality factorization + (geometric series summation + multiplicative inverse) + (Fermat's little theorem / + fast power Euler's theorem)

So then analyzed:

  1. How to use integer unique decomposition theorem

    Because the only decomposition theorem based on the integer \ (A = (P_1 k_1 ^ {^} * {P_2 P_3 K_2} * {K_3} ^ * \ ^ {K_n P_n ldots *}) \) , not difficult to find, if required \ (A ^ B \) is the product of expression, simply \ (a \) is the product of expression among all \ (k_i \) have done a \ (* B \) , apparently resulting expression can represent \ (a ^ B \) .

  2. This question is Euler's theorem and Fermat's Little Theorem usage:

    This question can be found in which, when we use about the number and formulas time, a plurality of geometric configuration and number of columns on a multiplication expressions, since the geometric series summation formula \ (sum = \ frac {a_1 (K-Q ^. 1)} {(. 1-Q)} \) , during the rapid power involves problems denominator, so we will consider seeking scores modulo operation, using the method of seeking the multiplicative inverse, then there Two Calculations about:

    • Fermat's little theorem seeking multiplicative inverse (due to the modulo 9901 is a prime number )

      ll inverse(ll x,ll p) {
          return qpow(x, mod - 2);
    • Using the extended Euclid method (EXGCD) find multiplicative inverse

      ll exgcd(ll a,ll b,ll &x,ll &y)
      {
          if(a==0&&b==0) return -1ll;
          if(b==0){ x=1ll,y=0ll; return a; }
          ll d=exgcd(b,a%b,y,x);
          y-=a/b*x; return d;
      }
      ll inverse(ll a,ll p)
      {
          if(a%p==0) return 1ll;
          else if((a+1)%p==0) return -2ll;
          ll x,y,d=exgcd(a,p,x,y);
          if(d==1) return (x%p+p)%p;
          return -1ll;
      }
  3. This question requires some special value judgment:

    • When \ (a = 0 \) when no matter \ (b \) what value, the answer must be to \ (0 \)
    • When \ (a \ not = 0 \ ) and $ b = 0 $ when the answer must be to \ (1 \)
    • When appropriate first modulo operation \ (+ mod \) then \ (\% MOD \) , to prevent a negative

AC Code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;
const ll mod = 9901;
const ll maxn = 100010;
ll cnt, ans = 1, prime[maxn], prime_cnt[maxn];
ll qpow(ll q, ll n)
{
    ll ans = 1;
    while (n)
    {
        if (n & 1)
            ans = ans * q % mod;
        n >>= 1; 
        q = q * q % mod; 
    }
    return ans;
}
ll inverse(ll x,ll p) 
{
    return qpow(x, mod - 2);
}
int main()
{
    ll a, b; 
    cin >> a >> b ;
    if (a == 0) { cout << 0 << endl; return 0; }
    if (b == 0) { cout << 1 << endl; return 0; }
    for (ll i = 2; i * i <= a; i ++ )
    {
        if (a % i == 0)
        {
            prime[ ++ cnt] = i % mod;
            while (a % i == 0)
            {
                a /= i;
                prime_cnt[cnt] ++ ;
            }
        }
    }
    if (a != 1)
    {
        prime[ ++ cnt] = a;
        prime_cnt[cnt] = 1;
    }
    for (int i = 1; i <= cnt; i ++ )
    {
        if (prime[i] % mod == 1)
            ans = ans * (prime_cnt[i] + 1) % mod;
        else
        {
            ll div = (prime[i] - 1 + mod) % mod;
            ll res1 = qpow(prime[i], prime_cnt[i] * b + 1) % mod - 1;
            ll res2 = inverse(div, mod);
            ans = ans * (res1 + mod) % mod * res2 % mod;
        }
    }
    cout << ans << endl ;
    return 0;
}

Guess you like

Origin www.cnblogs.com/littleseven777/p/11842130.html