func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int tag[10]; int main() { string a, b; while (cin >> a >> b) { memset(tag, 0, sizeof(tag)); ; int len = a.size(); int len1 = b.size(); for (int i = 0; i < len1; i++) { int num = b[i] - 0 ; tag[num]++; } for (int i = 0; i < len; i++) { int p = a[i] - 0 ; int j = 9; while (j > p) { if (!tag[j]) j--; if (tag[j] && j > p) { a[i] = j + 0 ; tag[j]--; break; } } } for (int i = 0; i < len; i++) cout << a[i]; cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int n, k; bool d[1001]; long long dp[1001][2][1001]; int cnt[10001]; long long DP(int i, bool f, int j) { if (i == n) return cnt[j] == k; if (dp[i][f][j] != -1) return dp[i][f][j]; long long ret = 0; if (d[i] == 1) ret = (ret + DP(i + 1, f, j + 1)) % mod; else if (d[i] == 0 and f) ret = (ret + DP(i + 1, f, j + 1)) % mod; ret = (ret + DP(i + 1, f | (d[i] == 1), j)) % mod; return dp[i][f][j] = ret; } int main() { for (int i = 1; i <= 1000; i++) { cnt[i] = 1 + cnt[__builtin_popcount(i)]; } string s; cin >> s; n = s.size(); cin >> k; int i = 0; for (char c : s) d[i++] = c - 0 ; memset(dp, -1, sizeof dp); cout << DP(0, 0, 0) - (k == 1); } |
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18, N = 2e6 + 3, BASE = 130; int main() { long long int n, k; cin >> n >> k; vector<int> arr(n), up(n); for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = n - 2; i >= 0; i--) { if (arr[i + 1] == 1) { up[i] = up[i + 1] + 1; } else { up[i] = 0; } } long long int ans = 0; for (int i = 0; i < n; i++) { int ind = i; long long int p = 1; long long int sum = 0; int cnt = 0; while (ind < n && cnt <= 20) { if (p > INF / arr[ind]) { break; } p *= arr[ind]; sum += arr[ind]; cnt++; if (p % sum == 0 && p / sum == k) { ans++; } if (up[ind]) { if (p % k == 0) { long long int num = p / k; if (sum + 1 <= num && num <= sum + up[ind]) { ans++; } } } sum += up[ind]; ind = ind + 1 + up[ind]; } } cout << ans; } |
#include <bits/stdc++.h> using namespace std; void PLAY() { cout << fixed << setprecision(10); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; vector<string> x(n), y(m), ans; for (int i = 0; i < n; i++) { cin >> x[i]; } for (int i = 0; i < m; i++) { cin >> y[i]; } long long q; cin >> q; while (q--) { long long a; cin >> a; a--; cout << x[a % n] + y[a % m] << endl; } return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /stack:64000000 ) using namespace std; const double PI = 2 * acos(0.0); const double eps = 1e-9; template <class T> T jog(T a, T b) { return a + b; } template <class T> T bog(T a, T b) { return a - b; } template <class T> T gon(T a, T b) { return a * b; } template <class T> T sq(T x) { return x * x; } template <class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <class T> T lcm(T a, T b) { return (a / gcd(a, b)) * b; } template <class T> T power(T a, T p) { T res = 1, x = a; while (p) { if (p & 1) res = res * x; x = x * x; p >>= 1; } return res; } template <class T> T cordinatlenth(T a, T b, T c, T d) { return sqrt(sq(a - c) + sq(b - d)); } template <class T> string toString(T n) { ostringstream oss; oss << n; oss.flush(); return oss.str(); } int toInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; } long long bigmod(long long a, long long p, long long mod) { long long res = 1, x = a; while (p) { if (p & 1) res = (res * x) % mod; x = (x * x) % mod; p >>= 1; } return res; } int diraction1[] = {-1, 0, 0, 1, 1, -1, -1, 1}; int diraction2[] = {0, -1, 1, 0, 1, -1, 1, -1}; int horsed1[] = {-2, -2, -1, 1, 2, 2, 1, -1}; int horsed2[] = {1, -1, -2, -2, -1, 1, 2, 2}; void input(); const int INF = 2147483647; const long long LINF = 9223372036854775807ll; int n, in[1000010]; int main() { input(); return 0; } void input() { memset(in, 0, sizeof(in)); cin >> n; for (int i = 0, tmp; i < n; i++) { scanf( %d , &tmp); in[tmp]++; } long long ans = 0; for (int i = 0; i < 1000005; i++) { if (in[i] == 0 || in[i] == 1) continue; while (in[i] > 1) { ans++; in[i + 1]++; in[i]--; } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int n; int changed; int answer1, answer2 = 1023; int main() { cin >> n; int i; for (i = 0; i < n; i++) { char tp; cin >> tp; if (tp == | ) { int number; cin >> number; answer1 |= number; answer2 |= number; changed |= number; } else if (tp == & ) { int number; cin >> number; answer1 &= number; answer2 &= number; number = 1023 - number; changed |= number; } else { int number; cin >> number; answer1 ^= number; answer2 ^= number; } } int answer11 = 0; int answer22 = 0; int answer33 = 0; for (i = 0; i < 10; i++) { if ((answer1 & (1 << i)) && (answer2 & (1 << i))) { answer11 += 1 << i; } else { if ((answer1 & (1 << i)) && (!(answer2 & (1 << i)))) { answer33 += 1 << i; answer22 += 1 << i; } if ((!(answer1 & (1 << i))) && (answer2 & (1 << i))) { answer22 += 1 << i; } } } cout << 3 << endl; cout << & << answer22 << endl; cout << | << answer11 << endl; cout << ^ << answer33 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; string s, t; char c[N]; int cs[2][N], ct[2][N], q; int main() { scanf( %s , c); s = c; scanf( %s , c); t = c; int at = 0; for (auto &i : s) { at++; if (i == C || i == B ) { i = B ; cs[1][at] = cs[1][at - 1] + 1; } else { cs[1][at] = cs[1][at - 1]; cs[0][at] = cs[0][at - 1] + 1; } } at = 0; for (auto &i : t) { at++; if (i == C || i == B ) { i = B ; ct[1][at] = ct[1][at - 1] + 1; } else { ct[1][at] = ct[1][at - 1]; ct[0][at] = ct[0][at - 1] + 1; } } scanf( %d , &q); for (int i = 0, a, b, c, d; i < q; i++) { scanf( %d%d%d%d , &a, &b, &c, &d); int difs = cs[1][b] - cs[1][a - 1]; int dift = ct[1][d] - ct[1][c - 1]; bool thereis = 0; int need = dift - difs; int trails = min(b - a + 1, cs[0][b]); int trailt = min(d - c + 1, ct[0][d]); if (need & 1) { printf( 0 ); continue; } if (need < 0) { printf( 0 ); continue; } if (!need) { if (trails < trailt || (trails - trailt) % 3) { printf( 0 ); continue; } printf( 1 ); continue; } if (need && trails > trailt) { trails = trailt; thereis = 1; } if ((trails - trailt) % 3 || trails < trailt) { printf( 0 ); continue; } thereis |= difs; if (!thereis && need) { printf( 0 ); continue; } printf( 1 ); } } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline T maxi(T a, T b) { return a > b ? a : b; } template <typename T> inline T mini(T a, T b) { return a < b ? a : b; } template <typename T> inline T absl(T a) { return a > 0 ? a : -a; } template <typename T> inline T gcd(T a, T b) { T t; if (a < b) { while (a) { t = a; a = b % a; b = t; } return b; } else { while (b) { t = b; b = a % b; a = t; } return a; } } template <typename T> inline T ceiling(T a, T b) { if (b < 0) { a *= -1; b *= -1; } if ((double)a / (double)b < 0) return 0; { if (a % b == 0) return (a / b); return ((a / b) + 1); } } vector<int> adj[500000]; map<pair<int, int>, int> m; vector<pair<int, int> > v(500000); vector<bool> visited(500000, false); int d; void dfs(int u, int c) { visited[u] = true; for (auto it : adj[u]) { if (!visited[it]) { m[{u, it}] = c % d; ++c; dfs(it, c); } } } void input() { int n, k; cin >> n >> k; for (int i = 0; i < n - 1; ++i) { int s, d; cin >> s >> d; --s; --d; adj[s].push_back(d); adj[d].push_back(s); v[i].first = s; v[i].second = d; } int b[n]; for (int i = 0; i < n; ++i) { b[i] = adj[i].size(); } sort(b, b + n, greater<int>()); d = b[k]; cout << d << endl; dfs(0, 0); for (int i = 0; i < n - 1; ++i) { cout << maxi(m[v[i]], m[{v[i].second, v[i].first}]) + 1 << ; } } int main() { input(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; struct node { int l, r, sum; } hjt[maxn * 40]; int a[maxn], root[maxn], cnt; int last[maxn]; void insert(int l, int r, int pre, int &now, int p, int v) { hjt[++cnt] = hjt[pre]; now = cnt; hjt[now].sum = hjt[pre].sum + v; if (l == r) return; int m = (l + r) >> 1; if (p <= m) insert(l, m, hjt[pre].l, hjt[now].l, p, v); else insert(m + 1, r, hjt[pre].r, hjt[now].r, p, v); } int query(int now, int l, int r, int k) { if (l == r) { if (hjt[now].sum <= k) return l; return l - 1; } int mid = (l + r) >> 1; int tmp = hjt[hjt[now].l].sum; if (tmp > k) return query(hjt[now].l, l, mid, k); else return query(hjt[now].r, mid + 1, r, k - tmp); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = n; i >= 1; i--) { if (last[a[i]]) { insert(1, n, root[i + 1], root[i], last[a[i]], -1); insert(1, n, root[i], root[i], i, 1); } else insert(1, n, root[i + 1], root[i], i, 1); last[a[i]] = i; } for (int i = 1; i <= n; i++) { int ans = 0, l = 1, r = 0; while (l <= n) { r = query(root[l], 1, n, i); l = r + 1; ans++; } cout << ans; if (i == n) cout << n ; else cout << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, k; int num = 0; int e[21][21]; int flag[21]; int dfs(int x, int sum, int mid) { if (x > n) { num = num + 1; } else { mid = dfs(x + 1, sum, mid); for (int y = 1; y <= n; y++) { if (num >= k) { break; } if (flag[y] == 0 && e[x][y] != 0 && sum + e[x][y] <= mid) { flag[y] = 1; mid = dfs(x + 1, sum + e[x][y], mid); flag[y] = 0; } } } return mid; } int main() { int u, v, w; cin >> n >> m >> k; for (int i = 0; i <= 20; i++) { for (int j = 0; j <= 20; j++) { e[i][j] = 0; } } int l = 0; int h = n * 1000; int mid = 0; for (int i = 0; i < m; i++) { cin >> u >> v >> w; e[u][v] = w; } while (l < h) { num = 0; mid = (l + h) / 2; mid = dfs(1, 0, mid); if (num < k) { l = mid + 1; } else { h = mid; } } cout << h << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 50005; const int MAXD = 55; const int MOD = 1e9 + 7; template <typename T> inline void read(T &AKNOI) { T x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) flag = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 0 ; ch = getchar(); } AKNOI = flag * x; } namespace ModCalculator { inline void Inc(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } inline void Dec(int &x, int y) { x -= y; if (x < 0) x += MOD; } inline int Add(int x, int y) { Inc(x, y); return x; } inline int Sub(int x, int y) { Dec(x, y); return x; } inline int Mul(int x, int y) { return 1LL * x * y % MOD; } } // namespace ModCalculator using namespace ModCalculator; char s[MAXN], sx[MAXD], sy[MAXD]; int n, d, dg[MAXD]; int tot, ch[MAXN][10], fail[MAXN], ed[MAXN], q[MAXN]; int dp[MAXD][MAXN]; void Insert(int l, int r) { int p = 0; for (int i = l; i <= r; ++i) { int x = s[i] - 0 ; if (!ch[p][x]) ch[p][x] = ++tot; p = ch[p][x]; } ed[p] = 1; } void GetFail() { int head = 1, tail = 0; for (int i = 0; i < 10; ++i) { if (ch[0][i]) { q[++tail] = ch[0][i]; } } while (head <= tail) { int u = q[head++]; for (int i = 0; i < 10; ++i) { if (ch[u][i]) { fail[ch[u][i]] = ch[fail[u]][i]; q[++tail] = ch[u][i]; } else { ch[u][i] = ch[fail[u]][i]; } } } } int DP(int i, int j, bool full) { if (i > d) return 1; if (!full && dp[i][j] != -1) return dp[i][j]; int ret = 0, r = (full ? dg[i] : 9); for (int k = 0; k <= r; ++k) { if (ed[ch[j][k]]) continue; Inc(ret, DP(i + 1, ch[j][k], full && (k == r))); } if (!full) dp[i][j] = ret; return ret; } int Calc(char *ss) { int ret = 0; for (int i = 1; i <= d; ++i) { ret = Add(Mul(ret, 10), (dg[i] = ss[i] - 0 )); } return Sub(ret, DP(1, 0, 1)); } void init() { scanf( %s , s + 1); n = strlen(s + 1); scanf( %s , sx + 1); scanf( %s , sy + 1); d = strlen(sx + 1); int d2 = d / 2; for (int i = 1; i + d2 - 1 <= n; ++i) { Insert(i, i + d2 - 1); } GetFail(); } void solve() { int p = d; for (; sx[p] == 0 ; sx[p--] = 9 ) ; sx[p] -= 1; memset(dp, -1, sizeof(dp)); printf( %d n , Sub(Calc(sy), Calc(sx))); } int main() { init(); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; long long mdin(long long a, long long m) { long long r1 = m, r2 = a, t1 = 0, t2 = 1; while (r2 > 0) { long long q = r1 / r2; long long r = r1 % r2; r1 = r2; r2 = r; long long t = (t1 - q * t2) % m; t1 = t2; t2 = t; } return (t1 + m) % m; } long long ex(long long a, long long n, long long m) { if (n == 0) return 1; long long ans = ex((a * a) % m, n / 2, m); if (n % 2) ans = (a * ans) % m; return ans; } void bfs(int r, vector<int> adj[], int n) { queue<int> q, l; vector<int> vis(n + 1, 0); q.push(r); l.push(1); while (!q.empty()) { int nd = q.front(); q.pop(); int lv = l.front(); l.pop(); for (auto it : adj[nd]) { if (vis[it] == 0) { vis[it] = 1; q.push(it); l.push(lv + 1); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { long long n, k; cin >> n >> k; if (k > n - 1) { cout << 0; return 0; } long long f[n + 1], inf[n + 1]; f[0] = 1; inf[0] = 1; for (long long i = 1; i <= n; i++) { f[i] = (i * f[i - 1]) % 998244353; inf[i] = mdin(f[i], 998244353); } if (k == 0) { cout << f[n] << endl; } else { long long ans = (((f[n] * inf[n - k]) % 998244353) * inf[k]) % 998244353; k = n - k; long long ml = 0; int s = 1; for (long long i = 0; i <= k - 1; i++) { long long cm = (((f[k] * inf[i]) % 998244353) * inf[k - i]) % 998244353; long long t = k - i; long long et = ex(t, n, 998244353); ml = (ml + (cm * et * s) % 998244353 + 998244353) % 998244353; s *= -1; } ans = (((ml * ans) % 998244353) * 2) % 998244353; cout << ans << endl; } } return 0; } |
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define pb push_back #define mp make_pair #define rep(n) for(int i=0;i<n;i++) #define repit(x) for(auto it : x) #define ll long long #define vi vector<int> #define vc vector<char> #define vs vector<string> #define mii map<int ,int> #define mci map<char ,int> /* bool prime[100001]; const int N=100000; void sieve() { int i,j; for(i=2;i<=N;i++) prime[i]=true; prime[0]=prime[1]=false; for(i=2;i*i<=N;i++){ if(prime[i]){ for(j=i*i;j<=N;j+=i) prime[j]=false; } } }*/ int gcd(int a,int b){ if(b==0) return a; else return gcd(b,a%b); } void swap(int *x,int *y){ int temp=*x; *x=*y; *y=temp; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--){ int i,n,c=0,j; cin>>n; string s,s2; cin>>s; map<string,int>m; for(i=97;i<=122;i++){ string s1; s1.push_back(i); m.insert(make_pair(s1,0)); } for(i=0;i<n;i++){ string s1; s1.push_back(s[i]); m[s1]++; } for(auto it=m.begin();it!=m.end();it++){ if(it->second==0){ c=1; s2=it->first; break; } } if(c){ cout<<s2<< n ; } else{ m.clear(); for(i=97;i<=122;i++){ for(j=97;j<=122;j++){ string s1; s1.push_back(i); s1.push_back(j); m.insert(make_pair(s1,0)); } } for(i=0;i<n-1;i++){ string s1; s1.push_back(s[i]); s1.push_back(s[i+1]); m[s1]++; } for(auto it=m.begin();it!=m.end();it++){ if(it->second==0){ c=1; s2=it->first; break; } } if(c) cout<<s2<< n ; else{ m.clear(); for(i=97;i<=122;i++){ for(j=97;j<=122;j++){ for(int k=97;k<=122;k++){ string s1; s1.push_back(i); s1.push_back(j); s1.push_back(k); m.insert(make_pair(s1,0)); } } } for(i=0;i<n-2;i++){ string s1; s1.push_back(s[i]); s1.push_back(s[i+1]); s1.push_back(s[i+2]); m[s1]++; } for(auto it=m.begin();it!=m.end();it++){ if(it->second==0){ c=1; s2=it->first; break; } } cout<<s2<< n ; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; int table[1000][1000]; int main() { int k = 0; scanf( %d %d %d , &n, &m, &k); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf( %d , &table[i][j]); } } while (k) { char command; int x, y; scanf( %c %d %d , &command, &x, &y); if (command == g ) printf( %d n , table[x - 1][y - 1]); else if (command == c ) for (int i = 0; i < n; ++i) swap(table[i][x - 1], table[i][y - 1]); else for (int i = 0; i < m; ++i) swap(table[x - 1][i], table[y - 1][i]); k--; } return 0; } |
#include <bits/stdc++.h> using namespace std; ifstream Cin( input.txt ); ofstream Cout( output.txt ); int main() { long long i, n4 = 0, n7 = 0, n; string a, b; cin >> a >> b; for (i = 0; i < a.length(); i++) if (a[i] != b[i]) { if (a[i] == 4 ) n4++; else n7++; } cout << n4 + n7 - min(n4, n7); } |
#include <bits/stdc++.h> using namespace std; using ll = long long; ll min(ll a, ll b) { if (a < b) return a; return b; } void solve() { ll testCases; cin >> testCases; while (testCases--) { ll n; cin >> n; vector<ll> arr(n); vector<ll> res(n, 1); for (int i = 0; i < n; i++) cin >> arr[i]; int i = 0; for (i = 0; i < n; i++) { if (arr[i] != arr[(i + 1) % n]) break; } if (i == n) { cout << 1 n ; for (i = 0; i < n; i++) cout << 1 << ; cout << n ; continue; } if (n % 2 == 0) { cout << 2 n ; for (i = 0; i < n; i++) cout << (i % 2) + 1 << ; cout << n ; continue; } else { bool isTwo = 0; for (i = 0; i < n; i++) { if (arr[i] == arr[(i + 1) % n]) { for (int j = 0, k = i + 1; k < n; k++, j ^= 1) res[k] = j + 1; for (int j = 0, k = i; k >= 0; k--, j ^= 1) res[k] = j + 1; cout << 2 n ; for (auto e : res) cout << e << ; cout << n ; isTwo = 1; break; } } if (!isTwo) { cout << 3 n ; for (i = 0; i < n - 1; i++) cout << i % 2 + 1 << ; cout << 3 n ; } } } } int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); }; solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxN = 3000 + 1; pair<int, int> a[maxN]; pair<int, int> b[maxN]; int nx[maxN], pr[maxN], v[maxN]; int r, c, n; inline int gg(int i) { pair<int, int> x = {a[i].second, a[i].first}; return lower_bound(b, b + n + 2, x) - b; } inline int ff(int i) { return b[i].first; } int nex(int i) { if (v[nx[i]]) return nx[i]; return nx[i] = nex(nx[i]); } int prev(int i) { if (v[pr[i]]) return pr[i]; return pr[i] = prev(pr[i]); } int L[11], R[11]; int main() { cin >> r >> c >> n; int k; cin >> k; for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; b[i] = {a[i].second, a[i].first}; } b[n] = {0, 0}, b[n + 1] = {c + 1, 0}; sort(a, a + n); sort(b, b + n + 2); v[0] = v[n + 1] = 1; pr[0] = 0, nx[n + 1] = n + 1; long long ans = 0; for (int i = 1; i <= r; i++) { int ind = 0; for (; ind < n && a[ind].first < i; ind++) ; if (ind == n) break; for (int j = ind; j < n; j++) v[gg(j)] = 1; for (int j = 1; j <= n; j++) { nx[j] = j + 1; pr[j] = j - 1; } int curr = 0; for (int j = 1; j <= n; j++) if (v[j]) { int x = j; for (int t = 1; t < k; t++, x = nex(x)) ; curr += (ff(j) - ff(prev(j))) * (c + 1 - ff(x)); } int ptr = n; for (int i2 = r; i2 >= i; i2--) { ans += curr; for (; ptr > ind && a[ptr - 1].first >= i2; ptr--) { L[0] = R[0] = gg(ptr - 1); for (int t = 1; t < k; t++) { L[t] = prev(L[t - 1]); R[t] = nex(R[t - 1]); } for (int t = 0; t < k; t++) { int left = L[t]; int right = R[k - t - 1]; curr -= (ff(left) - ff(prev(left))) * (ff(nex(right)) - ff(right)); } v[L[0]] = 0; } } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int arr[500005], brr[500005]; pair<int, int> tree[500005 * 4]; vector<int> vv[500005]; vector<pair<int, int> > qr[500005]; int ans[500005]; void build(int node, int b, int e) { if (b == e) { tree[node] = make_pair(500000000, 500000000); return; } int left = 2 * node, right = left + 1, mid = (b + e) / 2; build(left, b, mid); build(right, mid + 1, e); tree[node] = tree[left]; } void update(int node, int b, int e, int i, int v) { if (e < i || b > i) return; if (b == e && b == i) { tree[node] = make_pair(v, arr[b]); return; } int left = 2 * node, right = left + 1, mid = (b + e) / 2; update(left, b, mid, i, v); update(right, mid + 1, e, i, v); if (tree[left].first <= tree[right].first) { tree[node] = tree[left]; } else tree[node] = tree[right]; } pair<int, int> query(int node, int b, int e, int l, int r) { if (b > r || e < l) return make_pair(500000000, 500000000); if (b >= l && e <= r) return tree[node]; int left = 2 * node, right = left + 1, mid = (b + e) / 2; pair<int, int> p = query(left, b, mid, l, r); pair<int, int> q = query(right, mid + 1, e, l, r); if (p.first <= q.first) return p; return q; } int main() { int n; scanf( %d , &n); int mx = 0; for (int i = 1; i <= n; i++) { scanf( %d , &arr[i]); vv[arr[i]].push_back(i); mx = max(mx, arr[i]); } int q; scanf( %d , &q); for (int i = 1; i <= q; i++) { int l, r; scanf( %d %d , &l, &r); qr[r].push_back(make_pair(l, i)); } build(1, 1, n); for (int i = 1; i <= n; i++) { int id = lower_bound(vv[arr[i]].begin(), vv[arr[i]].end(), i) - vv[arr[i]].begin(); int v; if (id == 0) { v = 1; brr[arr[i]] = i; } else { v = vv[arr[i]][id - 1] + 1; update(1, 1, n, brr[arr[i]], 500000000); brr[arr[i]] = i; } update(1, 1, n, i, v); for (int j = 0; j < qr[i].size(); j++) { pair<int, int> ret = query(1, 1, n, qr[i][j].first, i); if (qr[i][j].first >= ret.first) ans[qr[i][j].second] = ret.second; } } for (int i = 1; i <= q; i++) printf( %d n , ans[i]); } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int MAXN = 200000; int n, tot; bool bz[N]; vector<int> g[N], edge[N]; int e[N * 2][2], gg[N]; int sz; long long ans[N]; void add(int x, int y) { e[++tot][0] = y; e[tot][1] = gg[x]; gg[x] = tot; } void dfs(int x) { bz[x] = 0; sz++; for (int i = gg[x]; i; i = e[i][1]) if (bz[e[i][0]]) dfs(e[i][0]); gg[x] = 0; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); for (int j = 1; 1ll * j * j <= x; j++) if ((x % j) == 0) { g[j].push_back(i); if (j * j != x) g[x / j].push_back(i); } } for (int i = 1; i < n; i++) { int x, y; scanf( %d %d , &x, &y); edge[x].push_back(y); edge[y].push_back(x); } for (int i = 1; i <= MAXN; i++) if (g[i].size()) { tot = 0; for (auto u : g[i]) bz[u] = 1; for (auto u : g[i]) for (auto v : edge[u]) if (bz[v]) add(u, v); for (auto u : g[i]) if (bz[u]) { sz = 0; dfs(u); ans[i] += 1ll * sz * (sz - 1) / 2 + sz; } } for (int i = MAXN; i; i--) for (int j = 2; 1ll * j * i <= MAXN; j++) ans[i] -= ans[i * j]; for (int i = 1; i <= MAXN; i++) if (ans[i]) printf( %d %lld n , i, ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int k, n; scanf( %d%d , &n, &k); vector<int> a(n), b(n), distributed(n), p(n); vector<vector<int> > cart(k); for (int i = 0; i < n; ++i) { scanf( %d%d , &a[i], &b[i]); distributed[i] = 0; p[i] = i; } for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (a[p[i]] < a[p[j]]) { swap(p[i], p[j]); } } } int cart_ptr = 0; for (int i = 0; i < n && cart_ptr < k; ++i) { if (b[p[i]] == 1) { distributed[p[i]] = 1; cart[cart_ptr].push_back(p[i]); ++cart_ptr; } } deque<int> available; for (int i = 0; i < n; ++i) { if (!distributed[p[i]]) { available.push_back(p[i]); } } while (cart_ptr < k) { cart[cart_ptr].push_back(available.front()); available.pop_front(); ++cart_ptr; } while (!available.empty()) { cart[k - 1].push_back(available.front()); available.pop_front(); } long long ans = 0; for (int i = 0; i < k; ++i) { int min_cost = 2000000000; for (int j = 0; j < (int)cart[i].size(); ++j) { ans += a[cart[i][j]] * 2; min_cost = min(min_cost, a[cart[i][j]]); } if (b[cart[i][0]] == 1) { ans -= min_cost; } } cout << ans / 2; if (ans % 2 == 0) { cout << .0 << endl; } else { cout << .5 << endl; } for (int i = 0; i < k; ++i) { cout << cart[i].size(); for (int j = 0; j < (int)cart[i].size(); ++j) { cout << << cart[i][j] + 1; } cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const int mod = (int)1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-9; const int maxn = (int)2e6 + 11; long long n; long long a[maxn]; long long sum[maxn]; int main() { cin >> n; a[1] = 2; sum[1] = 2; for (int i = 2; i <= 2000000; i++) { a[i] = a[i - 1] + 3; sum[i] = sum[i - 1] + a[i]; } int ans = 0; for (int i = 1; sum[i] <= n; i++) { if ((n - sum[i]) % 3 == 0) ans++; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; map<long long, long long> mp[12]; long long a[200005], dig[200005]; long long state[200005][12]; int main() { long long n, k; scanf( %lld %lld , &n, &k); for (int i = 0; i < n; i++) scanf( %lld , &a[i]); for (int i = 0; i < n; i++) { dig[i] = log10(a[i]) + 1; } for (int i = 0; i < n; i++) { long long x = a[i]; long long tencnt = 1; while (tencnt < 11) { x = (x * 10LL) % k; mp[tencnt][x]++; state[i][tencnt] = x; tencnt++; } } long long ans = 0; for (int i = 0; i < n; i++) { long long len = dig[i]; long long m = (k - (a[i] % k)) % k; if (mp[len].count(m) == 0) continue; ans += mp[len][m]; } for (int i = 0; i < n; i++) { long long len = dig[i]; long long x = state[i][len]; if ((x + a[i]) % k == 0) { ans--; } } printf( %lld n , ans); } |
#include <bits/stdc++.h> using namespace std; const long long LINF = ~0ULL >> 1; const int N = 1e3 + 7; const int INF = 2e8 + 7; const int MOD = 1e9 + 7; const double PI = acos(-1.0); const double EPS = 1e-8; int n; long long calc(long long A, long long B, long long C) { return (A + 1) * (B + 2) * (C + 2); } void solve() { long long mn = LINF, mx = -LINF, tmp; for (int i = 1; 1ll * i * i * i <= n; ++i) { if (n % i) continue; for (int j = i, J = n / i; 1ll * j * j <= J; ++j) { if (J % j) continue; int k = n / i / j; tmp = calc(i, j, k); mn = min(mn, tmp), mx = max(mx, tmp); tmp = calc(j, i, k); mn = min(mn, tmp), mx = max(mx, tmp); tmp = calc(k, i, j); mn = min(mn, tmp), mx = max(mx, tmp); } } cout << mn - n << << mx - n << endl; } int main() { std::ios_base::sync_with_stdio(false); cin >> n; solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, x, r[100000], pos[100000], flag = 1, flag2; pair<int, int> a[100000]; vector<int> ans, ans2; int l(int n) { if (!n) return 1; if (n % 10 == 4 || n % 10 == 7) return l(n / 10); return 0; } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &x); a[i] = pair<int, int>(x, i); } sort(a, a + n); for (int i = 0; i < n; i++) { r[a[i].second] = i; pos[i] = a[i].second; if (a[i].second != i) flag = 0; if (!flag2 && l(a[i].first)) x = i, flag2 = 1; } if (flag) { cout << 0 << endl; return 0; } if (!flag2) { cout << -1 << endl; return 0; } for (int i = 0; i < n; i++) if (i != x) { if (pos[i] == i) continue; if (pos[x] != i) { ans.push_back(pos[x]), ans2.push_back(i); int temp = pos[x]; swap(pos[x], pos[r[i]]); swap(r[temp], r[i]); } ans.push_back(pos[x]), ans2.push_back(pos[i]); swap(pos[x], pos[i]); swap(r[pos[x]], r[pos[i]]); } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 << << ans2[i] + 1 << endl; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 10; const int MOD = 998244353; const int INF = 10; int fact[MAXN], invfact[MAXN]; int add(int a, int b) { int sum = a + b; return (sum >= MOD ? sum -= MOD : (sum < 0 ? sum += MOD : sum)); } int mul(int a, int b) { return 1LL * a * b % MOD; } int pwr(int a, int b) { return (!b ? 1 : (b & 1 ? 1LL * a * pwr(1LL * a * a % MOD, b >> 1) % MOD : pwr(1LL * a * a % MOD, b >> 1) % MOD)); } int inv(int x) { return pwr(x, MOD - 2); } void first() { fact[0] = 1; for (int i = 1; i < MAXN; i++) fact[i] = mul(fact[i - 1], i); for (int i = 0; i < MAXN; i++) invfact[i] = inv(fact[i]); } int C(int n, int k) { return mul(mul(fact[n], invfact[k]), invfact[n - k]); } int P(int n, int k) { return mul(fact[n], invfact[n - k]); } int n, a[MAXN], dp[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); first(); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } dp[n] = 1; for (int i = n - 1; i >= 0; i--) { if (a[i] <= 0 || a[i] + i - 1 > n - 1) { continue; } for (int j = n - 1; j >= a[i] + i; j--) { dp[i] = add(dp[i], mul(dp[j + 1], C(j - i, a[i]))); } } int ans = 0; for (int i = 0; i < n; i++) { ans = add(ans, dp[i]); } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int* m = new int[n]; int* r = new int[n]; for (int i = 0; i < n; i++) { cin >> m[i]; } for (int i = 0; i < n; i++) { cin >> r[i]; } double total = 0; for (int i = 0; i < 1000000; i++) { for (int j = 0; j < n; j++) { if (i % m[j] == r[j]) { total++; break; } } } double percentage = total / 1000000; cout << fixed << setprecision(6) << percentage << endl; } |
#include <bits/stdc++.h> using namespace std; int dx[] = {1, 1, -1, -1}; int dy[] = {1, -1, -1, 1}; long long int n, k, d1, d2, rem; bool _solve(long long int w1, long long int w2, long long int w3) { long long int tar = n / 3; if (w1 < 0 || w2 < 0 || w3 < 0) return false; if (tar >= w1 && tar >= w2 && tar >= w3 && abs(w1 - w2) == d1 && abs(w2 - w3) == d2) return true; return false; } bool solve() { long long int _d1, w1, w2, w3, _d2; for (int i = 0; i < 4; i++) { _d1 = d1 * dx[i]; _d2 = d2 * dy[i]; w1 = (k + 2 * _d1 + _d2) / 3; w2 = w1 - _d1; w3 = w2 - _d2; if (w1 + w2 + w3 != k) continue; if (_solve(w1, w2, w3)) return true; } return false; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 == 0) { if (solve()) cout << yes << endl; else cout << no << endl; } else cout << no << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1.01e9; const double eps = 1e-9; const int N = 1010; char s[N][N]; char res[N * N]; int resc = 0; char ch[4] = { D , L , R , U }; int dx[4] = {1, 0, 0, -1}; int dy[4] = {0, -1, 1, 0}; pair<int, int> q[N * N]; int st = 0, en = 0; int d[N][N]; int n, m; void bfs(int x, int y) { for (int i = 0; (i) < (n); ++i) for (int j = 0; (j) < (m); ++j) d[i][j] = inf; d[x][y] = 0; q[en++] = {x, y}; while (st < en) { x = q[st].first; y = q[st].second; st++; for (int i = 0; (i) < (4); ++i) { int xx = x + dx[i]; int yy = y + dy[i]; if (0 <= xx && xx < n && 0 <= yy && yy < m && s[xx][yy] == . && d[xx][yy] == inf) { d[xx][yy] = d[x][y] + 1; q[en++] = {xx, yy}; } } } } int main() { int k; scanf( %d%d%d , &n, &m, &k); if (k & 1) { printf( IMPOSSIBLE n ); return 0; } for (int i = 0; (i) < (n); ++i) scanf( %s , s[i]); int px = -1, py = -1; for (int i = 0; (i) < (n); ++i) for (int j = 0; (j) < (m); ++j) if (s[i][j] == X ) { px = i; py = j; s[i][j] = . ; } bfs(px, py); int x = px, y = py; for (int it = 0; (it) < (k); ++it) { int ok = 0; for (int i = 0; (i) < (4); ++i) { int xx = x + dx[i]; int yy = y + dy[i]; if (0 <= xx && xx < n && 0 <= yy && yy < m && d[xx][yy] <= k - it - 1) { res[resc++] = ch[i]; x = xx; y = yy; ok = 1; break; } } if (!ok) { printf( IMPOSSIBLE n ); return 0; } } printf( %s n , res); return 0; } |
#include <bits/stdc++.h> using namespace std; char s[100010], a[110], b[110]; int pa[110], pb[110]; int main() { scanf( %s%s%s , s, a, b); int i, j; j = -1; pa[0] = -1; int lena = strlen(a); for (i = 1; i < lena; i++) { while (j > -1 && a[j + 1] != a[i]) j = pa[j]; if (a[j + 1] == a[i]) j++; pa[i] = j; } j = -1; pb[0] = -1; int lenb = strlen(b); for (i = 1; i < lenb; i++) { while (j > -1 && b[j + 1] != b[i]) j = pb[j]; if (b[j + 1] == b[i]) j++; pb[i] = j; } int len = strlen(s); int minn = len + 1; j = -1; for (i = 0; i < len; i++) { while (j > -1 && a[j + 1] != s[i]) j = pa[j]; if (a[j + 1] == s[i]) j++; if (j == lena - 1) { minn = i; break; } } bool flag1 = false, flag2 = false; j = -1; for (i = minn + 1; i < len; i++) { while (j > -1 && b[j + 1] != s[i]) j = pb[j]; if (b[j + 1] == s[i]) j++; if (j == lenb - 1) { flag1 = true; break; } } for (i = 0; i < len / 2; i++) swap(s[i], s[len - i - 1]); j = -1; minn = len; for (i = 0; i < len; i++) { while (j > -1 && a[j + 1] != s[i]) j = pa[j]; if (a[j + 1] == s[i]) j++; if (j == lena - 1) { minn = i; break; } } j = -1; for (i = minn + 1; i < len; i++) { while (j > -1 && b[j + 1] != s[i]) j = pb[j]; if (b[j + 1] == s[i]) j++; if (j == lenb - 1) { flag2 = true; break; } } if (flag1 && flag2) printf( both n ); else if (flag1) printf( forward n ); else if (flag2) printf( backward n ); else printf( fantasy n ); return 0; } |
#include <bits/stdc++.h> using namespace std; long long N, a[100010], s[100100], cnt[100010]; void update(long long *t, long long pos, long long val) { for (; pos <= N; pos += pos & (-pos)) t[pos] += val; } long long query(long long *t, long long r) { long long res = 0; for (; r > 0; r -= r & (-r)) res += t[r]; return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N; int i; long long nr, sum; long double res = 0; for (i = 1; i <= N; i++) { cin >> a[i]; update(cnt, a[i], 1); update(s, a[i], i); } for (i = 1; i <= N; i++) { nr = query(cnt, a[i] - 1); sum = query(s, a[i] - 1); res += nr - (1.0 * i * (nr * (N + 1) - sum)) / (N * (N + 1)); nr = N - i - nr; sum = query(s, N) - sum - i; res += (1.0 * i * (nr * (N + 1) - sum)) / (N * (N + 1)); update(cnt, a[i], -1); update(s, a[i], -i); } cout << fixed << setprecision(15) << res << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int res[111]; int a[111][111]; bool used[111]; ; bool used_j[111]; int v[111]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < m; i++) { for (int w = 0; w < 111; w++) { v[w] = 0; } for (int j = 0; j < n; j++) { if (!used[a[j][i]] && !used_j[j]) v[a[j][i]]++; else if (!used_j[j] && used[a[j][i]]) { if (res[j] == 0) res[j] = i + 1; used_j[j] = 1; } } for (int j = 0; j < n; j++) { if (v[a[j][i]] > 1 && a[j][i] != 0) { if (res[j] == 0) res[j] = i + 1; used[a[j][i]] = 1; used_j[j] = 1; } } } for (int i = 0; i < n; i++) { cout << res[i] << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 1e6 + 10; const long long INF = 1e18; const long double EPS = 1e-12; int a[300005]; map<int, int> mi; int l_, r_; long long pre[300005]; int b[300005]; int main() { int n; scanf( %d , &n); for (int i = (int)1; i < (int)n + 1; i++) { scanf( %d , &a[i]); } for (int i = (int)1; i < (int)n + 1; i++) { if (mi[a[i]] == 0) mi[a[i]] = i; } for (int i = (int)1; i < (int)n + 1; i++) { pre[i] = pre[i - 1]; if (a[i] >= 0) pre[i] += (long long)(a[i]); } long long ans = -(long long)(1e12); l_ = 0; r_ = 0; for (int i = n; i >= 1; i--) { int x = a[i]; int l = mi[x]; if (i == mi[x]) continue; long long net = pre[i - 1] - pre[l]; net += 2LL * (long long)(x); if (net > ans) { ans = net; l_ = l; r_ = i; } } int cnt = 0; for (int i = 0; i < (int)n + 1; i++) b[i] = 0; for (int i = (int)1; i < (int)n + 1; i++) { if (i == l_ || i == r_) { b[i] = 1; continue; } if (l_ < i && i < r_ && a[i] > 0) { b[i] = 1; } else { b[i] = 0; cnt++; } } cout << ans << << cnt << endl; for (int i = (int)1; i < (int)n + 1; i++) { if (b[i] == 0) { printf( %d , i); } } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; inline int Read() { int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == - ) f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 0 ; return x * f; } const int MAXN = 300005; int n, m, a[MAXN], f[MAXN], g[MAXN]; inline bool Chk(int d) { f[0] = g[0] = 1; for (int i = 1, j = 1; i <= n; i++) { while (a[i] - a[j] > d) j++; if (i >= m && g[i - m] - (j == 1 ? 0 : g[j - 2]) > 0) f[i] = 1; else f[i] = 0; g[i] = g[i - 1] + f[i]; } return f[n]; } int main() { n = Read(), m = Read(); for (int i = 1; i <= n; i++) a[i] = Read(); sort(a + 1, a + n + 1); int l = 0, r = a[n], ret = 0; while (l <= r) { int mid = l + r >> 1; if (Chk(mid)) ret = mid, r = mid - 1; else l = mid + 1; } return printf( %d n , ret), 0; } |
#include <bits/stdc++.h> using namespace std; int t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { int m; int n; cin >> m >> n; vector<int> a[500]; vector<int> b[500]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int temp; cin >> temp; a[i].push_back(temp); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int temp; cin >> temp; b[i].push_back(temp); } } pair<int, int> place; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[0][0] == b[i][j]) { place = make_pair(i, j); } } } map<int, int> rows; for (int i = 0; i < m; i++) { rows.insert({a[i][0], i}); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << a[rows[b[place.first][i]]][j] << ; } cout << n ; } } } |
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; auto solve = []() { int n; cin >> n; vector<pair<int, int>> a(n); for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].first--; } vector<int> ans(n + n); vector<int> used(n + n); for (int i = 0; i < n; i++) { ans[i * 2] = a[i].first; a[i].second = i * 2; used[a[i].first] = 1; } for (int i = 1; i < n + n; i += 2) { int x = ans[i - 1]; int j; for (j = x + 1; j < n + n && used[j]; j++) ; if (j == n + n) { cout << -1 << n ; return 0; } used[j] = 1; ans[i] = j; } for (int i = 0; i < n + n; i++) { if (i > 0) cout << ; cout << ans[i] + 1; } cout << n ; }; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; long long int res(long long int x) { long long int ans = 0; if (x > 0) ans = (x * (x - 1)) / 2; return ans; } int main() { long int n; long long int d; cin >> n >> d; vector<long long int> vect; long long int x; for (long int i = 0; i < n; i++) { cin >> x; vect.push_back(x); } vector<long long int>::iterator it, it2; long long int ans = 0; for (long int i = 2; i < n; i++) { it = lower_bound(vect.begin(), vect.end(), vect[i] - d); if (it != vect.end()) { ans = ans + res(i - (it - vect.begin())); } } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } int Set(int N, int pos) { return (N | (1 << pos)); } long long BigMod(long long B, long long P, long long M) { long long R = 1; while (P > 0) { if (P % 2 == 1) { R = (R * B) % M; } P /= 2; B = (B * B) % M; } return R; } int n, m; vector<pair<double, int> > v; int dp[5005][5005][2]; inline int call(int pos, int got, int stat) { if (got > m) return 10000; if (pos == n) { if (got == m) return 0; else return 10000; } if (dp[pos][got][stat] != -1) return dp[pos][got][stat]; int ret = 0; int g = 0; if (v[pos].second != got and stat == 0) g = 1; ret = min(call(pos + 1, got, 0), call(pos, got + 1, 1)); return dp[pos][got][stat] = ret + g; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); memset(dp, -1, sizeof(dp)); cin >> n >> m; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i].second >> v[i].first; } sort(v.begin(), v.end()); int mx = 10000; for (int i = 0; i < m; i++) { mx = min(mx, call(0, i + 1, 0)); } cout << mx; } |
#include <bits/stdc++.h> using namespace std; char bd[1100][1100]; int n, m; int rt[1100][1100]; int down[1100][1100]; int cnt[1100][1100]; int check(int width, int height, int x, int y) { int filled = 0; bool first = true; while (true) { int godown = down[x][y] - height; if (godown < 0) break; if (cnt[x][y] - cnt[x][y + width] - cnt[x + down[x][y]][y] + cnt[x + down[x][y]][y + width] == width * down[x][y]) { if (first) { filled += width * down[x][y]; first = false; } else { filled += height + width * godown; } } else break; if ((y + 1) + width <= m) { y++; x += godown; } else break; } return filled; } int main() { scanf( %d %d , &n, &m); for (int i = 0; i < n; i++) scanf( %s , bd[i]); for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { if (bd[i][j] == . ) { rt[i][j] = down[i][j] = 0; } else { rt[i][j] = 1 + rt[i][j + 1]; down[i][j] = 1 + down[i + 1][j]; } } } int tlx = -1; int tly = -1; int tot = 0; for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { if (bd[i][j] == X ) { tlx = i; tly = j; tot++; } cnt[i][j] = (bd[i][j] == X ) + cnt[i + 1][j] + cnt[i][j + 1] - cnt[i + 1][j + 1]; } } int best = 1000000000; for (int height = 1; height <= down[tlx][tly]; height++) { int width = rt[tlx][tly]; int filled = check(width, height, tlx, tly); if (filled == tot) best = min(best, width * height); } for (int width = 1; width <= rt[tlx][tly]; width++) { int height = down[tlx][tly]; int filled = check(width, height, tlx, tly); if (filled == tot) best = min(best, width * height); } printf( %d n , best == 1000000000 ? -1 : best); } |
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const int N = 100100; char a[N], b[N]; int n, m, k, p[27]; int Par(int i) { return p[i] = (i == p[i]) ? i : Par(p[i]); } bool same(int i, int j) { return Par(i) == Par(j); } int comp[27]; void mrg(int i, int j) { if (same(i, j)) return; i = Par(i); j = Par(j); p[i] = p[j]; comp[i] += comp[j]; } vector<pair<char, char> > v; int main() { scanf( %d , &n); scanf( %s , a); scanf( %s , b); for (int i = 0; i < 26; i++) { p[i] = i; comp[i] = 1; } for (int i = 0; i < n; i++) { if (same(b[i] - a , a[i] - a )) continue; mrg(b[i] - a , a[i] - a ); v.push_back({a[i], b[i]}); } printf( %d n , v.size()); for (auto x : v) printf( %c %c n , x.first, x.second); return 0; } |
#include <bits/stdc++.h> using namespace std; double pi = acos(-1); struct point { int x, y; } p[2005]; vector<double> v; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d%d , &p[i].x, &p[i].y); long long ans = 0; for (int i = 1; i <= n; i++) { v.clear(); for (int j = 1; j <= n; j++) if (j != i) v.push_back(atan2(p[j].y - p[i].y, p[j].x - p[i].x)); sort(v.begin(), v.end()); int k = 0; int now = 0; for (int j = 0; j < v.size(); j++) { while (v[k] + now * pi < v[j] + pi) { k++; if (k >= v.size()) k = 0, now += 2; } int w = (j - k >= 0 ? j - k : k - j - 1); ans += 1ll * w * (w - 1) / 2 * (n - w - 2) * (n - w - 3) / 2; } } printf( %lld n , ans / 2); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 6, MAX = 2e5 + 5, OO = 1000000009; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long x, long long y) { return (x * y / gcd(x, y)); } long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); } int a, b, aa, bb, ans; void Check(int p) { while (a % p == 0) { a /= p; aa++; } while (b % p == 0) { b /= p; bb++; } ans += abs(aa - bb); aa = bb = 0; } int main() { scanf( %d %d , &a, &b); Check(2); Check(3); Check(5); return printf( %d , a == b ? ans : -1), 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; const int INF = 2000 * 1000 * 1000; const double EPS = 1e-9; const double pi = acos(-1.0); const int maxn = 100010; inline long long sqr(int n) { return n * 1ll * n; } struct Point { int x, y; Point() {} inline bool operator<(const Point& p) const { return y < p.y; } inline long long dist_to(const Point& p) const { return sqr(x - p.x) + sqr(y - p.y); } }; Point a[maxn]; int n, x, sum; long long minans = INF * 1ll * INF; inline void update(const Point& p1, const Point& p2) { minans = min(minans, p1.dist_to(p2)); } inline void go(int l, int r) { if (r - l <= 3) { for (int i = l; i <= r; i++) for (int j = i + 1; j <= r; j++) update(a[i], a[j]); sort(a + l, a + r + 1); return; } int mid = (l + r) >> 1; int midx = a[mid].x; int buf_size = 0; go(l, mid - 1); go(mid, r); static Point buf[maxn]; merge(a + l, a + mid, a + mid, a + r + 1, buf); copy(buf, buf + (r - l + 1), a + l); for (int i = l; i <= r; i++) { if (sqr(abs(a[i].x - midx)) < minans) { for (int j = buf_size - 1; j >= 0 && sqr(a[i].y - buf[j].y) < minans; j--) update(a[i], buf[j]); buf[buf_size++] = a[i]; } } } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &x); sum += x; a[i].x = i + 1; a[i].y = sum; } go(0, n - 1); printf( %I64d , minans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m; cin >> n >> m; vector<long long> col(m); for (long long i = 0; i < m; i++) { cin >> col[i]; } vector<long long> suf(m, 0); suf[m - 1] = 0; for (long long i = m - 2; i >= 0; i--) { suf[i] = suf[i + 1] + col[i + 1]; } vector<long long> ans; for (long long i = 0, cur = 0, r = 0; i < m; i++) { ans.push_back(cur); r = cur + col[i]; if (r > n) { cout << -1 << endl; return 0; } cur++; if (suf[i] < n - r) { cout << -1 << endl; return 0; } else if (suf[i] < n - cur) { long long pos = n - suf[i]; for (long long j = i + 1; j < m; j++) { ans.push_back(pos); pos += col[j]; } break; } } for (long long i = 0; i < m; i++) { cout << ans[i] + 1 << ; } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int iinf = 1 << 29; const long long llinf = 1ll << 60; const double PI = 3.14159265; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long mrand(long long B) { return (unsigned long long)rng() % B; } long long cross(pair<long long, long long> p1, pair<long long, long long> p2) { return p1.first * p2.second - p2.first * p1.second; } bool online(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { pair<long long, long long> p1{b.first - a.first, b.second - a.second}; pair<long long, long long> p2{c.first - a.first, c.second - a.second}; return cross(p1, p2) == 0; } void z_func(vector<int> &z, vector<int> &s, int n) { int L = 0, R = 0; for (int i = 1; i < n; i++) { if (i > R) { L = R = i; while (R < n && s[R - L] == s[R]) R++; z[i] = R - L; R--; } else { int k = i - L; if (z[k] < R - i + 1) z[i] = z[k]; else { L = i; while (R < n && s[R - L] == s[R]) R++; z[i] = R - L; R--; } } } } const int MXN = 400; long long ans; int n, m; int v[MXN][MXN], up[MXN][MXN], down[MXN][MXN], leftt[MXN][MXN], rightt[MXN][MXN], ul[MXN][MXN], ur[MXN][MXN], lol[MXN][MXN], lr[MXN][MXN]; void populate() { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (!v[i][j]) continue; if (i && v[i - 1][j]) up[i][j] = 1 + up[i - 1][j]; if (j && v[i][j - 1]) leftt[i][j] = 1 + leftt[i][j - 1]; if (i && j && v[i - 1][j - 1]) ul[i][j] = 1 + ul[i - 1][j - 1]; if (i && j != m - 1 && v[i - 1][j + 1]) ur[i][j] = 1 + ur[i - 1][j + 1]; } } for (int i = n - 1; i >= 0; --i) { for (int j = m - 1; j >= 0; --j) { if (!v[i][j]) continue; if (i != n - 1 && v[i + 1][j]) down[i][j] = 1 + down[i + 1][j]; if (j != m - 1 && v[i][j + 1]) rightt[i][j] = 1 + rightt[i][j + 1]; if (i != n - 1 && j != m - 1 && v[i + 1][j + 1]) lr[i][j] = 1 + lr[i + 1][j + 1]; if (i != n - 1 && j && v[i + 1][j - 1]) lol[i][j] = 1 + lol[i + 1][j - 1]; } } } void do_lower_leftt(int x, int y) { for (int i = x + 1, j = y - 1; i < n && j >= 0 && v[i][j]; ++i, --j) { ans += down[x][y] >= 2 * (i - x) && lr[i][j] >= i - x; ans += down[x][y] >= i - x && rightt[i][j] >= y - j; ans += lr[x][y] >= i - x && rightt[i][j] >= 2 * (y - j); } } void do_down(int x, int y) { for (int i = x + 1; i < n && v[i][y]; ++i) { ans += lr[x][y] >= i - x && rightt[i][y] >= i - x; ans += rightt[x][y] >= i - x && ur[i][y] >= i - x; ans += (i - x) % 2 == 0 && lr[x][y] >= (i - x) / 2 && ur[i][y] >= (i - x) / 2; } } void do_lower_rightt(int x, int y) { for (int i = x + 1, j = y + 1; i < n && j < m && v[i][j]; ++i, ++j) { ans += rightt[x][y] >= j - y && up[i][j] >= i - x; ans += rightt[x][y] >= 2 * (j - y) && ur[i][j] >= i - x; } } void work() { cin >> n >> m; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { char c; cin >> c; while (c != 0 && c != 1 ) cin >> c; v[i][j] = c == 0 ? 1 : 0; } populate(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (!v[i][j]) continue; do_lower_leftt(i, j); do_down(i, j); do_lower_rightt(i, j); } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); work(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, x, sum = 0; long long int c[100009]; scanf( %lld %lld , &n, &x); for (i = 0; i < n; i++) { scanf( %lld , &c[i]); } sort(c, c + n); for (i = 0; i < n; i++) { sum = sum + c[i] * x; x--; if (x == 0) x++; } printf( %lld n , sum); } |
#include <bits/stdc++.h> using namespace std; const long long inf = 2147383647; const double pi = 2 * acos(0.0); const double eps = 1e-9; struct debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << ; return *this; } } dbg; inline long long gcd(long long a, long long b) { a = ((a) < 0 ? -(a) : (a)); b = ((b) < 0 ? -(b) : (b)); while (b) { a = a % b; swap(a, b); } return a; } int ext_gcd(int a, int b, int& x, int& y) { if (a == 0) { x = 0; y = 1; return b; } int x1, y1; int d = ext_gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } inline long long modInv(int a, int m) { int x, y; ext_gcd(a, m, x, y); if (x < 0) x += m; return x; } inline long long power(long long a, long long p) { long long res = 1, x = a; while (p) { if (p & 1) res = (res * x); x = (x * x); p >>= 1; } return res; } inline long long bigmod(long long a, long long p, long long m) { long long res = 1, x = a % m; while (p) { if (p & 1) res = (res * x) % m; x = (x * x) % m; p >>= 1; } return res; } bool ispalin(string s) { string s2 = s; reverse(s2.begin(), s2.end()); if (s == s2) return true; else return false; } int main() { string s, t, x; cin >> s; int len = s.size(); for (int i = (0); i <= (len); ++i) { for (int j = ( a ); j <= ( z ); ++j) { t = s; x = j; t.insert(i, x); if (ispalin(t)) { cout << t << endl; return 0; } } } cout << NA << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int f[4040][4040], n, k, ans; struct info { int be, ti; } e[4040]; int main() { scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) scanf( %d%d , &e[i].be, &e[i].ti); e[n + 1].be = 86401; if (n == k) { printf( 86400 ); return 0; } f[1][0] = e[1].be + e[1].ti - 1; for (int i = 2; i <= n; i++) { f[i][0] = max(e[i].be + e[i].ti - 1, f[i - 1][0] + e[i].ti); for (int j = 1; j <= min(i, k); j++) { if (i - 1 >= j) f[i][j] = min(max(f[i - 1][j] + e[i].ti, e[i].be + e[i].ti - 1), f[i - 1][j - 1]); else f[i][j] = min(e[i].be + e[i].ti - 1, f[i - 1][j - 1]); } } for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { ans = max(ans, e[min(n + 1, k - j + i + 1)].be - f[i][j] - 1); } } printf( %d , ans); } |
#include <bits/stdc++.h> using namespace std; inline long long fmm(long long a, long long b, long long m = 1000000007) { long long r = 0; a %= m; b %= m; while (b > 0) { if (b & 1) { r += a; r %= m; } a += a; a %= m; b >>= 1; } return r % m; } inline long long fme(long long a, long long b, long long m = 1000000007) { long long r = 1; a %= m; while (b > 0) { if (b & 1) { r *= a; r %= m; } a *= a; a %= m; b >>= 1; } return r % m; } inline long long sfme(long long a, long long b, long long m = 1000000007) { long long r = 1; a %= m; while (b > 0) { if (b & 1) r = fmm(r, a, m); a = fmm(a, a, m); b >>= 1; } return r % m; } std::vector<long long> primes; long long primsiz; std::vector<long long> fact; std::vector<long long> invfact; inline void sieve(long long n) { long long i, j; std::vector<bool> a(n); a[0] = true; a[1] = true; for (i = 2; i * i < n; ++i) { if (!a[i]) { for (j = i * i; j < n; j += i) { a[j] = true; } } } for (i = 2; i < n; ++i) if (!a[i]) primes.push_back(i); primsiz = primes.size(); } inline void sieve() { long long n = 1010000, i, j, k = 0; std::vector<bool> a(n); primes.resize(79252); a[0] = a[1] = true; for (i = 2; (j = (i << 1)) < n; ++i) a[j] = true; for (i = 3; i * i < n; i += 2) { if (!a[i]) { k = (i << 1); for (j = i * i; j < n; j += k) a[j] = true; } } k = 0; for (i = 2; i < n; ++i) if (!a[i]) primes[k++] = i; primsiz = k; } inline bool isPrimeSmall(unsigned long long n) { if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return false; for (unsigned long long k = 1; 36 * k * k - 12 * k < n; ++k) if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return false; return true; } bool _p(unsigned long long a, unsigned long long n) { unsigned long long t, u, i, p, c = 0; u = n / 2, t = 1; while (!(u & 1)) { u /= 2; ++t; } p = fme(a, u, n); for (i = 1; i <= t; ++i) { c = (p * p) % n; if ((c == 1) && (p != 1) && (p != n - 1)) return 1; p = c; } if (c != 1) return 1; return 0; } inline bool isPrime(unsigned long long n) { if (((!(n & 1)) && n != 2) || (n < 2) || (n % 3 == 0 && n != 3)) return 0; if (n < 1373653) { for (unsigned long long k = 1; (((36 * k * k) - (12 * k)) < n); ++k) if ((n % (6 * k + 1) == 0) || (n % (6 * k - 1) == 0)) return 0; return 1; } if (n < 9080191) { if (_p(31, n) || _p(73, n)) return 0; return 1; } if (_p(2, n) || _p(7, n) || _p(61, n)) return 0; return 1; } unsigned long long nCk(long long n, long long k, unsigned long long m = 1000000007) { if (k < 0 || k > n || n < 0) return 0; if (k == 0 || k == n) return 1; if (fact.size() >= (unsigned long long)n && isPrime(m)) { return (((fact[n] * invfact[k]) % m) * invfact[n - k]) % m; } unsigned long long i = 0, j = 0, a = 1; k = ((k) < (n - k) ? (k) : (n - k)); for (; i < (unsigned long long)k; ++i) { a = (a * (n - i)) % m; while (j < (unsigned long long)k && (a % (j + 1) == 0)) { a = a / (j + 1); ++j; } } while (j < (unsigned long long)k) { a = a / (j + 1); ++j; } return a % m; } void nCkInit(unsigned long long m = 1000000007) { long long i, mx = 1010000; fact.resize(mx + 1); invfact.resize(mx + 1); fact[0] = 1; for (i = 1; i <= mx; ++i) { fact[i] = (i * fact[i - 1]) % m; } invfact[mx] = fme(fact[mx], m - 2, m); for (i = mx - 1; i >= 0; --i) { invfact[i] = (invfact[i + 1] * (i + 1)) % m; } } template <class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } void extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1, y = 0; return; } long long x1, y1; extGCD(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; } inline void get(long long &x) { int n = 0; x = 0; char c = getchar_unlocked(); if (c == - ) n = 1; while (c < 0 || c > 9 ) { c = getchar_unlocked(); if (c == - ) n = 1; } while (c >= 0 && c <= 9 ) { x = (x << 3) + (x << 1) + c - 0 ; c = getchar_unlocked(); } if (n) x = -x; } inline int get(char *p) { char c = getchar_unlocked(); int i = 0; while (c != n && c != 0 && c != && c != r && c != EOF) { p[i++] = c; c = getchar_unlocked(); } p[i] = 0 ; return i; } inline void put(long long a) { int n = (a < 0 ? 1 : 0); if (n) a = -a; char b[20]; int i = 0; do { b[i++] = a % 10 + 0 ; a /= 10; } while (a); if (n) putchar_unlocked( - ); i--; while (i >= 0) putchar_unlocked(b[i--]); putchar_unlocked( ); } inline void putln(long long a) { int n = (a < 0 ? 1 : 0); if (n) a = -a; char b[20]; int i = 0; do { b[i++] = a % 10 + 0 ; a /= 10; } while (a); if (n) putchar_unlocked( - ); i--; while (i >= 0) putchar_unlocked(b[i--]); putchar_unlocked( n ); } const int K = 3; std::vector<std::vector<long long> > mul(std::vector<std::vector<long long> > a, std::vector<std::vector<long long> > b, unsigned long long m = 1000000007) { std::vector<std::vector<long long> > c(K, std::vector<long long>(K)); for (int ii = 0; ii < (K); ++ii) for (int jj = 0; jj < (K); ++jj) for (int kk = 0; kk < (K); ++kk) c[ii][jj] = (c[ii][jj] + a[ii][kk] * b[kk][jj]) % m; return c; } std::vector<std::vector<long long> > fme(std::vector<std::vector<long long> > a, unsigned long long n, unsigned long long m = 1000000007) { if (n == 1) return a; if (n & 1) return mul(a, fme(a, n - 1, m), m); std::vector<std::vector<long long> > x = fme(a, n / 2, m); return mul(x, x, m); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); long long t = 0, n = 0, m = 0, maxx = 0, minn = 0, curr = 0, k = 0, num = 0, siz = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0, ind = 0; long long root = 0, sum = 0, diff = 0, q = 0, choice = 0, d = 0, len = 0, begg = 0, endd = 0, pos = 0, cnt = 0, lo = 0, hi = 0, mid = 0, ans = 0; bool flag = false; std::string s1, s2, s3, str; char ch, ch1, ch2, ch3, *ptr; double dub = 0; cin >> n1 >> n2; root = 1; while (1) { if (pos == 0) { if (n1 < root) break; n1 -= root; } else { if (n2 < root) break; n2 -= root; } ++root; pos = 1 - pos; } if (pos == 0) cout << Vladik ; else cout << Valera ; return 0; } |
#include <bits/stdc++.h> using namespace std; long long t = 1; void solve() { long long n, x, y, cnt = 0, p = 1; cin >> n; vector<long long> arr(n + 1), vis(n + 1, 0); vector<vector<long long> > v(n + 1); for (long long i = 0; i < n - 1; i += 1) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } queue<long long> q1, q; for (long long i = 0; i < n; i += 1) { cin >> x; arr[i] = x; q1.push(x); } q.push(1); q1.pop(); while (!q.empty()) { x = q.front(); vector<long long> vv; set<long long> s; cnt = 0; for (long long i = 0; i < int((v[x]).size()); i += 1) { if (vis[v[x][i]] == 0) { cnt++; vv.push_back(v[x][i]); } } vis[q.front()] = 1; q.pop(); for (long long i = p; i < p + cnt; i += 1) { s.insert(arr[i]); q.push(arr[i]); vis[arr[i]] = 1; } for (long long i = 0; i < int((vv).size()); i += 1) { if (((s).find(vv[i]) != (s).end())) { s.erase(vv[i]); } else { cout << No ; return; } } p = p + cnt; } cout << Yes ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; pair<int, int> point[100]; double ang[100]; double dp[10000000]; int i, j, k, N, M, K, l, r; void clear() {} void init() { scanf( %d%d%d , &N, &l, &r); for (i = 0; i <= N - 1; i++) { int first; scanf( %d%d%d , &point[i].first, &point[i].second, &first); ang[i] = (first * 1.0) * 3.1415926535 / 180.0; } } double func(int a, int h, double ang, double st) { double alpha = atan(fabs(a - st) / h); if (a > st) { double beta = alpha - ang; if (beta > 0) { double q = h * tan(beta); return min(1.0 * r, a - q); } else { beta *= -1; return min(1.0 * r, a + h * tan(beta)); } } else { if (alpha + ang > 3.1415926535 / 2) { return 1.0 * r; } else { double beta = alpha + ang; return min(1.0 * r, h * tan(beta) + a); } } } void calc() { for (i = 0; i <= (1 << N) - 1; i++) { dp[i] = l * 1.0; } for (i = 0; i <= (1 << N) - 1; i++) { for (j = 0; j <= N - 1; j++) { if (!(i & (1 << j))) dp[i | (1 << j)] = max(dp[i | (1 << j)], func(point[j].first, point[j].second, ang[j], dp[i])); } } printf( %.8f n , dp[(1 << N) - 1] - l); } int main() { clear(); init(); calc(); } |
#include <bits/stdc++.h> using namespace std; int vis[100005], n, m, x, y, mi, eg, re; char t; vector<vector<pair<int, int> > > adj; vector<vector<int> > re1; vector<int> t0, t1; void dfs(int u, int ty) { if (vis[u] == -1) { vis[u] = ty; if (ty == 0) t0.push_back(u); else t1.push_back(u); for (int j = 0; j < adj[u].size(); j++) { pair<int, int> te = adj[u][j]; if (te.second == eg) dfs(te.first, ty); else dfs(te.first, 1 - ty); } } else if (vis[u] != ty) { mi = -1; return; } } int main() { cin >> n >> m; adj.assign(n + 1, vector<pair<int, int> >(0)); re1.assign(2, vector<int>(0)); for (int i = 0; i < m; i++) { cin >> x >> y >> t; adj[x].push_back(pair<int, int>(y, t == B )); adj[y].push_back(pair<int, int>(x, t == B )); } memset(vis, -1, sizeof(vis)); mi = 0; eg = 1; for (int i = 1; i <= n; i++) { t0.clear(); t1.clear(); if (vis[i] == -1) dfs(i, 0); if (mi == -1) break; if (t0.size() < t1.size()) { mi += t0.size(); for (int j = 0; j < t0.size(); j++) re1[0].push_back(t0[j]); } else { mi += t1.size(); for (int j = 0; j < t1.size(); j++) re1[0].push_back(t1[j]); } } re = mi; memset(vis, -1, sizeof(vis)); mi = 0; eg = 0; for (int i = 1; i <= n; i++) { t0.clear(); t1.clear(); if (vis[i] == -1) dfs(i, 0); if (mi == -1) break; if (t0.size() < t1.size()) { mi += t0.size(); for (int j = 0; j < t0.size(); j++) re1[1].push_back(t0[j]); } else { mi += t1.size(); for (int j = 0; j < t1.size(); j++) re1[1].push_back(t1[j]); } } if (mi == -1) { if (re == -1) cout << -1 << endl; else { cout << re << endl; for (int i = 0; i < re1[0].size(); i++) cout << re1[0][i] << ; cout << endl; } } else if (re == -1) { cout << mi << endl; for (int i = 0; i < re1[1].size(); i++) cout << re1[1][i] << ; cout << endl; } else { if (mi < re) { cout << mi << endl; for (int i = 0; i < re1[1].size(); i++) cout << re1[1][i] << ; cout << endl; } else { cout << re << endl; for (int i = 0; i < re1[0].size(); i++) cout << re1[0][i] << ; cout << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int hashCh = 31; struct HashCh { string code; int number; }; long long getHash(string text) { long long hash = 0; int size = text.size(); for (int i = 0; i < size; i++) { int ch = static_cast<int>(text[i]); hash += pow(hashCh, i) * ch; } return hash; } int main() { vector<string> password; map<long long, HashCh> numbers; char buf[80]; string oneNumber; for (int i = 0; i < 80; i++) { char ch; cin >> ch; oneNumber += ch; if ((i % 10) == 9) { password.push_back(oneNumber); oneNumber = ; } } for (int i = 0; i < 10; i++) { string number; cin >> number; HashCh ch = HashCh(); ch.code = number; ch.number = i; long long hash = getHash(number); numbers[hash] = ch; } vector<int> result; for (int i = 0; i < 8; i++) { auto it = numbers.find(getHash(password[i])); if (it != numbers.end() && it->second.code == password[i]) { result.push_back(it->second.number); } } for (auto i = result.begin(); i != result.end(); i++) { cout << *i; } return 0; } |
#include <bits/stdc++.h> using namespace std; struct fastIO { fastIO() { ios_base::sync_with_stdio(0); } }; const int MAXN = 1e6 * 2; long long n, a[MAXN], x, ans = MAXN, b; int main() { fastIO(); cin >> n; for (long long i = 0; i < n; i++) cin >> a[i]; for (x = 0; x < n; x++) { b = 0; for (long long i = 0; i < n; i++) { b += (abs(x - i) + i + x) * a[i] * 2; } if (0) cerr << ans << endl; ans = min(ans, b); } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k, x; cin >> n >> k; char s; string str; int p = n / k, q = n % k; for (int i = 0; i < k; ++i) { x = i + 97; s = x; if (i == k - 1) p = p + q; for (int j = 0; j < p; ++j) str += s; } cout << str << endl; } } |
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=405; const int P=998244353; ll dp[2][maxn][maxn][3][3],sum[maxn][maxn]; int cur,las,n,c[26]; void add(ll &dpp,ll add) { add%=P; while(add<0) add+=P; dpp+=add; while(dpp>=P) dpp-=P; } int main() { scanf( %d ,&n); for(int i=0;i<26;++i) scanf( %d ,&c[i]); dp[0][0][0][0][0]=1; las=1;cur=0; for(int i=1;i<=n;++i) { swap(las,cur); for(int j=0;j<=i;++j) for(int k=0;k<=i;++k) for(int l=0;l<3;++l) for(int m=0;m<3;++m) dp[cur][j][k][l][m]=0; for(int j=0;j<=i;++j) for(int k=0;k<=i;++k) for(int l=0;l<3;++l) for(int m=0;m<3;++m) { if(!dp[las][j][k][l][m])continue; add(dp[cur][j][k][m][0],1LL*dp[las][j][k][l][m]*((l||(i<=2))?24:23)%P); if(l!=1) add(dp[cur][j+1][k][m][1],dp[las][j][k][l][m]); if(l!=2) add(dp[cur][j][k+1][m][2],dp[las][j][k][l][m]); } } for(int i=0;i<=n;++i) for(int j=0;j<=n;++j) for(int k=0;k<3;++k) for(int kk=0;kk<3;++kk) add(sum[i][j],dp[cur][i][j][k][kk]); for(int i=n;i>=0;--i) for(int j=n;j>=0;--j) add(sum[i][j],sum[i+1][j]+sum[i][j+1]-sum[i+1][j+1]); ll res=1; for(int i=1;i<=n;++i) res=res*((i<=2)?26:25)%P; for(int i=0;i<26;++i) add(res,-sum[c[i]+1][0]); for(int i=0;i<26;++i) for(int j=i+1;j<26;++j) add(res,sum[c[i]+1][c[j]+1]); res+=P; res%=P; printf( %lld n ,res); } |
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { a = abs(a), b = abs(b); if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } vector<vector<int> > adj; set<int> st; const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; long long n, ary[300000 + 5] = {4, 8, 15, 16, 23, 42}, dp[300000 + 5]; int main(void) { ios::sync_with_stdio(false), cin.tie(NULL); cin >> n; for (int i = 0, j, x; i < n; ++i) { cin >> x; j = lower_bound(ary, ary + 6, x) - ary; if (j == 0) dp[0] += 1; else if (dp[j - 1]) { dp[j - 1] -= 1; dp[j] += 1; } } cout << n - dp[5] * 6; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> v[100001]; int n, cost[100001]; int gr, nod, gr2; void DFS(int x, int t, int val) { for (int i = 1; i <= v[x][0]; i++) { if (v[x][i] != t) { if (val == 0) { DFS(v[x][i], x, cost[v[x][i]]); } else { if (val != cost[v[x][i]]) { gr++; if (nod != x) gr2++; nod = x; } DFS(v[x][i], x, cost[v[x][i]]); } } } } int main() { int a, b, i, j; cin >> n; for (i = 1; i <= n; i++) { v[i].push_back(0); } for (i = 1; i < n; i++) { cin >> a >> b; v[a][0]++; v[b][0]++; v[a].push_back(b); v[b].push_back(a); } for (i = 1; i <= n; i++) cin >> cost[i]; DFS(1, 0, 0); if (gr2 <= 2) { if (gr == 0) { cout << YES << n << 1; } else { gr = 0; DFS(nod, 0, 0); if (gr != 0) { cout << NO ; } else { cout << YES ; cout << n << nod; } } } else { cout << NO ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int Ans[1001], A[1001], B[1001], C[1001], D[1001], E[1001]; int value(int Arr[], int i, int j, int k) { return (Arr[j] - Arr[i]) * (Arr[k] - Arr[i]); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i] >> B[i] >> C[i] >> D[i] >> E[i]; } int index = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j != i) { for (int k = 0; k < n; k++) { if (k != i && k != j) { if ((value(A, i, j, k) + value(B, i, j, k) + value(C, i, j, k) + value(D, i, j, k) + value(E, i, j, k)) > 0) { Ans[i] = -1; break; } } } } } } int count = 0; for (int i = 0; i < n; i++) { if (Ans[i] != -1) { count++; } } cout << count << endl; for (int i = 0; i < n; i++) { if (Ans[i] != -1) { cout << i + 1 << endl; count++; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> a(N); ; for (int& x : a) cin >> x; int ans = (a[0] == 1); for (int i = 1; i < N; ++i) { if (a[i] == 0) continue; int j = i; while (j < N && a[j] == 1) ++j; ans += (j - i) / 3; i = j - 1; } cout << ans << n ; } } |
#include <bits/stdc++.h> using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; int uplg(int n) { return 32 - __builtin_clz(n); } int uplg(ll n) { return 64 - __builtin_clzll(n); } void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); run(); cout << flush; _Exit(0); } ll ch2(ll n) { return n * (n - 1) / 2; } ll ch3(ll n) { return n * (n - 1) * (n - 2) / 6; } void run() { int n; double p; cin >> n >> p; constexpr ll SCALE = 10000; ll pScaled = llround(p * SCALE); ll low = pScaled * ch3(n) * 2; for (int a = (0); a < (n + 1); a++) { ll cur = (ch3(a) * 2 + ch2(a) * (n - a) * 2 + a * ch2(n - a)) * SCALE; if (cur >= low) { cout << a << n ; return; } } assert(0); } |
#include <bits/stdc++.h> using namespace std; int main() { string q[41]; long long n, p, i, count = 1, check = 1; cin >> n >> p; for (i = 0; i < n; i++) cin >> q[i]; for (i = n - 2; i >= 0; i--) { if (q[i] == halfplus ) { count = count * 2 + 1; check++; } else count = count * 2; } cout << p * count - (p / 2) * check << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; void solve(); const int N = 10000005; long long i, j, n, s; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); cout << n ; } return 0; } void solve() { cin >> n; for (i = 1; i <= n; i = i * 10) { s += n - i + 1; } cout << s; } |
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; int main() { long long dp[3001][100][2] = {0}; long long n, l; cin >> n >> l; vector<long long> a(n), b(n); for (long long i = 0; i < n; ++i) { cin >> a[i] >> b[i]; dp[a[i]][i][0] = 1; dp[b[i]][i][1] = 1; } for (long long i = 1; i <= l; ++i) { for (long long j = 0; j < n; ++j) { for (long long k = 0; k < n; ++k) { if (j != k) { if (a[j] == b[k] && i - a[j] >= 0) { dp[i][j][0] = (dp[i][j][0] + dp[i - a[j]][k][0]) % mod; } if (a[j] == a[k] && a[k] != b[k] && i - a[j] >= 0) { dp[i][j][0] = (dp[i][j][0] + dp[i - a[j]][k][1]) % mod; } if (b[j] == b[k] && a[j] != b[j] && i - b[j] >= 0) { dp[i][j][1] = (dp[i][j][1] + dp[i - b[j]][k][0]) % mod; } if (b[j] == a[k] && a[j] != b[j] && a[k] != b[k] && i - b[j] >= 0) { dp[i][j][1] = (dp[i][j][1] + dp[i - b[j]][k][1]) % mod; } } } } } long long res = 0; for (long long i = 0; i < n; ++i) { res = (res + dp[l][i][0]) % mod; if (a[i] != b[i]) res = (res + dp[l][i][1]) % mod; } cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int par[100005], val[100005]; int lefm[100005], rim[100005]; int root = 0; vector<vector<int> > adjl; int n; void pre(int node) { if (adjl[node].size() == 0) { lefm[node] = val[node]; rim[node] = val[node]; return; } for (int i = (0); i < (adjl[node].size()); ++i) { pre(adjl[node][i]); if (val[adjl[node][i]] < val[node]) lefm[node] = lefm[adjl[node][i]]; else rim[node] = rim[adjl[node][i]]; } } double res[100005]; pair<int, int> qu[100005]; void query(int node, int left, int right, long long cost, int dep) { if (adjl[node].size() == 0) { for (int i = (left); i < (right); ++i) res[qu[i].second] += cost * 1. / dep; return; } int ind = lower_bound(qu + left, qu + right, make_pair(val[node], 0)) - qu; if (ind > left && adjl[node].size() == 2) query(adjl[node][0], left, ind, cost + lefm[adjl[node][1]], dep + 1); if (ind < right && adjl[node].size() == 2) query(adjl[node][1], ind, right, cost + rim[adjl[node][0]], dep + 1); } int main() { int x, y, k; scanf( %d , &n); adjl.clear(); adjl.resize(n); for (int i = (0); i < (n); ++i) { scanf( %d %d , &x, &y); x--; val[i] = y; par[i] = x; if (x >= 0) adjl[x].push_back(i); if (x < 0) root = i; } for (int i = (0); i < (adjl.size()); ++i) { if (adjl[i].size() == 2) if (val[adjl[i][0]] > val[adjl[i][1]]) swap(adjl[i][0], adjl[i][1]); if (adjl[i].size() == 1) throw 2; } pre(root); scanf( %d , &k); for (int i = (0); i < (k); ++i) scanf( %d , &qu[i].first), qu[i].second = i; sort(qu, qu + k); query(root, 0, k, 0, 0); for (int i = (0); i < (k); ++i) printf( %.12lf n , res[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; long double judge1(long long k, long long d, long long t) { long double ans; long double unit = k + (d - k) * 1.0 / 2; ans = floor(t / unit) * d; long double res = t * 1.0 - floor(t / unit) * unit; if (res <= k) { ans += res; return ans; } else { ans += k; res -= k; } ans += (res * 2); return ans; } long double judge2(long long k, long long d, long long t) { long double ans; ans = t; return ans; } long double judge3(long long k, long long d, long long t) { long double ans; long double unitt = (k / d + 1) * d * 1.0; long double unitw = k + (unitt - k) / 2.0; ans += floor(t / unitw) * unitt; long double res = t - floor(t / unitw) * unitw; if (fabs(res - k) <= 1e-9 || res < k) { ans += res; return ans; } else { ans += k; res -= k; } ans += (res * 2.0); return ans; } int main() { ios::sync_with_stdio(false); long long k, d, t; while (cin >> k >> d >> t) { long double ans; if (k < d) ans = judge1(k, d, t); else if (k % d == 0) ans = judge2(k, d, t); else ans = judge3(k, d, t); cout << fixed << setprecision(1) << ans << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<bool> pref(1 << n); for (int i = 0; i < k; i++) { int x; cin >> x; pref[x - 1] = true; } const int oo = -1; vector<vector<vector<int>>> dp( 4, vector<vector<int>>(n + 1, vector<int>(1 << n, oo))); for (int i = 0; i < (1 << n); i += 2) { if (pref[i] && pref[i + 1]) dp[3][1][i] = 1; else if (pref[i] || pref[i + 1]) dp[1][1][i] = dp[2][1][i] = 1; else dp[0][1][i] = 0; } for (int i = 2; i <= n; i++) for (int j = 0; j < (1 << n); j += (1 << i)) for (int p = 0; p < 4; p++) for (int q = 0; q < 4; q++) if (dp[p][i - 1][j] != -1 && dp[q][i - 1][j + (1 << (i - 1))] != -1) { int t = dp[p][i - 1][j] + dp[q][i - 1][j + (1 << (i - 1))]; int upper = ((p & 2) > 0) + ((q & 2) > 0); int lower = ((p & 1) + (q & 1)); if (upper == 2) { t += (lower > 0) + 2; dp[3][i][j] = max(dp[3][i][j], t); if (lower < 2) dp[2][i][j] = max(dp[2][i][j], t); } else if (upper == 1) { t += (lower > 0) + 1; if (lower == 0) { dp[2][i][j] = max(dp[2][i][j], t); dp[1][i][j] = max(dp[1][i][j], t + 1); dp[0][i][j] = max(dp[0][i][j], t + 1); } if (lower == 1) { dp[3][i][j] = max(dp[3][i][j], t + 1); dp[2][i][j] = max(dp[2][i][j], t + 1); dp[1][i][j] = max(dp[1][i][j], t + 1); dp[0][i][j] = max(dp[0][i][j], t + 1); } if (lower == 2) { dp[3][i][j] = max(dp[3][i][j], t + 1); dp[2][i][j] = max(dp[2][i][j], t + 1); dp[1][i][j] = max(dp[1][i][j], t + 1); } } else if (upper == 0) { t += (lower > 0); if (lower == 0) dp[0][i][j] = max(dp[0][i][j], t); if (lower == 1) { dp[1][i][j] = max(dp[1][i][j], t + 1); dp[0][i][j] = max(dp[0][i][j], t + 1); } if (lower == 2) { dp[1][i][j] = max(dp[1][i][j], t + 1); dp[0][i][j] = max(dp[0][i][j], t + 1); } } } int ans = 0; for (int p = 0; p < 4; p++) ans = max(dp[p][n][0] + (p > 0), ans); cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int n; int l[2005], r[2005]; long long k, w[2005]; long long s[2005][2005]; long long f[2005]; long long h[2005]; long long up(long long x) { return (x / k + (x % k > 0)); } int main() { cin >> n >> k; for (int i = (1); i <= (n); i++) cin >> l[i] >> r[i] >> w[i]; for (int i = (1); i <= (n); i++) h[i] = -1; for (int i = (1); i <= (n); i++) for (int j = (1); j <= (n); j++) s[i][j] = -1; for (int i = (1); i <= (n); i++) { long long start = k; long long has = 0; for (int j = (i); j <= (n); j++) { if (start >= w[j]) { if (l[j] < l[j + 1]) s[i][j] = has + start; has += w[j]; start -= w[j]; if (j == n) h[i] = has; } else { long long t = up(w[j] - start) + 1; if (t > r[j] - l[j] + 1) break; if (l[j] + t - 1 < l[j + 1]) s[i][j] = has + (t - 1) * k + start; has += w[j]; if (j == n) h[i] = has; start = start + (t - 1) * k - w[j]; } } } for (int i = (1); i <= (n - 1); i++) { f[i] = -1; for (int j = (i - 1); j >= (0); j--) { if (s[j + 1][i] >= 0 && f[j] >= 0) if (f[i] < 0 || f[j] + s[j + 1][i] < f[i]) f[i] = f[j] + s[j + 1][i]; } } long long ans = -1; for (int i = (0); i <= (n - 1); i++) if (f[i] >= 0 && h[i + 1] >= 0) { if (ans < 0 || f[i] + h[i + 1] < ans) ans = f[i] + h[i + 1]; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 310; int n, a[maxn]; int main() { scanf( %d , &n); for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]); for (int i = (1); i <= (n - 1); ++i) { for (int j = (1); j <= (a[i]); ++j) printf( PRL ); printf( R ); } for (int j = (1); j <= (a[n]); ++j) printf( LRP ); } |
#include <bits/stdc++.h> using namespace std; long long n, h, ok; long long cal(long long dep, long long pos, int tag) { if (ok) return 0; if (dep == 0) { ok = 1; return 0; } long long tmp; if (!tag) { if (pos > (1LL << (dep - 1))) tmp = (1LL << dep) + cal(dep - 1, pos - (1LL << (dep - 1)), 0); else tmp = 1LL + cal(dep - 1, pos, 1); } else { if (pos > (1LL << (dep - 1))) tmp = 1LL + cal(dep - 1, pos - (1LL << (dep - 1)), 0); else tmp = (1LL << dep) + cal(dep - 1, pos, 1); } return tmp; } int main() { while (cin >> h >> n) { ok = 0; cout << cal(h, n, 0) << endl; } } |
#include <bits/stdc++.h> long long a[100][100]; using namespace std; struct DynamicDisjointSetDS { std::unordered_map<int, int> parent, degree; int N; DynamicDisjointSetDS(int n) { N = n; } int getParent(int vertex) { if (parent.find(vertex) != parent.end()) { if (parent[vertex] != vertex) { parent[vertex] = getParent(parent[vertex]); return parent[vertex]; } } else { parent.insert(make_pair(vertex, vertex)); degree.insert(make_pair(vertex, 1)); } return vertex; } void Union(int vertexA, int vertexB) { int x = getParent(vertexA); int y = getParent(vertexB); if (x == y) return; if (degree[x] > degree[y]) { parent[y] = x; degree[x] = degree[x] + degree[y]; } else { parent[x] = y; degree[y] = degree[y] + degree[x]; } } int GetTotalComponent() { unordered_set<int> total; for (auto itr = parent.begin(); itr != parent.end(); itr++) { total.insert(getParent(itr->first)); } return N - parent.size() + total.size(); } }; int main() { using namespace std; long long n, m, q; cin >> n >> m >> q; unordered_set<long long> s, s2; for (long long i = 1; i <= n; i++) { s.insert(i); } for (long long i = 1; i <= m; i++) { s2.insert(i); } DynamicDisjointSetDS ds(m + 1); vector<vector<long long>> v(m + 1); while (q--) { long long r, c; cin >> r >> c; v[c].push_back(r); s.erase(r); s2.erase(c); } for (vector<long long> v2 : v) { for (long long i = 1; i < v2.size(); i++) { ds.Union(v2[i], v2[i - 1]); } } unordered_set<long long> sc; for (long long i = 1; i <= n; i++) { sc.insert(ds.getParent(i)); } cout << (s2.size() + sc.size() - 1) << endl; } |
#include <bits/stdc++.h> using namespace std; int read() { int f = 1, x = 0; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = -f; c = getchar(); } while (c >= 0 && c <= 9 ) x = x * 10 + c - 0 , c = getchar(); return f * x; } inline int lowbit(int x) { return x & (-x); } inline int modadd(int x, int y) { return (x + y >= 1000000007 ? x + y - 1000000007 : x + y); } inline int sgn(int x) { return (x < 0 ? -1 : (x > 0 ? 1 : 0)); } template <typename T> T gcd(T a, T b) { return (!b) ? a : gcd(b, a % b); } int poww(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = 1ll * res * a % 1000000007; a = 1ll * a * a % 1000000007, b >>= 1; } return res; } const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; const int ddx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, ddy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; int n, a[100005]; void init() { n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); } bool check1() { long long tot = 0; for (int i = n - 1; i >= 1; --i) { if (a[i] < tot) return false; if (a[i] > a[i + 1]) tot += a[i] - a[i + 1]; } return true; } bool check2() { long long tot = 0; for (int i = 2; i <= n; ++i) { if (a[i] < tot) return false; if (a[i] > a[i - 1]) tot += a[i] - a[i - 1]; } return true; } void solve() { if (check1()) printf( YES n ); else if (check2()) printf( YES n ); else printf( NO n ); } int main() { int T = read(); while (T--) { init(); solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; struct grp { long long to, wt, edgeCode; char type; }; struct Node { long long x, dis; }; struct comp { inline bool operator()(const Node& n1, const Node& n2) const { if (n1.dis != n2.dis) return n1.dis < n2.dis; return n1.x < n2.x; } }; void dijkstra(long long n, vector<vector<grp> >& edges, vector<long long>& dis, vector<bool>& visited, vector<pair<long long, long long> >& par) { set<Node, comp> myset; for (long long i = 1; i <= n; ++i) { Node node; node.x = i, node.dis = dis[i]; myset.insert(node); } while (myset.size()) { set<Node>::iterator it = myset.begin(); Node node = *it; myset.erase(it); visited[node.x] = true; for (long long i = 0; i < edges[node.x].size(); ++i) { grp& edge = edges[node.x][i]; if (visited[edge.to]) { continue; } long long newDis = dis[node.x] + edge.wt; if (newDis < dis[edge.to] || (newDis == dis[edge.to] && edge.type == r )) { Node prevNode, newNode; prevNode.x = edge.to, prevNode.dis = dis[edge.to]; newNode.x = edge.to, newNode.dis = newDis; dis[edge.to] = newDis; par[edge.to] = {node.x, edge.edgeCode}; myset.erase(myset.find(prevNode)); myset.insert(newNode); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m, k; cin >> n >> m >> k; vector<vector<grp> > edges(n + 1); long long edgeCode = 0; for (long long i = 0; i < m; i++) { long long x, y, w; cin >> x >> y >> w; grp g; g.to = y, g.wt = w, g.type = r , g.edgeCode = edgeCode++; edges[x].push_back(g); g.to = x, g.edgeCode = edgeCode++; edges[y].push_back(g); } vector<pair<long long, long long> > trainPaths(k); for (long long i = 0; i < k; ++i) { long long y, w; cin >> y >> w; trainPaths[i] = {y, edgeCode}; grp g; g.to = y, g.wt = w, g.type = t , g.edgeCode = edgeCode++; edges[1].push_back(g); } vector<long long> dis(n + 1, 100000000000000000); vector<bool> visited(n + 1, false); vector<pair<long long, long long> > par(n + 1, {-1, -1}); dis[1] = 0; dijkstra(n, edges, dis, visited, par); long long ans = 0; for (long long i = 0; i < k; ++i) { long long dest = trainPaths[i].first, edgeCode = trainPaths[i].second; if (par[dest].first == 1 && par[dest].second == edgeCode) { continue; } ++ans; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> long long int power(long long int x, long long int b) { long long int p = 1; while (b > 0) { if (b & 1) { p = p * x; p %= 1000000007; } b >>= 1; x *= x; x %= 1000000007; } return p % 1000000007; } using namespace std; struct lex_compare { bool operator()( const pair<long long int, pair<long long int, long long int> > p1, const pair<long long int, pair<long long int, long long int> > p2) { return (p1.first == p2.first) ? p1.second.first < p2.second.first : p1.first > p2.first; } }; long long int n, m, b, mo; long long int dp[2][500 + 1][500 + 1]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int w, m; cin >> w >> m; bool f = 1; map<long long int, long long int> coeff; long long int term = 1; while (m >= term && term <= 10000000000) { long long int x = (term * (m / term) - (w * term) * (m / (w * term))); x /= term; if (x <= 1) { term *= w; } else { if (x != (w - 1)) { f = 0; break; } else { m += (w * term - term); } term *= w; } } if (f) cout << YES << n ; else cout << NO << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> di[100003]; struct trie { trie *zero, *one; int val; trie() { zero = one = NULL; val = 100003; } } * root[100003]; void add(trie *cur, int a) { int i, ck; for (i = 20; i >= 0; i--) { ck = (1 << i & a); if (ck == 0) { cur->val = min(cur->val, a); if (cur->zero == NULL) cur->zero = new trie(); cur = cur->zero; cur->val = min(cur->val, a); } else { cur->val = min(cur->val, a); if (cur->one == NULL) cur->one = new trie(); cur = cur->one; cur->val = min(cur->val, a); } } } int query(trie *cur, int a, int lim) { int i, ck, ans; for (i = 20; i >= 0; i--) { ck = (1 << i & a); if (ck == 0) { if (cur->one != NULL && cur->one->val + a <= lim) cur = cur->one; else if (cur->zero != NULL) cur = cur->zero; ans = cur->val; } else { if (cur->zero != NULL && cur->zero->val + a <= lim) cur = cur->zero; else if (cur->one != NULL) cur = cur->one; ans = cur->val; } } return ans; } void pre() { for (int i = 1; i < 100003; i++) { for (int j = i; j < 100003; j += i) di[j].push_back(i); root[i] = new trie(); } } int main() { pre(); int q, a, k, s, v, ty; scanf( %d , &q); while (q--) { scanf( %d , &ty); if (ty == 1) { scanf( %d , &a); for (int x : di[a]) { add(root[x], a); } } else { scanf( %d %d %d , &v, &k, &s); if (root[k]->val + v > s || root[k]->val == 0 || v % k) printf( -1 n ); else printf( %d n , query(root[k], v, s)); } } return 0; } |
#include <bits/stdc++.h> using namespace std; bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; vector<long long int> v; for (long long int i = 0; i < n; i++) { long long int x; cin >> x; v.push_back(x); } if (v[0] + v[1] <= v[n - 1]) { cout << 1 << << 2 << << n << n ; } else { cout << -1 << n ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; char bb[111][111]; int in[111][111]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) scanf( %s , bb[i]); vector<pair<pair<int, int>, int>> stars; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) { if (bb[i][j] != * ) continue; int l, r, u, d; l = r = i; u = d = j; for (int k = i; k < n; k++) if (bb[k][j] == * ) r = k; else break; for (int k = i; k >= 0; k--) if (bb[k][j] == * ) l = k; else break; for (int k = j; k < m; k++) if (bb[i][k] == * ) d = k; else break; for (int k = j; k >= 0; k--) if (bb[i][k] == * ) u = k; else break; int starLevel = min(min(r - i, i - l), min(d - j, j - u)); if (starLevel) { for (int k = i - starLevel; k <= i + starLevel; k++) in[k][j] = 1; for (int k = j - starLevel; k <= j + starLevel; k++) in[i][k] = 1; stars.push_back({{i + 1, j + 1}, starLevel}); } } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if ((bb[i][j] == * && in[i][j] == 0) || (in[i][j] == 1 && bb[i][j] == . )) return puts( -1 ); cout << stars.size() << endl; for (auto u : stars) { printf( %d %d %d n , u.first.first, u.first.second, u.second); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 500; const int LN = 21; const long long mod = 1e9 + 7; const long long INF = 1LL << 57; long long n, m, u, v, k, t, q, a, x, d; int tree[4 * N]; int sum[4 * N]; int lim = 1e6 + 2; int arr[N], brr[N]; bool print = false; void modify(int i, int l, int r, int index, int val) { if (l == r) { tree[i] += val; sum[i] = tree[i]; return; } int mid = (l + r) / 2; if (index <= mid) modify(i + 1 + i, l, mid, index, val); else modify(i + 2 + i, mid + 1, r, index, val); tree[i] = max(tree[i + 2 + i], tree[i + 1 + i] + sum[i + 2 + i]); sum[i] = sum[i + 1 + i] + sum[i + 2 + i]; } int find(int i, int l, int r, int sumx) { if (l == r) return l; int mid = (l + r) / 2; if (tree[i + 2 + i] + sumx > 0) { return find(i + 2 + i, mid + 1, r, sumx); } else return find(i + 1 + i, l, mid, sumx + sum[i + 2 + i]); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> arr[i]; } for (int i = 1; i <= m; ++i) cin >> brr[i]; for (int i = 1; i <= n; ++i) { modify(0, 0, lim, arr[i], 1); } for (int i = 1; i <= m; ++i) { modify(0, 0, lim, brr[i], -1); } cin >> q; while (q--) { int type; cin >> type; if (type == 1) { int i, x; cin >> i >> x; modify(0, 0, lim, arr[i], -1); modify(0, 0, lim, x, 1); arr[i] = x; int price = find(0, 0, lim, 0); if (price == 0) cout << -1 << n ; else cout << price << n ; } else { int i, x; cin >> i >> x; modify(0, 0, lim, brr[i], 1); modify(0, 0, lim, x, -1); brr[i] = x; int price = find(0, 0, lim, 0); if (price == 0) cout << -1 << n ; else cout << price << n ; } } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, d; cin >> n >> k >> d; long long a[101], b[101]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); a[0] = 1; long long sum = 0; for (int i = 0; i < k; i++) { sum += a[i]; sum = (sum) % 1000000007; a[i + 1] = sum; } for (int i = k; i < n; i++) { sum += a[i]; sum -= a[i - k]; sum = ((sum % 1000000007) + 1000000007) % 1000000007; a[i + 1] = sum; } for (int i = 1; i < d; i++) { b[i] = 0; } b[d] = 1; for (int i = d + 1; i <= k; i++) { b[i] = b[i - 1] + b[i - 1] - b[i - d] + a[i - d]; b[i] = ((b[i] % 1000000007) + 1000000007) % 1000000007; } for (int i = k + 1; i <= n; i++) { b[i] = b[i - 1] + b[i - 1] - b[i - d] + a[i - d] - a[i - k - 1]; b[i] = ((b[i] % 1000000007) + 1000000007) % 1000000007; } cout << b[n] << endl; } |
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &a) { T x = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } a = x * f; } template <typename T, typename... Args> void read(T &t, Args &...args) { read(t); read(args...); } long long n, k, a[100005]; struct node { long long len, k; bool operator<(const node &x) const { long long tmp1 = (len / k) * (len / k) * (k - len % k) + (len / k + 1) * (len / k + 1) * (len % k); long long tmp2 = (x.len / x.k) * (x.len / x.k) * (x.k - x.len % x.k) + (x.len / x.k + 1) * (x.len / x.k + 1) * (x.len % x.k); long long kk = k + 1; long long tmpp1 = (len / kk) * (len / kk) * (kk - len % kk) + (len / kk + 1) * (len / kk + 1) * (len % kk); kk = x.k + 1; long long tmpp2 = (x.len / kk) * (x.len / kk) * (kk - x.len % kk) + (x.len / kk + 1) * (x.len / kk + 1) * (x.len % kk); tmp1 -= tmpp1; tmp2 -= tmpp2; if (k == len) tmp1 = 0; if (x.k == x.len) tmp2 = 0; return tmp1 < tmp2; } }; priority_queue<node> q; void update(long long &ans, node &x) { ans -= (x.len / x.k) * (x.len / x.k) * (x.k - x.len % x.k) + (x.len / x.k + 1) * (x.len / x.k + 1) * (x.len % x.k); x.k++; ans += (x.len / x.k) * (x.len / x.k) * (x.k - x.len % x.k) + (x.len / x.k + 1) * (x.len / x.k + 1) * (x.len % x.k); } signed main() { read(n, k); long long ans = 0; for (long long i = 1; i <= n; i++) { read(a[i]); node tmp; tmp.len = a[i]; tmp.k = 1; q.push(tmp); } for (long long i = 1; i <= n; i++) ans += a[i] * a[i]; k -= n; while (k) { k += 19260817; node x = q.top(); q.pop(); update(ans, x); q.push(x); k -= 19260818; } printf( %lld n , ans + O - O ); return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, l, v1, v2, k; long double dn, dl, dv1, dv2, dk; long long times; long double x; double T; int main() { scanf( %lld%lld%lld%lld%lld , &n, &l, &v1, &v2, &k); dn = n; dl = l; dv1 = v1; dv2 = v2; dk = k; times = (n - 1) / k + 1; x = l / (v2 * times - (dv2 - dv1) / (dv2 + dv1) * dv2 * (times - 1)); T = x * (times + (dv2 - dv1) / (dv2 + dv1) * (times - 1)); printf( %lf n , T); return 0; } |
#include <bits/stdc++.h> using namespace std; int const N = 3e5; int n, p1[N], p2[N], p3[N], num1[N], num2[N], F[N][2], T[N * 4]; void buildT(int v, int tl, int tr) { if (tl == tr) { T[v] = 1; } else { int tm = (tl + tr) / 2; buildT(v * 2, tl, tm); buildT(v * 2 + 1, tm + 1, tr); T[v] = T[v * 2] + T[v * 2 + 1]; } } void updT(int v, int tl, int tr, int p) { if (tl == tr) { T[v] = 0; } else { int tm = (tl + tr) / 2; if (tm >= p) updT(v * 2, tl, tm, p); else updT(v * 2 + 1, tm + 1, tr, p); T[v] = T[v * 2] + T[v * 2 + 1]; } } int getT(int v, int tl, int tr, int k) { if (tl == tr) { return tl; } else { int tm = (tl + tr) / 2; if (T[v * 2] >= k) return getT(v * 2, tl, tm, k); else return getT(v * 2 + 1, tm + 1, tr, k - T[v * 2]); } } void upd(int x, int t) { while (x < n) { ++F[x][t]; x |= (x + 1); } } int get(int x, int t) { int r = 0; while (x >= 0) { r += F[x][t]; x = (x & (x + 1)) - 1; } return r; } int main() { scanf( %d , &n); for (int i = 0; i < n; ++i) scanf( %d , &p1[i]); for (int i = 0; i < n; ++i) scanf( %d , &p2[i]); for (int i = 0; i < n; ++i) { num1[i] = p1[i] - get(p1[i], 0); upd(p1[i] + 1, 0); num2[i] = p2[i] - get(p2[i], 1); upd(p2[i] + 1, 1); } for (int i = n - 1; i >= 0; --i) { num1[i] += num2[i]; if (i) num1[i - 1] += num1[i] / (n - i); num1[i] %= (n - i); } buildT(1, 0, n - 1); for (int i = 0; i < n; ++i) { int need = num1[i] + 1; int val = getT(1, 0, n - 1, need); p3[i] = val; updT(1, 0, n - 1, val); } for (int i = 0; i < n; ++i) printf( %d , p3[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<long long> v; long long x, y, z, a, b, c, sum, ans, total, t, cnt, n, m, k, p, kase; string s1, s2; map<long long, long long> mp; set<long long> s; map<long long, long long>::iterator itr1, ptr1; set<long long>::iterator itr, ptr; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { long long x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; ans = abs(x1 - x2); ans *= abs(y1 - y2); cout << ans + 1 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; long long n, sum, n1, pick, ans, t, a[N], b[N], f[N], f1[N], m, t1; int main() { scanf( %lld , &n); for (int i = 1; i <= 8; i++) { scanf( %lld , &a[i]); sum = sum + a[i] * i; } if (sum <= n) return printf( %lld n , sum), 0; n1 = std::max((long long)0, n - 840), n = n - n1; for (int i = 1; i <= 8; i++) { if (a[i] < n1 / i) pick = a[i]; else pick = n1 / i; if (a[i] - pick < 10) pick = std::max((long long)0, a[i] - 10); ans = ans + pick * i, n1 = n1 - pick * i, a[i] = a[i] - pick; } n += n1; for (int i = 1; i <= 8; i++) { t = 1; while (a[i] > 0) { t1 = std::min(t, a[i]); a[i] = a[i] - t1; f[++m] = t1 * i; t <<= 1; } } for (long long i = 1; i <= m; i++) for (long long j = n; j >= f[i]; j--) f1[j] = std::max(f1[j], f1[j - f[i]] + f[i]); printf( %lld n , ans + f1[n]); return 0; } |
#include <bits/stdc++.h> using namespace std; string st[5050]; int fr[27]; int main() { memset(fr, 0, sizeof(fr)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> k >> n; for (int i = 0; i < k; i++) { cin >> st[i]; } string res = st[0]; memset(fr, 0, sizeof(fr)); bool du = false; for (int i = 0; i < n; i++) { fr[res[i] - a ]++; if (fr[res[i] - a ] > 1) { du = true; } } int ma = 0; int mi = 0; bool po = true; for (int i = 1; i < k; i++) { int df = 0; int ct[27] = {0}; for (int j = 0; j < n; j++) { ct[st[i][j] - a ]++; if (st[i][j] != res[j]) { df++; } } if (df > 4) { po = false; break; } for (int j = 0; j < 27; j++) { if (ct[j] != fr[j]) { po = false; break; } } if (df > ma) { ma = df; mi = i; } } if (!po) { cout << -1 << n ; return 0; } if (ma == 0) { swap(res[0], res[1]); cout << res << n ; return 0; } if (ma == 2) { if (!du) { cout << -1 << n ; return 0; cout << -1 << n ; return 0; } cout << res << n ; return 0; } int q[4]; int ctr = 0; for (int i = 0; i < n; i++) { if (res[i] != st[mi][i]) { q[ctr++] = i; } } po = false; int tr[2] = {0, 0}; for (int id = 0; id < 3; id++) { if (ma < 4) { q[3] = q[id]; } else if (id > 0) break; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (po) break; int md = 0; swap(res[q[i]], res[q[j]]); for (int c = 1; c < k; c++) { int df = 0; for (int l = 0; l < n; l++) { if (res[l] != st[c][l]) { df++; } } md = max(md, df); } swap(res[q[i]], res[q[j]]); if (md == 2) { po = true; tr[0] = i; tr[1] = j; } } } } if (po) { swap(res[q[tr[0]]], res[q[tr[1]]]); cout << res << n ; } else { cout << -1 << n ; } } |
#include <bits/stdc++.h> using namespace std; int a[5]; int main() { scanf( %d%d%d%d , &a[0], &a[1], &a[2], &a[3]); while (a[0] != 1 || a[1] != 1 || a[2] != 1 || a[3] != 1) { bool f = 1; for (int i = 0; i < 4; i++) if (a[i] % 2 == 0 && a[(i + 1) % 4] % 2 == 0) { printf( /%d n , i + 1); a[i] >>= 1; a[(i + 1) % 4] >>= 1; f = 0; break; } if (f) { for (int i = 0; i < 4; i++) if (a[i] % 2 == 1 && a[(i + 1) % 4] % 2 == 1 && !(a[i] == 1 && a[(i + 1) % 4] == 1)) { a[i]++; a[(i + 1) % 4]++; printf( +%d n , i + 1); f = 0; break; } if (f) for (int i = 0; i < 4; i++) if (a[i] % 2 == 1 && a[(i + 1) % 4] % 2 == 0 && a[(i + 2) % 4] % 2 == 1) { printf( +%d n+%d n , i + 1, (i + 1) % 4 + 1); a[i]++; a[(i + 2) % 4]++; a[(i + 1) % 4] += 2; break; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; string multiply(string num1, string num2) { int len1 = num1.size(); int len2 = num2.size(); if (len1 == 0 || len2 == 0) return 0 ; vector<int> result(len1 + len2, 0); int i_n1 = 0; int i_n2 = 0; for (int i = len1 - 1; i >= 0; i--) { int carry = 0; int n1 = num1[i] - 0 ; i_n2 = 0; for (int j = len2 - 1; j >= 0; j--) { int n2 = num2[j] - 0 ; int sum = n1 * n2 + result[i_n1 + i_n2] + carry; carry = sum / 10; result[i_n1 + i_n2] = sum % 10; i_n2++; } if (carry > 0) result[i_n1 + i_n2] += carry; i_n1++; } int i = result.size() - 1; while (i >= 0 && result[i] == 0) i--; if (i == -1) return 0 ; string s = ; while (i >= 0) s += std::to_string(result[i--]); return s; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T; cin >> T; while (T--) { long int n; cin >> n; n = n / 2; string n1, n2, n3, ans1, ans; int temp; if (n % 3 == 0) { temp = 4 * n / 3; n1 = to_string(temp); n2 = to_string(n + 1); n3 = to_string(2 * n + 1); ans1 = multiply(n1, n2); ans = multiply(ans1, n3); } else if (n % 3 == 2) { temp = 4 * (n + 1) / 3; n1 = to_string(temp); n2 = to_string(n); n3 = to_string(2 * n + 1); ans1 = multiply(n1, n2); ans = multiply(ans1, n3); } else { temp = 4 * (2 * n + 1) / 3; n1 = to_string(temp); n2 = to_string(n + 1); n3 = to_string(n); ans1 = multiply(n1, n2); ans = multiply(ans1, n3); } cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, x[N], y[N], xs[N], ys[N], cnt[2][2]; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d%d , &x[i], &y[i]); xs[i] = x[i], ys[i] = y[i]; } sort(xs + 1, xs + n + 1); sort(ys + 1, ys + n + 1); int m = (n + 1) / 2; int cx = xs[m], cy = ys[m]; int dx = xs[m + 1] - xs[m], dy = ys[m + 1] - ys[m]; if (n & 1) { dx = min(dx, xs[m] - xs[m - 1]); dy = min(dy, ys[m] - ys[m - 1]); } long long ans = 0; bool flag = 0; for (int i = 1; i <= n; i++) { ans += abs(x[i] - cx) + abs(y[i] - cy); ++cnt[x[i] <= cx][y[i] <= cy]; if (x[i] == cx && y[i] == cy) flag = 1; } if (cnt[0][0] && cnt[0][1] && cnt[1][1] && cnt[1][0]) { if (n % 2 == 0 || flag) ans -= min(dx, dy); } printf( %lld n , ans * 2); } |
#include <bits/stdc++.h> using namespace std; int main() { cout << fixed; double d, L, v1, v2; cin >> d >> L >> v1 >> v2; cout << setprecision(8) << (L - d) / (v1 + v2) << endl; } |
#include <bits/stdc++.h> using namespace std; long long scan_d() { char t = getchar(); long long you = 0; while (t > 9 || t < 0 ) t = getchar(); while (t <= 9 && t >= 0 ) { you *= 10; you += t - 0 ; t = getchar(); } return you; } long long dp[1050][1050]; long long op[1001]; int main() { for (int i = 0; i <= 1010; i++) dp[1][i] = 1; for (int i = 2; i <= 1010; i++) for (int o = i - 1; o <= 1010; o++) { dp[i][o] = dp[i - 1][o - 1] + dp[i][o - 1]; dp[i][o] %= 1000000007; } long long y; y = scan_d(); long long sum = 0; for (int i = 1; i <= y; i++) { op[i] = scan_d(); } long long ans = 1; for (int i = 1; i <= y; i++) { sum += op[i]; ans *= dp[op[i]][sum - 1]; ans %= 1000000007; } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; template <class T> void read(T &x) { int f = 0; x = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) f |= (ch == - ); for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 0 ; if (f) x = -x; } const int N = 20, mod = 998244353; int f[N][N][N][N][2][2]; int g[N][N][N][N][2][2]; int x[N], y[N], n, m; void add(int &x, int y) { x = (x + y >= mod ? x + y - mod : x + y); } int main() { read(n), read(m); for (int i = (1); i <= (n); i++) read(x[i]), read(y[i]); f[0][0][0][0][0][0] = 1; for (; m; m >>= 1) { memset(g, 0, sizeof g); for (int px = 0; px < (N); px++) for (int nx = 0; nx < (N); nx++) for (int py = 0; py < (N); py++) for (int ny = 0; ny < (N); ny++) for (int fx = 0; fx < (2); fx++) for (int fy = 0; fy < (2); fy++) { int t = f[px][nx][py][ny][fx][fy]; if (!t) continue; for (int s = 0; s < (1 << n); s++) { int Px = px, Nx = nx, Py = py, Ny = ny; for (int i = (1); i <= (n); i++) if (s >> (i - 1) & 1) { Px += max(x[i], 0); Nx -= min(x[i], 0); Py += max(y[i], 0); Ny -= min(y[i], 0); } if ((Px & 1) != (Nx & 1) || (Py & 1) != (Ny & 1)) continue; int gx = ((Px & 1) < (m & 1) ? 0 : ((Px & 1) > (m & 1) ? 1 : fx)); int gy = ((Py & 1) < (m & 1) ? 0 : ((Py & 1) > (m & 1) ? 1 : fy)); Px >>= 1, Nx >>= 1, Py >>= 1, Ny >>= 1; add(g[Px][Nx][Py][Ny][gx][gy], t); } } swap(f, g); } cout << ((f[0][0][0][0][0][0] + mod - 1) % mod) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long mod = 1e9 + 7; const long long N = 1e5 + 10; inline long long add(long long x, long long y) { x += y; if (x >= mod) x -= mod; return x; } inline long long mul(long long x, long long y) { x = (1LL * x * y) % mod; return x; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long n; cin >> n; n--; cout << 0 0 << endl; fflush(stdout); if (n == 0) { cout << 1 0 1 1 ; fflush(stdout); return 0; } string prev; cin >> prev; long long l = 0; long long r = 1e9; long long i = 1; while (i <= n) { long long m = (l + r) / 2; cout << m << << 0 << endl; fflush(stdout); string s; cin >> s; if (s == prev) { l = m; } else { r = m; } i++; } if (r - l < 2) { cout << r << 1 << (r + 1) << << (2 * (r + 1) - 2 * l - 1); fflush(stdout); return 0; } long long m = (l + r) / 2; cout << m << 0 << m << 1 ; fflush(stdout); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> c; vector<bool> used; void dfs(int u, vector<vector<int>>& v) { used[u] = 1; for (int i = 0; i < v[u].size(); i++) { if (c[u] == c[v[u][i]] && !used[v[u][i]]) { dfs(v[u][i], v); } } } int main() { int n; cin >> n; vector<vector<int>> v(n); c.resize(n); used.resize(n, 0); vector<int> cnt(110000); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } for (int i = 0; i < n; i++) { cin >> c[i]; } for (int i = 0; i < n; i++) { if (!used[i]) { cnt[c[i]]++; dfs(i, v); } } multiset<int> data; for (int i = 0; i < 100010; i++) { for (int j = 0; j < cnt[i]; j++) { data.insert(i); } } for (int i = 0; i < n; i++) { bool check = 0; multiset<int> tmp; for (int j = 0; j < v[i].size(); j++) { if (c[v[i][j]] == c[i]) { check = 1; } tmp.insert(c[v[i][j]]); } if (!check) { tmp.insert(c[i]); } if (tmp == data) { cout << YES << endl; cout << i + 1 << endl; return 0; } } cout << NO << endl; } |
#include <bits/stdc++.h> using namespace std; void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } long long power(long long a, long long b) { if (b == 0) { return 1; } else { if (b % 2 == 0) { return (a * power((a * a) % 1000000007, b / 2)) % 1000000007; } else { return power((a * a) % 1000000007, b / 2); } } } int main() { fastio(); long long l, r; cin >> l >> r; long long even1 = 0, odd1 = 0; long long p = 0; l--; while (l > 0) { long long t = min((long long)pow(2LL, p), l); if (p % 2 == 0) { odd1 += t; odd1 %= 1000000007; } else { even1 += t; even1 %= 1000000007; } p++; l -= t; } long long even2 = 0, odd2 = 0; p = 0; while (r > 0) { long long t = min((long long)pow(2LL, p), r); if (p % 2 == 0) { odd2 += t; odd2 %= 1000000007; } else { even2 += t; even2 %= 1000000007; } p++; r -= t; } long long sum1 = (even1 * (even1 + 1)) % 1000000007; sum1 = (sum1 + (odd1 * odd1) % 1000000007) % 1000000007; long long sum2 = (even2 * (even2 + 1)) % 1000000007; sum2 = (sum2 + (odd2 * odd2) % 1000000007) % 1000000007; cout << (sum2 - sum1 + 1000000007) % 1000000007; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; struct node { double x, y; } po[1010]; double dis(node a, node b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } node mark; double cha(node a, node b, node c) { return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); } double get(node a, node b, node c) { return abs(cha(a, b, c)) / dis(a, b) / 2.0; } int main() { int n; cin >> n; double ans = 1e9; for (int i = (0); i < (n); ++i) cin >> po[i].x >> po[i].y; for (int i = (0); i < (n); ++i) { ans = min(ans, get(po[(i - 1 + n) % n], po[(i + 1) % n], po[i])); } cout << fixed << setprecision(10) << ans << n ; } |
#include <bits/stdc++.h> using namespace std; long long n, x, c, cnt; vector<long long> v1, v2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; while (n--) { cin >> x; if (x > 0) v1.push_back(x), c += x; else v2.push_back(abs(x)), cnt += abs(x); } if (c > cnt) { cout << first n ; return 0; } if (cnt > c) { cout << second n ; return 0; } bool a = 0; for (int i = 0; i < min(v1.size(), v2.size()); i++) { if (v1[i] > v2[i]) { cout << first n ; return 0; } else if (v1[i] < v2[i]) { cout << second n ; return 0; } } if (x < 0) cout << second << endl; else cout << first << endl; return 0; } |
#include <bits/stdc++.h> long long n, t, ans; short i, j, k; char cnt, l; long long C[43][43]; int main() { scanf( %I64d %I64d , &n, &t); for (i = 0; (1ll << i) < t; ++i) ; if ((1ll << i) ^ t) { puts( 0 ); return 0; } k = i + 1; for (i = 0; i < 43; ++i) C[0][i] = 1ll; for (i = 1; i < 43; ++i) for (j = 1; j <= i; ++j) C[j][i] = C[j - 1][i - 1] + C[j][i - 1]; ++n; for (cnt = i = 0; (1ll << i) <= n; ++i) if ((1ll << i) & n) ++cnt; for (l = i = 0; (1ll << i) <= n; ++i) if (n & (1ll << i)) { ++l; if (k - cnt + l >= 0) ans += C[k - cnt + l][i]; } if (cnt == k) ++ans; if (k == 1) --ans; printf( %I64d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1001; int dp2[MAX_N][MAX_N], dp5[MAX_N][MAX_N]; int n; string findPath(int (*p)[MAX_N]) { int i = n - 1, j = n - 1; string path = ; while (i != 0 && j != 0) { if (p[i - 1][j] > p[i][j - 1]) { path.insert(0, R ); j--; } else { path.insert(0, D ); i--; } } if (i == 0) { while (j != 0) { path.insert(0, R ); j--; } } else { while (i != 0) { path.insert(0, D ); i--; } } return path; } int main() { std::ios::sync_with_stdio(false); cin >> n; int temp1, temp2, zeroI = -1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cin >> temp1; if (!temp1) { zeroI = i; continue; } temp2 = temp1; while (!(temp1 & 1)) { dp2[i][j]++; temp1 >>= 1; } while (!(temp2 % 5)) { dp5[i][j]++; temp2 /= 5; } } for (int i = 1; i < n; i++) { dp2[i][0] += dp2[i - 1][0]; dp5[i][0] += dp5[i - 1][0]; dp2[0][i] += dp2[0][i - 1]; dp5[0][i] += dp5[0][i - 1]; } for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { dp2[i][j] += min(dp2[i - 1][j], dp2[i][j - 1]); dp5[i][j] += min(dp5[i - 1][j], dp5[i][j - 1]); } } int(*p)[MAX_N] = (dp2[n - 1][n - 1] < dp5[n - 1][n - 1]) ? dp2 : dp5; if ((~zeroI) && p[n - 1][n - 1] > 1) { cout << 1 << endl; for (int i = 0; i < zeroI; i++) cout << D << flush; for (int j = 1; j < n; j++) cout << R << flush; for (int i = zeroI + 1; i < n; i++) cout << D << flush; return 0; } else { cout << p[n - 1][n - 1] << endl; cout << findPath(p) << flush; } return 0; } |
#include <bits/stdc++.h> using namespace std; int B, n, m; int li[500008], ri[500008], mx[500008], tag[500008]; short g[480][100008]; int bel[500008], de[500008], ai[500008], pa[500008]; short size[500008]; int find(int x) { if (pa[x] == x) return x; return pa[x] = find(pa[x]); } void link(int x, int y) { if (x == y) return; if (size[x] > size[y]) swap(x, y); pa[x] = y; size[y] += size[x]; } int tot; int lst[500008], stamp[500008]; void makeup(int id) { ++tot; mx[id] = 0; for (int i = li[id]; i <= ri[id]; i++) pa[i] = i, size[i] = 1; for (int i = li[id]; i <= ri[id]; i++) { int x = ai[i]; mx[id] = max(mx[id], x); if (stamp[x] != tot) stamp[x] = tot, g[id][x] = lst[x] = i - li[id]; else link(i, g[id][x] + li[id]), g[id][x] = find(g[id][x] + li[id]) - li[id]; } } void pushdown(int id) { for (int i = li[id]; i <= ri[id]; i++) g[id][ai[i]] = -1; for (int i = li[id]; i <= ri[id]; i++) { g[id][ai[i] + de[i]] = -1; if (find(i) == i) ai[i] = ai[i] + de[i] - tag[id]; de[i] = 0; } for (int i = li[id]; i <= ri[id]; i++) ai[i] = ai[find(i)]; tag[id] = 0; } void refresh(int id) { while (-1 == g[id][mx[id]]) mx[id]--; } int cnt = 0; void subtract(int id, int x) { if (mx[id] - tag[id] <= x) return; if (mx[id] - tag[id] > x * 2 + 1 and 1) { for (int i = tag[id] + 1; i <= x + tag[id]; i++) if (g[id][i] != -1) { int j = i + x; de[g[id][i] + li[id]] += x; if (g[id][j] != -1) link(g[id][i] + li[id], g[id][j] + li[id]), g[id][j] = find(g[id][j] + li[id]) - li[id]; else g[id][j] = g[id][i]; g[id][i] = -1; } tag[id] += x; } else { for (int i = x + 1 + tag[id]; i <= mx[id]; i++) if (g[id][i] != -1) { int j = i - x; de[g[id][i] + li[id]] -= x; if (g[id][j] != -1) link(g[id][j] + li[id], g[id][i] + li[id]), g[id][j] = find(g[id][j] + li[id]) - li[id]; else g[id][j] = g[id][i]; g[id][i] = -1; } refresh(id); } } int ask(int id, int x) { if (x + tag[id] > 100001) return 0; if (g[id][x + tag[id]] == -1) return 0; g[id][x + tag[id]] = find(g[id][x + tag[id]] + li[id]) - li[id]; return size[g[id][x + tag[id]] + li[id]]; } signed main() { memset(g, -1, sizeof g); scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) scanf( %d , &ai[i]); B = sqrt(n); B = max(B, n / 475); memset(li, 0x3f, sizeof li); for (int i = 1; i <= n; i++) { bel[i] = i / B; li[bel[i]] = min(li[bel[i]], i); ri[bel[i]] = i; } for (int i = 0; i <= n / B; i++) makeup(i); while (m--) { int op, l, r, x; scanf( %d%d%d%d , &op, &l, &r, &x); if (op == 1) { if (bel[l] == bel[r]) { pushdown(bel[l]); for (int i = l; i <= r; i++) if (ai[i] > x) ai[i] -= x; makeup(bel[l]); } else { pushdown(bel[l]); for (int i = l; i <= ri[bel[l]]; i++) if (ai[i] > x) ai[i] -= x; pushdown(bel[r]); for (int i = li[bel[r]]; i <= r; i++) if (ai[i] > x) ai[i] -= x; makeup(bel[l]); makeup(bel[r]); for (int i = bel[l] + 1; i < bel[r]; i++) subtract(i, x); } } else { int ans = 0; if (bel[l] == bel[r]) { pushdown(bel[l]); for (int i = l; i <= r; i++) if (ai[i] == x) ans++; makeup(bel[l]); printf( %d n , ans); } else { pushdown(bel[l]); for (int i = l; i <= ri[bel[l]]; i++) ans += (ai[i] == x); pushdown(bel[r]); for (int i = li[bel[r]]; i <= r; i++) ans += (ai[i] == x); makeup(bel[l]); makeup(bel[r]); for (int i = bel[l] + 1; i < bel[r]; i++) ans += ask(i, x); printf( %d n , ans); } } ++cnt; if (cnt < -7) { for (int i = 0; i <= n / B; i++) pushdown(i); for (int i = 1; i <= n; i++) cout << ai[i] << ; cout << g[2][30] << : n n ; for (int i = 0; i <= n / B; i++) makeup(i); } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100010; long long a[N]; int n; bool check(long long x) { long long sum = 0; for (int i = 2; i <= n; ++i) { if (x - a[i] < 0) return false; sum += x - a[i]; } return (sum >= a[1]); } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) scanf( %I64d , &a[i]); sort(a + 1, a + n + 1); long long l = 0, r = (long long)1e14 + 1; while (l + 1 < r) { long long mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } if (check(l)) printf( %I64d , l); else printf( %I64d , r); return 0; } |
#include <bits/stdc++.h> using namespace std; bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } void asquare() { long long n, c = 0, ans = LONG_LONG_MAX; cin >> n; string sn; if (isPowerOfTwo(n) == true) cout << 0 n ; else { long long j = 0, k = 0, nn = 1; string s = to_string(n); for (int i = 0; i <= 60; i++) { nn = (long long)pow(2, i); sn = to_string(nn); c = 0, j = 0, k = 0; bool bb; while (j != s.length() && k != sn.length()) { if (s[j] == sn[k]) { j++; k++; bb = true; } else { j++; c++; bb = false; } } if (bb = true) { if (j == s.length()) { k--; c += sn.length() - k - 1; } else if (k == sn.length()) { j--; c += s.length() - j - 1; } } else { k--; c += sn.length() - k - 1; } if (c == 1) { ans = 1; break; } else ans = min(ans, c); } cout << ans << n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { asquare(); } return 0; } |