Guest User

Untitled

a guest
Jan 19th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.37 KB | None | 0 0
  1. # NoteBook
  2.  
  3. * Template
  4.  
  5. ```c++
  6. #include <bits/stdc++.h>
  7. #define all(x) x.begin(),x.end()
  8. #define ff first
  9. #define ss second
  10.  
  11. using namespace std;
  12.  
  13. typedef long long ll;
  14. typedef vector<ll> vi;
  15. typedef pair<ll,ll> pii;
  16. typedef vector<pii> vii;
  17. const ll INF = numeric_limits<ll>::max();
  18.  
  19.  
  20. int main()
  21. {
  22. ios::sync_with_stdio(false);
  23. cin.tie(NULL);
  24.  
  25.  
  26. return 0;
  27. }
  28. ```
  29.  
  30. ## Problem Solving Paradigms
  31.  
  32. ### Binary Search
  33.  
  34. * Implementation
  35.  
  36. ```c++
  37. ll binarySearch(vi array, ll value) {
  38. ll left = 0;
  39. ll right = array.size() - 1;
  40. ll middle;
  41. while (left <= right) {
  42. middle = (left + right) / 2;
  43. if (value == array[middle])
  44. return middle;
  45. if (value < array[middle])
  46. right = middle - 1;
  47. else
  48. left = middle + 1;
  49. }
  50. return -1; // not found
  51. }
  52. ```
  53.  
  54. * Lower bound
  55.  
  56. ```c++
  57. ll lowerBound(vi array, ll value) {
  58. ll left = 0;
  59. ll right = array.size(); // not n - 1;
  60. ll middle;
  61. while (left < right) {
  62. middle = (left + right) / 2;
  63. if (value <= array[middle])
  64. right = middle + 1;
  65. else
  66. left = middle;
  67. }
  68. return left;
  69. }
  70. ```
  71.  
  72. ### Bit Manipulation
  73.  
  74. | X | Y | X | Y | X & Y | X ^ Y |
  75. |:---:|:---:|:-----:|:-----:|:-----:|
  76. | 0 | 0 | 0 | 0 | 0 |
  77. | 0 | 1 | 1 | 0 | 1 |
  78. | 1 | 0 | 1 | 0 | 1 |
  79. | 1 | 1 | 1 | 1 | 0 |
  80.  
  81.  
  82. #### Aplications
  83.  
  84. * Count the number of ones in the binary representation of the given number
  85.  
  86. ```c++
  87. ll numberBits(ll x) {
  88. ll count = 0;
  89. while (x) {
  90. x &= (x - 1); // To turn off of the rightmost bits
  91. count++;
  92. }
  93. return count;
  94. }
  95. ```
  96.  
  97. * How to generate all subsets of a set
  98.  
  99. ```c++
  100. void subsets(vi array, ll n) {
  101. for (ll i = 0; i < (1 << n); i++) {
  102. for (ll j = 0; j < n; j++)
  103. if (i & (1 << j))
  104. cout << array[j] << ' ';
  105. cout << '\n';
  106. }
  107. }
  108. ```
  109.  
  110. #### Trick with bits
  111.  
  112. * `x << 1` is equal that `x * 2`
  113. * `x >> 1` is equal that `x / 2`
  114. * `x >> 2` is equal that `x / 4`
  115. * `x | (1 << j)` To turn on the `j-th` bit of the x
  116. * `x & (1 << j)` To check if the `j-th` bit of `x` is on
  117. * `x & ~(1 << j)` To turn off the `j-th` bit of `x`
  118. * `x ^ (1 << j)` To toggle (flip the status of) of the `j-th` bit of the x
  119. * `x & (-x)` To get the value of the least significant bits (first of the right)
  120. * `(1 << x) - 1` To turn on *all* bits in a set of size `x`
  121. * `x && !(x & (x - 1))` To check if x is power of 2
  122.  
  123.  
  124. ## Math
  125.  
  126. ### Number Theory
  127.  
  128. * Sieve of Erastothenes
  129.  
  130. ```c++
  131. const ll MAX_N = ll(1e7);
  132. bitset<MAX_N> sieve;
  133. sieve.set(); // all bits in true
  134. vi primes;
  135. primes.reserve(MAX_N / log(MAX_N));
  136. for (ll i = 2; i < MAX_N; i++)
  137. if (sieve[i]) {
  138. for (ll j = i * i; j < MAX_N; j += i)
  139. sieve[j] = false;
  140. primes.push_back(i);
  141. }
  142. ```
  143.  
  144. * Functions Involving Prime Factors
  145.  
  146. * Count the number of different prime factors of `n`
  147.  
  148. ```c++
  149. void numPF(ll n) {
  150. vi factors;
  151. for (ll pf: primes) {
  152. if (pf * pf > n)
  153. break;
  154. while (n % pf == 0) {
  155. n /= pf;
  156. factors.push_back(pf);
  157. }
  158. }
  159.  
  160. if (n != 1)
  161. factors.push_back(pf);
  162. }
  163. ```
  164.  
  165. * Count the number of divisor of `n`
  166.  
  167. ```c++
  168. ll numDiv(ll n) {
  169. ll power, answer = 1;
  170. for (ll pf: primes) {
  171. if (pf * pf > n)
  172. break;
  173. power = 0;
  174. while (n % pf == 0) {
  175. n /= pf;
  176. power++;
  177. }
  178. answer *= (power + 1);
  179. }
  180.  
  181. if (n != 1)
  182. answer *= 2;
  183. return answer;
  184. }
  185. ```
  186.  
  187. * Sum of divisors of `n`
  188.  
  189. ```c++
  190. ll numDiv(ll n) {
  191. ll power, answer = 1;
  192. for (ll pf: primes) {
  193. if (pf * pf > n)
  194. break;
  195. power = 0;
  196. while (n % pf == 0) {
  197. n /= pf;
  198. power++;
  199. }
  200. answer *= (pow(pf, power + 1) - 1) / (pf - 1);
  201. }
  202.  
  203. if (n != 1)
  204. answer *= (pow(pf, 2) - 1) / (n - 1);
  205. return answer;
  206. }
  207. ```
  208.  
  209. * Count the number of positive integers < `n` that are relatively prime to `n`.
  210.  
  211. ```c++
  212. ll eulerPhi(ll n) {
  213. ll answer;
  214. for (ll pf: primes) {
  215. if (pf * pf > n)
  216. break;
  217. if (n % pf == 0)
  218. answer -= answer / pf;
  219.  
  220. while (n % pf == 0)
  221. n /= pf;
  222. }
  223.  
  224. if (n != 1)
  225. answer -= answer / n;
  226. }
  227. ```
  228.  
  229. * Pollard's rho Integer Factoring Algorithm find a divisor of
  230. n. This is used for factoring integers with 64 bits.
  231. Pollard's rho can factor an integer `n` if `n` is a large prime or
  232. is one.
  233.  
  234. ```c++
  235. ll mulmod(ll a, ll b, ll c) { // return (a * b) % c
  236. ll x = 0, y = a % c;
  237. while (b > 0) {
  238. if (b % 2 == 1) // odd
  239. x = (x + y) % c
  240. y = (y * 2) % c;
  241. b /= 2;
  242. }
  243. return x % c;
  244. }
  245.  
  246. ll pollardRho(ll n) {
  247. ll i = 0, k = 2;
  248. ll x = 3, y = 3;
  249. while (true) {
  250. i++;
  251. x = (mulmod(x, x, n) + n - 1) % n;
  252. ll d = gcd(abs(y - x), n);
  253. if (d != 1 && d != n)
  254. return d;
  255. if (i == k) {
  256. y = x;
  257. k *= 2;
  258. }
  259. }
  260. }
  261. ```
  262.  
  263.  
  264.  
  265. * Modified Sieve (DP)
  266.  
  267. * Sieve of Erastothenes
  268.  
  269. ```c++
  270. vi numDiffPF(MAX_N);
  271. for (ll i = 0; i < MAX_N; i++)
  272. if (numDiffPF[i])
  273. for (ll j = i; j < MAX_N; j += i)
  274. numDiffPF[j]++;
  275. ```
  276.  
  277. * Euler Totient
  278. ```c++
  279. for (ll i = 0; i < MAX_N; i++)
  280. eulerPhi[i] = i;
  281. for (ll i = 2; i < MAX_N; i++)
  282. if (eulerPhi[i] == i) // i is a prime
  283. for (ll j = i; j < MAX_N; j += i)
  284. eulerPhi[j] = (eulerPhi[j] / i) * (i - 1);
  285. ```
  286.  
  287. * Extended Euclid: Solving Linear Diaphantine Equation
  288.  
  289. ```c++
  290. void extendedEuclid(ll a, ll b, ll &x, ll &y, ll &d) {
  291. if (b == 0) {
  292. x = 1;
  293. y = 0;
  294. d = a;
  295. return;
  296. }
  297. extendedEuclid(b, a % b, x, y, d);
  298. ll x1 = y;
  299. ll y1 = x - (a / b) * y;
  300. x = x1;
  301. y = y1;
  302. }
  303. ```
  304.  
  305. * Modulo Arithmetic
  306.  
  307. * `(a + b) % c = ((a % c) + (b % c)) % c`
  308. * `(a * b) % c = ((a % c) * (b % c)) % c`
  309. * `(a - b) % c = ((a % c) - (b % c)) % c`
  310. * `(a / b) % c = ((a % c) / (b % c)) % c`
  311. * `(a ^ b) % c = (2 * (a ^ {b / 2} % c)) % c` b is even
  312. * `(a / b) % c = ((a % c) * (b^{-1} % c)) % c`
  313. * `(x + y - z) % c = ((a % c) + (b % c) - (z % c) + c) % c`
  314.  
  315.  
  316. * Greatest Common Divisor and Least Common Multiple
  317.  
  318. ```c++
  319. ll gcd(ll a, ll b) {
  320. return b == 0 ? a : gcd(b, a % b);
  321. }
  322.  
  323. ll lcd(ll a, ll b) {
  324. return (a * b) / gcd(a, b);
  325. }
  326. ```
  327.  
  328. ## Data Structures
  329.  
  330. * Union-Find Disjoint Sets
  331.  
  332. ```c++
  333. class UnionFind {
  334. private:
  335. vi parent;
  336. vi heigth;
  337.  
  338. public:
  339. UnionFind (ll n) {
  340. parent = vi(n);
  341. heigth = vi(n, 1);
  342. for (ll i = 0; i < n; i++)
  343. parent[i] = i;
  344. }
  345.  
  346. ll findSet(ll p) {
  347. ll root = p, aux;
  348. while (root != parent[root])
  349. root = parent[root];
  350. while (p != root) {
  351. aux = parent[p];
  352. parent[p] = root;
  353. p = aux;
  354. }
  355. return root;
  356. }
  357.  
  358. bool isSameSet(ll p, ll q) {
  359. return findSet(p) == findSet(q);
  360. }
  361.  
  362. void unionSet(ll p, ll q) {
  363. ll rootP = findSet(p);
  364. ll rootQ = findSet(q);
  365. if (rootP == rootQ)
  366. return;
  367. if (heigth[rootP] < heigth[rootQ])
  368. parent[rootP] = rootQ;
  369. else {
  370. parent[rootQ] = rootP;
  371. if (heigth[rootP] == heigth[rootQ])
  372. heigth[rootP]++;
  373. }
  374. }
  375. };
  376. ```
  377.  
  378. * Segment Tree
  379. * Binary Indexed (Fenwick) Tree
  380.  
  381.  
  382. ## Graph
  383.  
  384. * **Depth first search:** Return the number of component conected. It is important fill the `color` with "white"
  385.  
  386. * Recursive
  387.  
  388. ```c++
  389. ll dfs (vector<vi> graph, vector<string> &color, vector<ll> &path, ll node) {
  390. ll totalMarquet = 1;
  391. color[node] = "gray";
  392. for (auto neighbor : graph[node])
  393. if (color[neighbor] == "white") {
  394. path[neighbor] = node;
  395. totalMarquet += dfs(graph, color, path, neighbor);
  396. }
  397.  
  398. color[node] = "black";
  399. return totalMarquet;
  400. }
  401. ```
  402.  
  403. * Iterative
  404.  
  405. ```c++
  406. ll dfs (vector<vi> graph, vector<string> &color, vector<ll> &path, ll initNode) {
  407.  
  408. ll node, totalMarquet = 1;
  409. stack<ll> nodeList;
  410. nodeList.push(initNode);
  411. color[node] = "gray";
  412.  
  413. while (!nodeList.empty()) {
  414. node = nodeList.top();
  415. nodeList.pop();
  416. for (auto neighbor: graph[node])
  417. if (color[neighbor] == "white") {
  418. nodeList.push(neighbor);
  419. path[neighbor] = node;
  420. color[node] = "gray";
  421. totalMarquet++;
  422. }
  423. color[node] = "black";
  424. }
  425. return totalMarquet;
  426. }
  427. ```
  428.  
  429. * Build Path
  430.  
  431. ```c++
  432. ll longPath(vector<ll> path, ll endNode) {
  433. ll answer = 0;
  434. ll currentNode = endNode;
  435. while (path[currentNode] != -1) {
  436. cout << currentNode << ' ';
  437. currentNode = path[currentNode];
  438. answer++;
  439.  
  440. }
  441. cout << currentNode << '\n';
  442. return answer;
  443. }
  444. ```
  445.  
  446. * **Breath first search:**
  447.  
  448.  
  449.  
  450. ```c++
  451. ll bfs (vector<vi> graph, vector<string> &color, vector<ll> &path, vector<ll> &depth, ll initNode){
  452.  
  453. ll node, totalMarquet = 1;
  454. depth[initNode] = 0;
  455. path[initNode] = -1;
  456. color[initNode] = "gray";
  457. queue<ll> nodeList;
  458. nodeList.push(initNode);
  459.  
  460. while (!nodeList.empty()) {
  461. node = nodeList.front();
  462. nodeList.pop();
  463. for (auto neighbor: graph[node])
  464. if (color[neighbor] == "white") {
  465. color[neighbor] = "gray";
  466. nodeList.push(neighbor);
  467. path[neighbor] = node;
  468. depth[neighbor] = depth[node] + 1;
  469. totalMarquet++;
  470. }
  471. color[node] = "black";
  472. }
  473. return totalMarquet;
  474. }
  475. ```
  476.  
  477. For test
  478.  
  479. ```c++
  480. ll n, m, l, r, initNode;
  481. cin >> n >> m >> initNode;
  482. vector<vi> graph(n, vi());
  483. vector<string> color(n, "white");
  484. vector<ll> depth(n, INF);
  485. vector<ll> path(n);
  486. path[initNode] = -1;
  487. while (m --> 0) {
  488. cin >> l >> r;
  489. graph[l].push_back(r);
  490. }
  491.  
  492. ll answer = bfs(graph, color, path, depth, initNode);
  493. cout << answer << endl;
  494. for (ll i = 0; i < n; i++) {
  495. cout << i << ' ';
  496. if (depth[i] == INF)
  497. cout << "inf";
  498. else
  499. cout << depth[i];
  500. cout << endl;
  501. }
  502. cout << endl;
  503. ```
  504.  
  505. * test case
  506.  
  507. ```
  508. 11
  509. 0 0
  510. 1 1
  511. 2 2
  512. 3 2
  513. 4 3
  514. 5 2
  515. 6 2
  516. 7 2
  517. 8 1
  518. 9 3
  519. 10 3
  520. ```
  521.  
  522. # Heap
  523.  
  524. ```c++
  525. class MaxHeap {
  526. private:
  527. ll *heap;
  528. ll size, scope;
  529. public:
  530. MaxHeap(ll n = 10) {
  531. heap = new ll[n];
  532. size = 0;
  533. scope = n;
  534. }
  535.  
  536. bool empty() {
  537. return size == 0;
  538. }
  539.  
  540. ll getMax() {
  541. return heap[1];
  542. }
  543.  
  544. void push(ll theElement) {
  545. if (size + 1 == scope) {
  546. ll *auxHeap = heap;
  547. scope = size * 2;
  548. heap = new ll[scope];
  549. for (ll i = 1; i <= size; i++)
  550. heap[i] = auxHeap[i];
  551. delete[] auxHeap;
  552. }
  553.  
  554. ll currentNode = ++size;
  555. while (currentNode != 1 && heap[currentNode / 2] < theElement) {
  556. heap[currentNode] = heap[currentNode / 2];
  557. currentNode /= 2;
  558. }
  559. heap[currentNode] = theElement;
  560. }
  561.  
  562. ll remove(ll theElement) {
  563.  
  564. ll maxElement = heap[1];
  565. ll lastElement = heap[size++];
  566. ll currentNode = 1, child = 2;
  567. while (child <= size) {
  568. if (child < size && heap[child] < heap[child + 1])
  569. child++;
  570.  
  571. if (lastElement >= heap[child])
  572. break;
  573.  
  574. heap[currentNode] = heap[child];
  575. currentNode = child;
  576. child *= 2;
  577. }
  578.  
  579. heap[currentNode] = lastElement;
  580. return maxElement;
  581. }
  582.  
  583. void initialize(ll *theHeap, ll theHeapSize) {
  584.  
  585. size = theHeapSize;
  586. if (scope < size + 1)
  587. heap = new ll[size + 1];
  588. for (ll i = 1; i <= size; i++)
  589. heap[i] = theHeap[i - 1];
  590.  
  591. ll rootElement, child;
  592. for (ll root = size / 2; root >= 1; root--) {
  593. rootElement = heap[root];
  594. child = 2 * root;
  595.  
  596. while (child <= size) {
  597. if (child < size && heap[child] < heap[child + 1])
  598. child++;
  599.  
  600. if (rootElement >= heap[child])
  601. break;
  602.  
  603. heap[child / 2] = heap[child];
  604. child *= 2;
  605. }
  606.  
  607. heap[child / 2] = rootElement;
  608. }
  609. }
  610. };
  611. ```
Add Comment
Please, Sign In to add comment