-
Notifications
You must be signed in to change notification settings - Fork 47
/
Number_Theory.cpp
172 lines (124 loc) · 2.52 KB
/
Number_Theory.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<bits/stdc++.h>
using namespace std;
#define ll long long
// Divisors of a number
vector<int> divs;
void solve(int x) {
int sq = sqrt(x);
for(int i=1; i<=sq; i++) {
if(x % i == 0) {
divs.push_back(i);
if(i * i != x) divs.push_back(x/i);
}
}
}
// -------------------------------------------
// prime divisors with calculation
vector<int> pdivs;
void solve2(int x) {
for(int i=2; i*i <= x; i++) {
if(x % i == 0) {
pdivs.push_back(i);
while(x % i==0) x /= i;
}
}
if(x > 1) pdivs.push_back(x);
}
// -----------------------------------
// Seive with smallest prime factor
const int N = 2e5+5;
vector<int> primes;
vector<bool> isPrime(N);
vector<int> spf(N,-1);
void seive() {
for(int i=2; i<N; i++) {
isPrime[i] = true;
}
for(int i=2; i<N; i++) {
if(isPrime[i] == true) {
spf[i]=i;
for(int j=i*i; j<N; j+=i) {
isPrime[j] = false;
if(spf[j] == -1)
spf[j] = i;
}
}
}
for(int i=2;i<N;i++)
if(isPrime[i])
primes.push_back(i);
}
// ---------------------------------------------
#define N 1000005
void solve3() {
int div[N];
for(int i = 1; i < N; i++){
for(int j = i; j < N ; j += i){
div[j] += 1;
}
cout << i << " " << div[i] << "\n";
}
}
// Q. In each query you will get an integer, you have to output number of divisors this number has.
/*
constraints:-
1 <= Q <= 1e5
1 <= n <= 1e6
*/
void solve4() {
int q;
cin >> q;
while(q--) {
int n;
cin >> n;
int ans = 1;
while(n > 1) {
int lp = spf[n], cnt = 1;
while(n%lp == 0) {
n /= lp;
cnt++;
}
ans *= cnt;
}
cout << ans << endl;
}
}
//// modular exponentiation --- O(logn)
#define mod 1000000007
ll power(ll a,ll n){
if(n == 0){
return 1;
}
a %= mod;
ll y = power(a, n/2)%mod;
if(n % 2 == 1){
return ((a*y) % mod * y)%mod;
}
else return (y*y)%mod;
}
// -----------------------------------------
// inverse modulo calculation (using fermat's little theorem)
ll inverse(ll a){
return power(a,mod-2); // a and m are coprime
}
// -----------------------------------------
int main(){
// int x;
// cin >> x;
// solve2(x);
// for(int factors: pdivs) {
// cout << factors << ' ' ;
// }
seive();
ll fact[N], ifact[N];
fact[0] = 1;
for(int i = 1; i <= N; i++){
fact[i] = (fact[i-1]*i)%mod;
}
for(int x : primes) cout << x << ' ';
return 0;
}
// https://www.spoj.com/problems/INS17M/
// https://www.codechef.com/SEPT18B/problems/ANDSQR
// https://forum.videolan.org/viewtopic.php?t=85347
// https://dzone.com/articles/htmlcssjavascript-gui-java-0