-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cpp
More file actions
151 lines (131 loc) · 2.82 KB
/
Copy pathmath.cpp
File metadata and controls
151 lines (131 loc) · 2.82 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
需要使用 long long 的情况:
数据取到 10^9 级别,同时涉及到数据的 add or multiply
2^31 ~= 10^9
*/
typedef long long LL;
/*
Reverse a number
123 -> 321
*/
int reverse(int num) {
int res = 0;
while (num) {
res = res * 10 + num % 10;
num /= 10;
}
return res;
}
/*
Get every prime factor of number n
A number n will only have at most 1 prime factor
which is greater than √n
*/
vector<int> getPrimeFactor(int n) {
vector<int> res;
for (int i = 2; i <= n / i; i++)
if (n % i == 0) {
while (n % i == 0) n /= i;
res.push_back(i);
}
if (n > 1) res.push_back(n);
return res;
}
/*
Get prime cnt of 1 - n
O(n)
*/
void get_primes(){
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
cout << cnt << endl;
}
/*
Get every factor of n
*/
vector<int> getFactor(int n) {
vector<int> res;
for (int i = 1; i <= n / i; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i) res.push_back(n / i);
}
}
return res;
}
/*
Given a1, a2, ... an
return the factor number of (a1 * a2 * ... * an)
*/
int getFactor(vector<int>& nums) {
unordered_map<int, int> primes;
int MOD = 1e9 + 7;
for (auto& n : nums) {
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
n /= i;
primes[i]++;
}
}
if (n > 1) primes[n]++;
}
long long res = 1;
for (auto& p : primes) {
res = res * (p.second + 1) % MOD;
}
return res;
}
/*
Roman numbers combinations < 4000
*/
string c[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int vals[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
/*
Given a number n, from 1 to n
return how many numbers and n are prime to each other
(gcd(n1, n2) == 1)
6 -> 1, 2, 3, 4, 5, 6
1, 5 meets requirements
return 2
*/
int phi(int n) {
int res = n;
for (int i = 2; i <= n / i; i++) {
if (n % i == 0) {
res = res / i * (i - 1);
while (n % i == 0) n /= i;
}
}
return res;
}
/*
Exponentiation by squaring
return (a^b) mod p
*/
LL qmi(int a, int b, int p) {
LL res = 1 % p;
while (b) {
if (b & 1) res = res * a % p;
b >>= 1;
a = a * (LL)a % p;
}
return res;
}
/*
a / b % p == a * a^p-2 % p
when p is prime
return a^p-2 % p
*/
LL getReversePower(int a, int b, int p) {
return qmi(a, p - 2, p);
}
/*
SG NIM ICG
*/
SG(0) = 0; // end point
SG(x) = mex(SG(y1), SG(y2), ..., SG(yk));