Upload basic README.md

master
Michael Reber 3 years ago
parent 8258e10e80
commit f4fa9cdf8e
  1. 3
      assembly/README.md
  2. 43
      c++/Basic/2D Array.cpp
  3. 17
      c++/Basic/Add n numbers.cpp
  4. 19
      c++/Basic/Add subtract multiply divide.cpp
  5. 17
      c++/Basic/Addition of digits.cpp
  6. 28
      c++/Basic/Addition of matrices.cpp
  7. 12
      c++/Basic/Area of a triangle.cpp
  8. 82
      c++/Basic/BellmanFord.cpp
  9. 73
      c++/Basic/Binary Operator Overloading.cpp
  10. 50
      c++/Basic/Binary Searching using recursion.cpp
  11. 19
      c++/Basic/Binary to Decimal.cpp
  12. 17
      c++/Basic/Binary to Hexadecimal.cpp
  13. 17
      c++/Basic/Binary to Octal.cpp
  14. 20
      c++/Basic/BinaryPow.cpp
  15. 20
      c++/Basic/Check Positive or Negative.cpp
  16. 37
      c++/Basic/Comparator.cpp
  17. 39
      c++/Basic/ConvexHull.cpp
  18. 14
      c++/Basic/Current Date.cpp
  19. 19
      c++/Basic/Decimal to Binary.cpp
  20. 18
      c++/Basic/Decimal to Octal.cpp
  21. 47
      c++/Basic/Delete Vowels.cpp
  22. 67
      c++/Basic/Diameter.cpp
  23. 73
      c++/Basic/Dijkstra.cpp
  24. 123
      c++/Basic/DijkstraHeap.cpp
  25. 36
      c++/Basic/DisjointSets.cpp
  26. 137
      c++/Basic/FFT.cpp
  27. 47
      c++/Basic/FenwickTree.cpp
  28. 31
      c++/Basic/FenwickTreeOnMap.cpp
  29. 12
      c++/Basic/Find ASCII value of a character.cpp
  30. 103
      c++/Basic/FindIntersection.cpp
  31. 8
      c++/Basic/Get IP Address.cpp
  32. 36
      c++/Basic/Hcf & Lcm.cpp
  33. 103
      c++/Basic/KdTree.cpp
  34. 65
      c++/Basic/Manacher.cpp
  35. 62
      c++/Basic/Matrix.cpp
  36. 87
      c++/Basic/MaxFlowDinic.cpp
  37. 86
      c++/Basic/MaxMatching.cpp
  38. 121
      c++/Basic/MinCostFlow.cpp
  39. 92
      c++/Basic/MinCostFlowBF.cpp
  40. 19
      c++/Basic/Odd or Even.cpp
  41. 18
      c++/Basic/Pattern 1.cpp
  42. 31
      c++/Basic/Pattern 10.cpp
  43. 27
      c++/Basic/Pattern 2.cpp
  44. 27
      c++/Basic/Pattern 3.cpp
  45. 27
      c++/Basic/Pattern 4.cpp
  46. 27
      c++/Basic/Pattern 5.cpp
  47. 27
      c++/Basic/Pattern 6.cpp
  48. 27
      c++/Basic/Pattern 7.cpp
  49. 34
      c++/Basic/Pattern 8.cpp
  50. 44
      c++/Basic/Pattern 9.cpp
  51. 55
      c++/Basic/Prim.cpp
  52. 43
      c++/Basic/PrimesGenerator.cpp
  53. 53
      c++/Basic/Scanner.cpp
  54. 14
      c++/Basic/Shutdown Computer.cpp
  55. 162
      c++/Basic/SuffixArrayLcp.cpp
  56. 164
      c++/Basic/TreeIsomorphism.cpp
  57. 506
      c++/Basic/bigint-full.cpp
  58. 446
      c++/Basic/bigint.cpp
  59. 41
      c++/Basic/nCr & nPr.cpp
  60. 88
      c++/Basic/polynom-roots.cpp
  61. 70
      c++/Numerical_Problems/C++ Perform to a 2D FFT Inplace Given a Complex 2D Array.cpp
  62. 40
      c++/Numerical_Problems/C++ Program to Check Multiplicability of Two Matrices.cpp
  63. 76
      c++/Numerical_Problems/C++ Program to Check if a Matrix is Invertible.cpp
  64. 54
      c++/Numerical_Problems/C++ Program to Check if it is a Sparse Matrix.cpp
  65. 65
      c++/Numerical_Problems/C++ Program to Compute DFT Coefficients Directly.cpp
  66. 69
      c++/Numerical_Problems/C++ Program to Compute Determinant of a Matrix.cpp
  67. 84
      c++/Numerical_Problems/C++ Program to Compute Discrete Fourier Transform Using Naive Approach.cpp
  68. 73
      c++/Numerical_Problems/C++ Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach.cpp
  69. 25
      c++/Numerical_Problems/C++ Program to Emulate N Dice Roller.cpp
  70. 74
      c++/Numerical_Problems/C++ Program to Find Basis and Dimension of a Matrix.cpp
  71. 137
      c++/Numerical_Problems/C++ Program to Find Closest Pair of Points in an Array.cpp
  72. 83
      c++/Numerical_Problems/C++ Program to Find Factoial of Large Numbers.cpp
  73. 55
      c++/Numerical_Problems/C++ Program to Find Factoial of a Number using Dynamic Programming.cpp
  74. 49
      c++/Numerical_Problems/C++ Program to Find Factoial of a Number using Iteration.cpp
  75. 47
      c++/Numerical_Problems/C++ Program to Find Factorial of a Number using Recursion.cpp
  76. 71
      c++/Numerical_Problems/C++ Program to Find Fibonacci Numbers using Dynamic Programming.cpp
  77. 68
      c++/Numerical_Problems/C++ Program to Find Fibonacci Numbers using Iteration.cpp
  78. 92
      c++/Numerical_Problems/C++ Program to Find Fibonacci Numbers using Matrix Exponentiation.cpp
  79. 62
      c++/Numerical_Problems/C++ Program to Find Fibonacci Numbers using Recursion.cpp
  80. 35
      c++/Numerical_Problems/C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm.cpp
  81. 565
      c++/Numerical_Problems/C++ Program to Find Inverse of a Matrix.cpp
  82. 109
      c++/Numerical_Problems/C++ Program to Find Path Between Two Nodes in a Graph.cpp
  83. 57
      c++/Numerical_Problems/C++ Program to Find the GCD and LCM of n Numbers.cpp
  84. 179
      c++/Numerical_Problems/C++ Program to Generate N Number of Passwords of Length M Each.cpp
  85. 72
      c++/Numerical_Problems/C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram.cpp
  86. 18
      c++/Numerical_Problems/C++ Program to Generate Random Hexadecimal Bytes.cpp
  87. 45
      c++/Numerical_Problems/C++ Program to Generate Random Numbers Using Middle Square Method.cpp
  88. 31
      c++/Numerical_Problems/C++ Program to Generate Random Numbers Using Multiply with Carry Method.cpp
  89. 34
      c++/Numerical_Problems/C++ Program to Generate Random Numbers Using Probability Distribution Function.cpp
  90. 26
      c++/Numerical_Problems/C++ Program to Generate Randomized Sequence of Given Range of Numbers.cpp
  91. 141
      c++/Numerical_Problems/C++ Program to Implement Booth’s Multiplication Algorithm for Multiplication of 2 signed Numbers.cpp
  92. 129
      c++/Numerical_Problems/C++ Program to Implement Coppersmith Freivald’s Algorithm.cpp
  93. 37
      c++/Numerical_Problems/C++ Program to Implement Euler Theorem.cpp
  94. 55
      c++/Numerical_Problems/C++ Program to Implement Extended Eucledian Algorithm.cpp
  95. 67
      c++/Numerical_Problems/C++ Program to Implement Fermat Primality Test.cpp
  96. 49
      c++/Numerical_Problems/C++ Program to Implement Fermat’s Little Theorem.cpp
  97. 48
      c++/Numerical_Problems/C++ Program to Implement Fisher-Yates Algorithm for Array Shuffling.cpp
  98. 80
      c++/Numerical_Problems/C++ Program to Implement Gauss Seidel Method.cpp
  99. 98
      c++/Numerical_Problems/C++ Program to Implement Miller Rabin Primality Test.cpp
  100. 48
      c++/Numerical_Problems/C++ Program to Implement Modular Exponentiation Algorithm.cpp
  101. Some files were not shown because too many files have changed in this diff Show More

@ -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)

@ -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

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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] << " ";
}

@ -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

@ -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.

@ -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";
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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();
}

@ -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;
}

@ -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;
}

@ -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

@ -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;
}

@ -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";
}

@ -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;
}
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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;
}

@ -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();
}

@ -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