Advertisement
cyberjab

Untitled

Nov 2nd, 2023
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.82 KB | Software | 0 0
  1. Старт программы
  2. --------------------------------------------------------------------------------------------------
  3. //#pragma GCC optimize("03")
  4. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cstdlib>
  8. #include <cstdio>
  9. #include <string>
  10. #include <vector>
  11. #include <map>
  12. #include <set>
  13. #include <unordered_map>
  14. #include <unordered_set>
  15. #include <queue>
  16. #include <deque>
  17. #include <cmath>
  18. #include <numeric>
  19. #include <algorithm>
  20. #include <ctime>
  21. #include <chrono>
  22. #include <random>
  23. #include <functional>
  24.  
  25. using namespace std;
  26. using ll = long long;
  27. using db = double;
  28. using ldb = long double;
  29. using pii = pair<int, int>;
  30. using pll = pair<ll, ll>;
  31. using vint = vector<int>;
  32. using vll = vector<ll>;
  33. #define all(x) x.begin(), x.end()
  34. #define size(x) int(x.size())
  35. #define rep(x) for(int rep_i = 0; rep_i < x; ++rep_i)
  36. #define forp(s, i, f) for(int i = s; i < f; ++i)
  37. #define form(s, i, f) for(int i = s; i > f; --i)
  38.  
  39. const int MOD = 1e9 + 7;
  40. const int MOD2 = 998244353;
  41. const int INF = 2e9 + 7;
  42. const ll LINF = 1e18 + 7;
  43. const double EPS = 1e-9;
  44. const long double PI = acosl(-1.0);
  45.  
  46. int main() {
  47.     ios_base::sync_with_stdio(0);
  48.     cin.tie(0);
  49.     cout << fixed;
  50. }
  51. ---------------------------------------------------------------------------------------------------
  52. sqrt
  53. ---------------------------------------------------------------------------------------------------
  54. #include <math.h>
  55.  
  56. double sqrt(double x) {
  57.     if (x <= 0)
  58.         return 0;       // if negative number throw an exception?
  59.     int exp = 0;
  60.     x = frexp(x, &exp); // extract binary exponent from x
  61.     if (exp & 1) {      // we want exponent to be even
  62.         exp--;
  63.         x *= 2;
  64.     }
  65.     double y = (1+x)/2; // first approximation
  66.     double z = 0;
  67.     while (y != z) {    // yes, we CAN compare doubles here!
  68.         z = y;
  69.         y = (y + x/y) / 2;
  70.     }
  71.     return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
  72. }
  73. --------------------------------------------------------------------------------------------------
  74. Быстрое возведение в степень по модулю
  75. --------------------------------------------------------------------------------------------------
  76. #include <iostream>
  77. #include <cmath>
  78. #include <algorithm>
  79.  
  80. using namespace std;
  81.  
  82. unsigned powmod(unsigned base, unsigned exp, unsigned modulo)
  83. {
  84.     unsigned res = 1;
  85.  
  86.     while (exp != 0)
  87.     {
  88.         if ((exp & 1) != 0)
  89.         {
  90.             res = (1ll * res * base) % modulo;
  91.         }
  92.  
  93.         base = (1ll * base * base) % modulo;
  94.         exp >>= 1;
  95.     }
  96.  
  97.     return res;
  98. }
  99. ----------------------------------------------------------------------------------------------------
  100. НОК и НОД
  101. ----------------------------------------------------------------------------------------------------
  102. #include <iostream>
  103. #include <cmath>
  104. #include <algorithm>
  105.  
  106. using namespace std;
  107.  
  108. int gcd1(int a, int b) {
  109.     if (b == 0)
  110.         return a;
  111.     return gcd1(b, a % b);
  112. }
  113.  
  114. int gcd2(int a, int b) {
  115.     while (b > 0) {
  116.         a %= b;
  117.         swap(a, b);
  118.     }
  119.     return a;
  120. }
  121.  
  122. int lcm(int a, int b) {
  123.     return a * b / gcd1(a, b);
  124. }
  125.  
  126. int main() {
  127.     long a, b;
  128.     cin >> a >> b;
  129.     cout << lcm(a, b);
  130.     return 0;
  131. }
  132. --------------------------------------------------------------------------------------------------
  133. Расширенный алгоритм Эвклида
  134. --------------------------------------------------------------------------------------------------
  135. #include <iostream>
  136. #include <cmath>
  137.  
  138. using namespace std;
  139.  
  140. int gcd_ext(int a, int b, int& x, int& y) {
  141.     if (b == 0) {
  142.         x = 1;
  143.         y = 0;
  144.         return a;
  145.     }
  146.     int d = gcd_ext(b, a % b, x, y);
  147.     x -= (a / b) * y;
  148.     swap(x, y);
  149.     return d;
  150. }
  151.  
  152. int main() {
  153.     return 0;
  154. }
  155. ---------------------------------------------------------------------------------------------------
  156. Решето Эратосфена
  157. ---------------------------------------------------------------------------------------------------
  158. #include <iostream>
  159. #include<cmath>
  160. #include <vector>
  161.  
  162. using namespace std;
  163.  
  164. int main() {
  165.     long n = 15485864;
  166.     vector <int> prime(n + 1, 1);
  167.     prime[0] = prime[1] = 0;
  168.     long k = 0;
  169.     long stop;
  170.     cin >> stop;
  171.     long ans;
  172.     for (int i = 2; i <= n; ++i) {
  173.         if (!prime[i] || i * 1ll * i > n) {
  174.             continue;
  175.         }
  176.         for (int j = i * i; j <= n; j += i)
  177.             prime[j] = 0;
  178.     }
  179.     for (long i = 1; i < n; i++) {
  180.         if (prime[i] == 1) {
  181.             k++;
  182.         }
  183.         if (k == stop) {
  184.             ans = i;
  185.             break;
  186.         }
  187.     }
  188.     cout << ans;
  189.     return 0;
  190. }
  191. ------------------------------------------------------------------------------------------------------
  192. Задача с бором
  193. ------------------------------------------------------------------------------------------------------
  194. //#pragma GCC optimize("03")
  195. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  196. #include <iostream>
  197. #include <iomanip>
  198. #include <cstdlib>
  199. #include <cstdio>
  200. #include <string>
  201. #include <vector>
  202. #include <map>
  203. #include <set>
  204. #include <unordered_map>
  205. #include <unordered_set>
  206. #include <queue>
  207. #include <deque>
  208. #include <cmath>
  209. #include <numeric>
  210. #include <algorithm>
  211. #include <ctime>
  212. #include <chrono>
  213. #include <random>
  214. #include <functional>
  215.  
  216. #define forp(i, s, f) for(int i = s; i < f; i++)
  217.  
  218. using namespace std;
  219. using ll = long long;
  220.  
  221. ll cnt = 0;
  222.  
  223. int dfs(vector<vector<int>> &bor, int row, int k, int ans) {
  224.     k++;
  225.     int a = 0;
  226.     forp(i, 0, 27) {
  227.         if (bor[row][i] != 0) {
  228.             a = dfs(bor, bor[row][i], k, 0);
  229.             bor[row][27] -= a;
  230.             bor[row][28] -= a;
  231.             ans += a;
  232.         }
  233.     }
  234.     int c = 0;
  235.     if (bor[row][27] >= bor[row][28]) {
  236.         c = bor[row][28];
  237.         cnt += k * c;
  238.         ans += c;
  239.         bor[row][27] -= c;
  240.         bor[row][28] = 0;
  241.     }
  242.     else {
  243.         c = bor[row][27];
  244.         cnt += k * c;
  245.         ans += c;
  246.         bor[row][28] -= c;
  247.         bor[row][27] = 0;
  248.     }
  249.     return ans;
  250. }
  251.  
  252. int main() {
  253.     ios_base::sync_with_stdio(0);
  254.     cin.tie(0);
  255.     cout << fixed;
  256.     int t;
  257.     long long ans;
  258.     cin >> t;
  259.     string s;
  260.     vector <int> p(29, 0);
  261.     vector<vector<int>> bor;
  262.     bor.push_back(p);
  263.     bor[0][27] = t;
  264.     bor[0][28] = t;
  265.     int last_row = 1;
  266.     forp(i, 0, t) {
  267.         cin >> s;
  268.         s += "$";
  269.         int row = 0;
  270.         reverse(s.begin(), s.end());
  271.         /*cout << s << endl;*/
  272.         for (auto now : s) {
  273.             int x = (int)now - 96;
  274.             if (x > 0 and bor[row][x] == 0) {
  275.                 bor.push_back(p);
  276.                 bor[row][x] = last_row;
  277.                 bor[last_row][27]++;
  278.                 row = last_row;
  279.                 last_row++;
  280.             }
  281.             else if (x > 0 and bor[row][x] != 0) {
  282.                 bor[bor[row][x]][27]++;
  283.                 row = bor[row][x];
  284.             }
  285.         }
  286.     }
  287.     forp(i, 0, t) {
  288.         cin >> s;
  289.         s += "$";
  290.         int row = 0;
  291.         reverse(s.begin(), s.end());
  292.         /*cout << s << endl;*/
  293.         for (auto now : s) {
  294.             int x = (int)now - 96;
  295.             if (x > 0 and bor[row][x] == 0) {
  296.                 bor.push_back(p);
  297.                 bor[row][x] = last_row;
  298.                 bor[last_row][28]++;
  299.                 row = last_row;
  300.                 last_row++;
  301.             }
  302.             else if (x > 0 and bor[row][x] != 0) {
  303.                 bor[bor[row][x]][28]++;
  304.                 row = bor[row][x];
  305.             }
  306.         }
  307.     }
  308.     /*for (auto now : bor) {
  309.         for (auto n : now) {
  310.             cout << setw(3);
  311.             cout << n << " ";
  312.         }
  313.         cout << endl;
  314.     }*/
  315.     dfs(bor, 0, -1, 0);
  316.     cout << cnt;
  317. }
  318. ----------------------------------------------------------------------------------------------------
  319. Задача на алгоритм Флойда
  320. ----------------------------------------------------------------------------------------------------
  321. //#pragma GCC optimize("03")
  322. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  323. #include <iostream>
  324. #include <iomanip>
  325. #include <cstdlib>
  326. #include <cstdio>
  327. #include <string>
  328. #include <vector>
  329. #include <map>
  330. #include <set>
  331. #include <unordered_map>
  332. #include <unordered_set>
  333. #include <queue>
  334. #include <deque>
  335. #include <cmath>
  336. #include <numeric>
  337. #include <algorithm>
  338. #include <ctime>
  339. #include <chrono>
  340. #include <random>
  341. #include <functional>
  342.  
  343. using namespace std;
  344.  
  345. const int N = 107;
  346. int graf[N][N];
  347. int cnt = 0;
  348. set <pair<int, int>> st;
  349.  
  350. bool floyd(int n) {
  351.     for (int k = 0; k < n; k++) {
  352.         for (int i = 0; i < n; i++) {
  353.             for (int j = 0; j < n; j++) {
  354.                 if (k != i and k != j) {
  355.                     if (graf[i][j] == graf[i][k] + graf[k][j]) {
  356.                         st.insert(make_pair(i, j));
  357.                         st.insert(make_pair(j, i));
  358.                     }
  359.                     else if (graf[i][j] > graf[i][k] + graf[k][j]) {
  360.                         //graf[i][j] = graf[i][k] + graf[k][j];
  361.                         return false;
  362.                     }
  363.                 }
  364.             }
  365.         }
  366.     }
  367.     return true;
  368. }
  369.  
  370. int main() {
  371.     ios_base::sync_with_stdio(0);
  372.     cin.tie(0);
  373.     cout << fixed;
  374.     int n, x;
  375.     cin >> n;
  376.     for (int i = 0; i < n; i++) {
  377.         for (int j = 0; j < n; j++) {
  378.             cin >> x;
  379.             graf[i][j] = x;
  380.         }
  381.     }
  382.     if (floyd(n)) {
  383.         cout << st.size() / 2;
  384.     }
  385.     else {
  386.         cout << -1;
  387.     }
  388. }
  389. --------------------------------------------------------------------------------------------------
  390.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement