poj1845 inverse element, fast modulus power

Source: Internet
Author: User
Tags modulus

Main topic:

a given two positive integers and the , Ask all the factors and the 9901 the value after the remainder is taken.

Analysis:

It's easy to know, to break it down first, then get it, then

All of the factors and expressions are as follows

The inverse is used because there is a division to be modulo.


for positive integers and, if so, the solution of the smallest positive integer in this congruence equation is called the inverse element of the module.

Inverse is generally used to expand Euclidean algorithm to obtain, if it is a prime number, then can also be based on the Fermat theorem to get the inverse element.

The derivation process is as follows

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

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

But you will find that the Fermat theorem and the extended Euclidean algorithm have limitations, and they all demand and mutual element. Actually, we still have a

The common method of solving the inverse element is suitable for all situations. The formula is as follows

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


Geometric series sum formula, use the following formula can

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



Next is the code: (Must say multiplication of two points, and after 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 long#define ULL long longconst long long inf=0x3fffffff;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;}


poj1845 inverse element, fast modulus power

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.