Skip to content

Commit 21048e3

Browse files
author
Nadim-Mahmud
committed
initial commit
1 parent 20aefee commit 21048e3

14 files changed

+670
-0
lines changed

Source/Big Interger/factorial.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/// ***Bigint Factorial ****
3+
4+
//package main;
5+
6+
import java.math.BigInteger;
7+
import java.util.Scanner;
8+
9+
public class Main {
10+
11+
public static Scanner sc;
12+
13+
public static void main(String [] arg) {
14+
15+
BigInteger [] fact = new BigInteger[200];
16+
17+
fact[0] = BigInteger.ONE;
18+
19+
for(int i=1;i<=150;i++) {
20+
fact[i] = fact[i-1].multiply(new BigInteger(i + ""));
21+
}
22+
23+
sc = new Scanner(System.in);
24+
int ts = sc.nextInt();
25+
int n,cas=0;
26+
27+
while(++cas<=ts) {
28+
n = sc.nextInt();
29+
System.out.println(fact[n]);
30+
}
31+
}
32+
}
33+
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
/// *** Convex Hull[Grahams Scan] [nlog(n)]
3+
4+
#define ll long long
5+
struct point{
6+
ll x,y;
7+
}convex_points[MX],points[MX];;
8+
9+
/// global scope decraltion of min-left point of collcetion
10+
point pivot;
11+
12+
///Distance calculation mathod
13+
ll dist(point p1,point p2){
14+
return ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
15+
}
16+
17+
/**
18+
* https://www.geeksforgeeks.org/orientation-3-ordered-points/
19+
* calculating orientation based on slope (yi-yj)/(xi-xj)
20+
* by compering slope of p-q and q-r;
21+
* if p-q<q-r then counter-clockwise
22+
* @return 0 if colinear
23+
* @return 1 if clockwise (Right rotetion)
24+
* @return 2 if counter-clockwise (left rotetion)
25+
*/
26+
int orientation(point p,point q,point r){
27+
ll val = ((q.y-p.y)*(r.x-q.x) - (r.y-q.y)*(q.x-p.x));
28+
if(val==0) return 0;
29+
if(val>0) return 1;
30+
return 2;
31+
}
32+
33+
/**
34+
* sorting by polor angle in counterclockwise order around point0.
35+
* If polor angle of two points is same, then put the nearest point first.
36+
*/
37+
bool cmp(point p1,point p2){
38+
ll o = orientation(pivot,p1,p2);
39+
if(o==0){
40+
return dist(pivot,p1) < dist(pivot,p2);
41+
}
42+
return (o==2);
43+
}
44+
45+
/// returning previous value of top element
46+
inline point nextToTop(stack<point>&st){
47+
point p,res;
48+
p = st.top();
49+
st.pop();
50+
res = st.top();
51+
st.push(p);
52+
return res;
53+
}
54+
55+
int total;
56+
57+
/**
58+
* This function will calculate convexHull points
59+
* All arrays are in 0 based indexing
60+
* @param n total numbers of points
61+
*/
62+
bool convexHull(int n){
63+
ll i,pos=0,in=0,miny = points[0].y,minx = points[0].x;
64+
stack<point>st;
65+
66+
/// Finding bottom-left most point
67+
for(i=0;i<n;i++){
68+
if((miny==points[i].y&&minx>points[i].x)||miny>points[i].y){
69+
minx = points[i].x;
70+
miny = points[i].y;
71+
pos = i;
72+
}
73+
}
74+
75+
///sorting element according to the criteria
76+
swap(points[0],points[pos]);
77+
pivot = points[0];
78+
sort(points+1,points+n,cmp);
79+
80+
///Now removing same angle point
81+
for(i=1;i<n;i++){
82+
while(i<n-1&&orientation(pivot,points[i],points[i+1])==0){
83+
i++;
84+
}
85+
points[++in] = points[i];
86+
}
87+
if(in<2) return 0;
88+
89+
st.push(points[0]);
90+
st.push(points[1]);
91+
st.push(points[2]);
92+
for(i=3;i<=in;i++){
93+
///only valid sequence is ant-clockwise
94+
while(orientation(nextToTop(st),st.top(),points[i])!=2){
95+
st.pop();
96+
}
97+
st.push(points[i]);
98+
}
99+
100+
in = total = st.size();
101+
point tmp;
102+
/// storing convex points
103+
while(!st.empty()){
104+
tmp = st.top();
105+
st.pop();
106+
convex_points[--in] = tmp;
107+
}
108+
return 1;
109+
}
110+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/// *** BIT [Log(n)] space [n]
3+
4+
/** 1 based index
5+
* which functions has inverse function that can be solve bye BIT
6+
* it works like consucative sums but in log(n)
7+
*/
8+
9+
int n=SIZE of space;
10+
void update(int idx,int val)//adding value val to idx index
11+
{
12+
while(idx<=n){
13+
bitree[idx]+=val;
14+
idx+=idx&(-idx); // Last set of digit
15+
}
16+
}
17+
int query(int idx){// returns sum of 1 to idx index
18+
int sum=0;
19+
while(idx>0){
20+
sum+=bitree[idx];
21+
idx-=idx&(-idx);
22+
}
23+
return sum;
24+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
/// *** Disjoint Set Union Find [n||1]
3+
4+
int parent(int n)
5+
{
6+
if(rp[n]==n)return n;
7+
return rp[n]=parent(rp[n]);
8+
}
9+
void setUp(int a,int b){
10+
rp[parent(b)]=parent(a);
11+
}
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/// *** Merge Sort Tree [nlog(n) + query*log(n)*log(n)]
2+
3+
4+
int ara[MX];
5+
vector<int>seg[MX*4];
6+
/// 1 based index
7+
8+
int bns(int n,int val){
9+
/// lower bound 1 2 2 2 3 4
10+
/// then returns 2
11+
int mid,i,j,low=0,high = seg[n].size()-1;
12+
while((high-low)>4){
13+
mid = (low+high)/2;
14+
if(seg[n][mid]<=val) low = mid;
15+
else high = mid - 1;
16+
}
17+
for(low;low<=high&&low<seg[n].size();low++){
18+
if(seg[n][low]>val) break;
19+
}
20+
return seg[n].size()-low; /// numbers greater than value
21+
}
22+
23+
void mergee(int x,int y,int z){ /// merging 2 vector x and y to Z in sorted order
24+
int i,j,k,md,sz;
25+
sz = seg[x].size() + seg[y].size();
26+
for(i=0,j=0,k=0;k<sz;k++){
27+
if(i>=seg[x].size()) seg[z].push_back(seg[y][j++]);
28+
else if(j>=seg[y].size()) seg[z].push_back(seg[x][i++]);
29+
else if(seg[x][i]<seg[y][j]) seg[z].push_back(seg[x][i++]);
30+
else seg[z].push_back(seg[y][j++]);
31+
}
32+
}
33+
34+
/** [low,high] total range :: variable range
35+
* [qlow,qhigh] query range
36+
* pos = current position
37+
*/
38+
void creat(int low,int high,int pos){ /// creating merge sort tree
39+
if(low==high){
40+
seg[pos].push_back(ara[low]);
41+
return ;
42+
}
43+
int mid = (low+high)/2;
44+
creat(low,mid,pos*2);
45+
creat(mid+1,high,pos*2+1);
46+
mergee(pos*2,pos*2+1,pos);
47+
/// merge with stl
48+
/// merge(seg[pos*2].begin() , seg[pos*2].end(), seg[pos*2].begin(), seg[pos*2].end(),back_inserter(seg[pos]));
49+
}
50+
51+
int query(int low,int high,int qlow,int qhigh,int pos,int val){
52+
if(qlow>qhigh) return 0;
53+
if(qlow>high||qhigh<low) return 0;
54+
if(qlow<=low&&qhigh>=high){
55+
return bns(pos,val);
56+
}
57+
int mid = (low + high)/2;
58+
return query(low,mid,qlow,qhigh,pos*2,val) + query(mid+1,high,qlow,qhigh,pos*2+1,val);
59+
}
60+
61+
/// *** For Rnage orders statistics (find k'th number in sorted segment)
62+
63+
vector<pii>input;
64+
vector<int>seg[MX*4];
65+
66+
void creat(int low,int high,int pos){ /// creating merge sort tree
67+
if(low==high){
68+
seg[pos].push_back(input[low-1].second); /// in is 0 based
69+
return ;
70+
}
71+
int mid = (low+high)/2;
72+
creat(low,mid,pos*2);
73+
creat(mid+1,high,pos*2+1);
74+
mergee(pos*2,pos*2+1,pos);
75+
}
76+
77+
/** calculating total number in left range lower than the given index
78+
* if numbers are greater than equals to the searging value than look into left
79+
* searhing on right sub array and substracting left sub arrys given up values
80+
*/
81+
82+
int query(int low,int high,int qlow,int qhigh,int pos,int val)
83+
{
84+
if(low==high) return seg[pos][0];
85+
int mid = (low+high)>>1,left=pos<<1;
86+
87+
int total = upper_bound(seg[left].begin(),seg[left].end(),qhigh) -
88+
lower_bound(seg[left].begin(),seg[left].end(),qlow);
89+
90+
if(total>=val){
91+
return query(low,mid,qlow,qhigh,pos*2,val);
92+
}
93+
else{
94+
return query(mid+1,high,qlow,qhigh,pos*2+1,val-total);
95+
}
96+
}
97+
98+
sort(input.begin(),input.end());
99+
100+
101+
102+

Source/Data Structure/segmet tree.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/// *** Segment Tree [log(total array size)*Query]
2+
3+
/** [ulow,uhigh] Query Range
4+
* [low,high] total range of root
5+
* [qlow,qhigh] Query Range
6+
* Currrent position = pos
7+
* 0 based Index And Root is also 0
8+
*/
9+
10+
int ara[MX],seg[4*MX],lazy[4*MX];
11+
12+
void creat(int low,int high,int pos)
13+
{
14+
if(low==high){
15+
seg[pos] = ara[low]; // reached leaf and update
16+
return ;
17+
}
18+
int mid = (high+low)/2;
19+
creat(low,mid,pos*2+1);
20+
creat(mid+1,high,pos*2+2);
21+
seg[pos] += seg[pos*2+1] + seg[pos*2+2];
22+
}
23+
24+
void update(int low,int high,int ulow,int uhigh,int val,int pos)
25+
{
26+
if(low>high) return ;
27+
if(lazy[pos]!=0){ /// is not propagated yet
28+
seg[pos] = 0;
29+
if(low!=high){ ///if not leaf node
30+
lazy[pos*2+1] += lazy[pos];
31+
lazy[pos*2+2] += lazy[pos];
32+
}
33+
lazy[pos] = 0;
34+
}
35+
36+
if(ulow>high||uhigh<low) return; ///No overlap
37+
if(ulow<=low&&uhigh>=high){ /// Total Overlap
38+
seg[pos] += val;
39+
if(low!=high){
40+
lazy[pos*2+1] += val;
41+
lazy[pos*2+2] += val;
42+
}
43+
return;
44+
}
45+
/// Partial overlap
46+
int mid = (high+low)/2;
47+
48+
update(low,mid,ulow,uhigh,val,pos*2+1);
49+
update(mid+1,high,ulow,uhigh,val,pos*2+2);
50+
seg[pos] = seg[pos*2+1] + seg[pos*2+2]; /// Updating the intermediate node
51+
}
52+
53+
int query(int low,int high,int qlow,int qhigh,int pos)
54+
{
55+
if(low>high) return 0;
56+
if(lazy[pos]!=0){
57+
seg[pos] += lazy[pos];
58+
if(low!=high){
59+
lazy[pos*2+1] += lazy[pos];
60+
lazy[pos*2+2] += lazy[pos];
61+
}
62+
lazy[pos] = 0;
63+
}
64+
65+
if(qlow>high||qhigh<low) return 0;
66+
67+
if(qlow<=low&&qhigh>=high)
68+
return seg[pos];
69+
70+
int mid = (high+low)/2;
71+
72+
return query(low,mid,qlow,qhigh,pos*2+1) + query(mid+1,high,qlow,qhigh,pos*2+2);
73+
}
74+

0 commit comments

Comments
 (0)