func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(0); cin.tie(NULL); int n, m; cin >> n >> m; if (n == 1) cout << n; else cout << ((m - 1) >= (n - m) ? m - 1 : m + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long double PI = acos((long double)-1); long long cnt[32][2]; class Node { public: Node* child[2]; long long cnt; Node() { child[0] = child[1] = NULL; cnt = 0; } }; class Trie { private: Node* root; public: Trie() { root = new Node(); } void insert(long long no) { Node* curr = root; for (long long i = 31; i >= 0; --i) { long long bit = (no >> i) & 1; if (!curr->child[bit]) { curr->child[bit] = new Node(); } if (curr->child[0] && curr->child[1]) { if (bit) { cnt[i][bit] += curr->child[0]->cnt; } else { cnt[i][bit] += curr->child[1]->cnt; } } curr = curr->child[bit]; ++curr->cnt; } } }; void solve() { long long n; cin >> n; Trie t; for (long long i = 1; i <= n; ++i) { long long x; cin >> x; t.insert(x); } long long x = 0; long long inv = 0; for (long long i = 31; i >= 0; --i) { if (cnt[i][1] < cnt[i][0]) { x += (1 << i); inv += cnt[i][1]; } else { inv += cnt[i][0]; } } cout << inv << << x << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /STACK:36777216 ) template <class T> inline T &RD(T &); template <class T> inline void OT(const T &); inline long long RD() { long long x; return RD(x); } inline double &RF(double &); inline double RF() { double x; return RF(x); } inline char *RS(char *s); template <class T0, class T1> inline T0 &RD(T0 &x0, T1 &x1) { RD(x0), RD(x1); return x0; } template <class T0, class T1, class T2> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) { RD(x0), RD(x1), RD(x2); return x0; } template <class T0, class T1, class T2, class T3> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { RD(x0), RD(x1), RD(x2), RD(x3); return x0; } template <class T0, class T1, class T2, class T3, class T4> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0; } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0; } template <class T0, class T1> inline void OT(const T0 &x0, const T1 &x1) { OT(x0), OT(x1); } template <class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) { OT(x0), OT(x1), OT(x2); } template <class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) { OT(x0), OT(x1), OT(x2), OT(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); } inline double &RF(double &a, double &b) { RF(a), RF(b); return a; } inline double &RF(double &a, double &b, double &c) { RF(a), RF(b), RF(c); return a; } inline double &RF(double &a, double &b, double &c, double &d) { RF(a), RF(b), RF(c), RF(d); return a; } inline double &RF(double &a, double &b, double &c, double &d, double &e) { RF(a), RF(b), RF(c), RF(d), RF(e); return a; } inline double &RF(double &a, double &b, double &c, double &d, double &e, double &f) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a; } inline double &RF(double &a, double &b, double &c, double &d, double &e, double &f, double &g) { RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a; } inline void RS(char *s1, char *s2) { RS(s1), RS(s2); } inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); } template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); } template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); } template <class T> inline T &SRT(T &A) { sort(A.begin(), A.end()); return A; } template <class T, class C> inline T &SRT(T &A, C B) { sort(A.begin(), A.end(), B); return A; } template <class T> inline T &UNQ(T &A) { A.resize(unique(SRT(A).begin(), SRT(A).end()) - A.begin()); return A; } const int dx4[] = {-1, 0, 1, 0}; const int dy4[] = {0, 1, 0, -1}; const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1}; const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2}; const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1}; const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long INFF = 1LL << 60; const double EPS = 1e-9; const double OO = 1e15; const double PI = acos(-1.0); template <class T> inline void checkMin(T &a, const T b) { if (b < a) a = b; } template <class T> inline void checkMax(T &a, const T b) { if (a < b) a = b; } template <class T> inline void checkMin(T &a, T &b, const T x) { checkMin(a, x), checkMin(b, x); } template <class T> inline void checkMax(T &a, T &b, const T x) { checkMax(a, x), checkMax(b, x); } template <class T, class C> inline void checkMin(T &a, const T b, C c) { if (c(b, a)) a = b; } template <class T, class C> inline void checkMax(T &a, const T b, C c) { if (c(a, b)) a = b; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } inline int ceil(int x, int y) { return (x - 1) / y + 1; } inline int sgn(double x) { return x < -EPS ? -1 : x > EPS; } inline int sgn(double x, double y) { return sgn(x - y); } namespace BO { inline bool _1(int x, int i) { return bool(x & 1 << i); } inline bool _1(long long x, int i) { return bool(x & 1LL << i); } inline long long _1(int i) { return 1LL << i; } template <class T> inline bool odd(T x) { return x & 1; } template <class T> inline bool even(T x) { return !odd(x); } template <class T> inline T low_bit(T x) { return x & -x; } inline int count_bits(int x) { return __builtin_popcount(x); } inline int count_bits(long long x) { return __builtin_popcountll(x); } } // namespace BO using namespace BO; namespace NT { inline void INC(int &a, int b) { a += b; if (a >= MOD) a -= MOD; } inline int sum(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline void DEC(int &a, int b) { a -= b; if (a < 0) a += MOD; } inline int dff(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } inline void MUL(int &a, int b) { a = (long long)a * b % MOD; } inline int pdt(int a, int b) { return (long long)a * b % MOD; } inline int sum(int a, int b, int c) { return sum(sum(a, b), c); } inline int sum(int a, int b, int c, int d) { return sum(sum(a, b), sum(c, d)); } inline int pdt(int a, int b, int c) { return pdt(pdt(a, b), c); } inline int pdt(int a, int b, int c, int d) { return pdt(pdt(pdt(a, b), c), d); } inline int pow(int a, int b) { int c(1); while (b) { if (b & 1) MUL(c, a); MUL(a, a), b >>= 1; } return c; } inline int pow(int a, long long b) { int c(1); while (b) { if (b & 1) MUL(c, a); MUL(a, a), b >>= 1; } return c; } template <class T> inline T pow(T a, long long b) { T c(1); while (b) { if (b & 1) c *= a; a *= a, b >>= 1; } return c; } inline int _I(int b) { int a = MOD, x1 = 0, x2 = 1, q; while (true) { q = a / b, a %= b; if (!a) return (x2 + MOD) % MOD; DEC(x1, pdt(q, x2)); q = b / a, b %= a; if (!b) return (x1 + MOD) % MOD; DEC(x2, pdt(q, x1)); } } inline void DIV(int &a, int b) { MUL(a, _I(b)); } inline int qtt(int a, int b) { return pdt(a, _I(b)); } inline int phi(int n) { int res = n; for (int i = 2; sqr(i) <= n; ++i) if (!(n % i)) { DEC(res, qtt(res, i)); do { n /= i; } while (!(n % i)); } if (n != 1) DEC(res, qtt(res, n)); return res; } } // namespace NT using namespace NT; namespace Math { long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; } long long extendGCD(long long a, long long b, long long &x, long long &y) { if (!b) return x = 1, y = 0, a; long long res = extendGCD(b, a % b, x, y), tmp = x; x = y, y = tmp - (a / b) * y; return res; } long long power(long long x, long long k) { long long res = 1; while (k) { if (k & 1) res *= x; x *= x, k >>= 1; } return res; } long long powerMod(long long x, long long k, long long m) { long long res = 1; while (x %= m, k) { if (k & 1) res *= x, res %= m; x *= x, k >>= 1; } return res; } long long inverse(long long a, long long p, long long t = 1) { long long pt = power(p, t); long long x, y; y = extendGCD(a, pt, x, y); return x < 0 ? x += pt : x; } long long linearCongruence(long long a, long long b, long long p, long long q) { long long x, y; y = extendGCD(p, q, x, y); while (b < a) b += q / y; x *= b - a, x = p * x + a, x %= p * q; if (x < 0) x += p * q; return x; } const int PRIMERANGE = 1000000; int prime[PRIMERANGE + 1]; int getPrime() { memset(prime, 0, sizeof(int) * (PRIMERANGE + 1)); for (int i = 2; i <= PRIMERANGE; i++) { if (!prime[i]) prime[++prime[0]] = i; for (int j = 1; j <= prime[0] && prime[j] <= PRIMERANGE / i; j++) { prime[prime[j] * i] = 1; if (i % prime[j] == 0) break; } } return prime[0]; } int factor[100][3], facCnt; int getFactors(int x) { facCnt = 0; int tmp = x; for (int i = 1; prime[i] <= tmp / prime[i]; i++) { factor[facCnt][1] = 1, factor[facCnt][2] = 0; if (tmp % prime[i] == 0) factor[facCnt][0] = prime[i]; while (tmp % prime[i] == 0) factor[facCnt][2]++, factor[facCnt][1] *= prime[i], tmp /= prime[i]; if (factor[facCnt][1] > 1) facCnt++; } if (tmp != 1) factor[facCnt][0] = tmp, factor[facCnt][1] = tmp, factor[facCnt++][2] = 1; return facCnt; } long long combinationModP(long long n, long long k, long long p) { if (k > n) return 0; if (n - k < k) k = n - k; long long a = 1, b = 1, x, y; int pcnt = 0; for (int i = 1; i <= k; i++) { x = n - i + 1, y = i; while (x % p == 0) x /= p, pcnt++; while (y % p == 0) y /= p, pcnt--; x %= p, y %= p, a *= x, b *= y; b %= p, a %= p; } if (pcnt) return 0; extendGCD(b, p, x, y); if (x < 0) x += p; a *= x, a %= p; return a; } }; // namespace Math namespace Geo { const double eps = 1e-8; int dblcmp(double d) { return d < -eps ? -1 : d > eps; } int sgn(double a) { return a < -eps ? -1 : a > eps; } inline double sqr(double x) { return x * x; } }; // namespace Geo template <class T> inline T &RD(T &x) { char c; for (c = getchar(); c < - ; c = getchar()) ; if (c == - ) { x = 0 - getchar(); for (c = getchar(); 0 <= c && c <= 9 ; c = getchar()) x = x * 10 + 0 - c; } else { x = c - 0 ; for (c = getchar(); 0 <= c && c <= 9 ; c = getchar()) x = x * 10 + c - 0 ; } return x; } inline double &RF(double &x) { scanf( %lf , &x); return x; } inline char *RS(char *s) { scanf( %s , s); return s; } int Case; template <class T> inline void OT(const T &x) { cout << x << endl; } int n; void solve() { cin >> n; vector<int> p, q; p.clear(); q.clear(); cout << 1 << << n - 1 << << 1; for (int i = 2; i <= n; ++i) { p.push_back(i); cout << << i; } cout << endl; cout.flush(); int d, dd; cin >> d; while (p.size() > 1) { q.clear(); while (p.size() - 1 >= q.size() + 1) { q.push_back(p.back()); p.pop_back(); } cout << 1 << << int(q.size()) << << 1; for (int i = 0; i < int(q.size()); ++i) cout << << q[i]; cout << endl; cout.flush(); cin >> dd; if (dd == d) { p = q; } } int x = p.back(); cout << 1 << << n - 1 << << x; for (int i = 1; i <= n; ++i) if (i != x) { cout << << i; } cout << endl; cout.flush(); cin >> d; cout << -1 << << d << endl; cout.flush(); } int main() { cin >> Case; while (Case--) solve(); }
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << << e2 << << e3 << endl; } template <class T1, class T2, class T3, class T4> void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << << e2 << << e3 << << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << << e2 << << e3 << << e4 << << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << << e2 << << e3 << << e4 << << e5 << << e6 << endl; } template <class T> T gcd(const T a, const T b) { return (b ? gcd<T>(b, a % b) : a); } template <class T> T lcm(const T a, const T b) { return (a / gcd<T>(a, b) * b); } struct node { long long x, y; node() {} node(long long x, long long y) : x(x), y(y) {} bool operator<(const node& p) const { return x < p.x; } }; string s1, s2; long long convert_10(long long n, string s) { long long l; l = s.size() - 1; long long x = 1, y = 0, p; for (long long i = l; i >= 0; i--) { if (s[i] >= 0 && s[i] <= 9 ) p = s[i] - 0 ; else p = 10 + s[i] - A ; y += x * p; x *= n; } return y; } string convert_base(long long n, long long x) { string s = ; while (x > 0) { long long y = x % n; x /= n; char c; if (y > 9) c = A + (y - 10); else c = 0 + y; s = c + s; } if (s == ) s = 0 ; return s; } string convert_roman(long long n) { string s = ; string one[10] = { , I , II , III , IV , V , VI , VII , VIII , IX }; string ten[10] = { , X , XX , XXX , XL , L , LX , LXX , LXXX , XC }; string hundred[10] = { , C , CC , CCC , CD , D , DC , DCC , DCCC , CM }; string thousand[4] = { , M , MM , MMM }; s += thousand[n / 1000]; n %= 1000; s += hundred[n / 100]; n %= 100; s += ten[n / 10]; n %= 10; s += one[n]; return s; } long long convert_num(string s) { long long x = 0; for (long long i = 0; i < s.size(); i++) x = x * 10 + s[i] - 0 ; return x; } int main() { long long i, j, k, l, x, y, z, t, m, n; cin >> n >> s1 >> s2; x = convert_10(n, s2); string s; if (s1 == R ) { s = convert_roman(x); } else { y = convert_num(s1); s = convert_base(y, x); } deb(s); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200005; int arr[N], f[N]; int main() { int n; scanf( %d , &n); set<int> st; for (int i = 1; i <= n; ++i) { st.insert(i); scanf( %d , arr + i); } st.insert(n + 1); int q; scanf( %d , &q); int t, x, v; while (q--) { scanf( %d , &t); if (t == 1) { scanf( %d %d , &x, &v); int cur = *st.lower_bound(x); while (v && cur <= n) { if (f[cur] + v < arr[cur]) { f[cur] += v; v = 0; } else { v -= (arr[cur] - f[cur]); f[cur] = arr[cur]; st.erase(cur); cur = *st.lower_bound(x); } } } else { scanf( %d , &x); printf( %d n , f[x]); } } }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define pb push_back #define ll long long #define all(x) x.begin() , x.end() #define rep(i,s,e) for (int i = s; i < e; ++i) #define rev(i,s,e) for (int i = s; i > e; --i) const int N = 2e5 + 99 ; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef CLion freopen( input.txt , r , stdin); // freopen( output.txt , w , stdout); #endif int t; cin >> t; while ( t-- ){ int n ; cin >> n; vector<int> vec(n); for ( int &i : vec ){ cin >> i; } sort( all(vec) ); cout << n - (upper_bound(all(vec) , vec.front()) - vec.begin()) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; long long g = n - 1, res = 0; while (g > 0 && k > 0) { res += g * 2 - 1; k--; g -= 2; } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> tmp; vector<vector<int>> sol[4]; int lov[7][7]; int hero(string s) { if (s == Troll ) return 0; if (s == Dracul ) return 1; if (s == Anka ) return 2; if (s == Snowy ) return 3; if (s == Hexadecimal ) return 4; if (s == Chapay ) return 5; else return 6; } void rec(int i, int a, int b, int c) { if (i == 7) { int z[] = {a, b, c}; sort(z, z + 3); if (z[2] == 5 && z[1] == 1 && z[0] == 1) sol[0].push_back(tmp); if (z[2] == 4 && z[1] == 2 && z[0] == 1) sol[1].push_back(tmp); if (z[2] == 3 && z[1] == 3 && z[0] == 1) sol[2].push_back(tmp); if (z[2] == 3 && z[1] == 2 && z[0] == 2) sol[3].push_back(tmp); return; } tmp.push_back(1); rec(i + 1, a + 1, b, c); tmp.pop_back(); tmp.push_back(2); rec(i + 1, a, b + 1, c); tmp.pop_back(); tmp.push_back(3); rec(i + 1, a, b, c + 1); tmp.pop_back(); } int main() { rec(0, 0, 0, 0); int arr[4][3]; pair<int, int> exp[3]; int mini = 1e9, maxi = 0; set<int> minI; arr[0][0] = 5, arr[0][1] = 1, arr[0][2] = 1; arr[1][0] = 4, arr[1][1] = 2, arr[1][2] = 1; arr[2][0] = 3, arr[2][1] = 3, arr[2][2] = 1; arr[3][0] = 3, arr[3][1] = 2, arr[3][2] = 2; int n; cin >> n; for (int i = 0; i < n; i++) { string s, tmp, s2; cin >> s >> tmp >> s2; int x = hero(s), y = hero(s2); lov[x][y] = 1; } for (int i = 0; i < 3; i++) { cin >> exp[i].second; exp[i].first = i; } for (int i = 0; i < 4; i++) { int a = 0; do { int ans[3] = {}; for (int j = 0; j < 3; j++) ans[j] = exp[j].second / arr[i][j]; sort(ans, ans + 3); if (ans[2] - ans[0] < mini) minI.clear(); if (ans[2] - ans[0] <= mini) minI.insert(i); mini = min(ans[2] - ans[0], mini); a++; } while (next_permutation(exp, exp + 3)); } for (set<int>::iterator it = minI.begin(); it != minI.end(); it++) { for (int i = 0; i < sol[*it].size(); i++) { int tmpA = 0; for (int x = 0; x < 7; x++) { for (int y = 0; y < 7; y++) { if (x != y && sol[*it][i][x] == sol[*it][i][y] && lov[x][y]) tmpA++; } } maxi = max(tmpA, maxi); } } cout << mini << << maxi << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int i, n; scanf( %d , &n); for (i = 1; i <= 4; ++i) { int m1, m2, a, b; scanf( %d%d , &a, &b); m1 = min(a, b); scanf( %d%d , &a, &b); m2 = min(a, b); if (m1 + m2 <= n) { printf( %d %d %d n , i, m1, n - m1); return 0; } } puts( -1 ); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; const double EPS = 1e-9; const char *crop[3] = { Carrots , Kiwis , Grapes }; int m, n, nw, nq; int main() { scanf( %d%d%d%d , &m, &n, &nw, &nq); set<int> waste[40000]; for (int i = (0); i < (nw); ++i) { int x, y; scanf( %d%d , &y, &x); x--, y--; waste[y].insert(x); } int hd[40000]; for (int i = 0, tmp = 0; i < m; i++) { hd[i] = tmp; tmp = (tmp + (n - waste[i].size())) % 3; } for (int i = (0); i < (nq); ++i) { int x, y; scanf( %d%d , &y, &x); x--, y--; if (waste[y].count(x) == 1) { puts( Waste ); } else { int tmp = distance(waste[y].begin(), waste[y].lower_bound(x)); tmp = x - tmp; tmp = (hd[y] + tmp) % 3; puts(crop[tmp]); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 100000000; const double eps = 1e-10; const int MAXN = 110; set<int> sx, sy; int in[10][2]; set<int>::iterator it1, it2; int main() { int x, y; for (int i = 0; i < (8); ++i) { scanf( %d%d , &x, &y); in[i][0] = x; in[i][1] = y; if (!sx.count(x)) sx.insert(x); if (!sy.count(y)) sy.insert(y); } if ((int)sx.size() != 3 || (int)sy.size() != 3) printf( %s n , ugly ); else { int mat[5][5]; memset(mat, 0, sizeof(mat)); int cnt = 0, f = 1; map<int, int> xx, yy; for (__typeof((sx).begin()) it1 = (sx).begin(); it1 != (sx).end(); it1++) xx[(*it1)] = cnt++; cnt = 0; for (__typeof((sy).begin()) it2 = (sy).begin(); it2 != (sy).end(); it2++) yy[(*it2)] = cnt++; for (int i = 0; i < (8); ++i) mat[xx[in[i][0]]][yy[in[i][1]]] = 1; for (int i = 0; i < (3); ++i) { for (int j = 0; j < (3); ++j) { if (i == 1 && j == 1 && mat[i][j]) f = 0; if ((i != 1 || j != 1) && !mat[i][j]) f = 0; } } if (f) printf( %s n , respectable ); else printf( %s n , ugly ); } }
#include <bits/stdc++.h> using namespace std; int main() { int t; long long int a, b, c; cin >> t; while (t--) { cin >> a >> b >> c; long long int max_val = (a + c); long long int min_val = b; if (a + c <= b) { cout << 0 << endl; } else if (b + c < a) { cout << c + 1 << endl; } else { long long int ans = (max_val - min_val + 1) / 2; cout << ans << endl; } } }
#include <bits/stdc++.h> using namespace std; const int N = 110; long long read(void) { char ch = getchar(); long long sum = 0; bool f = 0; while ((ch < 0 || ch > 9 ) && ch != - ) ch = getchar(); if (ch == - ) { f = 1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) sum = sum * 10 + ch - 0 , ch = getchar(); if (f) sum = -sum; return sum; } int T, n, m; string st; int col[N][N]; int tA; struct node { int x1, y1, x2, y2, x3, y3; } que[N * N * 3]; void update(int x, int y) { if (x + 1 <= n) { if (y + 1 <= m) { que[++tA] = (node){x, y, x, y + 1, x + 1, y + 1}; que[++tA] = (node){x, y, x + 1, y, x + 1, y + 1}; que[++tA] = (node){x, y, x + 1, y, x, y + 1}; } else { que[++tA] = (node){x, y, x, y - 1, x + 1, y - 1}; que[++tA] = (node){x, y, x + 1, y, x + 1, y - 1}; que[++tA] = (node){x, y, x + 1, y, x, y - 1}; } } else { if (y + 1 <= m) { que[++tA] = (node){x, y, x, y + 1, x - 1, y + 1}; que[++tA] = (node){x, y, x - 1, y, x - 1, y + 1}; que[++tA] = (node){x, y, x - 1, y, x, y + 1}; } else { que[++tA] = (node){x, y, x, y - 1, x - 1, y - 1}; que[++tA] = (node){x, y, x - 1, y, x - 1, y - 1}; que[++tA] = (node){x, y, x - 1, y, x, y - 1}; } } } void solve(void) { n = read(); m = read(); tA = 0; for (int i = 1; i <= n; i++) { cin >> st; for (int j = 1; j <= m; j++) col[i][j] = (st[j - 1] == 0 ) ? 0 : 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (col[i][j] == 1) update(i, j); printf( %d n , tA); for (int i = 1; i <= tA; i++) printf( %d %d %d %d %d %d n , que[i].x1, que[i].y1, que[i].x2, que[i].y2, que[i].x3, que[i].y3); return; } int main() { T = read(); while (T--) solve(); return 0; }
#include <bits/stdc++.h> int main(void) { int a[5]; int Min; while (scanf( %d%d%d%d%d , a, a + 1, a + 2, a + 3, a + 4) != EOF) { Min = a[0]; if (a[1] < Min) Min = a[1]; if (a[2] / 2 < Min) Min = a[2] / 2; if (a[3] / 7 < Min) Min = a[3] / 7; if (a[4] / 4 < Min) Min = a[4] / 4; printf( %d n , Min); } return EXIT_SUCCESS; }
#include <bits/stdc++.h> using namespace std; using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << : << arg1 << n ; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } long long int gcd(long long int a, long long int b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } long long int isPrime(long long int n) { long long int p = (long long int)sqrt(n); for (int i = (2); i <= (p); i += (1)) if (n % i == 0) return 0; return 1; } long long int pow(long long int b, long long int e) { if (e == 0) return 1; else if (e % 2 == 0) { long long int a = pow(b, e / 2); return a * a; } else { long long int a = pow(b, e / 2); return b * a * a; } } long long int powm(long long int x, long long int y, long long int m = 1000000007) { x = x % m; long long int res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } long long int modInverse(long long int a, long long int m) { return powm(a, m - 2, m); } void solve() { long long int x, y; cin >> x >> y; long long int c[7]; for (int i = (1); i <= (6); i += (1)) cin >> c[i]; long long int ans = LLONG_MAX; int isnegx = 1, isnegy = 1; if (x < 0) isnegx = -1; if (y < 0) isnegy = -1; swap(x, y); if (x >= 0 and y >= 0) { if (x <= y) { ans = min({ans, c[1] * x + (y - x) * c[6], c[1] * y + (y - x) * c[5], c[2] * x + c[6] * y}); } else { ans = min({ans, c[1] * x + (x - y) * c[3], c[1] * y + (x - y) * c[2], c[2] * x + c[6] * y}); } } else if (x <= 0 and y <= 0) { x *= -1; y *= -1; if (x <= y) { ans = min({ans, c[4] * x + (y - x) * c[3], c[4] * y + (y - x) * c[2], c[5] * x + c[3] * y}); } else { ans = min({ans, c[4] * x + (x - y) * c[6], c[4] * y + (x - y) * c[5], c[5] * x + c[3] * y}); } } else { x = isnegx * abs(x); y = isnegy * abs(y); if (x <= 0) { x *= -1; ans = min({ans, c[1] * x + (x + y) * c[3], c[4] * y + (x + y) * c[2], c[2] * x + c[3] * y}); } else { y *= -1; ans = min({ans, c[4] * x + (x + y) * c[6], c[1] * y + (x + y) * c[5], c[5] * x + c[6] * y}); } } cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int T; cin >> T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, ar[1000001], x, ars[1000001]; int main() { cin >> n >> x; for (int i = 0; i < n; i++) { cin >> ar[i]; ars[ar[i]]++; } long long ans = 0; for (int i = 0; i < n; i++) { ars[ar[i]]--; ans += (ars[(ar[i] ^ x)]); } cout << ans; return 0; }
#include <bits/stdc++.h> const int MaxN = 300; using namespace std; int n, m; long long K; int A1[MaxN + 5][2], A3[MaxN + 5][MaxN + 5]; const long long INF = 1e18; long long A2[MaxN + 5][MaxN + 5]; int fail[MaxN + 5]; void Get(string s) { int z = (int)s.length(); fail[0] = 0; for (int i = 1; i < z; i++) { int j = fail[i - 1]; while (j > 0 && s[j] != s[i]) j = fail[j - 1]; if (s[j] == s[i]) j++; fail[i] = j; } for (int i = 0; i <= z; i++) { for (int j = 0; j < 2; j++) { if (i < z && s[i] == char( 0 + j)) A1[i][j] = i + 1; else if (i == 0) A1[i][j] = 0; else A1[i][j] = A1[fail[i - 1]][j]; } } for (int i = 0; i <= z; i++) { for (int j = 0; j < 2; j++) { A3[i][j] = A1[i][j]; A2[i][j] = (A3[i][j] == z ? 1 : 0); } } for (int i = 2; i <= n; i++) { for (int j = 0; j <= z; j++) { A3[j][i] = A3[A3[j][i - 2]][i - 1]; A2[j][i] = min((A2[j][i - 2] + A2[A3[j][i - 2]][i - 1]), INF); } } } int main() { while (~scanf( %d%lld%d , &n, &K, &m)) { string res = ; for (int i = 0; i < m; i++) { if (res != ) Get(res); int x = 0; if (res != && A3[0][n] == i) x = 1; if (K == 1 && x == 1) break; K -= x; Get(res + 0 ); long long tmp = A2[0][n]; if (K > tmp) { res += 1 ; K -= tmp; } else res += 0 ; } cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int a, b, c, d, i; int digits(int n) { int d = 0; while (n) { n /= 10; d++; } return d; } int main() { cin >> a >> b; c = a; d = digits(b); for (i = 1; b; i++) { c += (b % 10) * (int)floor(pow((double)10, (double)d - i)); b /= 10; } cout << c; }
#include <bits/stdc++.h> using namespace std; int n; int main() { multiset<int> Q; cin >> n; long long ans = 0; long long times = 0; bool b = false; int count = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (Q.size() == 0 || (*Q.begin()) > x) { Q.insert(x); } else { ans += x - *Q.begin(); if (b) { times += 2; count++; if (count == 2) { b = false; count = 0; } } else b = true; Q.erase(Q.begin()); Q.insert(x); Q.insert(x); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int read() { char ch = getchar(); int x = 0; while (ch < 0 || ch > 9 ) ch = getchar(); while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); return x; } const int maxn = 2e5 + 5, inf = 1e9; struct Data { int next, to, w; }; struct LinkTable { Data data[maxn]; int head[maxn], cnt; void add(int x, int y, int w) { data[++cnt] = (Data){head[x], y, w}; head[x] = cnt; } } E, R; struct DP { int sum, id, lca; } f[maxn], g[maxn]; int N, K, mx[5], mi[5], ret[maxn], fa[maxn], w[maxn]; bool mark[maxn]; int update(int &x, int y) { if (x < y) return x = y, 1; if (x == y) return 0; return -1; } void F(int x) { int sum = mark[x] ? 0 : -inf, id = x; for (int i = E.head[x]; i; i = E.data[i].next) if (E.data[i].to != fa[x]) { Data &e = E.data[i]; fa[e.to] = x; w[e.to] = e.w; F(e.to); if (update(sum, f[e.to].sum + e.w) == 1) id = f[e.to].id; else if (!update(sum, f[e.to].sum + e.w)) id = x; } f[x].sum = sum; f[x].id = id; f[x].lca = x; } void G(int x) { g[x] = g[fa[x]]; g[x].sum += w[x]; if (mark[fa[x]] && w[x] > g[x].sum) g[x].sum = w[x], g[x].id = g[x].lca = fa[x]; for (int i = R.head[fa[x]]; i; i = R.data[i].next) if (R.data[i].to != x) { Data &e = R.data[i]; if (update(g[x].sum, f[e.to].sum + e.w + w[x]) == 1) g[x].id = f[e.to].id, g[x].lca = fa[x]; else if (update(g[x].sum, f[e.to].sum + e.w + w[x]) == 0) g[x].id = g[x].lca = fa[x]; } for (int i = 0; i < 3; ++i) mi[i] = 0, mx[i] = -inf; for (int i = E.head[x]; i; i = E.data[i].next) if (E.data[i].to != fa[x]) { Data &e = E.data[i]; if (update(mx[0], f[e.to].sum + e.w) == 1) { mx[2] = mx[1], mi[2] = mi[1]; mx[1] = mx[0], mi[1] = mi[0]; mi[0] = e.to; } else if (update(mx[1], f[e.to].sum + e.w) == 1) mx[2] = mx[1], mi[2] = mi[1], mi[1] = e.to; else if (update(mx[2], f[e.to].sum + e.w) == 1) mi[2] = e.to; } for (int i = 0; i < 3; ++i) if (mi[i]) R.add(x, mi[i], w[mi[i]]); for (int i = E.head[x]; i; i = E.data[i].next) if (E.data[i].to != fa[x]) G(E.data[i].to); } void init() { N = read(), K = read(); for (int i = 1; i <= K; ++i) mark[read()] = true; for (int i = 1, u, v, w; i < N; ++i) u = read(), v = read(), w = read(), E.add(u, v, w), E.add(v, u, w); g[0].sum = -inf; } void dfs(int x) { for (int i = E.head[x]; i; i = E.data[i].next) if (E.data[i].to != fa[x]) { Data &e = E.data[i]; dfs(e.to); ret[x] += ret[e.to]; } } void solve() { for (int i = 1; i <= N; ++i) if (mark[i] && f[i].sum != g[i].sum) if (f[i].sum > g[i].sum) ++ret[f[i].id], --ret[f[i].lca]; else ++ret[g[i].id], --ret[g[i].lca], ++ret[i], --ret[fa[g[i].lca]]; dfs(1); int sum = -inf, once = 0; for (int i = 1; i <= N; ++i) if (!mark[i]) if (ret[i] > sum) sum = ret[i], once = 1; else if (ret[i] == sum) ++once; printf( %d %d n , sum, once); } int main() { init(); F(1), G(1); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; const int INF = 0x3f3f3f3f; long long get(long long n) { long long res = 2; long long ans = 0; while (res <= n) { ans += n / res; res *= 2; } return ans; } int main() { long long n; cin >> n; long long m = n; long long ans = n; long long p = 1; while (p < n) { ans += ((n - (p + 1)) / (2 * p) * p) + p; p *= 2; } cout << ans - n << endl; }
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; set<int> s; for (int i = 0; i < n; i++) { int a; cin >> a; s.insert(a); } if (s.size() > k) { cout << -1 << endl; return; } cout << n * k << endl; for (int i = 0; i < n; i++) { for (int b : s) cout << b << ; for (int j = 0; j < k - (int)s.size(); j++) cout << 1 << ; } cout << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
#include <bits/stdc++.h> using namespace std; long parent[100]; long nm[100]; vector<long> v[100]; long pp(long z) { if (parent[z] == z) { return z; } else { parent[z] = pp(parent[z]); } } int main() { long i, j, k, l, m, n, p, two, one; scanf( %ld%ld , &n, &m); for (i = 1; i <= n; i++) parent[i] = i; for (i = 1; i <= m; i++) { scanf( %ld%ld , &k, &l); parent[l] = k; } for (i = 1; i <= n; i++) { k = pp(i); } for (i = 1; i <= n; i++) { nm[parent[i]]++; if (nm[parent[i]] > 3) { printf( -1 n ); return 0; } } l = one = two = 0; for (i = 1; i <= n; i++) { if (nm[i] == 3) { k = 0; for (j = 1; j <= n; j++) { if (parent[j] == i) { v[l].push_back(j); parent[j] = -1; k++; } } nm[i] = -1; l++; } if (nm[i] == 2) two++; else if (nm[i] == 1) one++; } if (one >= two) { if ((one - two) % 3) { printf( -1 n ); return 0; } while (two) { for (i = 1; i <= n; i++) { if (nm[i] == 2) { for (j = 1; j <= n; j++) { if (nm[j] == 1) { for (k = 1; k <= n; k++) { if (parent[k] == j) { v[l].push_back(k); nm[j] = -1; parent[k] = -1; break; } } break; } } for (j = 1; j <= n; j++) { if (parent[j] == i) { v[l].push_back(j); parent[j] = -1; } } nm[i] = -1; two--; one--; l++; break; } } } while (one) { k = 0; for (i = 1; i <= n; i++) { if (nm[i] == 1) { v[l].push_back(i); nm[i] = -1; k++; one--; if (k == 3) { l++; break; } } } } for (i = 0; i < l; i++) printf( %ld %ld %ld n , v[i][0], v[i][1], v[i][2]); } else { printf( -1 n ); return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; int compare(const void* pa, const void* pb) { long long int p1 = *(const long long int*)pa; long long int p2 = *(const long long int*)pb; if (p1 < p2) return 1; else if (p1 > p2) return -1; else if (p1 == p2) return 0; } long long int arr[200500], s[200500][2], ans[200500]; int main() { int j, t, n; cin >> t; while (t--) { int p = 0, x, y, a1, b1, a, b, low, high, mx, l = 0, m = 0, h = 0, w = -1; long long sum = 0, k; cin >> n; for (j = 0; j < n; j++) cin >> arr[j]; cin >> x >> a; cin >> y >> b; cin >> k; a1 = a; b1 = b; qsort(arr, n, sizeof(arr[0]), compare); while (1) { if (a < b) { s[p][0] = a; s[p][1] = x; p++; a += a1; } else if (b < a) { s[p][0] = b; s[p][1] = y; p++; b += b1; } else { s[p][0] = a; s[p][1] = x + y; p++; a += a1; b += b1; } if (a > n && b > n) break; } low = min(x, y); high = max(x, y); mx = x + y; for (j = 0; j < p; j++) { if (s[j][1] == low) { sum += (arr[l] / 100) * (s[j][1] - ans[l]); ans[l] = low; l++; } else if (s[j][1] == high) { sum += (arr[h] / 100) * (s[j][1] - ans[h]); ans[h] = high; if (h != l) { sum += (arr[l] / 100) * (low - ans[l]); ans[l] = low; } h++; l++; } else if (s[j][1] == mx) { sum += (arr[m] / 100) * (s[j][1] - ans[m]); ans[m] = mx; if (m != h) { sum += (arr[h] / 100) * (high - ans[h]); ans[h] = high; } if (h != l) { sum += (arr[l] / 100) * (low - ans[l]); ans[l] = low; } m++; h++; l++; } if (sum >= k) { w = s[j][0]; break; } } cout << w << n ; for (j = 0; j < n; j++) { s[j][0] = 0; s[j][1] = 0; arr[j] = 0; ans[j] = 0; } } }
#include <bits/stdc++.h> using namespace std; int n, m; char s[505]; int sz; int val[505]; int ch[505][11]; void insert() { int f, k = 0; scanf( %s%d , s, &f); for (int i = 0; s[i]; ++i) { int c = s[i] - 0 ; if (!ch[k][c]) ch[k][c] = ++sz; k = ch[k][c]; } val[k] += f; } void init() { scanf( %d%d , &n, &m); while (n--) insert(); } int dep[505]; int dp[505][505][15], f[505][15]; void dfs(int i) { if (val[i]) for (int l = 0; l <= dep[i]; ++l) for (int k = 0; k <= m; ++k) dp[i][l][k] = l * val[i]; for (int c = 0; c < 11; ++c) { int j = ch[i][c]; if (!j) continue; dep[j] = dep[i] + 1; dfs(j); memcpy(f, dp[i], sizeof(f)); memset(dp[i], 0x3f, sizeof(dp[i])); for (int l = 0; l <= dep[i]; ++l) for (int k = m; ~k; --k) for (int p = 0; p <= k; ++p) { dp[i][l][k] = min(dp[i][l][k], f[l][k - p] + dp[j][l + 1][p]); if (p) dp[i][l][k] = min(dp[i][l][k], f[l][k - p] + dp[j][0][p - 1]); } } } void solve() { dfs(0); printf( %d n , dp[0][0][m]); } int main() { init(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int LEN = (int)1e5; int n; long long a[LEN]; vector<int> g[60]; int bfs(int r, int t) { queue<int> q; static int d[LEN]; memset(d, 0, sizeof d); q.push(r); d[r] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int j = 0; (1ll << j) <= a[u]; j++) if ((1ll << j) & a[u]) for (auto v : g[j]) if (!d[v] && !(v == t && u == r)) { d[v] = d[u] + 1; if (v == t) return d[v] > 2 ? d[v] : INT_MAX; q.push(v); } } return INT_MAX; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; for (int j = 0; (1ll << j) <= a[i]; j++) if ((1ll << j) & a[i]) g[j].push_back(i); ; ; } for (int i = 0; i < 60; i++) if ((int)(g[i]).size() >= 3) { cout << 3; return 0; } int ans = INT_MAX; for (int i = 0; i < 60; i++) if ((int)(g[i]).size() > 1) { a[g[i][0]] &= ~(1ll << i); ans = min(ans, bfs(g[i][0], g[i][1])); a[g[i][0]] |= 1ll << i; } cout << (ans < INT_MAX ? ans : -1); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int s[10], a, b, c; cin >> a >> b >> c; s[0] = a + (b * c); s[1] = a * (b + c); s[2] = a * b * c; s[3] = (a + b) * c; s[4] = a + b + c; s[5] = (a * b) + c; sort(s, s + 5, greater<int>()); cout << s[0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> T __trace(string s, T x) { cerr << s << x << endl; return x; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { for (T x : v) os << x << ; return os; } template <class T> istream& operator>>(istream& is, vector<T>& v) { for (T& x : v) is >> x; return is; } void run() { long long n; cin >> n; map<pair<long long, long long>, long long> m; long long ans = 0; for (long long i = 0; i < (n); i++) { long long h, mi; cin >> h >> mi; m[make_pair(h, mi)]++; ans = max(ans, m[make_pair(h, mi)]); } cout << ans << endl; } int main() { run(); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline void _read(T& x) { char ch = getchar(); bool sign = true; while (!isdigit(ch)) { if (ch == - ) sign = false; ch = getchar(); } for (x = 0; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - 0 ; if (!sign) x = -x; } bool mark[1505]; int n, ans[1505], lim; int d[5] = {0, 1, 5, 10, 50}; void dfs(int id, int x, int tot) { if (x == lim + 1) { mark[tot] = true; return; } for (int i = id; i <= 4; i++) dfs(i, x + 1, tot + d[i]); } int main() { cin >> n; for (int i = 1; i <= 30; i++) { lim = i; memset(mark, 0, sizeof(mark)); dfs(1, 1, 0); int temp = 0; for (int j = 1; j <= i * 50; j++) if (mark[j]) temp++; ans[i] = temp; } if (n <= 12) { cout << ans[n]; return 0; } cout << 1ll * 49 * (n - 12) + ans[12]; }
#include <bits/stdc++.h> using namespace std; set<string> S; void flip(string& s1) { char temp = s1[0]; for (int i = 1; i < 4; i++) s1[i - 1] = s1[i]; s1[3] = temp; return; } bool isFirst(string s1) { for (int i = 0; i < 4; i++) { if (S.find(s1) != S.end()) return false; flip(s1); } return true; } int main() { int N, result(0); string s1, s2; cin >> N; cin >> s1 >> s2; s1 += s2[1]; s1 += s2[0]; if (isFirst(s1)) { S.insert(s1); result++; } for (int i = 1; i < N; i++) { cin >> s1; cin >> s1 >> s2; s1 += s2[1]; s1 += s2[0]; if (isFirst(s1)) { S.insert(s1); result++; } } cout << result << endl; }
#include <bits/stdc++.h> using namespace std; inline void Boost() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int NMax = 1e5 + 50; struct Event { int d, w, t; bool operator<(const Event &e) const { if (w != e.w) return w > e.w; return d > e.d; } }; vector<int> DP, jump; map<Event, int> Map; vector<Event> v[NMax]; int main() { Boost(); int n, k, m; cin >> n >> m >> k; DP.resize(n); jump.resize(n); for (int i = 0; i < n; ++i) jump[i] = i + 1; for (int i = 0; i < k; ++i) { int s, t, d, w; cin >> s >> t >> d >> w; v[s - 1].push_back({d, w, 1}); v[t].push_back({d, w, 0}); } for (int i = 0; i < n; ++i) { for (auto x : v[i]) { if (x.t == 1) { if (Map.count(x)) { ++Map[x]; } else { Map[x] = 1; } } else { --Map[x]; if (Map[x] == 0) { Map.erase(x); } } } if (!Map.empty()) { DP[i] = (*Map.begin()).first.w; jump[i] = (*Map.begin()).first.d; } } vector<long long int> ans[m + 1]; for (auto &x : ans) x.resize(n + 1, LLONG_MAX); ans[0][0] = 0; for (int i = 0; i <= n; ++i) { for (int j = 1; j <= m && i; ++j) { ans[j][i] = min(ans[j][i], ans[j - 1][i - 1]); } for (int j = 0; j <= m && i < n; ++j) { if (ans[j][i] == LLONG_MAX) continue; ans[j][jump[i]] = min(ans[j][jump[i]], ans[j][i] + DP[i]); } } long long int ret = ans[0][n]; for (int i = 0; i <= m; ++i) { ret = min(ret, ans[i][n]); } cout << ret; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2510; const int INF = 0x3f3f3f3f; int prime[400]; bool isprime[N]; int k; void init() { k = 0; for (int i = 2; i <= N; i++) { if (!isprime[i]) { prime[k++] = i; for (int j = i + i; j <= N; j += i) isprime[j] = 1; } } } char s[N]; int ma[N * 2][N * 2], pre[N * 2][N * 2]; int main() { init(); memset(pre, 0, sizeof(pre)); int n, m; scanf( %d%d , &n, &m); for (int i = 1; i < 2 * N; i++) { if (i <= n) scanf( %s , s + 1); int tmp = 0; for (int j = 1; j <= m; j++) { if (i > n) ma[i][j] = 0; else { if (s[j] == 0 ) ma[i][j] = 0; else { ma[i][j] = 1; tmp++; } } pre[i][j] = tmp + pre[i - 1][j]; } for (int j = m + 1; j < N * 2; j++) { pre[i][j] = tmp + pre[i - 1][j]; } } int ans = INF; for (int f = 0; f < k; f++) { int p = prime[f]; int res = 0; for (int i = p; i < n + p; i += p) { for (int j = p; j < m + p; j += p) { int ff = pre[i][j] + pre[i - p][j - p] - pre[i][j - p] - pre[i - p][j]; res = res + min(p * p - ff, ff); } } ans = min(ans, res); } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; class Node { public: int vol, l, r, tot; Node(int val) { tot = vol = val; l = r = 0; } Node() { tot = vol = 0; l = r = 0; } }; int main() { int H, q; cin >> H >> q; vector<Node> tree(1); string s; int v, val; for (int query = 0; query < q; ++query) { cin >> s; if (s[0] == a ) { cin >> v >> val; vector<int> vbit; while (v) { vbit.push_back(v % 2); v /= 2; } int it = 0; for (int h = (int)vbit.size() - 2; h >= 0; --h) { if (!tree[it].r) { tree[it].l = tree.size(); tree[it].r = tree.size() + 1; tree.resize(tree.size() + 2); } tree[it].tot += val; int nextit = tree[it].l; if (vbit[h]) { nextit = tree[it].r; } it = nextit; } tree[it].vol += val; tree[it].tot += val; } else { double res = 0.; int it = 0; int ma = 0; int h = H; for (; h > 0 && tree[it].r; --h) { int nextit = tree[it].r; if (tree[it].vol + tree[tree[it].r].tot > tree[tree[it].l].tot) { res += max(ma, tree[it].vol + tree[tree[it].r].tot) * (double)(1 << (h - 1)); ma = max(ma, tree[it].vol + tree[tree[it].l].tot); } else { res += max(ma, tree[it].vol + tree[tree[it].l].tot) * (double)(1 << (h - 1)); ma = max(ma, tree[it].vol + tree[tree[it].r].tot); nextit = tree[it].l; } it = nextit; } res += max(ma, tree[it].vol) * (double)(1 << h); printf( %lf n , res / (1 << (H))); } } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n; cin >> n; long long int a, c1 = 0, c2 = 0, c3 = 0; vector<long long int> v1; vector<long long int> v2; vector<long long int> v3; for (int i = 1; i <= n; i++) { cin >> a; if (a == 0) { v3.push_back(a); } else if (a > 0) { v2.push_back(a); } else { v1.push_back(a); } } if (v2.size() == 0) { for (int i = 0; i < 2; i++) { v2.push_back(v1.back()); v1.pop_back(); } } if (v1.size() % 2 == 0) { v3.push_back(v1.back()); v1.pop_back(); } cout << v1.size() << ; for (int i = 0; i < v1.size(); i++) cout << v1[i] << ; cout << endl; cout << v2.size() << ; for (int i = 0; i < v2.size(); i++) cout << v2[i] << ; cout << endl; cout << v3.size() << ; for (int i = 0; i < v3.size(); i++) cout << v3[i] << ; cout << endl; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int w, h; cin >> w >> h; int ans = 1; for (int i = 0; i < w + h; i++) { ans = (ans * 2) % 998244353; } cout << ans % 998244353 << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int sum = 0; string s; for (int i = 0; i < n; i++) { cin >> s; if (s == Icosahedron ) sum += 20; else if (s == Dodecahedron ) sum += 12; else if (s == Octahedron ) sum += 8; else if (s == Cube ) sum += 6; else if (s == Tetrahedron ) sum += 4; } cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>( chrono::high_resolution_clock::now().time_since_epoch()) .count()); const long long int N = 20005; const long long int LG = 22; long long int n, m; long long int power(long long int x, long long int y, long long int m) { if (y == 0) return 1; long long int p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } long long int fact[N], ifact[N]; void proecss() { fact[0] = 1; for (long long int i = 1; i < N; i++) fact[i] = (fact[i - 1] * i) % 1000000007; for (long long int i = 0; i < N; i++) ifact[i] = power(fact[i], 1000000007 - 2, 1000000007); } long long int nCr(long long int n, long long int r) { if (r > n) return 0; if (r == n) return 1; long long int ans = (ifact[n - r] * ifact[r]) % 1000000007; ans = (ans * fact[n]) % 1000000007; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); proecss(); cin >> n >> m; long long int b[n + 5], a[n + 5]; for (long long int i = 1; i <= n; i++) { long long int nn = n - i + 1; long long int rr = m - 1; b[i] = nCr(nn + rr - 1, rr); a[i] = nCr(i + rr - 1, rr); } for (long long int i = n - 1; i >= 1; i--) b[i] += b[i + 1], b[i] %= 1000000007; long long int ans = 0; for (long long int i = 1; i <= n; i++) { ans = (ans + (a[i] * b[i]) % 1000000007) % 1000000007; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string out; long long int T; unsigned canDo1[5000 / 32 + 1], canDo2[5000 / 32 + 1]; unsigned *cd1 = canDo1, *cd2 = canDo2, *sw; int A[5000]; list<int> P; int N; void process() { cin >> N; N *= 2; P.clear(); for (int i = 0; i < N; i++) cin >> A[i]; int ps = 0, p = 0; for (int i = 0; i < N; i++) if (A[i] <= A[ps]) p++; else P.push_back(p), ps = i, p = 1; P.push_back(p); for (int i = 0; i <= N / 32 + 1; i++) cd1[i] = cd2[i] = 0; cd1[0] = cd2[0] = 1; for (int p : P) { for (int i = 0; i <= N; i++) if ((cd2[i / 32] & 1 << i % 32) || (i >= p && (cd2[(i - p) / 32] & 1 << (i - p) % 32))) cd1[i / 32] |= 1 << i % 32; sw = cd1, cd1 = cd2, cd2 = sw; } out += (cd2[N / 2 / 32] & 1 << N / 2 % 32) ? YES n : NO n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> T; for (int t = 1; t <= T; t++) process(); cout << out; return 0; }
#include <bits/stdc++.h> using namespace std; long n, k, bd, s, ar[6000], ans, calc[1200000]; long lst[1200000]; long del; long q; void check(long mod) { del = 0; for (int i = 1; i <= n; i++) { q = ar[i] % mod; if (lst[q] == mod) ++del; else lst[q] = mod; if (del > k) break; } if (del <= k) ans = mod; } int main() { ios_base::sync_with_stdio(0); cin >> n >> k; if (k == 0) bd = 0; if (k == 1) bd = 1; if (k == 2) bd = 3; if (k == 3) bd = 6; if (k == 4) bd = 10; for (int i = 1; i <= n; i++) cin >> ar[i]; sort(ar + 1, ar + n + 1); for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) calc[ar[j] - ar[i]]++; ans = 0; for (int mod = 1; mod <= 1000001; mod++) { s = 0; if (ans > 0) break; for (int p = mod; p <= 1000001; p += mod) { s += calc[p]; if (s > bd) break; } if (s <= bd) check(mod); } cout << ans << endl; cin.get(); cin.get(); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline istream &operator>>(istream &s, vector<T> &v) { for (T &t : v) { s >> t; } return s; } template <typename T> inline ostream &operator<<(ostream &s, const vector<T> &v) { for (int i = 0; i < int(v.size()); ++i) { s << ( + !i) << v[i]; } return s; } template <typename T> inline T fromString(const string &s) { T res; istringstream iss(s); iss >> res; return res; }; template <typename T> inline string toString(const T &a) { ostringstream oss; oss << a; return oss.str(); }; const long long mod = 1000000007; const long long INF = 1e18; int main() { long long t; t = 1; cin >> t; while (t--) { long long n; cin >> n; char s[2][n]; for (int i = (0); i < (2); i++) { for (int j = (0); j < (n); j++) { cin >> s[i][j]; } } long long f = 0; for (int i = (0); i < (1); i++) { for (int j = (0); j < (n); j++) { if (s[i][j] == 1 && s[i + 1][j] == 1 ) { f = 1; } } } if (f) { cout << NO << n ; } else { cout << YES << n ; } } }
#include <bits/stdc++.h> using namespace std; int n, q, k; struct node { int c[2], fa, siz, tot; int val, id, mid, mv; bool rev; node() { rev = false; } } t[600010]; void maintain(int rt) { t[rt].siz = t[t[rt].c[0]].siz + t[t[rt].c[1]].siz + t[rt].tot; t[rt].mid = t[rt].id; t[rt].mv = t[rt].val; if (t[rt].c[0] && t[t[rt].c[0]].mv < t[rt].mv) t[rt].mv = t[t[rt].c[0]].mv, t[rt].mid = t[t[rt].c[0]].mid; if (t[rt].c[1] && t[t[rt].c[1]].mv < t[rt].mv) t[rt].mv = t[t[rt].c[1]].mv, t[rt].mid = t[t[rt].c[1]].mid; return; } bool isroot(int k) { return (t[t[k].fa].c[0] != k && t[t[k].fa].c[1] != k); } void connect(int k, int f, int p) { t[k].fa = f; t[f].c[p] = k; return; } int id(int k) { return (t[t[k].fa].c[0] == k ? 0 : 1); } void rotate(int x) { int y = t[x].fa, z = t[y].fa, fx = id(x), fy = id(y); if (!isroot(y)) t[z].c[fy] = x; t[x].fa = z; connect(t[x].c[fx ^ 1], y, fx); connect(y, x, fx ^ 1); maintain(y); maintain(x); return; } void pushdown(int rt) { if (!t[rt].rev) return; t[t[rt].c[0]].rev ^= 1; t[t[rt].c[1]].rev ^= 1; swap(t[rt].c[0], t[rt].c[1]); t[rt].rev = false; return; } stack<int> s; void splay(int x) { s.push(x); for (int i = x; !isroot(i); i = t[i].fa) s.push(t[i].fa); while (!s.empty()) { pushdown(s.top()); s.pop(); } while (!isroot(x)) { int y = t[x].fa; if (isroot(y)) { rotate(x); break; } if (id(x) == id(y)) { rotate(y); rotate(x); } else { rotate(x); rotate(x); } } return; } void access(int k) { for (int i = 0; k; i = k, k = t[k].fa) { splay(k); t[k].tot += t[t[k].c[1]].siz; t[k].tot -= t[i].siz; t[k].c[1] = i; maintain(k); } return; } void makeroot(int k) { access(k); splay(k); t[k].rev ^= 1; return; } void link(int x, int y) { makeroot(x); makeroot(y); t[x].fa = y; t[y].tot += t[x].siz; maintain(y); return; } void cut(int x, int y) { makeroot(x); access(y); splay(x); t[x].c[1] = 0; t[y].fa = 0; t[x].siz -= t[y].siz; maintain(x); return; } struct edge { int u, v, t; bool del; } es[600010]; int en = 0; int main() { scanf( %d%d%d , &n, &q, &k); int ecur = 0; int cur = 1; int ty, x, y; for (int i = 1; i <= n; ++i) t[i].siz = t[i].tot = 1, t[i].val = t[i].mv = 0x3f3f3f3f; for (int i = 1; i <= q; ++i) { scanf( %d , &ty); if (ty == 1) { scanf( %d%d , &x, &y); es[++en] = (edge){x, y, cur, false}; makeroot(x); access(y); splay(x); int v = y; while (t[v].fa) v = t[v].fa; if (v == x) { splay(y); int cute = t[y].mid; cut(es[cute].u, cute + n); cut(es[cute].v, cute + n); es[cute].del = true; } t[en + n].siz = 1; t[en + n].val = cur; t[en + n].id = en; t[en + n].mv = cur; t[en + n].mid = en; link(x, en + n); link(y, en + n); } if (ty == 2) { scanf( %d , &x); makeroot(x); printf( %d n , t[x].siz); } if (ty == 3) { ++cur; while (ecur < en && es[ecur + 1].t <= cur - k) { ++ecur; if (!es[ecur].del) { cut(es[ecur].u, ecur + n); cut(es[ecur].v, ecur + n); } } } } return 0; }
#include <bits/stdc++.h> using std::cerr; const int N = 998244, M = 1001; int n, m, d[N], las[N], seq[N], cnt[M][M], blo, x[N], y[N]; inline void add(int a, int b, int val) { seq[a] += val, seq[b + 1] -= val; } signed main() { std::ios::sync_with_stdio(0); scanf( %d%d , &n, &m), blo = sqrt(m); for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); int op, k; for (int now = 1; now <= m; now++) { scanf( %d%d , &op, &k); if (op == 1) { if (x[k] + y[k] <= blo) { for (int i = x[k]; i < x[k] + y[k]; i++) cnt[x[k] + y[k]][(now + i) % (x[k] + y[k])]++; } else for (int i = now + x[k]; i <= m; i += x[k] + y[k]) add(i, i + y[k] - 1, 1); las[k] = now; } else { if (x[k] + y[k] <= blo) { for (int i = x[k]; i < x[k] + y[k]; i++) cnt[x[k] + y[k]][(las[k] + i) % (x[k] + y[k])]--; } else { for (int i = las[k] + x[k]; i <= m; i += x[k] + y[k]) { if (i + y[k] - 1 < now) continue; if (i <= now && i + y[k] - 1 >= now) add(now, i + y[k] - 1, -1); else add(i, i + y[k] - 1, -1); } } } int ans = 0; for (int i = 2; i <= blo; i++) { ans += cnt[i][now % i]; } seq[now] += seq[now - 1]; printf( %d n , ans + seq[now]); } }
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<queue> #define ll long long using namespace std; const int N=310,M=100005,mod=1e9+7; int n,tot; ll inv,ans; int head[N],ver[M],Nxt[M],dep[N],fa[N][20]; ll f[N][N]; void add(int x,int y){ ver[++tot]=y; Nxt[tot]=head[x]; head[x]=tot; } void dfs(int u,int now){ dep[u]=now; for(int i=head[u];i;i=Nxt[i]){ int y=ver[i]; if(y==fa[u][0]) continue; fa[y][0]=u; for(int j=1;j<=18;j++){ fa[y][j]=fa[fa[y][j-1]][j-1]; } dfs(y,now+1); } } int LCA(int x,int y){ if(dep[x]<dep[y]) swap(x,y); for(int j=18;j>=0;j--){ if(dep[fa[x][j]]>=dep[y]) x=fa[x][j]; } if(x==y) return x; for(int j=18;j>=0;j--){ if(fa[x][j]!=fa[y][j]){ x=fa[x][j];y=fa[y][j]; } } return fa[x][0]; } ll ksm(ll x,ll p){ ll sum=1; while(p){ if(p&1) sum=sum*x%mod; x=x*x%mod; p>>=1; } return sum; } int main() { scanf( %d ,&n); inv=ksm(2,mod-2); for(int i=1;i<=n;i++){ f[i][0]=1; f[0][i]=0; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ f[i][j]=(f[i-1][j]+f[i][j-1])*inv%mod; } } for(int i=1;i<n;i++){ int x,y; scanf( %d%d ,&x,&y); add(x,y);add(y,x); } for(int i=1;i<=n;i++){ memset(fa,0,sizeof(fa)); memset(dep,0,sizeof(dep)); dfs(i,1); // for(int j=1;j<=n;j++){ // printf( %d %d n ,j,dep[j]); // } for(int j=1;j<=n;j++){ for(int k=j+1;k<=n;k++){ int lca=LCA(j,k); // printf( 以%d为根 %d 和 %d 的 LCA 是%d n ,i,j,k,lca); ans=(ans+f[dep[j]-dep[lca]][dep[k]-dep[lca]])%mod; } } } printf( %lld n ,ans*ksm(n,mod-2)%mod); return 0; }
#include <bits/stdc++.h> using namespace std; int lcp[25][25]; int b[25]; string s[25]; int ans = 0; void BT(int now, int n, int k) { if (now == n) { int have = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (b[i] && b[j]) have += lcp[i][j]; ans = max(ans, have); return; } b[now] = 0; if (n - now > k) BT(now + 1, n, k); if (k) { b[now] = 1; BT(now + 1, n, k - 1); } } int main() { ios::sync_with_stdio(false); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < i; j++) { int k; for (k = 0; k < s[i].length() && k < s[j].length() && s[i][k] == s[j][k]; k++) ; lcp[i][j] = lcp[j][i] = k; } } BT(0, n, k); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; pair<double, double> arr[1000]; int n; double m, r; void solve() { vector<pair<int, int> > v; double h = m; int t = 0; while (h > 0) { int best_i = -1; for (int i = 0; i < n; ++i) { if (arr[i].first >= h) { if (best_i == -1) { best_i = i; } else { if (arr[i].second > arr[best_i].second) { best_i = i; } } } } if (best_i == -1 && r >= 0) { printf( NO ); return; } if (best_i > -1) { r -= arr[best_i].second; arr[best_i].first = -10; v.push_back(make_pair(t, best_i + 1)); } h += r; h = min(h, m); t++; } printf( YES n ); printf( %d %d n , t, int(v.size())); for (int i = 0; i < int(v.size()); ++i) { printf( %d %d n , v[i].first, v[i].second); } } int main() { scanf( %d%lf%lf , &n, &m, &r); for (int i = 0; i < n; ++i) { double p, d; scanf( %lf%lf , &p, &d); arr[i].first = m / 100 * p; arr[i].second = d; } solve(); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:128777216 ) const int null = 0; using namespace std; template <class T> int size(const T &a) { return int(a.size()); } template <class T> T abs(const T &a) { return (a < 0 ? -a : a); } template <class T> T sqr(const T &a) { return a * a; } const int max_n = 52, mod = 1000000007; int dist[max_n][max_n][2]; int dp[max_n][max_n][2]; bool was[max_n][max_n][2]; struct Pos { int i, j, s; int d; void init(int i, int j, int s, int d) { this->i = i; this->j = j; this->s = s; this->d = d; was[i][j][s] = true; dist[i][j][s] = d; } } q[max_n * max_n * 2]; int cmn[max_n][max_n]; int main() { int n, k; scanf( %d %d , &n, &k); for (int i = 0; i <= n; i++) { cmn[0][i] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cmn[i][j] = cmn[i - 1][j - 1] + cmn[i][j - 1]; } } k /= 50; int c0 = 0, c1 = 0; for (int i = 0; i < n; i++) { int t; scanf( %d , &t); if (t == 50) { c0++; } else { c1++; } } q[0].init(0, 0, 1, 0); dp[0][0][1] = 1; for (int d = 0, u = 1; d < u; d++) { Pos p = q[d]; int mc0, mc1; if (p.s) { mc0 = c0 - p.i; mc1 = c1 - p.j; } else { mc0 = p.i; mc1 = p.j; } long long cnt = dp[p.i][p.j][p.s]; for (int i = 0; i <= mc0; i++) { for (int j = 0; j <= mc1; j++) { if (i + j > 0 && i + j * 2 <= k) { int c0, c1, s0 = p.s ^ 1; if (p.s) { c0 = p.i + i; c1 = p.j + j; } else { c0 = p.i - i; c1 = p.j - j; } long long cur = cnt * cmn[i][mc0] % mod * cmn[j][mc1] % mod; if (!was[c0][c1][s0]) { q[u++].init(c0, c1, s0, p.d + 1); dp[c0][c1][s0] = cur; } else if (p.d + 1 == dist[c0][c1][s0]) { dp[c0][c1][s0] = (cur + dp[c0][c1][s0]) % mod; } } } } } if (!was[c0][c1][0]) { printf( -1 n0 n ); return 0; } printf( %d n%d n , dist[c0][c1][0], dp[c0][c1][0]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using vi = vector<int>; const int N = 4e5; vi cnt(N, 0); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int a; cin >> a; string s; cin >> s; int n = s.size(); for (int i = 0; i < n; i++) { int sum = 0; for (int j = i; j < n; j++) { sum += int(s[j] - 0 ); cnt[sum]++; } } ll ans = 0; if (a == 0) { for (int i = 0; i < N; i++) ans += cnt[i]; ans = 2 * cnt[0] * ans - 1LL * cnt[0] * cnt[0]; } else { for (int i = 1; i < N; i++) { if (a % i) continue; int b = a / i; if (b >= N) continue; ans += 1LL * cnt[i] * cnt[b]; } } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; inline int read() { int f = 1, res = 0, ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) res = res * 10 + ch - 48, ch = getchar(); return f * res; } int _sta[1 << 8]; inline void write(int val) { if (!val) { putchar( 0 ); return; } if (val < 0) putchar( - ), val = -val; *_sta = 0; while (val > 0) _sta[++*_sta] = val % 10, val /= 10; for (int i = *_sta, _Lim = 1; i >= _Lim; i--) putchar(_sta[i] + 48); } const long long INF = 1e18; long long Exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1, y = 0; return a; } int res = Exgcd(b, a % b, x, y); int x_ = x; x = y; y = x_ - a / b * y; return res; } int n, m, k; long long Cal(long long x, long long y) { long long a, b; long long gcd = Exgcd(2 * n, 2 * m, a, b); long long lcm = 1LL * 2 * n * 2 * m / gcd; if (a < 0) a = -a; else a = 2 * m / gcd - a; if ((x - y) % gcd != 0) return INF; x += 1LL * (x - y) / gcd * a * 2 * n; x -= x / lcm * lcm; if (x < 0) x += lcm; return x; } int main() { n = read(); m = read(); k = read(); while (k--) { int x = read(), y = read(); long long Ans = INF; Ans = min(Ans, Cal(x, y)); Ans = min(Ans, Cal(x, 2 * m - y)); Ans = min(Ans, Cal(2 * n - x, y)); Ans = min(Ans, Cal(2 * n - x, 2 * m - y)); if (Ans < INF) printf( %I64d n , Ans); else printf( -1 n ); } }
#include <bits/stdc++.h> using namespace std; const int SZ = 2000010; const int INF = 1e9 + 10; const int mod = 1e9 + 7; const long double eps = 1e-8; long long read() { long long n = 0; char a = getchar(); bool flag = 0; while (a > 9 || a < 0 ) { if (a == - ) flag = 1; a = getchar(); } while (a <= 9 && a >= 0 ) { n = n * 10 + a - 0 , a = getchar(); } if (flag) n = -n; return n; } vector<int> g2[SZ]; vector<int> g[SZ]; int n, m, q; int ccnt = 0; int dfn[SZ], low[SZ], dfs_clock; stack<int> S; bool cut[SZ], ins[SZ]; void Tarjan(int u, int f) { dfn[u] = low[u] = ++dfs_clock; S.push(u); ins[u] = 1; int child = 0; for (int i = 0; i < g2[u].size(); i++) { int v = g2[u][i]; if (v == f) continue; if (!dfn[v]) { child++; Tarjan(v, u); low[u] = min(low[u], low[v]); if (low[v] >= dfn[u]) { ccnt++; cut[u] = 1; int x; do { x = S.top(); S.pop(); ins[x] = 0; g[ccnt + n].push_back(x); g[x].push_back(ccnt + n); } while (x != v && !S.empty()); g[ccnt + n].push_back(u); g[u].push_back(ccnt + n); } } else if (ins[v]) { low[u] = min(low[u], dfn[v]); } } if (f == 0 && child == 1) cut[u] = 0; } int anc[SZ][22], deep[SZ]; void dfsLca(int u, int f) { deep[u] = deep[f] + 1; anc[u][0] = f; for (int i = 1; anc[u][i - 1]; i++) anc[u][i] = anc[anc[u][i - 1]][i - 1]; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == f) continue; dfsLca(v, u); } } int askLca(int u, int v) { if (deep[u] < deep[v]) swap(u, v); int dd = deep[u] - deep[v]; for (int i = 20; i >= 0; i--) if (dd >> i & 1) u = anc[u][i]; if (u == v) return u; for (int i = 20; i >= 0; i--) if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i]; return anc[u][0]; } int main() { n = read(), m = read(), q = read(); for (int i = 1; i <= m; i++) { int x = read(), y = read(); g2[x].push_back(y); g2[y].push_back(x); } Tarjan(1, 0); n += ccnt; dfsLca(1, 0); while (q--) { int x = read(), y = read(); int lca = askLca(x, y); int ans = deep[x] + deep[y] - 2 * deep[lca]; printf( %d n , ans / 2); } return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { int t; cin >> t; while(t--) { int xa, ya, xb, yb, xf, yf; cin >> xa >> ya >> xb >> yb >> xf >> yf; if(xa==xb && xb==xf && yf > ya && yf < yb) { cout << abs(yb-ya) + 2 << endl; continue; } else if(xa==xb && xb==xf && yf < ya && yf > yb) { cout << abs(yb-ya) + 2 << endl; continue; } else if(ya==yb && yb==yf && xf>xa && xf<xb) { cout << abs(xb-xa) + 2 << endl; continue; } else if(ya==yb && yb==yf && xf<xa && xf>xb) { cout << abs(xb-xa) + 2 << endl; continue; } else { cout << abs(xb-xa) + abs(yb-ya) << endl; continue; } } }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:102400000,102400000 ) using namespace std; const int mo = 1000000007; const int inf = 0x3f3f3f3f; const int INF = 2000000000; int n, m, phi; int lt[100008], size[100008], f[100008], vis[100008], a[100008], b[100008], sum, tot, root, pre1[100008], pre2[100008]; long long ans, Alltmp; map<int, int> mp; struct edge { int u, v, w, nt; } eg[100008 * 2]; void add(int u, int v, int w) { eg[++sum] = (edge){u, v, w, lt[u]}; lt[u] = sum; } long long quick(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = res * x % m; x = x * x % m; y >>= 1; } return res; } void init() { int mm = m; phi = m; for (int i = 2; i * i <= m; i++) { if (mm % i == 0) { while (mm % i == 0) mm /= i; phi = phi / i * (i - 1); } } if (mm > 1) phi = phi / mm * (mm - 1); memset(lt, 0, sizeof(lt)); ; sum = 1; memset(f, 0, sizeof(f)); ; f[0] = INF; memset(vis, 0, sizeof(vis)); ; ans = 0; pre1[0] = 1; pre2[0] = 1; for (int i = 1; i <= 100000; i++) { pre1[i] = 1ll * pre1[i - 1] * 10 % m; pre2[i] = quick(pre1[i], phi - 1); } } void getRoot(int u, int fa) { size[u] = 1; f[u] = 0; for (int i = lt[u]; i; i = eg[i].nt) { int v = eg[i].v; if (vis[v] || v == fa) continue; getRoot(v, u); size[u] += size[v]; f[u] = max(f[u], size[v]); } f[u] = max(f[u], tot - size[u]); if (f[u] < f[root]) root = u; } void getA(int u, int fa, int len) { for (int i = lt[u]; i; i = eg[i].nt) { int v = eg[i].v; if (vis[v] || v == fa) continue; a[v] = (1ll * eg[i].w * pre1[len] + a[u]) % m; getA(v, u, len + 1); } Alltmp += mp[a[u]]; int tp = (m - b[u]) % m; tp = 1ll * tp * pre2[len] % m; if (a[u] == tp) Alltmp--; } void getB(int u, int fa, int len) { int tp = (m - b[u]) % m; tp = 1ll * tp * pre2[len] % m; mp[tp]++; for (int i = lt[u]; i; i = eg[i].nt) { int v = eg[i].v; if (vis[v] || v == fa) continue; b[v] = (1ll * b[u] * 10 + eg[i].w) % m; getB(v, u, len + 1); } } long long calc(int u, int key) { key %= m; Alltmp = 0; mp.clear(); a[u] = b[u] = key; getB(u, 0, key == 0 ? 0 : 1); getA(u, 0, key == 0 ? 0 : 1); return Alltmp; } void solve(int u) { ans += calc(u, 0); vis[u] = 1; for (int i = lt[u]; i; i = eg[i].nt) { int v = eg[i].v; if (vis[v]) continue; ans -= calc(v, eg[i].w); root = 0; tot = size[v]; getRoot(v, u); solve(root); } } int main() { scanf( %d%d , &n, &m); init(); for (int i = 1; i <= n - 1; i++) { int u, v, w; scanf( %d%d%d , &u, &v, &w); add(u + 1, v + 1, w); add(v + 1, u + 1, w); } root = 0; tot = n; getRoot(1, 0); solve(root); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, x1, y1, x2, y2; scanf( %d%d%d%d%d%d , &n, &m, &x1, &y1, &x2, &y2); int x, y; x = abs(x1 - x2); y = abs(y1 - y2); if (x > y) swap(x, y); if (y < 4) { cout << First << endl; return 0; } if (x < 3 && y < 5) { cout << First << endl; return 0; } cout << Second << endl; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 55; int n, m; int a[MAXN]; double dp[MAXN][MAXN][MAXN]; double C[MAXN][MAXN]; void init() { C[0][0] = 1.0; for (int i = 1; i <= 50; i++) { C[i][0] = 1.0; for (int j = 1; j <= i; j++) C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } int main() { scanf( %d%d , &n, &m); init(); for (int i = 1; i <= m; i++) scanf( %d , &a[i]); for (int i = 0; i <= n; i++) dp[0][0][i] = i; for (int i = 1; i <= m; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= n; k++) { for (int c = 0; c <= j; c++) { int maxq = max(k, (c + a[i] - 1) / a[i]); dp[i][j][k] += dp[i - 1][j - c][maxq] * pow(i - 1, j - c) / pow(i, j) * C[j][c]; } } } } printf( %.12f n , dp[m][n][0]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j; long long int sum = 0, x; cin >> n >> m; int a[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> a[i][j]; } } for (i = n - 2; i >= 1; i--) { for (j = m - 2; j >= 1; j--) { if (a[i][j] == 0) { x = min(a[i][j + 1], a[i + 1][j]); a[i][j] = x - 1; } } } for (i = 0; i < n; i++) { for (j = 0; j < m - 1; j++) { if (a[i][j] >= a[i][j + 1]) { cout << -1 << endl; return 0; } } } for (i = 0; i < m; i++) { for (j = 0; j < n - 1; j++) { if (a[j][i] >= a[j + 1][i]) { cout << -1 << endl; return 0; } } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { sum = sum + a[i][j]; } } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; double f(double m, vector<int> &v) { int n = v.size(); vector<long double> v2(n); for (int i = 0; i < (n); i++) v2[i] = v[i] - m; long double mx_otr = 0; long double mn_otr = 0; long double cur = 0; for (int i = 0; i < (n); i++) { cur += v2[i]; if (cur < 0) cur = 0; mx_otr = max(mx_otr, cur); } cur = 0; for (int i = 0; i < (n); i++) { cur += v2[i]; if (cur >= 0) cur = 0; mn_otr = min(mn_otr, cur); } return max(mx_otr, abs(mn_otr)); } inline void solve() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < (n); i++) cin >> v[i]; long double l = -10000, r = 10000; while (abs(r - l) > 1e-12) { long double m1 = l + (r - l) / 3; long double m2 = r - (r - l) / 3; if (f(m1, v) <= f(m2, v)) r = m2; else l = m1; } cout << fixed << setprecision(10) << f(l + (r - l) / 2.0, v); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q = 1; while (q--) { solve(); cout << n ; } }
#include <bits/stdc++.h> using namespace std; const int N = 300005; int n, tt, rt, ans, ls[N], rs[N], val[N], tg[N], rd[N]; inline int nw(int v) { val[++tt] = v; rd[tt] = rand(); return tt; } inline void add(int x, int v) { val[x] += v; tg[x] += v; } inline void dn(int x) { add(ls[x], tg[x]); add(rs[x], tg[x]); tg[x] = 0; } void split(int x, int k, int& a, int& b) { if (!x) { a = b = 0; return; } dn(x); val[x] < k ? (a = x, split(rs[x], k, rs[a], b)) : (b = x, split(ls[x], k, a, ls[b])); } int merge(int x, int y) { if (!x || !y) return x | y; else if (rd[x] < rd[y]) { dn(x); rs[x] = merge(rs[x], y); return x; } else { dn(y); ls[y] = merge(x, ls[y]); return y; } } int main() { scanf( %d , &n); while (n--) { int l, r, x, y, z; scanf( %d%d , &l, &r); split(rt, r, x, z); split(x, l, x, y); add(y, 1); if (z) { int w = z; while (ls[w]) dn(w), w = ls[w]; split(z, val[w] + 1, w, z); } else ans++; rt = merge(merge(x, nw(l)), merge(y, z)); } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6; char buff[MAXN + 5]; string input, subs; int freq[10]; int counter(int num) { int ret = 0; while (num) { ret++; num /= 10; } return ret; } string res; bool flag; void solve(bool taken) { for (int i = 0; i <= 10; i++) { if (!taken && (subs[0] - 0 ) < i) { res += subs; solve(1); return; } else if (i <= 9 && freq[i]) { char c = (i + 0 ); res += c; freq[i]--; solve(taken); freq[i]++; return; } } return; } void solve2(bool taken) { for (int i = 0; i <= 10; i++) { if (!taken && (subs[0] - 0 ) <= i) { res += subs; solve2(1); return; } else if (i <= 9 && freq[i]) { char c = (i + 0 ); res += c; freq[i]--; solve2(taken); freq[i]++; return; } } return; } int main() { scanf( %s , buff); input = buff; scanf( %s , buff); subs = buff; if (input.size() == 2) { if (input[0] == 1 ) swap(input[0], input[1]); printf( %c n , input[0]); return 0; } int k; string sk; for (int i = 1; i <= MAXN; i++) { if (i == input.size() - counter(i)) { k = i; break; } } int temp = k; while (temp) { char c = ((temp % 10) + 0 ); sk = c + sk; temp /= 10; } for (int i = 0; i < input.size(); i++) freq[input[i] - 0 ]++; for (int i = 0; i < subs.size(); i++) freq[subs[i] - 0 ]--; for (int i = 0; i < sk.size(); i++) freq[sk[i] - 0 ]--; flag = 0; char d = sk[0]; for (int i = 1; i < sk.size(); i++) { if (sk[i] < d) { flag = 1; break; } else if (sk[i] == d) ; else break; } solve(1); string res1 = subs + res; res.clear(); for (int i = 1; i <= 9; i++) { if (freq[i]) { char c = (i + 0 ); res += c; freq[i]--; solve(0); freq[i]++; break; } } string res2 = res; res.clear(); for (int i = 1; i <= 9; i++) { if (freq[i]) { char c = (i + 0 ); res += c; freq[i]--; solve2(0); freq[i]++; break; } } string res3 = res; char c = 9 + 1; if (res1[0] == 0 || res1 == ) res1 = , res1 += c; if (res2[0] == 0 || res2 == ) res2 = , res2 += c; if (res3[0] == 0 || res3 == ) res3 = , res3 += c; if (res1 < res2 && res1 < res3) printf( %s n , res1.c_str()); else if (res2 < res1 && res2 < res3) printf( %s n , res2.c_str()); else printf( %s n , res3.c_str()); return 0; }
#include <bits/stdc++.h> using namespace std; void B(); int main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); B(); } class segmen { public: long long x1, y1, x2, y2; segmen(long long xx1, long long yy1, long long xx2, long long yy2) : x1{xx1}, x2{xx2}, y1{yy1}, y2{yy2} {} segmen() {} }; vector<segmen> hor, ver; const long long N = 10010; const long long shift = 5001; bool compar(segmen A, segmen B) { return A.y1 <= B.y1; } long long T[N]; void upd(long long x, long long val) { for (; x < N; x += (x & (-x))) T[x] += val; } long long F(long long x) { long long ret = 0; for (; x; x -= (x & (-x))) ret += T[x]; return ret; } long long gett(long long l, long long r) { return F(r) - F(l - 1); } vector<pair<long long, long long> > H[N]; vector<pair<long long, long long> > V[N]; void B() { long long n; cin >> n; for (long long i = 0; i < n; i++) { long long x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; x1 += shift; x2 += shift; y1 += shift; y2 += shift; if (y1 == y2) { H[y1].push_back({min(x1, x2), max(x1, x2)}); } if (x1 == x2) { V[x1].push_back({min(y1, y2), max(y1, y2)}); } } long long ans = 0; vector<long long> endy[N]; for (long long i = 1; i < N; i++) { for (auto A : H[i]) { long long xl = A.first; long long xr = A.second; long long maxn = 0; for (long long j = xl; j <= xr; j++) { for (auto B : V[j]) { if (B.first <= i && B.second > i) { upd(j, 1); endy[B.second].push_back(j); maxn = max(maxn, B.second); } } } for (long long j = i + 1; j <= maxn; j++) { for (auto B : H[j]) { long long ret = gett(B.first, B.second); ans += (ret * (ret - 1)) >> 1; } for (auto B : endy[j]) { upd(B, -1); } endy[j].clear(); } } } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; const long long N = 50; long long fac[N + 1]; void init() { fac[0] = 1; for (long long i = 1; i <= N; i++) { fac[i] = (i * fac[i - 1]) % 1000000007LL; } } long long pw(long long a, long long e) { if (e == 0) { return 1; } long long ret = pw(a, e / 2); ret = (ret * ret) % 1000000007LL; if (e % 2 == 1) { ret = (a * ret) % 1000000007LL; } return ret; } long long choose(long long x, long long y, long long x0, long long y0) { return (((((fac[x] * pw(fac[x0], 1000000007LL - 2)) % 1000000007LL) * pw(fac[x - x0], 1000000007LL - 2)) % 1000000007LL) * ((((fac[y] * pw(fac[y0], 1000000007LL - 2)) % 1000000007LL) * pw(fac[y - y0], 1000000007LL - 2)) % 1000000007LL)) % 1000000007LL; } long long X, Y; vector<pair<long long, long long> > adj[(N + 1) * (N + 1) * 2]; long long getVertex(long long x, long long y, long long sh) { return sh * (X + 1) * (Y + 1) + x * (Y + 1) + y; } queue<long long> q; long long dist[(N + 1) * (N + 1) * 2], ways[(N + 1) * (N + 1) * 2]; int main() { init(); long long n, k; cin >> n >> k; while (n--) { long long w; cin >> w; if (w == 50) { X++; } else { Y++; } } for (long long x = 0; x <= X; x++) { for (long long y = 0; y <= Y; y++) { for (long long x0 = 0; x0 <= x; x0++) { for (long long y0 = 0; y0 <= y; y0++) { if (x0 + y0 >= 1 && (50 * x0 + 100 * y0 <= k)) { long long w = choose(x, y, x0, y0); adj[getVertex(x, y, 0)].push_back(pair<long long, long long>( getVertex(X - x + x0, Y - y + y0, 1), w)); adj[getVertex(x, y, 1)].push_back(pair<long long, long long>( getVertex(X - x + x0, Y - y + y0, 0), w)); } } } } } long long start = getVertex(X, Y, 0), end = getVertex(X, Y, 1); for (long long u = 0; u <= (X + 1) * (Y + 1) * 2 - 1; u++) { dist[u] = 1000000000000000000LL; } q.push(start); dist[start] = 0; ways[start] = 1; while (!q.empty()) { long long u = q.front(); q.pop(); for (pair<long long, long long> vw : adj[u]) { long long v = vw.first, w = vw.second; if (dist[u] + 1 < dist[v]) { q.push(v); dist[v] = dist[u] + 1; ways[v] = (ways[u] * w) % 1000000007LL; } else if (dist[u] + 1 == dist[v]) { ways[v] = (ways[v] + ways[u] * w) % 1000000007LL; } } } cout << (dist[end] == 1000000000000000000LL ? -1 : dist[end]) << n << ways[end] << n ; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; char a, b; a = s[0]; b = s[s.length() - 1]; int j = s.length() - 2; int i = 0; int maxcount = 0; while (j - i >= 0) { int k1 = j - i; int k = s[k1]; int q1 = s.length() - 1; int q = s[q1]; int count = 0; while (k1 > 0 && q == k) { count++; q1--; q = s[q1]; k1--; k = s[k1]; } if (q == k) { count++; } else { k1 = -1; } if (maxcount < count && k1 == 0) { maxcount = count; } i++; } string x = s.substr(0, s.length() - maxcount); string y = s.substr(s.length() - maxcount, s.length()); for (int i = 0; i < k; i++) { cout << x; } cout << y; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, d, t, result, stored = 0; cin >> n >> d; for (int i = 0; i < n; i++) { cin >> t; stored += t; } d -= (n - 1) * 10; if (d - stored >= 0) { cout << (n - 1) * 2 + (d - stored) / 5; } else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; void solution() { long long n; long long k; cin >> n >> k; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; long long sum = 0; sort(v.begin(), v.end()); for (long long i = 0; i < (k); i++) { sum += (v[n - k - 1 - i] / v[n - 1 - i]); } for (long long i = 0; i < n - 2 * k; i++) sum += v[i]; cout << sum << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) solution(); return 0; }
#include <bits/stdc++.h> using namespace std; int Get() { char c; while (c = getchar(), (c < 0 || c > 9 ) && c != - ) ; bool Flag = (c == - ); if (Flag) c = getchar(); int X = 0; while (c >= 0 && c <= 9 ) { X = X * 10 + c - 48; c = getchar(); } return Flag ? -X : X; } int main() { int N = Get(), Sum = 0; for (int i = 0; i < N; i++) Sum += Get(); printf( %d n , (Sum % N) ? (N - 1) : N); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; namespace StrAlgo { vector<int> Kmp(string s) { int n = (int(s.size())); vector<int> ans(n + 1); for (int i = 2; i <= n; i++) { ans[i] = ans[i - 1]; while (ans[i] > 0 && s[ans[i]] != s[i - 1]) ans[i] = ans[ans[i]]; ans[i] += s[ans[i]] == s[i - 1]; } return ans; } vector<int> Z(string s) { int n = (int(s.size())); vector<int> ans(n + 1); int L = -1, R = -1; for (int i = 2; i <= n; i++) { if (i < R) ans[i] = min(ans[i - L], R - i); else L = i, R = i; while (i + ans[i] <= n && s[i + ans[i] - 1] == s[ans[i] - 1]) ans[i]++; if (i + ans[i] > R) L = i, R = i + ans[i]; } return ans; } }; // namespace StrAlgo void print(string s, int len) { if (len == 0) cout << Just a legend n ; else cout << s.substr((int(s.size())) - len) << n ; exit(0); } int cnt[maxn]; int main() { ios_base::sync_with_stdio(0), cin.tie(), cout.tie(); string s; cin >> s; vector<int> kmp = StrAlgo::Kmp(s); for (int x : kmp) cnt[x]++; if (cnt[kmp.back()] > 1) print(s, kmp.back()); print(s, kmp[kmp.back()]); }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-9; const long long MOD = 100000000; const int N = 300009; const int N2 = 300009; void fast() { std::ios_base::sync_with_stdio(0); } int main() { long long n, m; long long x = 0; scanf( %lld%lld , &n, &m); vector<long long> b(n), g(m); for (long long &i : b) { scanf( %lld , &i); x += i * m; } for (long long &i : g) scanf( %lld , &i); sort(b.rbegin(), b.rend()); sort(g.rbegin(), g.rend()); long long i = 0, cnt = 0; long long ans = 0; if (g[m - 1] < b[0] || b[0] > g[m - 1]) { puts( -1 ); return 0; } for (long long j = 0; j < m; j++) { if (cnt == m - 1) i++, cnt = 0; if (g[j] > b[0]) { ans += g[j] - b[i]; cnt++; } } printf( %lld , ans + x); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 21; int n; long long s, a[N], down = 1, ans; void exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return; } exgcd(b, a % b, y, x); y -= a / b * x; } long long inv(long long x) { long long res, y; exgcd(x, 1000000007, res, y); return res; } long long C(long long u, long long d) { if (d < u) return 0; long long res = 1; for (long long i = d; i > d - u; i--) (res *= i % 1000000007) %= 1000000007; return res * down % 1000000007; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> s; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) (down *= i) % 1000000007; down = inv(down); for (int i = 0; i < (1 << n); i++) { long long f = 1, d = s + n - 1, u = n - 1; for (int j = 0; j < n; j++) if (i & (1 << j)) { d -= a[j] + 1; f *= -1; } (ans += f * C(u, d)) %= 1000000007; } cout << (ans + 1000000007) % 1000000007; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 500001; const int oo = 1e9; const int LOGN = 20; const int MOD = 1000000007; const double PI = acos(-1); int n, a[N]; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } int mn = oo, idx = -1; for (int t = 1; t <= 101; ++t) { int cur = 0; for (int j = 0; j < n; ++j) { int cst = max(0, abs(a[j] - t) - 1); cur += cst; } if (cur < mn) { mn = cur; idx = t; } } cout << idx << << mn << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double EPS = 1e-6; const int MOD = 1e9 + 7; const int MAXN = 1100001; const double PI = acos(-1.0); long long ipt[MAXN]; long long lsum[MAXN], rsum[MAXN]; long long sum[MAXN]; int main() { long long n, len; while (cin >> n >> len) { memset(sum, 0, sizeof(sum)); memset(lsum, 0, sizeof(lsum)); memset(rsum, 0, sizeof(rsum)); for (int i = 0; i < (n); ++i) cin >> ipt[i]; for (int i = (1); i < (n); ++i) { sum[i] += sum[i - 1] + ipt[i] - ipt[i - 1]; } long long group = 0; for (long long i = 0; i + len < n; i += len) { long long l = len * group, r = l + len; if (group) lsum[group] = lsum[group - 1]; lsum[group] += (sum[r] - sum[l]) * (group + 1); group++; } group = 0; for (long long i = n - 1; i - len >= 0; i -= len) { long long r = len * group, l = r + len; l = n - 1 - l; r = n - 1 - r; if (group) rsum[group] = rsum[group - 1]; rsum[group] += (sum[r] - sum[l]) * (group + 1); group++; } long long ans = 1e18; for (long long i = 0; i < n; i++) { long long t = 0; if (i / len > 0) t += lsum[i / len - 1] * 2; if ((n - 1 - i) / len > 0) t += rsum[(n - 1 - i) / len - 1] * 2; t += (sum[i] - sum[i / len * len]) * 2 * (i / len + 1); t += (sum[n - 1 - (n - 1 - i) / len * len] - sum[i]) * 2 * ((n - 1 - i) / len + 1); ans = min(ans, t); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using cat = long long; int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N, MX = 500000; cin >> N; vector<cat> A(N); for (int i = 0; i < N; i++) cin >> A[i]; sort(begin(A), end(A)); vector<int> cnt(MX + 1, 0), cnt_ps(MX + 1, 0); for (int i = 0; i < N; i++) cnt[A[i]]++; for (int i = 0; i <= MX; i++) cnt_ps[i] = (i ? cnt_ps[i - 1] : 0) + cnt[i]; cat ans = 0; for (int y = 2; y <= MX; y++) { cat cnt_y = 0; for (int i = 1; i <= MX / y; i++) cnt_y += 1LL * (cnt_ps[min(MX, y * i + y - 1)] - cnt_ps[y * i - 1]) * i; if (cnt_y < 2) continue; int min_x = min(1LL * y, cnt_y - 2), max_x = min(MX + 1LL, cnt_y + 1); while (max_x - min_x > 1 && 1LL * (max_x - 1) * y > ans) { int x = (min_x + max_x) / 2; if (2 * (x / y) + x > cnt_y) { max_x = x; continue; } if (2 * x <= MX) { int cnt2_r = 0; for (int i = (2 * x) / y; i <= MX / y; i++) cnt2_r += cnt_ps[min(MX, y * i + y - 1)] - cnt_ps[min(MX, max(0, y * i + (2 * x) % y - 1))]; if (cnt2_r >= 1 && x + (2 * x) / y <= cnt_y) { min_x = x; continue; } if (cnt_ps[MX] > cnt_ps[2 * x - 1] && x + (2 * x + y - 1) / y <= cnt_y) { min_x = x; continue; } } int cnt_r = 0; for (int i = x / y; i <= MX / y; i++) cnt_r += cnt_ps[min(MX, y * i + y - 1)] - cnt_ps[min(MX, max(0, y * i + x % y - 1))]; if (cnt_r >= 2) { min_x = x; continue; } if (cnt_ps[MX] >= 2 + cnt_ps[x - 1] && x + 2 * ((x + y - 1) / y) - cnt_r <= cnt_y) { min_x = x; continue; } max_x = x; } if (min_x >= 2) ans = max(ans, 1LL * min_x * y); } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 4e4; int main() { ios::sync_with_stdio(false); int n; while (cin >> n) { int a[12]; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); long long p = clock(); while (clock() - p < 1400) ; cout << a[0]; for (int i = 1; i < n; ++i) cout << << a[i]; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; class DTrie { int N = 30; struct node { node* left = nullptr; node* right = nullptr; int cnt = 0; }; void _add(node* cur, int a, int p) { cur->cnt++; if (p == -1) return; if (a & (1 << p)) { if (cur->right == nullptr) cur->right = new node(); _add(cur->right, a, p - 1); } else { if (cur->left == nullptr) cur->left = new node(); _add(cur->left, a, p - 1); } } int _get(node* cur, int a, int p) { cur->cnt--; if (p == -1) return 0; int result = 0; if (a & (1 << p)) { if (cur->right == nullptr || cur->right->cnt == 0) result = _get(cur->left, a, p - 1); else result = (1 << p) + _get(cur->right, a, p - 1); } else { if (cur->left == nullptr || cur->left->cnt == 0) result = (1 << p) + _get(cur->right, a, p - 1); else result = _get(cur->left, a, p - 1); } return result; } node* root = nullptr; public: DTrie() { root = new node(); } void add(int a) { _add(root, a, N); } int get(int a) { return _get(root, a, N); } }; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n), p(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> p[i]; } DTrie dt; for (int i = 0; i < n; i++) { dt.add(p[i]); } for (int i = 0; i < n; i++) { cout << (a[i] ^ dt.get(a[i])) << ; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int s, tens, sum = 0; cin >> s; for (; s > 0;) { tens = s / 10; s -= tens * 10; s += tens; sum += tens * 10; if (s >= 10 && s <= 18) { s -= 10; s++; sum += 10; if (s) { sum += s; s = 0; } } else if (s <= 9) { sum += s; s = 0; } } cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using std::map; using std::max; using std::min; using std::set; using std::sort; using std::swap; using std::vector; inline int readInt() { int ans = 0; char c; do c = getchar(); while (!isdigit(c)); while (isdigit(c)) { ans = ans * 10 + c - 0 ; c = getchar(); } return ans; } const int N = 55; int A[N][N]; int main() { int n = readInt(); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) A[i][j] = readInt(); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (A[i][j] != 1) { bool ok = false; for (int k = 0; k < n; ++k) if (i != k) { for (int l = 0; l < n; ++l) if (j != l && A[i][j] == A[k][j] + A[i][l]) { ok = true; break; } if (ok) break; } if (!ok) return puts( NO ), 0; } return puts( YES ), 0; }
#include <bits/stdc++.h> const int N = 4e6 + 10; int idx[N], rank[N]; struct Point { int x, y; } A[N], Line[N]; inline double slope(Point a) { return double(A[a.x].y - A[a.y].y) / double(A[a.x].x - A[a.y].x); } inline long long area(Point A, Point B, Point C) { return fabs(1LL * (A.x - C.x) * (B.y - C.y) - 1LL * (B.x - C.x) * (A.y - C.y)); } inline int find1(int l, int r, int u, int v, long long s) { int res = -1; while (l <= r) { int mid = (l + r) >> 1; if (area(A[idx[mid]], A[u], A[v]) >= s) l = mid + 1, res = mid; else r = mid - 1; } return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1; } inline int find2(int l, int r, int u, int v, long long s) { int res = -1; while (l <= r) { int mid = (l + r) >> 1; if (area(A[idx[mid]], A[u], A[v]) <= s) l = mid + 1, res = mid; else r = mid - 1; } return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1; } inline int solve(int u, int v, int n, long long s) { if (rank[u] > rank[v]) std::swap(u, v); int res1 = find1(1, rank[u] - 1, u, v, s); if (res1 != -1) return res1; int res2 = find2(rank[v] + 1, n, u, v, s); if (res2 != -1) return res2; std::swap(rank[u], rank[v]), std::swap(idx[rank[u]], idx[rank[v]]); return 0; } int main() { int n, tot = 0; long long s; std::cin >> n >> s; s <<= 1; for (int i = 1; i <= n; ++i) std::cin >> A[i].x >> A[i].y, idx[i] = i, rank[i] = i; std::sort(A + 1, A + n + 1, [&](const Point &a, const Point &b) { return a.x < b.x || a.x == b.x && a.y < b.y; }); for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) Line[++tot] = Point{i, j}; std::sort(Line + 1, Line + tot + 1, [&](const Point &a, const Point &b) { int x1 = A[a.y].x - A[a.x].x, y1 = A[a.y].y - A[a.x].y; int x2 = A[b.y].x - A[b.x].x, y2 = A[b.y].y - A[b.x].y; return 1LL * x1 * y2 - 1LL * x2 * y1 > 0; }); for (int i = 1; i <= tot; ++i) { int res = solve(Line[i].x, Line[i].y, n, s); if (res) return printf( Yes n%d %d n%d %d n%d %d n , (A[Line[i].x]).x, (A[Line[i].x]).y, (A[Line[i].y]).x, (A[Line[i].y]).y, (A[idx[res]]).x, (A[idx[res]]).y), 0; } return puts( No ), 0; }
#include <bits/stdc++.h> using namespace std; int N; pair<int, int> Result[100005]; bool Use[100005]; int counter, Array[100005]; void buildArray(int prime) { Array[0] = 0; for (int k = prime; k <= N; k += prime) { if (Use[k] == 0) { Array[++Array[0]] = k; Use[k] = 1; } } } void makeArrayValid() { if (Array[0] % 2 == 0) return; Use[Array[2]] = 0; swap(Array[Array[0]], Array[2]); Array[0]--; } void addToResult() { int i; for (i = 1; i <= Array[0]; i += 2) { Result[++counter].first = Array[i]; Result[counter].second = Array[i + 1]; } } void Eratostenes() { int i, j; int last = 3; for (i = 3; i <= N / 2; i += 2) { if (Use[i] == 1) continue; buildArray(i); makeArrayValid(); addToResult(); } buildArray(2); makeArrayValid(); addToResult(); } int main() { cin >> N; Eratostenes(); cout << counter << n ; for (int i = 1; i <= counter; i++) cout << Result[i].first << << Result[i].second << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<long long> a(n); long long i, j; for (i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (a[i] == 0) continue; if (a[j] % a[i] == 0) { a[j] = 0; } } } long long cnt = 0; for (i = 0; i < n; i++) if (a[i] > 0) cnt++; cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a[10][10]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } int x1 = a[0][1] + a[0][2]; int x2 = a[1][0] + a[1][2]; int x3 = a[2][0] + a[2][1]; int y = (x1 + x2 + x3) / 2; a[0][0] = y - x1; a[1][1] = y - x2; a[2][2] = y - x3; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << a[i][j] << ; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; struct point { double x, y; point(double x = 0, double y = 0) : x(x), y(y) {} point operator-(const point &b) const { return point(x - b.x, y - b.y); } double operator*(const point &b) const { return x * b.y - y * b.x; } }; point p[200050]; int a[200050], q[200050]; double sum[200050], t[200050], E[200050], dp[55][200050]; bool check(int i, int j, double v) { return p[j].y - p[i].y <= (p[i].x - p[j].x) * v; } int main() { int n, k; scanf( %d%d , &n, &k); for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + a[i]; t[i] = t[i - 1] + 1.0 / a[i]; E[i] = E[i - 1] + sum[i] / a[i]; } for (int i = 1; i <= n; ++i) dp[1][i] = E[i]; for (int x = 2; x <= k; ++x) { int head = 0, tail = 0; q[tail++] = 0; for (int i = 1; i <= n; ++i) { if (i < x) { p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]); while (head + 1 < tail && (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0) tail--; q[tail++] = i; } while (head + 1 < tail && check(q[head], q[head + 1], t[i])) head++; int j = q[head]; dp[x][i] = p[j].x * t[i] + p[j].y + E[i]; p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]); while (head + 1 < tail && (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0) tail--; q[tail++] = i; } } printf( %.9f n , dp[k][n]); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e9; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n + 5]; long long ans = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; ans += a[i] * a[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= n / 2; i++) { ans += 2 * a[i] * a[n - i + 1]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = (1 << 22) + 5; bool vis[maxn << 2][2]; int n, m; int a[maxn]; bool p[maxn]; void dfs(int id, int s) { if (vis[id][s]) return; vis[id][s] = 1; for (int i = 0; i < n; i++) { if (id & (1 << i)) { dfs(id ^ (1 << i), s); } } if (p[id]) { if (vis[id][0]) return; vis[id][0] = 1; dfs((1 << n) - 1 - id, 1); } } int main() { scanf( %d%d , &n, &m); memset(vis, 0, sizeof(vis)); memset(p, 0, sizeof(p)); for (int i = 1; i <= m; i++) { int x; scanf( %d , a + i); p[a[i]] = 1; } int ans = 0; for (int i = 1; i <= m; i++) { if (!vis[a[i]][0]) { vis[a[i]][0] = 1; ans++; dfs((1 << n) - 1 - a[i], 1); } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> int main(int argc, char* argv[]) { int t, cups = 0, medals = 0, n, shelives_needed = 0; for (int i = 0; i < 3; i++) { scanf( %d , &t); cups += t; } for (int i = 0; i < 3; i++) { scanf( %d , &t); medals += t; } scanf( %d , &n); if (cups % 5 != 0) { shelives_needed += cups / 5 + 1; } else { shelives_needed += cups / 5; } if (medals % 10 != 0) { shelives_needed += medals / 10 + 1; } else { shelives_needed += medals / 10; } if (shelives_needed > n) { printf( NO n ); } else { printf( YES n ); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) using namespace std; int a, b, c, d, n, m, k; long long pc[1000001]; long long dp[7][333335]; int f[6]; const int N = 333333; const long long infll = (long long)1000000000 * 1000000000; inline void upd(long long& a, long long b) { if (a < b) a = b; } long long solve(int n) { long long ans = pc[n]; for (int _n((n)-1), i(0); i <= _n; i++) { int rem = n - i; if (rem % 3) continue; ans = max(ans, pc[i] + dp[5][rem / 3]); } return ans; } int main() { scanf( %d , &k); --k; for (int _n((6) - 1), i(0); i <= _n; i++) scanf( %d , &f[i]); pc[0] = 0; for (int _n((999999)), i(1); i <= _n; i++) { int t = i; long long a = 0; for (int _n((6) - 1), j(0); j <= _n; j++) { int dig = t % 10; if (dig == 3 || dig == 6 || dig == 9) { a += (long long)f[j] * (dig / 3); } t /= 10; } pc[i] = a; } for (int _n((7) - 1), i(0); i <= _n; i++) for (int _n((N + 2) - 1), j(0); j <= _n; j++) dp[i][j] = -8 * infll; for (int _n((N + 1) - 1), i(0); i <= _n; i++) { if (i > 3 * k) break; dp[0][i] = (long long)f[0] * i; } auto ev1 = [&](int i, int s) { return (long long)i * (f[1] - 10LL * f[0]) + (long long)s * f[0]; }; for (int _n((N + 1) - 1), i(0); i <= _n; i++) { if (i > 33LL * k) break; int l = 0; if (i > 3 * k) { int rem = i - 3 * k; l = rem / 10; if (rem % 10) ++l; } int r = min(3 * k, i / 10); if (l > r) continue; dp[1][i] = max(ev1(l, i), ev1(r, i)); } for (int _n(5), d(2); d <= _n; d++) { long long o = 0; for (int _n((d + 1) - 1), z(0); z <= _n; z++) o = o * 10 + 3; o *= k; o = min<long long>(o, N); int t = 1; for (int _n((d)-1), j(0); j <= _n; j++) t *= 10; for (int _n((o + 1) - 1), i(0); i <= _n; i++) { int r = min(3 * k, i / t); long long cd = 0; for (int _n((r + 1) - 1), j(0); j <= _n; j++) { upd(cd, dp[d - 1][i - j * t] + (long long)f[d] * j); } dp[d][i] = cd; } } scanf( %d , &m); for (int _n((m)), hod(1); hod <= _n; hod++) { scanf( %d , &n); long long ans = solve(n); printf( %I64d n , ans); } }
#include <bits/stdc++.h> using namespace std; bool vis[10000001]; int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } int lcm(int a, int b) { return ((a * b) / gcd(a, b)); } int main() { int n; scanf( %d , &n); int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; scanf( %d , &a); if (i == j) sum += a; } } sum %= 2; int q; cin >> q; for (int i = 0; i < q; i++) { int t; scanf( %d , &t); if (t == 3) cout << sum; else { int y; scanf( %d , &y); sum = 1 - sum; } } }
#include <bits/stdc++.h> using namespace std; vector<vector<int> > re; vector<pair<int, int> > e, o; int n, c[404][404], sink, Find, pre[404], flow; bool ch[100009], visit[404]; void init() { for (int i = 2; i * i <= 100000; i++) { if (!ch[i]) { for (int j = i * i; j <= 100000; j += i) ch[j] = 1; } } } void dfs(int cur) { visit[cur] = 1; if (cur == sink) Find = 1; if (Find) return; for (int i = 0; i <= sink && !Find; i++) { if (c[cur][i] > 0 && !visit[i]) { dfs(i); pre[i] = cur; } } } void solve() { init(); scanf( %d , &n); for (int i = 0; i < n; i++) { int a; scanf( %d , &a); if (a % 2) o.push_back(pair<int, int>(a, i + 1)); else e.push_back(pair<int, int>(a, i + 1)); } for (int i = 0; i < o.size(); i++) { c[0][i + 1] = 2; } sink = e.size() + o.size() + 1; for (int i = 0; i < e.size(); i++) { c[o.size() + i + 1][sink] = 2; } for (int i = 0; i < o.size(); i++) { for (int j = 0; j < e.size(); j++) { int t = o[i].first + e[j].first; if (!ch[t]) c[i + 1][o.size() + j + 1] = 1; } } for (;;) { memset(visit, 0, sizeof(visit)); Find = 0; dfs(0); if (!Find) break; flow++; int st, en; en = sink; while (en) { st = pre[en]; c[st][en]--; c[en][st]++; en = st; } } if (flow != 2 * e.size()) { puts( Impossible ); return; } memset(visit, 0, sizeof(visit)); for (int i = 1; i < sink; i++) { if (visit[i]) continue; vector<int> tmp; int cur = i; while (!visit[cur]) { visit[cur] = 1; if (cur <= o.size()) tmp.push_back(o[cur - 1].second); else tmp.push_back(e[cur - o.size() - 1].second); for (int i = 1; i < sink; i++) { if (!visit[i] && ((cur <= o.size() && c[i][cur] > 0) || cur > o.size() && c[cur][i] > 0)) { cur = i; break; } } } re.push_back(tmp); } printf( %d n , re.size()); for (int i = 0; i < re.size(); i++) { printf( %d , re[i].size()); for (int j = 0; j < re[i].size(); j++) printf( %d , re[i][j]); puts( ); } } int main(void) { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int max = INT_MIN, min = INT_MAX; for (int i = 0; i < N; ++i) { int x; cin >> x; if (x > max) max = x; if (x < min) min = x; } cout << max - min + 1 - N << endl; }
#include <bits/stdc++.h> using namespace std; long long a, b[1001], c, cnt1, cnt2, d, n, m; string s; int main() { cin >> a; if (a == 1) { cout << 1; return 0; } for (int i = 1; i <= a; i++) { cin >> b[i]; c += b[i]; } sort(b + 1, b + a + 1); reverse(b + 1, b + a + 1); for (int i = 1; i <= a; i++) { d = (c - b[i]); n += c - d; cnt1++; if (n > d) { cout << cnt1; return 0; } c = d; } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 20; const int MAXX = 65536; int n, m; long long year; vector<int> graph[MAXN], rev_graph[MAXN]; vector<int> sol; long long dp[MAXX]; long long countPermutations(int mask, int pos) { dp[0] = 1; for (int i = 1; i < 1 << (n - pos); ++i) { dp[i] = 0; } for (int i = 0; i < 1 << (n - pos); ++i) { int value = n - 1; while (value >= 0 && (mask & (1 << value))) --value; for (int j = 0; j < n - pos; ++j) if (i & (1 << j)) { do { --value; } while (value >= 0 && (mask & (1 << value))); } for (int j = 0; j < n - pos; ++j) if ((i & (1 << j)) == 0) { int ok = 1; for (size_t k = 0; k < graph[j + pos].size() && ok; ++k) if ((graph[j + pos][k] >= pos && (i & (1 << (graph[j + pos][k] - pos))) == 0) || (graph[j + pos][k] < pos && sol[graph[j + pos][k]] < value)) { ok = 0; } for (size_t k = 0; k < rev_graph[j + pos].size() && ok; ++k) if (rev_graph[j + pos][k] < pos && sol[rev_graph[j + pos][k]] > value) ok = 0; if (ok) dp[i ^ (1 << j)] += dp[i]; } } return dp[(1 << (n - pos)) - 1]; } void findPermutation(int mask, int pos) { if (pos < n) { for (int i = 0; i < n; ++i) if ((mask & (1 << i)) == 0) { int ok = 1; for (size_t j = 0; j < graph[pos].size() && ok; ++j) if (graph[pos][j] < pos && sol[graph[pos][j]] < i) ok = 0; for (size_t j = 0; j < rev_graph[pos].size() && ok; ++j) if (rev_graph[pos][j] < pos && sol[rev_graph[pos][j]] > i) ok = 0; if (!ok) { continue; } mask ^= 1 << i; sol.push_back(i); long long cnt = countPermutations(mask, pos + 1); if (cnt >= year) { findPermutation(mask, pos + 1); break; } year -= cnt; mask ^= 1 << i; sol.pop_back(); } } } int main() { cin >> n >> year >> m; year -= 2000; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; --x, --y; graph[x].push_back(y); rev_graph[y].push_back(x); } findPermutation(0, 0); if ((int)sol.size() != n) { cout << The times have changed n ; return 0; } for (int i = 0; i < n - 1; ++i) cout << sol[i] + 1 << ; cout << sol[n - 1] + 1 << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long m = 0, a = 1, b = 1, ans = 0; while (m <= n) { m = a + b; a = b; b = m; ans++; } cout << ans - 1; return 0; }
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5; const int Inf = 1 << 30; double a[105]; int main() { int n, cnt; double pos, pp, ans, sum; while (~scanf( %d , &n)) { sum = 0; pos = 4.5; pos *= n; for (int i = 1; i <= n; i++) { scanf( %lf , &a[i]); sum += a[i]; } cnt = 0; sort(a + 1, a + 1 + n); while (sum < pos) { cnt++; sum += 5 - a[cnt]; } printf( %d n , cnt); } return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<int> tree[300003]; int colors[300003]; int blueC[300003]; int redC[300003]; int ans = 0; pair<int, int> dfs(int nd, int pr) { int blue = (colors[nd] == 2); int red = (colors[nd] == 1); for (int i = 0; i < (int)tree[nd].size(); i++) { if (tree[nd][i] == pr) continue; pair<int, int> sub = dfs(tree[nd][i], nd); if ((bool)sub.first ^ (bool)sub.second) { if (sub.first) ans += (blueC[0] == sub.first); else ans += (redC[0] == sub.second); } blue += sub.first; red += sub.second; } return make_pair(blue, red); } void solve() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &colors[i]); int u, v; for (int i = 1; i < n; i++) { scanf( %d %d , &u, &v); tree[u].push_back(v); tree[v].push_back(u); } memset(blueC, 0, sizeof(blueC)); memset(redC, 0, sizeof(redC)); for (int i = 1; i <= n; i++) { if (colors[i] == 1) redC[0]++; else if (colors[i] == 2) blueC[0]++; } dfs(1, 0); printf( %d , ans); } int main() { solve(); }
#include <bits/stdc++.h> using namespace std; int n, p[50], sz[50], v[50], id[50], ct, lg[(1 << 24)], bc[(1 << 24)]; unsigned int f[(1 << 24)], av[50]; char s[50][50]; vector<int> vc[50]; int fd(int x) { return p[x] == x ? x : p[x] = fd(p[x]); } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) p[i] = i; for (int i = 1; i <= n; i++) { scanf( %s , s[i] + 1); for (int j = 1; j <= n; j++) if (s[i][j] == A ) { int xx = fd(i), yy = fd(j); p[xx] = yy; } } for (int i = 1; i <= n; i++) { int xx = fd(i); sz[xx]++; vc[xx].push_back(i); } for (int i = 1; i <= n; i++) if (sz[i] > 1 && p[i] == i) { id[i] = ct++; for (auto x : vc[i]) for (auto y : vc[i]) if (s[x][y] == X ) { printf( -1 n ); return 0; } } for (int i = 0; i < ct; i++) bc[1 << i] = i; if (!ct) { printf( %d , n - 1); return 0; } for (int i = 1; i <= n; i++) { if (p[i] == i && sz[i] == 1) continue; for (int j = 1; j <= n; j++) { if (p[j] == j && sz[j] == 1) continue; if (s[i][j] == X ) { int xx = fd(i), yy = fd(j); v[id[xx]] |= (1 << id[yy]); v[id[yy]] |= (1 << id[xx]); } } } f[0] = 1; for (int i = 1; i < (1 << ct); i++) { int lb = i & -i; lg[i] = lg[i - lb] + 1; f[i] = f[i - lb] + f[(i - lb) ^ (i & v[bc[lb]])]; } for (int i = 0; i < (1 << ct); i++) f[i]--; for (int i = 0; i < (1 << ct); i++) { unsigned int r = 1; for (int j = 1; j <= ct; j++) { r *= f[i]; if ((ct - lg[i]) & 1) av[j] -= r; else av[j] += r; } } for (int j = 1; j <= ct; j++) if (av[j]) { printf( %d , j + n - 1); return 0; } }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 1123; int ans[MAX]; int main() { int n; cin >> n; memset(ans, INF, sizeof(ans)); for (int i = 0; i < 10; i++) { vector<int> data1, data2; for (int j = 1; j <= n; j++) { if (j & (1 << i)) data1.push_back(j); else data2.push_back(j); } if (data1.size()) { cout << data1.size() << endl; for (int j = 0; j < data1.size(); j++) { cout << data1[j] << ; } for (int j = 1; j <= n; j++) { int t; cin >> t; if ((j & (1 << i)) == 0) { ans[j] = min(ans[j], t); } } cout << endl; fflush(stdout); } if (data2.size()) { cout << data2.size() << endl; for (int j = 0; j < data2.size(); j++) { cout << data2[j] << ; } for (int j = 1; j <= n; j++) { int t; cin >> t; if (j & (1 << i)) ans[j] = min(ans[j], t); } cout << endl; fflush(stdout); } } fflush(stdout); cout << -1 << endl; for (int i = 1; i <= n; i++) cout << ans[i] << ; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int r, c, i, j, flag = 0; cin >> r >> c; char a[r + 2][c + 2]; for (i = 0; i < (r + 2); i++) { for (j = 0; j < (c + 2); j++) a[i][j] = . ; } for (i = 1; i < (r + 1); i++) { for (j = 1; j < (c + 1); j++) { cin >> a[i][j]; } } for (i = 1; i < (r + 1); i++) { for (j = 1; j < (c + 1); j++) { if (a[i][j] == S ) { if (a[i + 1][j] == W || a[i][j + 1] == W || a[i - 1][j] == W || a[i][j - 1] == W ) { flag = 1; cout << No n ; break; } } } if (flag == 1) break; } if (flag == 0) { cout << Yes n ; for (i = 1; i < (r + 1); i++) { for (j = 1; j < (c + 1); j++) { if (a[i][j] == . ) cout << D ; else cout << a[i][j]; } cout << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, k; vector<int> adj[100005]; int din[100005]; queue<int> que; int Vec[100005], V_size; int a, b, c, d, cur; bool succ; int main() { int Test; scanf( %d , &Test); while (Test--) { scanf( %d%d%d , &n, &m, &k); for (int i = 1; i <= n; i += 1) { adj[i].clear(); din[i] = 0; } while (!que.empty()) que.pop(); for (int i = 1; i <= m; i += 1) { scanf( %d%d , &a, &b); adj[a].push_back(b), adj[b].push_back(a); din[a]++, din[b]++; } if (k == 1) { puts( 2 ); puts( 1 ); continue; } if (k > ((int)sqrt((double)2 * m) + 2)) { puts( -1 ); continue; } for (int i = 1; i <= n; i += 1) sort(adj[i].begin(), adj[i].end()); for (int i = 1; i <= n; i += 1) if (din[i] < (k - 1)) que.push(i); while (!que.empty()) { cur = que.front(); que.pop(); for (auto& i : adj[cur]) { if (din[i] <= 0) continue; din[cur]--, din[i]--; if (din[i] == (k - 2)) que.push(i); } if (din[cur] != 0) { cout << fds ; return 0; } } for (int i = 1; i <= n; i += 1) if (din[i] == (k - 1)) que.push(i); bool nex_loop = false; while (!que.empty()) { cur = que.front(); que.pop(); if (din[cur] == (k - 1)) { V_size = 0; for (auto& i : adj[cur]) { if (din[i] <= 0) continue; Vec[V_size++] = i; } succ = true; for (int i = 0; i <= V_size - 1; i += 1) { for (int j = i + 1; j <= V_size - 1; j += 1) { a = Vec[i], b = Vec[j]; auto it = lower_bound(adj[a].begin(), adj[a].end(), b); if ((it == adj[a].end()) || (*it != b)) { succ = false; break; } } if (!succ) break; } if (succ) { puts( 2 ); printf( %d , cur); for (int i = 0; i <= V_size - 1; i += 1) printf( %d , Vec[i]); printf( n ); nex_loop = true; break; } } for (auto& i : adj[cur]) { if (din[i] <= 0) continue; din[cur]--, din[i]--; if (din[i] == (k - 1)) que.push(i); } } if (nex_loop) continue; V_size = 0; for (int i = 1; i <= n; i += 1) { if (din[i] <= 0) continue; Vec[V_size++] = i; } if (V_size == 0) puts( -1 ); else { printf( 1 %d n , V_size); for (int i = 0; i <= V_size - 1; i += 1) { printf( %d , Vec[i]); } printf( n ); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; int n, num, ctr; string s; while (t--) { cin >> n >> s; num = 0; ctr = 0; for (char ch : s) { if (ch == ) ) { num--; if (num < 0) ctr++, num++; } else num++; } cout << ctr << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; cin >> t; while (t--) { int n, m, i, x = 0, y, z; cin >> n; string s[n + 1]; for (int i = 0; i < n; i++) cin >> s[i]; vector<pair<int, int>> v; if (s[0][1] == s[1][0]) { if (s[n - 1][n - 2] == s[0][1]) { v.push_back({n, n - 1}); } if (s[n - 2][n - 1] == s[0][1]) { v.push_back({n - 1, n}); } } else if (s[n - 1][n - 2] == s[n - 2][n - 1]) { if (s[n - 1][n - 2] == s[0][1]) { v.push_back({1, 2}); } if (s[n - 2][n - 1] == s[1][0]) { v.push_back({2, 1}); } } else { if (s[0][1] != 0 ) { v.push_back({1, 2}); } if (s[1][0] != 0 ) { v.push_back({2, 1}); } if (s[n - 2][n - 1] != 1 ) { v.push_back({n - 1, n}); } if (s[n - 1][n - 2] != 1 ) { v.push_back({n, n - 1}); } } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { cout << v[i].first << << v[i].second << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007LL; const long long MXSZ = 1000000LL; long long fpow(long long b, long long e) { long long ret = 1; while (e > 0) { if (e & 1) (ret *= b) %= mod; (b *= b) %= mod; e >>= 1; } return ret; } long long fac[MXSZ + 5]; void prec() { fac[0] = 1; for (long long i = 1; i <= MXSZ; i++) { fac[i] = fac[i - 1] * i; fac[i] %= mod; } } long long inv(long long u) { return fpow(fac[u], mod - 2); } inline long long comb(long long n, long long k) { long long ret = fac[n]; (ret *= inv(k)) %= mod; (ret *= inv(n - k)) %= mod; return ret; } inline long long perm(long long n, long long k) { long long ret = fac[n]; (ret *= inv(n - k)) %= mod; return ret; } long long n, m, a, b; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); prec(); cin >> n >> m >> a >> b; long long tot = 0LL; for (int btwn = 0; btwn < n - 2; btwn++) { if (m < btwn + 1) break; long long vleft = n - btwn - 2; long long k = btwn + 2; long long curr = comb(m - 1, btwn) * perm(n - 2, btwn); curr %= mod; curr *= k; curr %= mod; curr *= fpow(n, n - k - 1); curr %= mod; curr *= fpow(m, vleft); curr %= mod; tot += curr; tot %= mod; } if (m - 1 >= n - 2) { long long curr = comb(m - 1, n - 2) * perm(n - 2, n - 2); curr %= mod; tot += curr; tot %= mod; } cout << tot << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long dist[200001] = {0}; int n, ans[200001], Val[200001], lg, tree[200001][20]; vector<vector<pair<int, long long> > > G(200001); void Build(int v, int par = 0) { tree[v][0] = par; for (int i = 1; i <= lg; i++) tree[v][i] = tree[tree[v][i - 1]][i - 1]; for (int i = 0; i < G[v].size(); i++) { dist[G[v][i].first] = dist[v] + G[v][i].second; Build(G[v][i].first, v); } } int FinalDfs(int v) { for (int i = 0; i < G[v].size(); i++) ans[v] += FinalDfs(G[v][i].first) + 1; return ans[v]; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &Val[i]); while ((1 << lg) <= n) lg++; lg--; for (int i = 2; i <= n; i++) { long long dis; int par; scanf( %d%I64d , &par, &dis); G[par].push_back(make_pair(i, dis)); } Build(1); for (int i = 1; i <= n; i++) { if (dist[i] <= Val[i]) continue; int Pos = i; int Value = Val[i]; for (int j = lg; j >= 0; j--) if (dist[Pos] - dist[tree[Pos][j]] <= Value && tree[Pos][j] != 0) { Value -= dist[Pos] - dist[tree[Pos][j]]; Pos = tree[Pos][j]; } ans[tree[Pos][0]]--; } FinalDfs(1); for (int i = 1; i <= n; i++) printf( %d , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; string A, B; int used[10][2]; int memo[10][2]; int m; int dp(int pos, int mayor) { if (pos == m) return mayor; if (used[pos][mayor]) return memo[pos][mayor]; used[pos][mayor] = 1; int &dev = memo[pos][mayor] = 0; if (B[pos] == ? ) { if (mayor) { if (pos == 0) { dev = dp(pos + 1, 1); } else { dev = dp(pos + 1, 1); } } else { for (int c = A[pos]; c <= 9 ; ++c) { if (pos == 0 && c == 0 ) continue; dev |= dp(pos + 1, c > A[pos]); } } } else { if (!mayor && B[pos] < A[pos]) return 0; else dev = dp(pos + 1, mayor ? 1 : (B[pos] > A[pos])); } return dev; } string rec(int pos, int mayor) { if (pos == m) return ; if (B[pos] == ? ) { if (mayor) { if (pos == 0) { return string(1, 1 ) + rec(pos + 1, 1); } else { return string(1, 0 ) + rec(pos + 1, 1); } } else { for (int c = A[pos]; c <= 9 ; ++c) { if (pos == 0 && c == 0 ) continue; if (dp(pos + 1, c > A[pos])) return string(1, c) + rec(pos + 1, c > A[pos]); } } } else { return string(1, B[pos]) + rec(pos + 1, mayor ? 1 : (B[pos] > A[pos])); } } int toi(string &s) { istringstream in(s); int x; in >> x; return x; } string tos(int x) { ostringstream out; out << x; return out.str(); } string S[100000 + 5]; void fix(string &s) { for (int i = 0; i < ((int)(s).size()); ++i) { if (s[i] == ? ) { if (i == 0) s[i] = 1 ; else s[i] = 0 ; } } } bool f(string &s, string &t) { if (((int)(s).size()) > ((int)(t).size())) return 0; for (int i = 0; i < ((int)(s).size()); ++i) if (s[i] == ? ) return 0; if (((int)(s).size()) < ((int)(t).size())) { fix(t); return 1; } memset(used, 0, sizeof(used)); m = ((int)(s).size()); A = s, B = t; if (!dp(0, 0)) return 0; string C = rec(0, 0); t = C; return 1; } int main() { ios_base ::sync_with_stdio(0); int n; while (cin >> n) { S[0] = ; bool possible = 1; for (int i = 0; i < n; ++i) { string s; cin >> s; bool ok = f(S[i], s); if (!ok) possible = 0; S[i + 1] = s; } if (!possible) cout << NO << n ; else { cout << YES << n ; for (int i = 0; i < n; ++i) cout << S[i + 1] << n ; } } }
#include <bits/stdc++.h> using namespace std; struct ufds { int n; vector<int> p, s; ufds(int n = 0) : n(n), p(n), s(n, 1) { for (int i = 0; i < n; i++) p[i] = i; } int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } bool inSameSet(int i, int j) { return findSet(i) == findSet(j); } int getSizeOfSet(int i) { return s[findSet(i)]; } void unionSet(int i, int j) { i = findSet(i); j = findSet(j); if (i != j) { if (s[i] < s[j]) swap(i, j); s[i] += s[j]; p[j] = i; } } }; struct maximum_matching { int n, mm = 0, t = 0; vector<int> m, l, p, o, v; vector<vector<int>> &e; queue<int> q; maximum_matching(vector<vector<int>> &e) : n(e.size()), m(n, -1), l(n), p(n), o(n), v(n), e(e) { for (int i = 0; i < n; i++) if (m[i] == -1) mm += bfs(i); } int lca(int i, int j) { for (t++;; swap(i, j)) if (i != -1) { if (v[i] == t) return i; v[i] = t; i = m[i] == -1 ? -1 : o[p[m[i]]]; } } void blossom(int i, int j, int a) { while (o[i] != a) { p[i] = j; j = m[i]; if (l[j] == 1) l[j] = 0, q.push(j); o[i] = o[j] = a; i = p[j]; } } bool bfs(int s) { for (int i = 0; i < n; i++) l[i] = -1, o[i] = i; while (!q.empty()) q.pop(); l[s] = 0; q.push(s); while (!q.empty()) { int i = q.front(); q.pop(); for (int j : e[i]) if (l[j] == -1) { l[j] = 1; p[j] = i; if (m[j] == -1) { while (j != -1) m[j] = p[j], swap(j, m[m[j]]); return true; } l[m[j]] = 0; q.push(m[j]); } else if (l[j] == 0 && o[i] != o[j]) { int a = lca(o[i], o[j]); blossom(i, j, a); blossom(j, i, a); } } return false; } }; int n, a[300100], r[300100], value, m, rng[300100][3], c = 1, comp[700]; bool u[300100], rngu[300100]; vector<int> er[2], te[700]; void fill() { int nv = 1; for (int i = 0; i < n - 1; i++) if (r[i] == 0 && r[i + 1] == 0) { while (u[nv]) nv++; r[i] = r[i + 1] = nv, value++, u[nv] = true; } for (int i = 0; i < n; i++) if (r[i] == 0) r[i] = 1; } void fillOE(int k, int av) { int i = rng[k][0], j = rng[k][1]; if (av == a[i - 1]) r[i] = av; else r[j] = av; value++, u[av] = true; } void fillE(int k) { int i = rng[k][0], j = rng[k][1]; r[i] = r[i - 1], r[j] = r[j + 1]; u[a[i - 1]] = u[a[j + 1]] = true; value++; } void dfsTree(int i, int p = -1) { for (int k : te[i]) { int j = i ^ rng[k][2]; if (j != p) { fillOE(k, j); dfsTree(j, i); } } } void dfsComp(int i) { comp[i] = c; for (int k : te[i]) { int j = i ^ rng[k][2]; if (comp[j] == 0) dfsComp(j); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); cin >> n; for (int i = 0; i < n; i++) cin >> a[i], r[i] = a[i]; int s = 0; while (s < n && a[s] == 0) s++; if (s < n && s % 2 == 1) a[s - 1] = r[s - 1] = a[s], s--; int e = n - 1; while (e >= 0 && a[e] == 0) e--; if (e >= 0 && (n - e - 1) % 2 == 1) a[e + 1] = r[e + 1] = a[e], e++; if (s < e) { for (int i = s + 1; i < e; i++) if (a[i] == 0) { int j = i; while (a[j + 1] == 0) j++; rng[m][0] = i, rng[m][1] = j; rng[m][2] = a[i - 1] ^ a[j + 1]; er[(j - i + 1) & 1].push_back(m); m++; i = j; } ufds uf(700); for (int k : er[1]) { int i = rng[k][0], j = rng[k][1]; if (!uf.inSameSet(a[i - 1], a[j + 1])) { uf.unionSet(a[i - 1], a[j + 1]); te[a[i - 1]].push_back(k); te[a[j + 1]].push_back(k); rngu[k] = true; } } for (int k : er[1]) { int i = rng[k][0], j = rng[k][1]; if (!rngu[k] && uf.inSameSet(a[i - 1], a[j + 1]) && !u[a[i - 1]]) { fillOE(k, a[i - 1]); dfsTree(a[i - 1]); } } for (int i = s; i < e; i++) if (a[i] > 0 && a[i] == a[i + 1] && !u[a[i]]) value++, u[a[i]] = true, dfsTree(a[i]); for (int i = 1; i <= min(n, 600); i++) if (!u[i] && comp[i] == 0) dfsComp(i), c++; vector<vector<int>> e(c); for (int k : er[0]) { int i = rng[k][0], j = rng[k][1]; if (!u[a[i - 1]] && !u[a[j + 1]] && comp[a[i - 1]] != comp[a[j + 1]]) { e[comp[a[i - 1]]].push_back(comp[a[j + 1]]), e[comp[a[j + 1]]].push_back(comp[a[i - 1]]); } } maximum_matching mm(e); for (int k : er[0]) { int i = rng[k][0], j = rng[k][1]; if (mm.m[comp[a[i - 1]]] != -1 && mm.m[comp[a[i - 1]]] == comp[a[j + 1]]) { fillE(k); dfsTree(a[i - 1]); dfsTree(a[j + 1]); mm.m[comp[a[i - 1]]] = -1, mm.m[comp[a[j + 1]]] = -1; } } for (int i = 1; i <= min(n, 600); i++) if (!u[i]) dfsTree(i); } fill(); for (int i = 0; i < n; i++) cout << r[i] << (i < n - 1 ? : n ); }