Advertisement
newb_ie

Library

Apr 21st, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.81 KB | None | 0 0
  1. == == == == == =
  2. [TEMPLATES]
  3. == == == == == =
  4. Direction array :
  5. int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
  6. int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
  7. //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  8. #define filein freopen ("in.txt", "r", stdin)
  9. #define fileout freopen ("out.txt", "w", stdout)
  10. == == ==
  11. [PBDS]
  12. == == ==
  13. #include <ext/pb_ds/assoc_container.hpp>
  14. #include <ext/pb_ds/tree_policy.hpp>
  15. using namespace __gnu_pbds;
  16. typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  17.  
  18. freopen ("input.txt", "r", stdin);
  19. freopen ("output.txt", "w", stdout);
  20. template<typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
  21. template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ", "; os << "]"; return os; }
  22. template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ", "; os << "]"; return os; }
  23. template<typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  24. template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  25. template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  26. template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ", "; os << "}"; return os; }
  27. template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << ", " << pa.second << ")"; return os; }
  28. template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }
  29. template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }
  30. template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
  31. template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
  32. template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
  33. template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
  34. template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
  35. template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
  36. == == == == == == == == == == == =
  37. [ BITWISE FUNCTIONS ]
  38. == == == == == == == == == == == =
  39.  
  40. int set_bit(int n, int pos) {return n = (n | (1 << pos));}
  41. int reset_bit(int n, int pos) {return n = n & ~(1 << pos);}
  42. bool check_bit(int n, int pos) {return n = n & (1 << pos);}
  43.  
  44. void add (int & s, int n) {
  45. if ((s & (1 << (n - 1)))) return;
  46. s ^= (1 << (n - 1));
  47. }
  48.  
  49. void remove (int & s, int n) {
  50. if (!(s & (1 << (n - 1)))) return;
  51. s ^= (1 << (n - 1));
  52. }
  53.  
  54. void display (int s) {
  55. for (int bit = 0; bit < 9; ++bit) {
  56. if (s & (1 << bit)) {
  57. cout << bit + 1 << ' ';
  58. }
  59. }
  60. cout << '\n';
  61. }
  62. == == == == == == == == == == == =
  63. [NUMBER THEORY SECTION]
  64. == == == == == == == == == == == =
  65.  
  66. /*
  67. Fibonacchi
  68. */
  69.  
  70. long long Fibo (long long n) {
  71. //res[-1] = 0,res[0] = res[1] = 1;
  72. //check fibo of n, cout << Fibo(n - 1);
  73. if (res.find(n) != res.end()) {
  74. return res[n];
  75. }
  76. if (n % 2 == 0) {
  77. return res[n] = (Fibo(n / 2) * Fibo(n / 2) + Fibo(n / 2 - 1) * Fibo(n / 2 - 1)) % mod;
  78. } else {
  79. return res[n] = (Fibo(n / 2) * Fibo(n / 2 + 1) + Fibo(n / 2 - 1) * Fibo(n / 2)) % mod;
  80. }
  81. }
  82.  
  83. /*
  84. GCD & LCM Template
  85. */
  86.  
  87. template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); }
  88. template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
  89.  
  90. /*
  91. Moduler Arithmetic
  92. */
  93. inline void normal(long long &a) { a %= mod; (a < 0) && (a += mod); }
  94. inline long long modMul(long long a, long long b) { a %= mod, b %= mod; normal(a), normal(b); return (a * b) % mod; }
  95. inline long long modAdd(long long a, long long b) { a %= mod, b %= mod; normal(a), normal(b); return (a + b) % mod; }
  96. inline long long modSub(long long a, long long b) { a %= mod, b %= mod; normal(a), normal(b); a -= b; normal(a); return a; }
  97. inline long long modPow(long long b, long long p) { long long r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
  98. inline long long modInverse(long long a) { return modPow(a, mod - 2); }
  99. inline long long modDiv(long long a, long long b) { return modMul(a, modInverse(b)); }
  100.  
  101. == == == == == == =
  102. [MOD Inverse]
  103. == == == == == == =
  104.  
  105.  
  106. == == == == == == ==
  107. [MATH FORMULA]
  108. == == == == == == ==
  109. //sum of (n)
  110. int sum = n * (n + 1) / 2;
  111. //sum of(n^2)
  112. int sum = ((n * (n + 1)) * (2 * n + 1)) / 6;
  113. //sum of range
  114. sum_of_range = n * ((r - l + 1) * (l + r)) / 2;
  115. /*
  116. Bitwise Sieve_of_Eratosthenes
  117. */
  118. const int maxN = 10000;
  119. long long isVisited[maxN / 64 + 200];
  120. vector<int> primes;
  121. void Sieve_of_Eratosthenes(int limit) {
  122. limit += 100;
  123. for (long long i = 3; i <= sqrt(limit) ; i += 2) {
  124. if (!(isVisited[i / 64] & (1LL << (i % 64)))) {
  125. for (long long j = i * i; j <= limit; j += 2 * i) {
  126. isVisited[j / 64] |= (1LL << (j % 64));
  127. }
  128. }
  129. }
  130. primes.push_back(2);
  131. for (long long i = 3; i <= limit; i += 2) {
  132. if (!(isVisited[i / 64] & (1LL << (i % 64)))) {
  133. primes.push_back(i);
  134. }
  135. }
  136. }
  137.  
  138. /*
  139. IS PRIME Function(Bitwise Seive)
  140. */
  141. bool is_prime(int n) {
  142. if (n < 2) return false;
  143. if (n == 2) return true;
  144. if (n % 2 == 0) return false;
  145. if (!(isVisited[n / 64] & (1LL << (n % 64)))) return true;
  146. return false;
  147. }
  148.  
  149.  
  150. /*
  151. Generate All Divisors of a Number Using Prime Factorization
  152. */
  153.  
  154. //Seive CODE Here
  155.  
  156. vector<int> setDivisors(int n, int i, vector<int> & divisors, vector<pair<int, int>> factors) {
  157. int j, x, k;
  158. for (j = i; j < (int) factors.size(); j++) {
  159. x = factors[j].first * n;
  160. for (k = 0; k < factors[j].second; k++) {
  161. divisors.push_back(x);
  162. setDivisors(x, j + 1, divisors, factors);
  163. x *= factors[j].first;
  164. }
  165. }
  166. return divisors;
  167. }
  168.  
  169. vector<int> PF(int n) {
  170. int x = n;
  171. vector<pair<int, int>> factors;
  172. for (auto i : primes) {
  173. if (i * i > n) {
  174. break;
  175. }
  176. if (n % i == 0) {
  177. int count = 0;
  178. while (n % i == 0) {
  179. n /= i;
  180. count += 1;
  181. }
  182. factors.push_back(make_pair(i, count));
  183. }
  184. }
  185. if (n > 1) {
  186. factors.push_back(make_pair(n, 1));
  187. }
  188. vector<int> d = {1};
  189. vector<int> divisors = setDivisors(1, 0, d, factors);
  190. return divisors;
  191. }
  192.  
  193. /*
  194. Generate Divisors in Sqrt(n) Time
  195. */
  196. vector<int> GEN_DIVS_SQRT(int n) {
  197. vector<int> divisors;
  198. for (int i = 1; i * i <= n; i++) {
  199. if (n % i == 0) {
  200. if (n / i != i) {
  201. divisors.push_back(n / i);
  202. }
  203. divisors.push_back(i);
  204. }
  205. }
  206. return divisors;
  207. }
  208.  
  209. /*
  210. Count Divisors Using Prime Factorization
  211. */
  212. int COUNT_DIVISORS(int n) {
  213. int divisors = 1;
  214. for (auto i : primes) {
  215. if (n % i == 0) {
  216. if (i * i > n) {
  217. return divisors;
  218. }
  219. int count = 1;
  220. while (n % i == 0) {
  221. n /= i;
  222. count += 1;
  223. }
  224. divisors *= count;
  225. }
  226. }
  227. return divisors;
  228. }
  229.  
  230. == == == == == == == == == =
  231. [ BIG MOD SECTION ]
  232. == == == == == == == == == =
  233. //divide a long number(like 10^18)
  234. int Quotient(string s, int m) {
  235. int count = 0;
  236. for (int i = 0; i < (int) s.size(); i++) {
  237. count = (count * 10 + (s[i] - 48)) % m;
  238. }
  239. return count;
  240. }
  241.  
  242. == == == == == == == == == == == == == == =
  243. [Range Minimum Query SECTION]
  244. == == == == == == == == == == == == == == =
  245. == == == == == == == == == == == == == ==
  246. [SEGMENT TREE (SUM) EDITABLE]
  247. == == == == == == == == == == == == == ==
  248. const int maxN = 10000 * 4 + 1;
  249. int input[maxN / 4], tree[maxN];
  250. void buildTree(int idx, int start, int end) {
  251. if (start == end) {
  252. tree[idx] = input[start];
  253. } else {
  254. int mid = start + (end - start) / 2;
  255. buildTree(idx * 2, start, mid);
  256. buildTree(idx * 2 + 1, mid + 1, end);
  257. tree[idx] = tree[2 * idx] + tree[2 * idx + 1];
  258. }
  259. }
  260. int query(int idx, int start, int end, int l, int r) {
  261. if (l > end or r < start) {
  262. return 0;
  263. }
  264. if (start >= l and end <= r) {
  265. return tree[idx];
  266. }
  267. int mid = start + (end - start) / 2;
  268. return query(idx * 2, start, mid, l, r) + query(idx * 2 + 1, mid + 1, end, l, r);
  269. }
  270. void update(int idx, int l, int r, int pos, int val) {
  271. set<char> res;
  272. if (l == r) {
  273. tree[idx] = val;
  274. } else {
  275. int mid = l + (r - l) / 2;
  276. if (pos <= mid) {
  277. update(idx * 2, l, mid, pos, val);
  278. } else {
  279. update(idx * 2 + 1, mid + 1, r, pos, val);
  280. }
  281. res.insert(tree[idx * 2].begin(), tree[idx * 2].end());
  282. res.isnert(Tree[idx * 2 + 1].begin(), tree[idx * 2].end());
  283. tree[idx] = res;
  284. }
  285. }
  286. == == == == == == == == == == == == ==
  287. [DIS - JOINT SET UNION (DSU)]
  288. == == == == == == == == == == == == ==
  289.  
  290.  
  291. const int maxN = 1e6 + 100;
  292. int p[maxN],r[maxN];
  293.  
  294. int __find__ (int a) {
  295. return p[a] = (p[a] == a ? a : __find__(p[a]));
  296. }
  297.  
  298. void __union__ (int a, int b) {
  299. a = __find__(a);
  300. b = __find__(b);
  301. if (r[a] == r[b]) ++r[a];
  302. if (r[a] > r[b]) p[b] = a;
  303. else p[a] = b;
  304. }
  305.  
  306. == == == == == == == == == =
  307. [STRING ALGORITHMS]
  308. == == == == == == == == == =
  309. == == == == == == == == == == == == =
  310. [KMP SEARCH ALGORITHM]
  311. [COMPLEXITY : O(N + M) ]
  312. == == == == == == == == == == == == =
  313.  
  314. vector<int> lps_array(string p) {
  315. vector<int> lps((int) p.size());
  316. int i = 1, idx = 0;
  317. while (i < (int) p.size()) {
  318. if (p[idx] == p[i]) {
  319. lps[i] = idx + 1;
  320. idx += 1, i += 1;
  321. } else {
  322. if (idx != 0) {
  323. idx = lps[idx - 1];
  324. } else {
  325. lps[i] = 0;
  326. i += 1;
  327. }
  328. }
  329. }
  330. return lps;
  331. }
  332. void kmp(string s, string p) {
  333. vector<int> lps = lps_array(p);
  334. int i = 0, j = 0;
  335. while (i < (int) s.size()) {
  336. if (s[i] == p[j]) {
  337. i += 1, j += 1;
  338. } else {
  339. if (j != 0) {
  340. j = lps[j - 1];
  341. } else {
  342. i += 1;
  343. }
  344. }
  345. if (j == (int) p.size()) {
  346. cout << i - (int) p.size() << "\n";
  347. }
  348. }
  349. }
  350. == == == == == == ==
  351. [GRAPH THEORY]
  352. == == == == == == ==
  353. /*
  354. Bipartile(Also Known as Two Coloring Problem) Checking
  355. */
  356. const int maxN = 101;
  357. vector<int> adj[maxN];
  358. bool isVisited[maxN];
  359. int color[maxN];
  360. int Two_Coloring(int node, int col) {
  361. isVisited[node] = true;
  362. color[node] = col;
  363. for (auto u : adj[node]) {
  364. if (!isVisited[node]) {
  365. if (!Two_Coloring(u, col ^ 1)) {
  366. return false;
  367. }
  368. } else {
  369. if (color[node] == color[u`]) {
  370. return false;
  371. }
  372. }
  373. }
  374. return true;
  375. }
  376.  
  377. int dx[4] = {1, 0, -1, 0};
  378. int dy[4] = {0, 1, 0, -1};
  379.  
  380. /*
  381. Cycle Detection / Finding Back Edge
  382. */
  383. const int maxN = 101;
  384. vector<int> adj[maxN];
  385. bool isVisited[maxN];
  386. bool CycleDetection(int node, int parent) {
  387. isVisited[node] = true;
  388. for (auto u : adj[node]) {
  389. if (!isVisited[u]) {
  390. if (CycleDetection(u, node)) {
  391. return true;
  392. }
  393. }
  394. } else {
  395. if (u != parent) {
  396. return true;
  397. }
  398. }
  399. }
  400. return false;
  401. }
  402.  
  403. /*
  404. In and Out Time of a Node
  405. */
  406. const int maxN = 101;
  407. vector<int> adj[maxN];
  408. bool isVisited[maxN];
  409. int InTime[maxN], OutTime[maxN];
  410. int Timer = 1;
  411. void InOutTime(int node) {
  412. isVisited[node] = true;
  413. InTime[node] = Timer++;
  414. for (auto u : adj[node]) {
  415. if (!isVisited[u]) {
  416. InOutTime(u);
  417. }
  418. }
  419. OutTime[node] = Timer++;
  420. }
  421.  
  422. /*
  423. BFS : Breath First Search
  424. */
  425. const int maxN = 101;
  426. vector<int> adj[maxN];
  427. bool isVisited[maxN];
  428. int dist[maxN];
  429. void BFS(int node) {
  430. queue<int> q;
  431. q.push(node);
  432. isVisited[node] = true;
  433. dist[node] = 0;
  434. while (!q.empty()) {
  435. int v = q.front();
  436. q.pop();
  437. for (auto u : adj[v]) {
  438. if (!isVisited[u]) {
  439. isVisited[u] = true;
  440. dist[u] = dist[v] + 1;
  441. q.push(u);
  442. }
  443. }
  444. }
  445. }
  446.  
  447. /*
  448. Diameter of a Tree Using DFS
  449. */
  450.  
  451. const int maxN = 10100;
  452. int max_distance, max_node;
  453. vector<int> adj[maxN];
  454. bool isVisited[maxN];
  455. void diameter(int node, int distance) {
  456. isVisited[node] = true;
  457. if (distance > max_distance) {
  458. max_distance = distance;
  459. max_node = node; `
  460. }
  461. for (int child : adj[node]) {
  462. if (!isVisited[child]) {
  463. dfs(child, distance + 1);
  464. }
  465. }
  466. }
  467.  
  468. /*
  469. Diameter of a Tree Using BFS
  470. */
  471. const int maxN = 10100;
  472. vector<int> adj[maxN];
  473. bool isVisited[maxN];
  474. int dist[maxN];
  475. pair<int, int> BFS(int node, int distance) {
  476. int max_node = -1, max_dist = -1;
  477. queue<int> q;
  478. q.push(node);
  479. dist[node] = distance;
  480. while (!q.empty()) {
  481. int v = q.front();
  482. q.pop();
  483. for (auto u : adj[v]) {
  484. if (!isVisited[u]) {
  485. q.push(u);
  486. isVisited[u] = true;
  487. dist[u] = dist[v] + 1;
  488. if (dist[u] > max_dist) {
  489. max_dist = dist[u];
  490. max_node = u;
  491. }
  492. }
  493. }
  494. }
  495. return {max_node, max_dist};
  496. }
  497. /* Main Functon (DFS) */
  498. max_distance = -1;
  499. dfs(1, 0);
  500. for (int i = 1; i <= n; i++) {
  501. isVisited[i] = false;
  502. }
  503. max_distance = -1;
  504. dfs(max_node, 0);
  505. cout << max_distance << "\n";
  506.  
  507. /* Main Function (BFS) */
  508. pair<int, int> node = BFS(1, 0);
  509. for (int i = 1; i <= n; i++) {
  510. isVisited[i] = false;
  511. dist[i] = 0;
  512. }
  513. node = BFS(node.first, 0);
  514. cout << node.second << "\n";
  515.  
  516. /*
  517. Calculating SUbtree size using dfs in O(n) Time
  518. */
  519.  
  520. const int maxN = 101;
  521. vector<int> adj[maxN];
  522. bool isVisited[maxN];
  523. int subTreeSize[maxN];
  524. int dfs(int node) {
  525. isVisited[node] = true;
  526. int count = 1;
  527. for (auto child : adj[node]) {
  528. if (!isVisited[child]) {
  529. count += dfs(child);
  530. }
  531. }
  532. subTreeSize[node] = count;
  533. }
  534.  
  535. /*
  536. Find Bridges Using DFS
  537. */
  538.  
  539. const int maxN = 101;
  540. const int maxN = 101;
  541. vector<int> adj[maxN];
  542. bool visited[maxN];
  543. int in[maxN], low[maxN];
  544. int timer = 0;
  545. void dfs(int node, int parent) {
  546. visited[node] = true;
  547. in[node] = low[node] = timer++;
  548. for (auto child : adj[node]) {
  549. if (child == parent) {
  550. continue;
  551. }
  552. if (visited[child]) {
  553. low[node] = min(low[node], in[child]);
  554. } else {
  555. dfs(child, node);
  556. if (low[child] > in[node]) {
  557. cout << node << " -> " << child << " Is a Bridge\n";
  558. }
  559. low[node] = min(low[node], low[child]);
  560. }
  561. }
  562. }
  563.  
  564. /*
  565. Build Suffix Array (basic)
  566. */
  567.  
  568. #include<bits/stdc++.h>
  569. using namespace std;
  570.  
  571. int main () {
  572. ios::sync_with_stdio(false);
  573. cin.tie(nullptr);
  574. cout.tie(nullptr);
  575. int T = 1;
  576. //~ cin >> T;
  577. for (int t = 1; t <= T; ++t) {
  578. string s;
  579. cin >> s;
  580. s += '$';
  581. int n = s.size();
  582. int order[n]; // order
  583. int class_[n]; // equivalent class
  584. pair<char, int> pos[n]; // position
  585. for (int i = 0; i < n; ++i) {
  586. pos[i] = make_pair(s[i], i);
  587. }
  588. sort(pos, pos + n);
  589. for (int i = 0; i < n; ++i) {
  590. order[i] = pos[i].second;
  591. }
  592. class_[order[0]] = 0;
  593. for (int i = 1; i < n; ++i) {
  594. if (pos[i].first == pos[i - 1].first) {
  595. class_[order[i]] = class_[order[i - 1]];
  596. } else {
  597. class_[order[i]] = class_[order[i - 1]] + 1;
  598. }
  599. }
  600. int k = 0;
  601. while ((1 << k) < n) {
  602. pair<pair<int, int>, int> suf[n]; //positon array
  603. for (int i = 0; i < n; ++i) {
  604. suf[i] = make_pair(make_pair(class_[i], class_[(i + (1 << k)) % n]), i);
  605. }
  606. sort(suf, suf + n);
  607. for (int i = 0; i < n; ++i) {
  608. order[i] = suf[i].second;
  609. }
  610. class_[order[0]] = 0; // first class to be order[0] = 0
  611. for (int i = 1; i < n; ++i) {
  612. if (suf[i].first == suf[i - 1].first) {
  613. class_[order[i]] = class_[order[i - 1]];
  614. } else {
  615. class_[order[i]] = class_[order[i - 1]] + 1;
  616. }
  617. }
  618. k += 1;
  619. }
  620. for (int i = 0; i < n; ++i) {
  621. cout << order[i] << " ";
  622. }
  623. }
  624. }
  625.  
  626.  
  627. //[Count Set Bits]
  628. int count = 0;
  629. while (n) {
  630. count += (n & 1);
  631. n >>= 1;
  632. }
  633.  
  634. == == == == == == =
  635. [Z Algorithm]
  636. == == == == == == =
  637. vector<int> z_algo (string s) {
  638. int n = (int) s.size();
  639. int l = 0, r = 0;
  640. vector<int> z(n);
  641. for (int i = 1; i < n; ++i) {
  642. if (i > r) {
  643. l = r = i;
  644. while (r < n and s[r] == s[r - l]) {
  645. ++r;
  646. }
  647. z[i] = r - l;
  648. --r;
  649. } else {
  650. if (i + z[i - l] <= r) {
  651. z[i] = z[i - l];
  652. } else {
  653. l = i;
  654. while (r < n and s[r] == s[r - l]) {
  655. ++r;
  656. }
  657. z[i] = r - l;
  658. --r;
  659. }
  660. }
  661. }
  662. return z;
  663. }
  664.  
  665. int main () {
  666. ios::sync_with_stdio(false);
  667. cin.tie(nullptr);
  668. cout.tie(nullptr);
  669. int T = 1;
  670. //~ cin >> T;
  671. for (int test_case = 1; test_case <= T; ++test_case) {
  672. string s, t;
  673. cin >> s >> t;
  674. string total = t + "$" + s;
  675. vector<int> z = z_algo(total);
  676. for (int i = 0; i < (int) z.size(); ++i) {
  677. if (z[i] == (int) t.size()) {
  678. cout << i - (int) t.size() - 1 << "\n";
  679. }
  680. }
  681. }
  682. //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  683. }
  684.  
  685. == == == == == ==
  686. [BITMASK DP]
  687. == == == == == ==
  688. #include<bits/stdc++.h>
  689. using namespace std;
  690.  
  691. int mem[1000][1030];
  692.  
  693. bool check (int mask) {
  694. return (bool) (mask & ((1 << 10) - 1));
  695. }
  696.  
  697. int solve (int pos, int n, int mask) {
  698. if (pos >= n) {
  699. return check(mask);
  700. }
  701. if (mem[pos][mask] != -1) {
  702. return mem[pos][mask];
  703. }
  704. int res = 0;
  705. for (int i = (pos == 0 ? 1 : pos); i <= 9; ++i) {
  706. res += solve (pos + 1, n, mask | (1 << pos));
  707. }
  708. return mem[pos][mask] = res;
  709. }
  710.  
  711. int main () {
  712. ios::sync_with_stdio(false);
  713. cin.tie(nullptr);
  714. cout.tie(nullptr);
  715. int T = 1;
  716. //~ cin >> T;
  717. for (int test_case = 1; test_case <= T; ++test_case) {
  718. memset(mem, -1, sizeof(mem));
  719. int n;
  720. cin >> n;
  721. cout << solve (1, n, 0) << "\n";
  722. }
  723. //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  724. }
  725.  
  726. == == == == ==
  727. [DIJKSTRA]
  728. == == == == ==
  729.  
  730. /*
  731. ======================
  732. [ ___T_ ]
  733. [ | 6=6 | =>HI :-)]
  734. [ |__o__| ]
  735. [ >===]__o[===< ]
  736. [ [o__] ]
  737. [ .". ]
  738. [ |_| ]
  739. [ ]
  740. ======================
  741. */
  742.  
  743. #include<bits/stdc++.h>
  744. using namespace std;
  745.  
  746. const int maxN = 2 * 1e5;
  747. vector<pair<long long, long long>> adj[maxN];
  748. long long dist[maxN];
  749.  
  750. void dijkstra (int source, int n) {
  751. for (int i = 1; i <= n; ++i) dist[i] = LLONG_MAX;
  752. dist[source] = 0;
  753. priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq;
  754. pq.push(make_pair(0, source));
  755.  
  756. while (!pq.empty()) {
  757. long long u = pq.top().second;
  758. long long current_dist = pq.top().first;
  759. pq.pop();
  760.  
  761. if (dist[u] < current_dist) {
  762. continue;
  763. }
  764.  
  765. for (pair<long long, long long> v : adj[u]) {
  766. if (current_dist + v.second < dist[v.first]) {
  767. dist[v.first] = current_dist + v.second;
  768. pq.push(make_pair(dist[v.first], v.first));
  769. }
  770. }
  771. }
  772. }
  773.  
  774. int main () {
  775. ios::sync_with_stdio(false);
  776. cin.tie(nullptr);
  777. cout.tie(nullptr);
  778. int T = 1;
  779. //~ cin >> T;
  780. for (int test_case = 1; test_case <= T; ++test_case) {
  781. int n, m;
  782. cin >> n >> m;
  783. for (int i = 1; i <= m; ++i) {
  784. long long u, v, w;
  785. cin >> u >> v >> w;
  786. adj[u].push_back(make_pair(v, w));
  787. }
  788. dijkstra(1, n);
  789. for (int i = 1; i <= n; ++i) {
  790. cout << dist[i] << " ";
  791. }
  792. }
  793. //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  794. }
  795.  
  796.  
  797. Sparse Table
  798.  
  799. const int maxN = 1e5 + 10;
  800. long long st[maxN][20];
  801. int bin_log[maxN];
  802. long long input[maxN];
  803.  
  804. void sparseTable (int n) {
  805. for (int i = 2; i <= n; ++i) bin_log[i] = bin_log[i / 2] + 1;
  806. for (int i = 0; i < n; ++i) st[i][0] = input[i];
  807. for (int j = 1; j < 20; ++j) {
  808. for (int i = 0; i + (1 << j) - 1 < n; ++i) {
  809. st[i][j] = max(st[i][j - 1],st[i + (1 << (j - 1))][j - 1]);
  810. }
  811. }
  812. //for query min_max
  813. /*
  814. int l,r;
  815. cin >> l >> r;
  816. int j = bin_log[r - l + 1];
  817. cout << min(st[l][j],st[r - (1 << j) + 1][j]) << '\n';
  818. */
  819. }
  820.  
  821.  
  822. String Hashing
  823.  
  824. int single_hash (const string & s) {
  825. const int p = 31;
  826. const int m = 1e9 + 9;
  827. long long hash_value = 0;
  828. long long p_pow = 1;
  829. for (char c : s) {
  830. hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
  831. p_pow = (p_pow * p) % m;
  832. }
  833. return hash_value;
  834. }
  835.  
  836. int compute_hash (const string & s) {
  837. int n = (int) s.size();
  838. const int p = 31;
  839. const int m = 1e9 + 9;
  840. vector<long long> p_pow(n);
  841. p_pow[0] = 1;
  842. for (int i= 1; i < n; ++i) p_pow[i] = (p_pow[i - 1] * p) % m;
  843. vector<long long> h(n + 1,0);
  844. for (int i = 0; i < n; ++i) {
  845. h[i + 1] = (h[i] + (s[i] - 'a' + 1) * p_pow[i]) % m;
  846. }
  847. //number of different substring(for small length)
  848. int cnt = 0;
  849. for (int l = 1; l <= n; ++l) {
  850. set<long long> hs;
  851. for (int i = 0; i <= n - l; ++i) {
  852. long long cur_h = (h[i + l] + m - h[i]) % m;
  853. cur_h = (cur_h * p_pow[n - i - 1]) % m;
  854. hs.insert(cur_h);
  855. }
  856. cnt += (int) hs.size();
  857. }
  858. return cnt;
  859. }
  860.  
  861.  
  862.  
  863. const int maxN = 2100;
  864. const long long p = 31;
  865. const long long m = 1e9 + 7;
  866. long long h[maxN],inv[maxN];
  867.  
  868. long long bin_pow (long long a,long long b) {
  869. a %= m;
  870. long long res = 1;
  871. while (b > 0) {
  872. if (b & 1) res = res * a % m;
  873. a = a * a % m;
  874. b >>= 1;
  875. }
  876. return res;
  877. }
  878.  
  879. void compute_hash (string & s) {
  880. long long p_pow = 1;
  881. inv[0] = 1;
  882. h[0] = s[0] - 'a' + 1;
  883. for (int i = 1; i < (int) s.size(); ++i) {
  884. p_pow = (p_pow * p) % m;
  885. inv[i] = bin_pow(p_pow,m - 2);
  886. h[i] = (h[i - 1] + (s[i] - 'a' + 1) * p_pow) % m;
  887. }
  888. }
  889.  
  890. long long sub_hash (int l,int r) {
  891. long long res = h[r];
  892. if (l > 0) res -= h[l - 1];
  893. res = (res * inv[l]) % m;
  894. if (res < 0) res = (res + m) % m;
  895. return res;
  896. }
  897.  
  898. long long single_hash (string & s) {
  899. long long hash_value = 0;
  900. long long p_pow = 1;
  901. for (char c : s) {
  902. hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
  903. p_pow = (p_pow * p) % m;
  904. }
  905. return hash_value;
  906. }
  907.  
  908.  
  909. struct StringHash {
  910. const long long p = 31;
  911. const long long m = 1e9 + 7;
  912. vector<long long> h;
  913. vector<long long> inv;
  914. inline void init (string & s) {
  915. h.resize((int) s.size());
  916. inv.resize((int) s.size());
  917. compute_hash(s);
  918. }
  919. inline long long bin_pow (long long a,long long b) {
  920. a %= m;
  921. long long res = 1;
  922. while (b > 0) {
  923. if (b & 1) res = res * a % m;
  924. a = a * a % m;
  925. b >>= 1;
  926. }
  927. return res;
  928. }
  929. inline void compute_hash (string & s) {
  930. long long p_pow = 1;
  931. inv[0] = 1;
  932. h[0] = s[0] - 'a' + 1;
  933. for (int i = 1; i < (int) s.size(); ++i) {
  934. p_pow = (p_pow * p) % m;
  935. inv[i] = bin_pow(p_pow,m - 2);
  936. h[i] = (h[i - 1] + (s[i] - 'a' + 1) * p_pow) % m;
  937. }
  938. }
  939. inline long long sub_hash (int l,int r) {
  940. long long res = h[r];
  941. if (l > 0) res -= h[l - 1];
  942. res = (res * inv[l]) % m;
  943. if (res < 0) res += m;
  944. return res;
  945. }
  946. inline long long single_hash (string & s) {
  947. long long hash_value = 0;
  948. long long p_pow = 1;
  949. for (int i = 0; i < (int) s.size(); ++i) {
  950. hash_value = (hash_value + (s[i] - 'a' + 1) * p_pow) % m;
  951. p_pow = (p_pow * p) % m;
  952. }
  953. return hash_value;
  954. }
  955. };
  956.  
  957.  
  958. //sparse table min / max / gcd
  959.  
  960. const int maxN = 3 * 1e5 + 100;
  961.  
  962. template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); }
  963. template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
  964.  
  965. pair<pair<int, int>, int> st[maxN][20];
  966. int bin_log[maxN];
  967. vector<long long> input;
  968.  
  969. void sparseTable (int n) {
  970. for (int i = 2; i <= n; ++i) bin_log[i] = bin_log[i / 2] + 1;
  971. for (int i = 0; i < n; ++i) st[i][0].first.first = st[i][0].first.second = st[i][0].second = input[i];
  972. for (int j = 1; j < 20; ++j) {
  973. for (int i = 0; i + (1 << j) - 1 < n; ++i) {
  974. st[i][j].first.first = min(st[i][j - 1].first.first, st[i + (1 << (j - 1))][j - 1].first.first);
  975. st[i][j].first.second = max(st[i][j - 1].first.second, st[i + (1 << (j - 1))][j - 1].first.second);
  976. st[i][j].second = gcd(st[i][j - 1].second, st[i + (1 << (j - 1))][j - 1].second);
  977. }
  978. }
  979. }
  980.  
  981. int gcd_query (int l, int r) {
  982. int j = bin_log[r - l + 1];
  983. return gcd(st[l][j].second,st[r - (1 << j) + 1][j].second);
  984. }
  985.  
  986. int min_query (int l, int r) {
  987. int j = bin_log[r - l + 1];
  988. return min(st[l][j].first.first,st[r - (1 << j) + 1][j].first.first);
  989. }
  990.  
  991. int max_query (int l, int r) {
  992. int j = bin_log[r - l + 1];
  993. return max(st[l][j].first.second,st[r - (1 << j) + 1][j].first.second);
  994. }
  995.  
  996. const int maxN = 2e5 + 100;
  997. int64_t st[maxN][20];
  998. int bin_log[maxN];
  999. int64_t input[maxN];
  1000.  
  1001. void sparseTable (int n) {
  1002. for (int i = 2; i <= n; ++i) bin_log[i] = bin_log[i / 2] + 1;
  1003. for (int i = 0; i < n; ++i) st[i][0] = input[i];
  1004. for (int j = 1; j < 20; ++j) {
  1005. for (int i = 0; i + (1 << j) - 1 < n; ++i) {
  1006. st[i][j] = min(st[i][j - 1],st[i + (1 << (j - 1))][j - 1]);
  1007. }
  1008. }
  1009. }
  1010.  
  1011. int query (int l, int r) {
  1012. //0 based idx
  1013. int j = bin_log[r - l + 1];
  1014. return min(st[l][j],st[r - (1 << j) + 1][j]);
  1015. }
  1016.  
  1017. //Construction of Flat tree
  1018.  
  1019. const int maxN = 1e5;
  1020. vector<int> adj[maxN];
  1021. int ft[maxN], st[maxN], et[maxN];
  1022. int timer = 0;
  1023.  
  1024. void construct_FlatTree(int node) {
  1025. ++timer;
  1026. ft[timer] = node;
  1027. st[node] = timer;
  1028. for (int child : adj[node]) {
  1029. if (st[child] == 0) {
  1030. construct_FlatTree(child);
  1031. }
  1032. }
  1033. ++timer;
  1034. ft[timer] = node;
  1035. et[node] = timer;
  1036. }
  1037.  
  1038.  
  1039. [Palindrome Query]
  1040.  
  1041.  
  1042. vector<vector<int>> p;
  1043.  
  1044. void construct (string & s) {
  1045. int n = (int) s.size();
  1046. p.clear();
  1047. p.resize(2, vector<int> (n, 0));
  1048. for (int z = 0, l = 0, r = 0; z < 2; ++z, l = 0, r = 0) {
  1049. for (int i = 0; i < n; ++i) {
  1050. if (i < r) p[z][i] = min(r - i + !z, p[z][l + r - i + !z]);
  1051. int L = i - p[z][i], R = i + p[z][i] - !z;
  1052. while (L - 1 >= 0 and R + 1 < n and s[L - 1] == s[R + 1]) {
  1053. ++p[z][i];
  1054. --L, ++R;
  1055. }
  1056. if (R > r) l = L, r = R;
  1057. }
  1058. }
  1059. }
  1060.  
  1061. bool query (int l, int r) {
  1062. if (r - l + 1 == 1) return true;
  1063. int len = r - l + 1;
  1064. int mid = l + (r - l) / 2;
  1065. if (len & 1) {
  1066. if (mid - p[1][mid] <= l and mid + p[1][mid] >= r) {
  1067. return true;
  1068. }
  1069. } else {
  1070. ++mid;
  1071. if (mid - p[0][mid] <= l and mid + p[0][mid] - 1 >= r) {
  1072. return true;
  1073. }
  1074. }
  1075. return false;
  1076. }
  1077.  
  1078.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement