Upload basic README.md

This commit is contained in:
Michael Reber 2019-11-18 14:44:36 +01:00
parent 8258e10e80
commit f4fa9cdf8e
867 changed files with 35571 additions and 0 deletions

3
assembly/README.md Normal file
View File

@ -0,0 +1,3 @@
# Assembly language
An assembly language is an extremely low-level programming language that has a 1-to-1 correspondence to machine code — the series of binary instructions which move values in and out of registers in a CPU (or other microprocessor)

43
c++/Basic/2D Array.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
#define size 16
using namespace std;
int main ()
{
int m,n;
int a[size][size];
cout<<"Enter the number of rows"<<endl;
cin>>m;
cout<<"Enter the number of columns"<<endl;
cin>>n;
cout<<"Enter the Elements in Table"<<endl;
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ )
{
cin >>a[i][j];
}
}
// output each array element"s value
for ( int i = 0; i < m; i++ )
for ( int j = 0; j < n; j++ )
{
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}
return 0;
}
Output:
Enter the number of rows
2
Enter the number of columns
3
Enter the Elements in Table
1 2 3 4 5 6
a[0][0]: 1
a[0][1]: 2
a[0][2]: 3
a[1][0]: 4
a[1][1]: 5
a[1][2]: 6

View File

@ -0,0 +1,17 @@
#include < iostream.h >
int main()
{
int n, sum = 0, c, value;
cout<<"Enter the number of integers you want to add\n";
cin>>n;
cout<<"Enter"<<n<<"integers"<<"\n";
for (c = 1; c <= n; c++)
{
cin>>value;
sum = sum + value;
/*adding each no in sum*/
}
cout<<"Sum of entered integers ="<<sum<<"\n";
return 0;
}

View File

@ -0,0 +1,19 @@
#include < iostream.h >
int main()
{
int first, second, add, subtract, multiply;
float divide;
cout<<"Enter two integers\n";
cin>>first>>second;
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second;
//typecasting
cout<<"Sum = "<<add<<"\n";
cout<<"Difference ="<<subtract<<"\n";
cout<<"Multiplication ="<<multiply<<"\n";
cout<<"Division ="<<divide<<"\n";
return 0;
}

View File

@ -0,0 +1,17 @@
#include <iostream>
int main()
{
int n, sum = 0, remainder;
cout<<"Enter an integer\n";
cin>>n;
while(n != 0)
{
remainder = n % 10;
/*stores unit place digit to remainder*/
sum = sum + remainder;
n = n / 10;
/*dividing no to discard unit place digit*/
}
cout<<"Sum of digits of entered number = "<<sum<<endl;
return 0;
}

View File

@ -0,0 +1,28 @@
#include < iostream.h >
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout<<"Enter the number of rows and columns of matrix\n";
cin>>m>>n;
cout<<"Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin>>first[c][d];
cout<<"Enter the elements of second matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin>>second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
/* Matrix addition */
cout<<"Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout<<sum[c][d]<<"\t";
cout<<"\n";
}
return 0;
}

View File

@ -0,0 +1,12 @@
#include < iostream.h >
void main()
{
int height, base;
float ans;/*ans may come in fractions*/
cout<<"Enter height and base";
cin>>height>>base;
ans= (1/2)*height*base;
/* mathematical formula*/
cout<<"Area if triangle is"<<ans;
}

82
c++/Basic/BellmanFord.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <vector>
#include <climits>
#include <iostream>
using namespace std;
typedef pair<int, int> pii;
typedef vector<vector<pii> > Graph;
const int INF = INT_MAX / 3;
bool bellmanFord(Graph &g, int s, vector<int> &prio, vector<int> &pred) {
int n = g.size();
pred.assign(n, -1);
prio.assign(n, INF);
prio[s] = 0;
bool wasChanged = true;
for (int k = 0; k < n; k++) {
wasChanged = false;
for (int u = 0; u < n; u++) {
for (int i = 0; i < (int) g[u].size(); i++) {
int v = g[u][i].first;
int cost = g[u][i].second;
if (prio[v] > prio[u] + cost) {
prio[v] = prio[u] + cost;
pred[v] = u;
wasChanged = true;
}
}
}
if (!wasChanged)
break;
}
// wasChanged is true iff graph has a negative cycle
return wasChanged;
}
vector<int> findNegativeCycle(Graph &g) {
int n = g.size();
vector<int> pred(n, -1);
vector<int> prio(n, INF);
prio[0] = 0;
int last = 0;
for (int k = 0; k < n; k++) {
last = -1;
for (int u = 0; u < n; u++) {
for (int i = 0; i < (int) g[u].size(); i++) {
int v = g[u][i].first;
int cost = g[u][i].second;
if (prio[v] > prio[u] + cost) {
prio[v] = prio[u] + cost;
pred[v] = u;
last = v;
}
}
}
if (last == -1)
return vector<int>();
}
vector<int> path(n);
vector<int> pos(n, -1);
for (int i = 0;; i++) {
if (pos[last] != -1)
return vector<int>(path.rend() - i, path.rend() - pos[last]);
path[i] = last;
pos[last] = i;
last = pred[last];
}
}
int main() {
Graph g(4);
g[0].push_back(make_pair(1, 1));
g[1].push_back(make_pair(0, 1));
g[1].push_back(make_pair(2, 1));
g[2].push_back(make_pair(3, -10));
g[3].push_back(make_pair(1, 1));
vector<int> cycle = findNegativeCycle(g);
for (int i = 0; i < (int) cycle.size(); i++)
cout << cycle[i] << " ";
}

View File

@ -0,0 +1,73 @@
#include < iostream.h >
using namespace std;
class Cube
{
public:
void setLength( double l )
{
length = l;
}
void setBreadth( double b )
{
breadth = b;
}
void setHeight( double h )
{
height = h;
}
double getVolume(void)
{
return length * breadth * height;
}
// Overload + operator to add two Cube objects.
Cube operator+(const Cube& b)
{
Cube C;
C.length = this->length + b.length;
C.breadth = this->breadth + b.breadth;
C.height = this->height + b.height;
return C;
}
private:
double length; // Length of a Cube
double breadth; // Breadth of a Cube
double height; // Height of a Cube
};
// Main function for the program
int main( )
{
Cube C1; // Declare C1 of type Cube
Cube C2; // Declare C2 of type Cube
Cube C3; // Declare C3 of type Cube
double volume = 0.0; // Store the volume of a Cube here
// Cube 1 specification
C1.setLength(4.0);
C1.setBreadth(6.0);
C1.setHeight(5.0);
// Cube 2 specification
C2.setLength(8.0);
C2.setBreadth(4.0);
C2.setHeight(10.0);
// volume of Cube 1
volume = C1.getVolume();
cout << "Volume of Cube 1 : " << volume <<endl;
// volume of Cube 2
volume = C2.getVolume();
cout << "Volume of Cube 2 : " << volume <<endl;
// Add two object as follows:
C3 = C1 + C2;
// volume of Cube 3
volume = C3.getVolume();
cout << "Volume of Cube 3 : " << volume <<endl;
return 0;
}
Output
Volume of Cube 1 : 120
Volume of Cube 2 : 320
Volume of Cube 3 : 1800

View File

@ -0,0 +1,50 @@
#include < iostream.h >
int main()
{
int a[10],i,n,m,c,l,u;
cout<<"Enter the size of an array: ";
cin>>n;
cout<<"Enter the elements of the array: " ;
for(i=0; i < n; i++)
cin>>a[i];
cout<<"Enter the number to be search: ";
cin>>m;
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
cout<<"Number is not found.";
else
cout<<"Number is found.";
return 0;
}
/*Binary search will search element at middle, if element is not found and if element to be searched is less than middle then it will search only in lower part and if greater then in upper part */
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l < = u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else if(m < a[mid])
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
Output:
Enter the size of an array:4
Enter the elements of the array:5 3 6 2
Enter the number to be search:3
Number is found.
Number is found.

View File

@ -0,0 +1,19 @@
#include < iostream.h >
void main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
cout<<"Enter a binary number(1s and 0s) \n";
cin>>amp num;
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
num = num / 10 ;
base = base * 2;
}
cout<<"The Binary number is ="<<binary_val<<" \n";
cout<<"Its decimal equivalent is ="<<decimal_val<<" \n";
}

View File

@ -0,0 +1,17 @@
#include < iostream.h>
int main()
{
long int binaryval, hexadecimalval = 0, i = 1, remainder;
cout<<"Enter the binary number: ";
cin>>binaryval;
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
cout<<"Equivalent hexadecimal value:"<<hexadecimalval;
return 0;
}

View File

@ -0,0 +1,17 @@
#include < iostream.h>
int main()
{
long int binarynum, octalnum = 0, j = 1, remainder;
cout<<"Enter the value for binary number: ";
cin>>binarynum;
while (binarynum != 0)
{
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
cout<<"Equivalent octal value:"<<octalnum;
return 0;
}

20
c++/Basic/BinaryPow.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
int pow(int x, int n, int MOD) {
long long y = x;
int res = 1;
for (; n > 0; n >>= 1) {
if (n & 1)
res = res * y % MOD;
y = y * y % MOD;
}
return res;
}
int main() {
const int MOD = 1000000007;
int x = pow(2, 10, MOD);
cout << x << endl;
}

View File

@ -0,0 +1,20 @@
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr(); //clear screen
int number;
cout<< "Enter an integer: ";
cin>> number;
if ( number >= 0)
{
cout << "You entered a positive integer: "<<number<<endl;
}
else
{
cout<<"You entered a negative integer: "<<number<<endl;
}
cout<<"This statement is always executed because it's outside if...else statement.";
getch();
}

37
c++/Basic/Comparator.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <algorithm>
#include <set>
#include <iostream>
using namespace std;
struct item {
int x, y;
bool operator<(const item &o) const {
return x < o.x || x == o.x && y < o.y;
}
};
struct item_cmp {
bool operator()(const item &a, const item &b) {
return a.x < b.x || a.x == b.x && a.y < b.y;
}
};
bool cmp(const item &a, const item &b) {
return a.x < b.x || a.x == b.x && a.y < b.y;
}
int main() {
item a[] = { { 2, 3 }, { 1, 2 } };
//typedef set<item, bool(*)(const item&, const item&)> myset;
//myset s(a, a + 2, cmp);
//typedef set<item, item_cmp> myset;
typedef set<item> myset;
myset s(a, a + 2);
for (myset::iterator it = s.begin(); it != s.end(); it++) {
cout << it->x << " " << it->y << endl;
}
sort(a, a + 2, cmp);
sort(a, a + 2, item_cmp());
cout << a[0].x << " " << a[0].y << endl;
}

39
c++/Basic/ConvexHull.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<long long, long long> point;
long long cross(const point &a, const point &b, const point &c) {
return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first);
}
vector<point> convexHull(vector<point> points) {
if (points.size() <= 1)
return points;
sort(points.begin(), points.end());
vector<point> h;
for (auto p: points) {
while (h.size() >= 2 && cross(h.end()[-2], h.back(), p) >= 0)
h.pop_back();
h.push_back(p);
}
reverse(points.begin(), points.end());
int upper = h.size();
for (auto p: points) {
while (h.size() > upper && cross(h.end()[-2], h.back(), p) >= 0)
h.pop_back();
h.push_back(p);
}
h.resize(h.size() - 1 - (h[0] == h[1]));
return h;
}
// Usage example
int main() {
vector<point> hull1 = convexHull((vector<point>) {point(0, 0), point(3, 0), point(0, 3), point(1, 1)});
cout << (3 == hull1.size()) << endl;
vector<point> hull2 = convexHull((vector<point>) {point(0, 0), point(0, 0)});
cout << (1 == hull2.size()) << endl;
}

View File

@ -0,0 +1,14 @@
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << "Current Date is : "
cout << now->tm_mday << '-'<< (now->tm_mon + 1) << '-' << (now->tm_year + 1900) << endl;
}
Output
Current Date is : 16-6-2015

View File

@ -0,0 +1,19 @@
#include < iostream.h >
int main()
{
int n, c, k;
cout<<"Enter an integer in decimal number system\n";
cin>>n;
cout<<n<<"in binary number system is:\n";
for (c = 31; c >= 0; c--)
{
k = n >> c;
/*Right shift(Binary Divide by 2)*/
if (k & 1)//k is logically ANDed with 1
cout<<"1";
else
cout<<"0";
}
return 0;
}

View File

@ -0,0 +1,18 @@
#include <iostream.h>
void main()
{
long num, decimal_num, remainder, base = 1, octal = 0;
cout<<"Enter a decimal integer \n";
cin>>amp num;
decimal_num = num;
while (num > 0)
{
remainder = num % 8;
octal = octal + remainder * base;
num = num / 8;
base = base * 10;
}
cout<<"Input number is ="<<decimal_num<<"\n";
cout<<"Its octal equivalent is ="<<Octal<<"\n";
}

View File

@ -0,0 +1,47 @@
#include < iostream.h >
#include < string.h >
int check_vowel(char);
int main()
{
char s[100], t[100];
int i, j = 0;
cout<<"Enter a string to delete vowels\n";
gets(s);
/* In the program we create a new string and process entered string character by character, and if a vowel is found it is not added to new string otherwise the character is added to new string, after the string ends we copy the new string into original string*/
for(i = 0; s[i] != '\0'; i++)
{
if(check_vowel(s[i]) == 0)
{
/* not a vowel */
t[j] = s[i];
j++
;
}
}
t[j] = '\0';
strcpy(s, t);
/* We are changing initial string */
cout<<"String after deleting vowels:"<< s<<"\n";
return 0;
}
int check_vowel(char c)
{
switch(c)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return 1;
default:
return 0;
}
}

67
c++/Basic/Diameter.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;
typedef pair<double, double> point;
bool cw(const point &a, const point &b, const point &c) {
return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first) < 0;
}
vector<point> convexHull(vector<point> p) {
int n = p.size();
if (n <= 1)
return p;
int k = 0;
sort(p.begin(), p.end());
vector<point> q(n * 2);
for (int i = 0; i < n; q[k++] = p[i++])
for (; k >= 2 && !cw(q[k - 2], q[k - 1], p[i]); --k)
;
for (int i = n - 2, t = k; i >= 0; q[k++] = p[i--])
for (; k > t && !cw(q[k - 2], q[k - 1], p[i]); --k)
;
q.resize(k - 1 - (q[0] == q[1]));
return q;
}
double area(const point &a, const point &b, const point &c) {
return abs((b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first));
}
double dist(const point &a, const point &b) {
return hypot(a.first - b.first, a.second - b.second);
}
double diameter(const vector<point> &p) {
vector<point> h = convexHull(p);
int m = h.size();
if (m == 1)
return 0;
if (m == 2)
return dist(h[0], h[1]);
int k = 1;
while (area(h[m - 1], h[0], h[(k + 1) % m]) > area(h[m - 1], h[0], h[k]))
++k;
double res = 0;
for (int i = 0, j = k; i <= k && j < m; i++) {
res = max(res, dist(h[i], h[j]));
while (j < m && area(h[i], h[(i + 1) % m], h[(j + 1) % m]) > area(h[i], h[(i + 1) % m], h[j])) {
res = max(res, dist(h[i], h[(j + 1) % m]));
++j;
}
}
return res;
}
int main() {
vector<point> points(4);
points[0] = point(0, 0);
points[1] = point(3, 0);
points[2] = point(0, 3);
points[3] = point(1, 1);
double d = diameter(points);
cout << d << endl;
}

73
c++/Basic/Dijkstra.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <vector>
#include <queue>
#include <set>
#include <climits>
#include <iostream>
using namespace std;
typedef pair<int, int> pii;
typedef vector<vector<pii> > Graph;
void dijkstra(Graph &g, int s, vector<int> &prio, vector<int> &pred) {
int n = g.size();
prio.assign(n, INT_MAX);
prio[s] = 0;
pred.assign(n, -1);
priority_queue<pii, vector<pii> , greater<pii> > q;
q.push(make_pair(prio[s], s));
while (!q.empty()) {
int d = q.top().first;
int u = q.top().second;
q.pop();
if (d != prio[u])
continue;
for (int i = 0; i < (int) g[u].size(); i++) {
int v = g[u][i].first;
int nprio = prio[u] + g[u][i].second;
if (prio[v] > nprio) {
prio[v] = nprio;
pred[v] = u;
q.push(make_pair(nprio, v));
}
}
}
}
void dijkstra2(Graph &g, int s, vector<int> &prio, vector<int> &pred) {
int n = g.size();
prio.assign(n, INT_MAX);
prio[s] = 0;
pred.assign(n, -1);
set<pii> q;
q.insert(make_pair(prio[s], s));
while (!q.empty()) {
int u = q.begin()->second;
q.erase(q.begin());
for (int i = 0; i < (int) g[u].size(); ++i) {
int v = g[u][i].first;
int nprio = prio[u] + g[u][i].second;
if (prio[v] > nprio) {
q.erase(make_pair(prio[v], v));
prio[v] = nprio;
pred[v] = u;
q.insert(make_pair(prio[v], v));
}
}
}
}
int main() {
Graph g(3);
g[0].push_back(make_pair(1, 10));
g[1].push_back(make_pair(2, -5));
g[0].push_back(make_pair(2, 8));
vector<int> prio;
vector<int> pred;
dijkstra(g, 0, prio, pred);
for (int i = 0; i < prio.size(); i++)
cout << prio[i] << endl;
}

123
c++/Basic/DijkstraHeap.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <algorithm>
#include <climits>
#include <iostream>
using namespace std;
const int maxnodes = 200000;
const int maxedges = 1000000;
// graph
int edges;
int last[maxnodes], head[maxedges], previous[maxedges], len[maxedges];
int prio[maxnodes], pred[maxnodes];
void graphClear() {
fill(last, last + maxnodes, -1);
edges = 0;
}
void addEdge(int u, int v, int length) {
head[edges] = v;
len[edges] = length;
previous[edges] = last[u];
last[u] = edges++;
}
// heap
int h[maxnodes];
int pos2Id[maxnodes];
int id2Pos[maxnodes];
int hsize;
void hswap(int i, int j) {
swap(h[i], h[j]);
swap(pos2Id[i], pos2Id[j]);
swap(id2Pos[pos2Id[i]], id2Pos[pos2Id[j]]);
}
void moveUp(int pos) {
while (pos > 0) {
int parent = (pos - 1) >> 1;
if (h[pos] >= h[parent]) {
break;
}
hswap(pos, parent);
pos = parent;
}
}
void add(int id, int prio) {
h[hsize] = prio;
pos2Id[hsize] = id;
id2Pos[id] = hsize;
moveUp(hsize++);
}
void increasePriority(int id, int prio) {
int pos = id2Pos[id];
h[pos] = prio;
moveUp(pos);
}
void moveDown(int pos) {
while (pos < (hsize >> 1)) {
int child = 2 * pos + 1;
if (child + 1 < hsize && h[child + 1] < h[child]) {
++child;
}
if (h[pos] <= h[child]) {
break;
}
hswap(pos, child);
pos = child;
}
}
int removeMin() {
int res = pos2Id[0];
int lastNode = h[--hsize];
if (hsize > 0) {
h[0] = lastNode;
int id = pos2Id[hsize];
id2Pos[id] = 0;
pos2Id[0] = id;
moveDown(0);
}
return res;
}
void dijkstra(int s) {
fill(pred, pred + maxnodes, -1);
fill(prio, prio + maxnodes, INT_MAX);
prio[s] = 0;
hsize = 0;
add(s, prio[s]);
while (hsize) {
int u = removeMin();
for (int e = last[u]; e >= 0; e = previous[e]) {
int v = head[e];
int nprio = prio[u] + len[e];
if (prio[v] > nprio) {
if (prio[v] == INT_MAX)
add(v, nprio);
else
increasePriority(v, nprio);
prio[v] = nprio;
pred[v] = u;
}
}
}
}
int main() {
graphClear();
addEdge(0, 1, 10);
addEdge(1, 2, -5);
addEdge(0, 2, 8);
dijkstra(0);
for (int i = 0; i < 3; i++)
cout << prio[i] << endl;
}

View File

@ -0,0 +1,36 @@
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 200000;
int Rank[maxn];
int p[maxn];
int n;
void init(int n) {
::n = n;
fill(Rank, Rank + n, 0);
for (int i = 0; i < n; i++) p[i] = i;
}
int root(int x) {
return x == p[x] ? x : (p[x] = root(p[x]));
}
void unite(int a, int b) {
a = root(a);
b = root(b);
if (a == b) return;
if (Rank[a] < Rank[b]) swap(a, b);
if (Rank[a] == Rank[b]) ++Rank[a];
p[b] = a;
}
int main() {
init(3);
unite(0, 2);
cout << (0 == root(0)) << endl;
cout << (1 == root(1)) << endl;
cout << (0 == root(2)) << endl;
}

137
c++/Basic/FFT.cpp Normal file
View File

@ -0,0 +1,137 @@
// https://web.stanford.edu/~liszt90/acm/notebook.html#file16
// Fast Fourier Transform : Used in many applications(one is fast polynomial multiplication)
#include <cassert>
#include <cstdio>
#include <cmath>
struct cpx
{
cpx(){}
cpx(double aa):a(aa),b(0){}
cpx(double aa, double bb):a(aa),b(bb){}
double a;
double b;
double modsq(void) const
{
return a * a + b * b;
}
cpx bar(void) const
{
return cpx(a, -b);
}
};
cpx operator +(cpx a, cpx b)
{
return cpx(a.a + b.a, a.b + b.b);
}
cpx operator *(cpx a, cpx b)
{
return cpx(a.a * b.a - a.b * b.b, a.a * b.b + a.b * b.a);
}
cpx operator /(cpx a, cpx b)
{
cpx r = a * b.bar();
return cpx(r.a / b.modsq(), r.b / b.modsq());
}
cpx EXP(double theta)
{
return cpx(cos(theta),sin(theta));
}
const double two_pi = 4 * acos(0);
// in: input array
// out: output array
// step: {SET TO 1} (used internally)
// size: length of the input/output {MUST BE A POWER OF 2}
// dir: either plus or minus one (direction of the FFT)
// RESULT: out[k] = \sum_{j=0}^{size - 1} in[j] * exp(dir * 2pi * i * j * k / size)
void FFT(cpx *in, cpx *out, int step, int size, int dir)
{
if(size < 1) return;
if(size == 1)
{
out[0] = in[0];
return;
}
FFT(in, out, step * 2, size / 2, dir);
FFT(in + step, out + size / 2, step * 2, size / 2, dir);
for(int i = 0 ; i < size / 2 ; i++)
{
cpx even = out[i];
cpx odd = out[i + size / 2];
out[i] = even + EXP(dir * two_pi * i / size) * odd;
out[i + size / 2] = even + EXP(dir * two_pi * (i + size / 2) / size) * odd;
}
}
// Usage:
// f[0...N-1] and g[0..N-1] are numbers
// Want to compute the convolution h, defined by
// h[n] = sum of f[k]g[n-k] (k = 0, ..., N-1).
// Here, the index is cyclic; f[-1] = f[N-1], f[-2] = f[N-2], etc.
// Let F[0...N-1] be FFT(f), and similarly, define G and H.
// The convolution theorem says H[n] = F[n]G[n] (element-wise product).
// To compute h[] in O(N log N) time, do the following:
// 1. Compute F and G (pass dir = 1 as the argument).
// 2. Get H by element-wise multiplying F and G.
// 3. Get h by taking the inverse FFT (use dir = -1 as the argument)
// and *dividing by N*. DO NOT FORGET THIS SCALING FACTOR.
int main(void)
{
printf("If rows come in identical pairs, then everything works.\n");
cpx a[8] = {0, 1, cpx(1,3), cpx(0,5), 1, 0, 2, 0};
cpx b[8] = {1, cpx(0,-2), cpx(0,1), 3, -1, -3, 1, -2};
cpx A[8];
cpx B[8];
FFT(a, A, 1, 8, 1);
FFT(b, B, 1, 8, 1);
for(int i = 0 ; i < 8 ; i++)
{
printf("%7.2lf%7.2lf", A[i].a, A[i].b);
}
printf("\n");
for(int i = 0 ; i < 8 ; i++)
{
cpx Ai(0,0);
for(int j = 0 ; j < 8 ; j++)
{
Ai = Ai + a[j] * EXP(j * i * two_pi / 8);
}
printf("%7.2lf%7.2lf", Ai.a, Ai.b);
}
printf("\n");
cpx AB[8];
for(int i = 0 ; i < 8 ; i++)
AB[i] = A[i] * B[i];
cpx aconvb[8];
FFT(AB, aconvb, 1, 8, -1);
for(int i = 0 ; i < 8 ; i++)
aconvb[i] = aconvb[i] / 8;
for(int i = 0 ; i < 8 ; i++)
{
printf("%7.2lf%7.2lf", aconvb[i].a, aconvb[i].b);
}
printf("\n");
for(int i = 0 ; i < 8 ; i++)
{
cpx aconvbi(0,0);
for(int j = 0 ; j < 8 ; j++)
{
aconvbi = aconvbi + a[j] * b[(8 + i - j) % 8];
}
printf("%7.2lf%7.2lf", aconvbi.a, aconvbi.b);
}
printf("\n");
return 0;
}

47
c++/Basic/FenwickTree.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
using namespace std;
const int maxn = 200000;
int t[maxn];
void add(int t[], int i, int value) {
for (; i < maxn; i |= i + 1)
t[i] += value;
}
// sum[0,i]
int sum(int t[], int i) {
int res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
res += t[i];
return res;
}
// Returns min(p|sum[0,p]>=sum)
int lower_bound(int t[], int sum) {
--sum;
int pos = -1;
for (int blockSize = 1 << 30; blockSize != 0; blockSize >>= 1) {
if (blockSize > maxn) continue;
int nextPos = pos + blockSize;
if (nextPos < maxn && sum >= t[nextPos]) {
sum -= t[nextPos];
pos = nextPos;
}
}
return pos + 1;
}
// Usage example
int main() {
add(t, 0, 4);
add(t, 1, 5);
add(t, 2, 5);
add(t, 2, 5);
cout << (4 == sum(t, 0)) << endl;
cout << (19 == sum(t, 2)) << endl;
cout << (2 == lower_bound(t, 19)) << endl;
cout << (maxn == lower_bound(t, 20)) << endl;
}

View File

@ -0,0 +1,31 @@
#include <iostream>
#include <map>
using namespace std;
const int n = 2000000000;
void add(map<int, int> &t, int i, int value) {
for (; i < n; i |= i + 1)
t[i] += value;
}
// sum[0,i]
int sum(map<int, int> &t, int i) {
int res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
if (t.count(i)) res += t[i];
return res;
}
// Usage example
int main() {
map<int, int> t;
add(t, 0, 4);
add(t, 1, 5);
add(t, 2, 5);
add(t, 2, 5);
cout << (4 == sum(t, 0)) << endl;
cout << (19 == sum(t, 2)) << endl;
cout << (19 == sum(t, 1000000000)) << endl;
}

View File

@ -0,0 +1,12 @@
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
getch();
}

View File

@ -0,0 +1,103 @@
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef pair<int, int> pii;
int cross(int ax, int ay, int bx, int by, int cx, int cy) {
return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
}
int cross(pii a, pii b, pii c) {
return cross(a.first, a.second, b.first, b.second, c.first, c.second);
}
class segment {
public:
pii a, b;
int id;
segment(pii a, pii b, int id) :
a(a), b(b), id(id) {
}
bool operator<(const segment &o) const {
if (a.first < o.a.first) {
int s = cross(a, b, o.a);
return (s > 0 || s == 0 && a.second < o.a.second);
} else if (a.first > o.a.first) {
int s = cross(o.a, o.b, a);
return (s < 0 || s == 0 && a.second < o.a.second);
}
return a.second < o.a.second;
}
};
class event {
public:
pii p;
int id;
int type;
event(pii p, int id, int type) :
p(p), id(id), type(type) {
}
bool operator<(const event &o) const {
return p.first < o.p.first || p.first == o.p.first && (type > o.type || type == o.type && p.second < o.p.second);
}
};
bool intersect(segment s1, segment s2) {
int x1 = s1.a.first, y1 = s1.a.second, x2 = s1.b.first, y2 = s1.b.second;
int x3 = s2.a.first, y3 = s2.a.second, x4 = s2.b.first, y4 = s2.b.second;
if (max(x1, x2) < min(x3, x4) || max(x3, x4) < min(x1, x2) || max(y1, y2) < min(y3, y4) || max(y3, y4) < min(y1, y2)) {
return false;
}
int z1 = (x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1);
int z2 = (x4 - x1) * (y2 - y1) - (y4 - y1) * (x2 - x1);
if (z1 < 0 && z2 < 0 || z1 > 0 && z2 > 0) {
return false;
}
int z3 = (x1 - x3) * (y4 - y3) - (y1 - y3) * (x4 - x3);
int z4 = (x2 - x3) * (y4 - y3) - (y2 - y3) * (x4 - x3);
if (z3 < 0 && z4 < 0 || z3 > 0 && z4 > 0) {
return false;
}
return true;
}
pii findIntersection(vector<segment> s) {
int n = s.size();
vector<event> e;
for (int i = 0; i < n; ++i) {
if (s[i].a > s[i].b)
swap(s[i].a, s[i].b);
e.push_back(event(s[i].a, i, 1));
e.push_back(event(s[i].b, i, -1));
}
sort(e.begin(), e.end());
set<segment> q;
for (int i = 0; i < n * 2; ++i) {
int id = e[i].id;
if (e[i].type == 1) {
set<segment>::iterator it = q.lower_bound(s[id]);
if (it != q.end() && intersect(*it, s[id]))
return make_pair(it->id, s[id].id);
if (it != q.begin() && intersect(*--it, s[id]))
return make_pair(it->id, s[id].id);
q.insert(s[id]);
} else {
set<segment>::iterator it = q.lower_bound(s[id]), next = it, prev = it;
if (it != q.begin() && it != --q.end()) {
++next, --prev;
if (intersect(*next, *prev))
return make_pair(next->id, prev->id);
}
q.erase(it);
}
}
return make_pair(-1, -1);
}
int main() {
}

View File

@ -0,0 +1,8 @@
#include<stdlib.h>
int main()
{
system("C:\\Windows\\System32\\ipconfig");
/* ipconfig command to get ip of system */
return 0;
}

36
c++/Basic/Hcf & Lcm.cpp Normal file
View File

@ -0,0 +1,36 @@
#include < iostream.h >
long gcd(long, long);
int main()
{
long x, y, hcf, lcm;
cout<<"Enter two integers\n";
cin>>x>>y;
hcf = gcd(x, y);
lcm = (x*y)/hcf;
cout<<"Greatest common divisor of "<<x <<"and"<<y<<" = "<<hcf<<"\n";
cout<<"Least common multiple of "<< x <<"and "<< y<<"= "<<lcm<<"\n";