-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_functions.cpp
54 lines (48 loc) · 1.26 KB
/
power_functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
#include<stdint.h>
typedef int_fast64_t ll;
typedef int_fast64_t i64;
typedef int_fast32_t i32;
ll pow_recursion(ll base, ll exponent){
if(exponent == 0) return 1;
ll ans = pow_recursion(base, exponent / 2);
ans *= ans;
if(exponent % 2 == 1) ans *= base;
return ans;
}
ll pow_recursion_with_modulo(ll base, ll exponent, ll MOD){
if(exponent == 0) return 1;
ll h = pow_recursion_with_modulo(base, exponent/2, MOD);
h = h * h % MOD;
if((exponent & 1) == 0) return h;
else return h * base % MOD;
}
ll pow_while_loop(ll base, ll exponent){
ll ans = 1;
while(exponent > 0){
if((exponent & 1) == 1) ans *= base;
base *= base;
exponent >>= 1;
}
return ans;
}
ll pow_while_loop_with_modulo(ll base, ll exponent, ll MOD){
ll ans = 1;
while(exponent > 0){
if((exponent & 1) == 1) ans = ans * base % MOD;
base = base * base % MOD;
exponent >>= 1;
}
return ans;
}
i32 choose(i32 out_of, i32 choose, i32 mod){
i32 ans = 1;
for(i32 i = 0; i < choose; i++) ans = ans * (out_of - i) % mod;
for(i32 i = choose; i > 0; i--) ans = ans / i % mod;
return ans;
}