Skip to content

Commit cf73115

Browse files
committed
Algorithms
1 parent 3a983f2 commit cf73115

File tree

6 files changed

+347
-0
lines changed

6 files changed

+347
-0
lines changed

Graphs/DijkstraAlgo.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <bits/stdc++.h>
2+
#define INF 0x3f3f3f3f
3+
using namespace std;
4+
5+
typedef pair<int,int> iPair;
6+
7+
class Graph
8+
{
9+
int V;
10+
std::vector<iPair> *v;
11+
public:
12+
Graph(int V){
13+
this->V = V;
14+
v = new vector<iPair>[V];
15+
}
16+
17+
void addEdge(int a,int b,int d){
18+
v[a].push_back(make_pair(b,d));
19+
v[b].push_back(make_pair(a,d));
20+
}
21+
22+
void shortestPath(int src);
23+
};
24+
25+
void Graph::shortestPath(int src){
26+
priority_queue < iPair , std::vector<iPair> , greater<iPair> > pq;
27+
std::vector<int> distance(V,INF);
28+
29+
pq.push(make_pair(0,src));
30+
distance[src] = 0;
31+
while(!pq.empty()){
32+
int u = pq.top().second;
33+
pq.pop();
34+
35+
for(int i=0;i<v[u].size();i++){
36+
int vertex = v[u][i].first;
37+
int dist = v[u][i].second;
38+
39+
if(distance[vertex] > dist + distance[u]){
40+
distance[vertex] = dist + distance[u];
41+
pq.push(make_pair(distance[vertex],vertex));
42+
}
43+
}
44+
}
45+
46+
cout << "Shortest path of all the nodes from " << src << " node is\n";
47+
for(int i=0;i<V;i++){
48+
cout << i << " " << distance[i] << endl;
49+
}
50+
}
51+
52+
int main(int argc, char const *argv[])
53+
{
54+
int V=9;
55+
Graph g(V);
56+
57+
g.addEdge(0, 1, 4);
58+
g.addEdge(0, 7, 8);
59+
g.addEdge(1, 2, 8);
60+
g.addEdge(1, 7, 11);
61+
g.addEdge(2, 3, 7);
62+
g.addEdge(2, 8, 2);
63+
g.addEdge(2, 5, 4);
64+
g.addEdge(3, 4, 9);
65+
g.addEdge(3, 5, 14);
66+
g.addEdge(4, 5, 10);
67+
g.addEdge(5, 6, 2);
68+
g.addEdge(6, 7, 1);
69+
g.addEdge(6, 8, 6);
70+
g.addEdge(7, 8, 7);
71+
72+
g.shortestPath(0);
73+
return 0;
74+
}

Graphs/a.out

37.9 KB
Binary file not shown.

Mo-sAlgorithm.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
pair< pair<int,int> , int> queries[100050];
5+
long long current_answer;
6+
long long cnt[100050];
7+
long long answers[100050];
8+
int BLOCK_SIZE;
9+
int arr[100050];
10+
11+
bool mo_comp(const pair< pair<int,int>,int > &x,const pair< pair<int,int>,int > &y){
12+
int block_x = x.first.first/BLOCK_SIZE;
13+
int block_y = y.first.first/BLOCK_SIZE;
14+
if(block_x != block_y)
15+
return block_x < block_y ;
16+
return x.first.second < y.first.second;
17+
}
18+
19+
void add(int x){
20+
current_answer -= cnt[x] * cnt[x] * x ;
21+
cnt[x]++;
22+
current_answer += cnt[x] * cnt[x] * x ;
23+
}
24+
25+
void remove(int x){
26+
current_answer -= cnt[x] * cnt[x] * x;
27+
cnt[x]--;
28+
current_answer += cnt[x] * cnt[x] * x;
29+
}
30+
31+
int main(int argc, char const *argv[])
32+
{
33+
ios_base::sync_with_stdio(false);
34+
cin.tie(NULL);
35+
int N,Q;
36+
cin >> N >> Q;
37+
for(int i=0;i<N;i++)
38+
cin >> arr[i];
39+
BLOCK_SIZE = sqrt(N);
40+
for(int i=0;i<Q;i++){
41+
cin >> queries[i].first.first >> queries[i].first.second ;
42+
queries[i].second = i;
43+
}
44+
sort(queries,queries+Q,mo_comp);
45+
46+
int mo_left = 0,mo_right = -1;
47+
for(int i=0;i<Q;i++){
48+
int left = queries[i].first.first;
49+
int right = queries[i].first.second;
50+
51+
while(mo_right < right){
52+
mo_right++;
53+
add(arr[mo_right]);
54+
}
55+
56+
while(mo_right > right){
57+
remove(arr[mo_right]);
58+
mo_right--;
59+
}
60+
61+
while(mo_left < left){
62+
remove(arr[mo_left]);
63+
mo_left++;
64+
}
65+
66+
while(mo_left > left){
67+
mo_left--;
68+
add(arr[mo_left]);
69+
}
70+
answers[queries[i].second] = current_answer;
71+
}
72+
for(int i=0;i<Q;i++){
73+
cout << answers[i] << '\n';
74+
}
75+
return 0;
76+
}

SegmentedSieve.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include<iostream>
2+
#include<math.h>
3+
#include<string.h>
4+
#include<stdlib.h>
5+
6+
#define DIFF_SIZE 100000
7+
using namespace std;
8+
9+
int myPrimes[DIFF_SIZE];
10+
int cnt;
11+
12+
void populateMyPrimes(int N) // i need to find the primes upto root(N)
13+
{
14+
int i=0,j=0;
15+
bool primeArray[DIFF_SIZE];
16+
for(i=2;i<DIFF_SIZE;i++)
17+
{
18+
primeArray[i]=true;
19+
myPrimes[i]=0;//myPrimes[] array will hold our primes calculated via traditional sieve
20+
}
21+
int myRange=floor(sqrt((double)N)); //we need to calculate prime numbers till myRange
22+
//TRADITIONAL SIEVE STARTS HERE
23+
24+
int k=floor(sqrt((double)myRange));//to calculate prime numbers till myRange,loop needs to run till sqrt(myRange)
25+
for(i=2;i<=k;i=i++)
26+
{
27+
if(primeArray[i]==true)
28+
{
29+
for(j=i*i;j<=myRange;j=j+i)
30+
{
31+
primeArray[j]=false; // j is a composite number
32+
}
33+
}
34+
}
35+
cnt=0; //stores the number of primes till sqrt(N)
36+
for(i=2;i<=myRange;i++)
37+
{
38+
if(primeArray[i] == true) // false means composite number,true means prime number
39+
{
40+
myPrimes[cnt++]=i; //store the primes in myPrimes[]
41+
}
42+
}
43+
cout<<endl;
44+
}
45+
46+
int main()
47+
{
48+
int T;//the number of test cases
49+
scanf("%d",&T);
50+
int i,p=0,s;
51+
52+
int N;//larger number
53+
int M;//smaller number
54+
bool primesNow[DIFF_SIZE];
55+
56+
while(T--)
57+
{
58+
scanf("%d %d",&M,&N); //input the range
59+
60+
for(i=0;i<DIFF_SIZE;i++)
61+
primesNow[i]=true; //initialise to all true . we will mark the composite number as false and then
62+
//the remaining true numbers will be prime
63+
64+
populateMyPrimes(N); //this populates the primes in sqrt(N) in an array myPrimes
65+
66+
for(i=0;i<cnt;i++) //for each prime in sqrt(N) we need to use it in the segmented sieve process
67+
{
68+
p=myPrimes[i]; //store the prime
69+
s=M/p;
70+
s=s*p; //the closest number less than M that is a composite number for this prime p
71+
72+
for(int j=s;j<=N;j=j+p)
73+
{
74+
if(j<M) continue; //because composite numbers less than M are of no concern to use.
75+
primesNow[j-M]=false;//j-M = index in the array primesNow,this is because max index allowed in the array
76+
//is not N ,it is DIFF_SIZE so we are storing the numbers offset from M
77+
//while printing we will add M and print to get the actual number
78+
}
79+
}
80+
81+
for(int i=0;i<cnt;i++) //in the above loop the first prime numbers for example say 2,3 are also set to false
82+
{ //hence we need to print them in case they are within range.
83+
if(myPrimes[i]>=M && myPrimes[i]<=N) //without this loop you will see that for an range(1,30), 2 and 3 does
84+
cout<<myPrimes[i]<<endl; //not get printed
85+
}
86+
87+
for(int i=0;i<N-M+1;++i) // primesNow[]=false for all composite numbers,so prime numbers can be found by checking with true
88+
{
89+
if(primesNow[i] == true && (i+M)!=1) //i+M != 1 to ensure that for i=0 and M=1 , 1 is not considered a prime number
90+
cout<<i+M<<endl; //print our prime numbers in the range
91+
}
92+
}
93+
return 0;
94+
}

SieveOfEratosthenes.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int isPrime[1000005];
4+
int minPrime[1000005];
5+
void sieveOfEratosthenes(int n){
6+
for(int i=0;i<=n;i++)
7+
isPrime[i] = 1;
8+
isPrime[0] = isPrime[1] = 0;
9+
for(int i=2;i<=n;i++){
10+
if(isPrime[i] == 1){
11+
for(int j=i*i;j<=n;j+=i)
12+
isPrime[j]=0;
13+
}
14+
}
15+
return ;
16+
}
17+
18+
//fast factorization
19+
std::vector<int> v;
20+
int k;
21+
std::vector<int> factorization(int n){
22+
std::vector<int> res;
23+
//res.push_back(1);
24+
for(int i=2;i*i<=n;i++){
25+
while(n%i == 0){
26+
res.push_back(i);
27+
n/=i;
28+
}
29+
}
30+
if(n!=1) res.push_back(n);
31+
return res;
32+
}
33+
34+
// void function(int n){
35+
// int minPrime[n+1];
36+
// for(int i=0;i<=n;i++)
37+
// minPrime[i] = !isPrime[i];
38+
// for(int i=2;i*i<=n;i++){
39+
// if(minPrime[i] == 0){
40+
// for(int j=i*i;j<=n;j+=i){
41+
// if(minPrime[j] == )
42+
// minPrime[j] = ;
43+
// }
44+
// }
45+
// }
46+
// }
47+
48+
// log(n) factorization
49+
std::vector<int> factorizationlog(int n){
50+
for(int i=0;i<=n;i++)
51+
minPrime[i] = 0;
52+
minPrime[0] = minPrime[1] = 1;
53+
for(int i=2;i*i<=n;i++){
54+
if(minPrime[i] == 0){ /// if i is prime
55+
for(int j=i*i;j<=n;j+=i){
56+
if(minPrime[j] == 0)
57+
minPrime[j] = i;
58+
}
59+
}
60+
}
61+
62+
for(int i=2;i<=n;i++){
63+
if(minPrime[i] == 0)
64+
minPrime[i] = i;
65+
}
66+
67+
std::vector<int> res;
68+
std::map<int,int> m;
69+
std::map<int,int>::iterator it;
70+
while(n != 1){
71+
res.push_back(minPrime[n]);
72+
m[minPrime[n]]++;
73+
n /= minPrime[n];
74+
}
75+
cout << "map is \n";
76+
for(it=m.begin();it!=m.end();it++){
77+
cout << it->first << " " << it->second << endl;
78+
}
79+
cout << endl;
80+
return res;
81+
}
82+
83+
84+
int main(int argc, char const *argv[])
85+
{
86+
int n;
87+
cin >> n;
88+
sieveOfEratosthenes(n);
89+
for(int i=2;i<=n;i++){
90+
if(isPrime[i])
91+
cout << i << " ";
92+
}
93+
cout << "\n";
94+
std::vector<int> factors = factorization(n);
95+
for(int i=0;i<factors.size();i++)
96+
cout << factors[i] << " ";
97+
cout << endl;
98+
//cout << n << "\n";
99+
std::vector<int> factors2 = factorizationlog(n);
100+
for(int i=0;i<factors2.size();i++)
101+
cout << factors2[i] << " ";
102+
return 0;
103+
}

a.out

-7.55 KB
Binary file not shown.

0 commit comments

Comments
 (0)