Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Старт программы
- --------------------------------------------------------------------------------------------------
- //#pragma GCC optimize("03")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <unordered_map>
- #include <unordered_set>
- #include <queue>
- #include <deque>
- #include <cmath>
- #include <numeric>
- #include <algorithm>
- #include <ctime>
- #include <chrono>
- #include <random>
- #include <functional>
- using namespace std;
- using ll = long long;
- using db = double;
- using ldb = long double;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using vint = vector<int>;
- using vll = vector<ll>;
- #define all(x) x.begin(), x.end()
- #define size(x) int(x.size())
- #define rep(x) for(int rep_i = 0; rep_i < x; ++rep_i)
- #define forp(s, i, f) for(int i = s; i < f; ++i)
- #define form(s, i, f) for(int i = s; i > f; --i)
- const int MOD = 1e9 + 7;
- const int MOD2 = 998244353;
- const int INF = 2e9 + 7;
- const ll LINF = 1e18 + 7;
- const double EPS = 1e-9;
- const long double PI = acosl(-1.0);
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout << fixed;
- }
- ---------------------------------------------------------------------------------------------------
- sqrt
- ---------------------------------------------------------------------------------------------------
- #include <math.h>
- double sqrt(double x) {
- if (x <= 0)
- return 0; // if negative number throw an exception?
- int exp = 0;
- x = frexp(x, &exp); // extract binary exponent from x
- if (exp & 1) { // we want exponent to be even
- exp--;
- x *= 2;
- }
- double y = (1+x)/2; // first approximation
- double z = 0;
- while (y != z) { // yes, we CAN compare doubles here!
- z = y;
- y = (y + x/y) / 2;
- }
- return ldexp(y, exp/2); // multiply answer by 2^(exp/2)
- }
- --------------------------------------------------------------------------------------------------
- Быстрое возведение в степень по модулю
- --------------------------------------------------------------------------------------------------
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- unsigned powmod(unsigned base, unsigned exp, unsigned modulo)
- {
- unsigned res = 1;
- while (exp != 0)
- {
- if ((exp & 1) != 0)
- {
- res = (1ll * res * base) % modulo;
- }
- base = (1ll * base * base) % modulo;
- exp >>= 1;
- }
- return res;
- }
- ----------------------------------------------------------------------------------------------------
- НОК и НОД
- ----------------------------------------------------------------------------------------------------
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int gcd1(int a, int b) {
- if (b == 0)
- return a;
- return gcd1(b, a % b);
- }
- int gcd2(int a, int b) {
- while (b > 0) {
- a %= b;
- swap(a, b);
- }
- return a;
- }
- int lcm(int a, int b) {
- return a * b / gcd1(a, b);
- }
- int main() {
- long a, b;
- cin >> a >> b;
- cout << lcm(a, b);
- return 0;
- }
- --------------------------------------------------------------------------------------------------
- Расширенный алгоритм Эвклида
- --------------------------------------------------------------------------------------------------
- #include <iostream>
- #include <cmath>
- using namespace std;
- int gcd_ext(int a, int b, int& x, int& y) {
- if (b == 0) {
- x = 1;
- y = 0;
- return a;
- }
- int d = gcd_ext(b, a % b, x, y);
- x -= (a / b) * y;
- swap(x, y);
- return d;
- }
- int main() {
- return 0;
- }
- ---------------------------------------------------------------------------------------------------
- Решето Эратосфена
- ---------------------------------------------------------------------------------------------------
- #include <iostream>
- #include<cmath>
- #include <vector>
- using namespace std;
- int main() {
- long n = 15485864;
- vector <int> prime(n + 1, 1);
- prime[0] = prime[1] = 0;
- long k = 0;
- long stop;
- cin >> stop;
- long ans;
- for (int i = 2; i <= n; ++i) {
- if (!prime[i] || i * 1ll * i > n) {
- continue;
- }
- for (int j = i * i; j <= n; j += i)
- prime[j] = 0;
- }
- for (long i = 1; i < n; i++) {
- if (prime[i] == 1) {
- k++;
- }
- if (k == stop) {
- ans = i;
- break;
- }
- }
- cout << ans;
- return 0;
- }
- ------------------------------------------------------------------------------------------------------
- Задача с бором
- ------------------------------------------------------------------------------------------------------
- //#pragma GCC optimize("03")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <unordered_map>
- #include <unordered_set>
- #include <queue>
- #include <deque>
- #include <cmath>
- #include <numeric>
- #include <algorithm>
- #include <ctime>
- #include <chrono>
- #include <random>
- #include <functional>
- #define forp(i, s, f) for(int i = s; i < f; i++)
- using namespace std;
- using ll = long long;
- ll cnt = 0;
- int dfs(vector<vector<int>> &bor, int row, int k, int ans) {
- k++;
- int a = 0;
- forp(i, 0, 27) {
- if (bor[row][i] != 0) {
- a = dfs(bor, bor[row][i], k, 0);
- bor[row][27] -= a;
- bor[row][28] -= a;
- ans += a;
- }
- }
- int c = 0;
- if (bor[row][27] >= bor[row][28]) {
- c = bor[row][28];
- cnt += k * c;
- ans += c;
- bor[row][27] -= c;
- bor[row][28] = 0;
- }
- else {
- c = bor[row][27];
- cnt += k * c;
- ans += c;
- bor[row][28] -= c;
- bor[row][27] = 0;
- }
- return ans;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout << fixed;
- int t;
- long long ans;
- cin >> t;
- string s;
- vector <int> p(29, 0);
- vector<vector<int>> bor;
- bor.push_back(p);
- bor[0][27] = t;
- bor[0][28] = t;
- int last_row = 1;
- forp(i, 0, t) {
- cin >> s;
- s += "$";
- int row = 0;
- reverse(s.begin(), s.end());
- /*cout << s << endl;*/
- for (auto now : s) {
- int x = (int)now - 96;
- if (x > 0 and bor[row][x] == 0) {
- bor.push_back(p);
- bor[row][x] = last_row;
- bor[last_row][27]++;
- row = last_row;
- last_row++;
- }
- else if (x > 0 and bor[row][x] != 0) {
- bor[bor[row][x]][27]++;
- row = bor[row][x];
- }
- }
- }
- forp(i, 0, t) {
- cin >> s;
- s += "$";
- int row = 0;
- reverse(s.begin(), s.end());
- /*cout << s << endl;*/
- for (auto now : s) {
- int x = (int)now - 96;
- if (x > 0 and bor[row][x] == 0) {
- bor.push_back(p);
- bor[row][x] = last_row;
- bor[last_row][28]++;
- row = last_row;
- last_row++;
- }
- else if (x > 0 and bor[row][x] != 0) {
- bor[bor[row][x]][28]++;
- row = bor[row][x];
- }
- }
- }
- /*for (auto now : bor) {
- for (auto n : now) {
- cout << setw(3);
- cout << n << " ";
- }
- cout << endl;
- }*/
- dfs(bor, 0, -1, 0);
- cout << cnt;
- }
- ----------------------------------------------------------------------------------------------------
- Задача на алгоритм Флойда
- ----------------------------------------------------------------------------------------------------
- //#pragma GCC optimize("03")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <unordered_map>
- #include <unordered_set>
- #include <queue>
- #include <deque>
- #include <cmath>
- #include <numeric>
- #include <algorithm>
- #include <ctime>
- #include <chrono>
- #include <random>
- #include <functional>
- using namespace std;
- const int N = 107;
- int graf[N][N];
- int cnt = 0;
- set <pair<int, int>> st;
- bool floyd(int n) {
- for (int k = 0; k < n; k++) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (k != i and k != j) {
- if (graf[i][j] == graf[i][k] + graf[k][j]) {
- st.insert(make_pair(i, j));
- st.insert(make_pair(j, i));
- }
- else if (graf[i][j] > graf[i][k] + graf[k][j]) {
- //graf[i][j] = graf[i][k] + graf[k][j];
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout << fixed;
- int n, x;
- cin >> n;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cin >> x;
- graf[i][j] = x;
- }
- }
- if (floyd(n)) {
- cout << st.size() / 2;
- }
- else {
- cout << -1;
- }
- }
- --------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement