@@ -9,38 +9,16 @@ typedef long long ll;
9
9
typedef pair<int , int > pii;
10
10
typedef vector<int > vi;
11
11
12
- const ll M = 1e9 +7 , LIM = 200000 ;
13
-
14
- bool isPrime (int x) {
15
- if (x <= 1 ) return false ;
16
- for (int i = 2 ; i*i <= x; ++i) {
17
- if (x % i == 0 ) return false ;
18
- }
19
- return true ;
12
+ ll modpow (ll a, ll e, ll mod) {
13
+ if (e == 0 ) return 1 ;
14
+ ll x = modpow (a * a % mod, e >> 1 , mod);
15
+ return e & 1 ? x * a % mod : x;
20
16
}
21
17
22
- int ar[5 ] = {1 ,2 ,3 ,4 ,5 };
23
-
24
18
int main () {
25
- vector<ll> inv (LIM, 1 );
26
- for (int M = 1 ; M <= 1000 ; ++M) {
27
- if (!isPrime (M)) continue ;
28
- inv.assign (LIM, 123123 );
29
- inv[0 ] = -1000 ;
30
- inv[1 ] = 1 ;
31
- rep (i,2 ,LIM)
32
- inv[i] = M - (M / i) * inv[M % i] % M;
33
- rep (i,0 ,M) {
34
- bool works = (i * inv[i] % M == 1 );
35
- bool relp = (__gcd (i, M) == 1 );
36
- assert (works == relp);
37
- }
38
- }
19
+ #include " ../../content/number-theory/ModInverse.h"
20
+ for (int i=1 ; i<10000 ; i++)
21
+ assert (inv[i] == modpow (i, mod-2 , mod));
22
+ cout<<" Tests pass!" <<endl;
39
23
}
40
24
41
- ll inv[LIM] = {-10000 , 1 };
42
- int main2 () {
43
- rep (i,2 ,LIM) inv[i] = M - (M / i) * inv[M % i] % M;
44
- cout << inv[0 ] << ' ' << inv[1 ] << ' ' << inv[2 ] << ' ' << inv[3 ] << endl;
45
- return 0 ;
46
- }
0 commit comments