poj1845 inverse, fast modulo power __ number theory

Source: Internet
Author: User
Tags extend

The main effect of the topic:

Given two positive integers and, all the factors and the value of 9901 after the remainder.

Analysis:

It's easy to know, first to get the decomposition, then get, then

All of the factors and expressions are as follows

Because you want to take a model and there is division, you need to use the inverse.


For positive integers and, if so, the solution of the smallest positive integer in the congruence equation is called the inverse of modulo.

The inverse is generally used to extend Euclidean algorithm to obtain, if is prime, then can also according to Fermat's little theorem to get inverse.

The derivation process is as follows

Now look at an inverse of the most common problem, find the following expression value (known)

Of course this classic problem has many methods, the most common is to extend Euclid, if it is prime, you can also use the Fermat's little theorem.

But you will find that the Fermat's little Theorem and the extended Euclidean algorithm have limitations, and they all require a reciprocal element. Actually, we still have a

A general method of negation is suitable for all cases. The formula is as follows

Now let's prove it, known, and prove the steps as follows


Geometric progression sum formula, use the following formula

Because it can be so large that it exceeds the int range, you need to multiply it in a fast power.

Next is the code: (Must tell the multiplication of two points, and after taking the mold must be added)

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <fstream > #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector
> #include <map> #include <set> #include <iomanip> using namespace std; #pragma COMMENT (linker, "/stack:102400000,102400000") #define MAXN 10005 #define MOD 1000000007 #define MEM (A, b) memset (
A, B, sizeof (a)) #define LL long #define ULL long long const long;
BOOL PRIME[MAXN];

int P[MAXN];
    void Is_prime () {mem (prime, true);
    int id = 0;
    Prime[0] = prime[1] = false;
            for (int i = 2; I < MAXN i + +) {if (Prime[i]) {p[id++] = i;
            Int J = 2 * i;
        while (J <= maxn) prime[j] = False, j + = i;
    } ll Mulit (ll A, ll B, ll MoD) {a%= mod;
    b%= MoD;
    LL res = 0;
   while (b) {if (B & 1)     {res = (res + a)% MoD;
        } b >>= 1;
    A = (a + a)% MoD;
return res% MoD;
    ll Quick_mod (ll A, ll B, ll MoD) {ll res = 1;
        while (b) {if (b & 1) {res = Mulit (res, a, mod);
        A = Mulit (A, A, mod);
    b >>= 1;
return res;
}//ofstream ofile;
    int main () {LL A, B;
    Is_prime ();
        while (scanf ("%lld%lld", &a, &b)!= EOF) {LL ans = 1;  for (int i = 0; p[i] * p[i] <= A; i++) {if (a% p[i] = = 0) {int num =
                0;
                    While (a% p[i] = = 0) {num + +;
                A/= p[i];
                Ans *= (Quick_mod (p[i), num * b+1, 9901 * (p[i]-1)) + (9901 * (p[i]-1))-1)/(P[i)-1);
            Ans%= 9901;  } if (a > 1) {ans *= quick_mod (A,B+1, 9901 * (A-1)) + 9901 * (A-1)-1)/(A-1);
        Ans%= 9901;
    cout << ans << endl;
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.