Skip to content

Commit 4c6365e

Browse files
committed
Number Theory Part 1 Added
1 parent 36e3f1a commit 4c6365e

File tree

7 files changed

+1125
-0
lines changed

7 files changed

+1125
-0
lines changed

Algorithm/18 Power.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
2+
/** Which of the favors of your Lord will you deny ? **/
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
#define LL long long
8+
#define PII pair<int,int>
9+
#define PLL pair<LL,LL>
10+
#define MP make_pair
11+
#define F first
12+
#define S second
13+
#define INF INT_MAX
14+
15+
#define ALL(x) (x).begin(), (x).end()
16+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
17+
#define READ freopen("alu.txt", "r", stdin)
18+
#define WRITE freopen("vorta.txt", "w", stdout)
19+
20+
#include <ext/pb_ds/assoc_container.hpp>
21+
#include <ext/pb_ds/tree_policy.hpp>
22+
using namespace __gnu_pbds;
23+
24+
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
25+
26+
/**
27+
28+
PBDS
29+
-------------------------------------------------
30+
1) insert(value)
31+
2) erase(value)
32+
3) order_of_key(value) // 0 based indexing
33+
4) *find_by_order(position) // 0 based indexing
34+
35+
**/
36+
37+
template<class T1, class T2>
38+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
39+
template <class T>
40+
ostream &operator <<(ostream &os, vector<T>&v);
41+
template <class T>
42+
ostream &operator <<(ostream &os, set<T>&v);
43+
44+
inline void optimizeIO()
45+
{
46+
ios_base::sync_with_stdio(false);
47+
cin.tie(NULL);
48+
}
49+
50+
const int nmax = 2e5+7;
51+
const LL LINF = 1e17;
52+
53+
template <class T>
54+
string to_str(T x)
55+
{
56+
stringstream ss;
57+
ss<<x;
58+
return ss.str();
59+
}
60+
61+
//bool cmp(const PII &A,const PII &B)
62+
//{
63+
//
64+
//}
65+
66+
template <class T>
67+
T po(T x,T n)
68+
{
69+
if(n==0)
70+
return 1;
71+
72+
T u = po(x,n/2);
73+
u = u*u;
74+
75+
if(n%2==1)
76+
u = u*x;
77+
78+
return u;
79+
}
80+
81+
template <class T>
82+
T po_iter(T b, T p) /// Faster than recursive one
83+
{
84+
T res = 1, x = b;
85+
while ( p ) {
86+
if ( p & 1 ) res = ( res * x );
87+
x = ( x * x );
88+
p >>= 1;
89+
}
90+
return res;
91+
}
92+
93+
int main()
94+
{
95+
optimizeIO();
96+
97+
cout<<po(3LL,25LL)<<endl;
98+
cout<<po_iter(3LL,25LL)<<endl;
99+
100+
return 0;
101+
}
102+
103+
/**
104+
105+
**/
106+
107+
template<class T1, class T2>
108+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
109+
{
110+
os<<"{"<<p.first<<", "<<p.second<<"} ";
111+
return os;
112+
}
113+
template <class T>
114+
ostream &operator <<(ostream &os, vector<T>&v)
115+
{
116+
os<<"[ ";
117+
for(int i=0; i<v.size(); i++)
118+
{
119+
os<<v[i]<<" " ;
120+
}
121+
os<<" ]";
122+
return os;
123+
}
124+
125+
template <class T>
126+
ostream &operator <<(ostream &os, set<T>&v)
127+
{
128+
os<<"[ ";
129+
for(T i:v)
130+
{
131+
os<<i<<" ";
132+
}
133+
os<<" ]";
134+
return os;
135+
}
136+
137+

Algorithm/19 BigMod.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
/** Which of the favors of your Lord will you deny ? **/
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
#define LL long long
8+
#define PII pair<int,int>
9+
#define PLL pair<LL,LL>
10+
#define MP make_pair
11+
#define F first
12+
#define S second
13+
#define INF INT_MAX
14+
15+
#define ALL(x) (x).begin(), (x).end()
16+
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
17+
#define READ freopen("alu.txt", "r", stdin)
18+
#define WRITE freopen("vorta.txt", "w", stdout)
19+
20+
#include <ext/pb_ds/assoc_container.hpp>
21+
#include <ext/pb_ds/tree_policy.hpp>
22+
using namespace __gnu_pbds;
23+
24+
template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
25+
26+
/**
27+
28+
PBDS
29+
-------------------------------------------------
30+
1) insert(value)
31+
2) erase(value)
32+
3) order_of_key(value) // 0 based indexing
33+
4) *find_by_order(position) // 0 based indexing
34+
35+
**/
36+
37+
template<class T1, class T2>
38+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
39+
template <class T>
40+
ostream &operator <<(ostream &os, vector<T>&v);
41+
template <class T>
42+
ostream &operator <<(ostream &os, set<T>&v);
43+
44+
inline void optimizeIO()
45+
{
46+
ios_base::sync_with_stdio(false);
47+
cin.tie(NULL);
48+
}
49+
50+
const int nmax = 2e5+7;
51+
const LL LINF = 1e17;
52+
53+
template <class T>
54+
string to_str(T x)
55+
{
56+
stringstream ss;
57+
ss<<x;
58+
return ss.str();
59+
}
60+
61+
//bool cmp(const PII &A,const PII &B)
62+
//{
63+
//
64+
//}
65+
66+
template <class T>
67+
T bigMod(T x,T n,T mo)
68+
{
69+
if(n==0)
70+
return 1;
71+
72+
T u = bigMod(x,n/2,mo);
73+
u = ((u%mo)*(u%mo))%mo;
74+
75+
if(n%2==1)
76+
u = ((u%mo)*(x%mo))%mo;
77+
78+
return u;
79+
}
80+
81+
template <class T>
82+
T bigMod_iter(T b, T p, T m ) /// Faster than recursive one
83+
{
84+
T res = 1 % m, x = b % m;
85+
while ( p )
86+
{
87+
if ( p & 1 )
88+
res = ( res * x ) % m;
89+
x = ( x * x ) % m;
90+
p >>= 1;
91+
}
92+
return res;
93+
}
94+
95+
int main()
96+
{
97+
optimizeIO();
98+
99+
LL mod = 1e9+7;
100+
101+
cout<<bigMod(3LL,25LL,mod)<<endl;
102+
cout<<bigMod_iter(3LL,25LL,mod)<<endl;
103+
104+
return 0;
105+
}
106+
107+
/**
108+
109+
**/
110+
111+
template<class T1, class T2>
112+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
113+
{
114+
os<<"{"<<p.first<<", "<<p.second<<"} ";
115+
return os;
116+
}
117+
template <class T>
118+
ostream &operator <<(ostream &os, vector<T>&v)
119+
{
120+
os<<"[ ";
121+
for(int i=0; i<v.size(); i++)
122+
{
123+
os<<v[i]<<" " ;
124+
}
125+
os<<" ]";
126+
return os;
127+
}
128+
129+
template <class T>
130+
ostream &operator <<(ostream &os, set<T>&v)
131+
{
132+
os<<"[ ";
133+
for(T i:v)
134+
{
135+
os<<i<<" ";
136+
}
137+
os<<" ]";
138+
return os;
139+
}
140+
141+

0 commit comments

Comments
 (0)