solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; template <typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template <typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } const int N = 2e6 + 7; const int M = 1e9 + 7; int n, m, last, ans[N], lex[N]; vector<pair<int, int> > G[N]; bool vis[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; last = n; for (int j = 1; j <= m; j++) { int a, b; cin >> a >> b; vector<int> v; int x = j; while (x > 0) { v.push_back(x % 10); x /= 10; } reverse((v).begin(), (v).end()); int from = a; for (int i = 0; i + 1 < (int)(v).size(); i++) { G[from].push_back({++last, v[i]}); from = last; } G[from].push_back({b, v.back()}); from = b; for (int i = 0; i + 1 < (int)(v).size(); i++) { G[from].push_back({++last, v[i]}); from = last; } G[from].push_back({a, v.back()}); } vector<int> que = {1}; vis[1] = true; while (!que.empty()) { vector<pair<pair<int, int>, pair<int, int> > > vec; for (auto v : que) { for (auto [u, c] : G[v]) { if (!vis[u]) { vec.push_back({{lex[v], c}, {v, u}}); } } } sort((vec).begin(), (vec).end()); que.clear(); pair<int, int> l = {-1, -1}; int no = 0; for (auto [a, b] : vec) { if (a != l) no++; auto [v, u] = b; if (!vis[u]) { vis[u] = true; que.push_back(u); lex[u] = no; ans[u] = (10LL * ans[v] + a.second) % M; } l = a; } } for (int i = 2; i <= n; i++) cout << ans[i] << "\n"; return 0; }
12
CPP
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> ii; int n; int getbit(int s,int k) { return (s>>k)&1; } int main() { ios::sync_with_stdio(0); cin>>n; vector<vector<ii>> g(n); for(int i=0;i<n;i++) { int Ai; cin>>Ai; for(int j=0;j<Ai;j++) { int x,y; cin>>x>>y; x--; g[i].emplace_back(ii(x,y)); } } int maxmask=(1<<n),ans=0; for(int msk=0;msk<maxmask;msk++) { int cnt=0; bool ok=true; for(int i=0;i<n;i++) if(getbit(msk,i)) { cnt++; for(auto v:g[i]) if((getbit(msk,v.first))^v.second) ok=false; } if(ok) ans=max(ans,cnt); } cout<<ans<<endl; return 0; }
0
CPP
def Is_Possible(W): if W == 2 or W & 1: return False return True W = int(input()) print('YES') if Is_Possible(W) else print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> void db(T a) { cerr << a; } template <class L, class R> void db(pair<L, R> a) { cerr << "(" << a.first << ", " << a.second << ")"; } template <class T> void db(vector<T> v) { cerr << "{"; for (int i = 0; i <= (int)v.size() - 1; i++) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; } template <class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; } template <class T, class... TS> void dump(const char *s, T a, TS... x) { while (*s != ',') cerr << *s++; cerr << ": "; db(a); dump(s + 1, x...); } using LL = long long; using PII = pair<int, int>; using VI = vector<int>; using VLL = vector<LL>; using VVI = vector<VI>; using VPII = vector<PII>; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; VI v(n); for (int i = 0; i <= n - 1; i++) cin >> v[i]; int ans = v[n - 1] - v[0] + 1; VI a(n - 1); for (int i = 0; i <= n - 1 - 1; i++) a[i] = v[i + 1] - v[i] - 1; sort(a.rbegin(), a.rend()); for (int i = 0; i <= k - 1 - 1; i++) ans -= a[i]; cout << ans << "\n"; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 10; long long mod = 1e9 + 7; long long binpow(long long a, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * a) % mod; n >>= 1; a = (a * a) % mod; } return res; } int mm[MAXN]; void solve() { int n, m; cin >> n >> m; vector<vector<int>> g(n + 1); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } vector<pair<int, int>> order(n); for (int i = 0; i < n; ++i) { int x; cin >> x; mm[i + 1] = x; order[i] = make_pair(x, i + 1); } sort(order.begin(), order.end()); for (int i = 0; i < order.size(); ++i) { int val = order[i].first; int rem = order[i].first - 1; int ind = order[i].second; unordered_map<int, bool> used; for (int j = 0; j < g[ind].size(); ++j) { int to = g[ind][j]; if (mm[to] == val) { cout << -1; return; } if (mm[to] <= val - 1 && !used[mm[to]]) { used[mm[to]] = 1; rem--; } } if (rem) { cout << -1; return; } } for (int i = 0; i < order.size(); ++i) { cout << order[i].second << " "; } } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); int T = 1; for (int i = 1; i < T + 1; ++i) { solve(); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; int q; cin >> n >> q; long long int x; while (q--) { cin >> x; while (!(x & 1)) { x = n - (x / 2) + x; } cout << (x + 1) / 2 << "\n"; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long eps = 1e-10; const long long maxn = 3e5 + 10; const int INF = 2e8; const long long mod = 1e9 + 7; set<int> g[maxn]; set<int> re; bool bfs(int i) { queue<int> q; q.push(i); re.erase(i); bool fl = 0; while (!q.empty()) { int c = q.front(); q.pop(); vector<int> v; for (auto r : re) { if (!g[c].count(r)) { q.push(r); v.push_back(r); } } for (auto e : v) re.erase(e); if (!g[c].count(1)) fl = 1; } return fl; } int main() { int n, m, k; ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 2; i <= n; ++i) re.insert(i); int co = n - 1; for (int t = (0); t < (m); ++t) { int a, b; cin >> a >> b; g[a].insert(b), g[b].insert(a); if (a == 1 || b == 1) --co; } if (co < k) { printf("impossible"); return 0; } int cnt = 0; for (int i = 2; i <= n; ++i) { if (re.count(i)) { if (!bfs(i)) { printf("impossible"); return 0; }; ++cnt; } } if (cnt > k) { printf("impossible"); return 0; }; printf("possible"); }
11
CPP
def solution(): n, k, t = map(int, input().split()) if t <= k: return t if t <= n: return k return n + k - t if __name__ == '__main__': print(solution())
7
PYTHON3
import math t=int(input()) for i in range(t): m,d,w=map(int,input().split()) if d==1: print(0) elif d>=m: p=math.gcd((d-1),w) q=w//p count=(m-1)//q print(m*count-q*(((count)*(count+1))//2)) elif d<m: p = math.gcd((d - 1), w) q = w // p count = (d - 1) // q print(d* count - q * (((count) * (count + 1)) // 2))
11
PYTHON3
n,a,b,c=map(int,input().split()) s=0 c,b,a=sorted([a,b,c]) if c==1: print(n) else: for i in range(n//a+1): for j in range((n-i*a)//b+1): p=n-a*i-b*j if p%c==0: ss=i+j+p/c s=max(s,ss) print(int(s))
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> isprime(1e5+1,1); isprime[0]=isprime[1]=0; vector<long long int> primes; for(int i=0;i<1e5+1;i++){ if(isprime[i]){ primes.push_back(i); for(int j=i*2;j<1e5+1;j+=i) isprime[j]=0; } } int m,a,b; while(cin>>m>>a>>b,m){ int p,q; long long int res=0; for(int pindex=0;pindex<primes.size();pindex++){ for(int qindex=pindex;qindex<primes.size()&&primes[pindex]*primes[qindex]<=m;qindex++) if(a*primes[qindex]<=primes[pindex]*b && primes[qindex]*primes[pindex]>res){ p=primes[pindex]; q=primes[qindex]; res=p*q; } } cout<<p<<" "<<q<<endl; } }
0
CPP
n, x = list(map(int, input().split(" "))) b = 0 for i in range(n): s = input().split(" ") if s[0] == "+": x += int(s[1]) elif x < int(s[1]): b+=1 else: x -= int(s[1]) print(x, b)
7
PYTHON3
l = input() s = set(l) n = len(s) if(n % 2 == 0): print("CHAT WITH HER!") else: print("IGNORE HIM!")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; bool u[100001], u2[100001]; vector<int> g; vector<pair<int, int> > ans; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 2; i <= n; i += 2) { u[i] = 1; } for (int i = 3; i <= n / 2; ++i) { if (!u[i]) { g.push_back(i); for (int j = 3 * i; j <= n; j += i) { if (!u[j]) g.push_back(j); u[j] = 1; } if (int(g.size()) % 2 == 1) { g.push_back(2 * i); u2[2 * i] = 1; } for (int j = 0; j < int(g.size()); j += 2) ans.push_back(make_pair(g[j], g[j + 1])); g.clear(); } } for (int i = 2; i <= n; i += 2) { if (!u2[i]) g.push_back(i); } for (int i = 0; i + 1 < int(g.size()); i += 2) ans.push_back(make_pair(g[i], g[i + 1])); cout << int(ans.size()); for (auto to : ans) cout << endl << to.first << " " << to.second; return 0; }
9
CPP
l=input() if(int(l[-1])%2==0): print("0") else: print("1")
10
PYTHON3
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") using namespace std; using llong = long long; using ld = long double; using ii = pair<int, int>; using ull = unsigned long long; using pll = pair<llong, llong>; using psi = pair<string, int>; using ll = pair<llong, llong>; const llong over999 = 1e9; const ld over999ld = 1e16; const llong md = 1e9 + 7; const ld eps = 1e-9; const ld Pi = acos(-1); const llong nich = 0; const ld mnogo = 2e14; const int base = 1e9; const llong MX = 1e9 + 1; unordered_map<llong, llong> t, t2, m; llong x, n, sum; ld ans; bool isBigger(string a, string b) { if (a.size() > b.size()) return true; else if (b.size() > a.size()) return false; return (a > b); } string dlink(string s1, string s2) { vector<int> a, b; int minus = 0; if (s1[0] == '-') { minus++; s1.erase(s1.begin()); } if (s2[0] == '-') { minus += 3; s2.erase(s2.begin()); } for (int i = (int)s1.length(); i > 0; i -= 9) if (i < 9) a.push_back(atoi(s1.substr(0, i).c_str())); else a.push_back(atoi(s1.substr(i - 9, 9).c_str())); for (int i = (int)s2.length(); i > 0; i -= 9) if (i < 9) b.push_back(atoi(s2.substr(0, i).c_str())); else b.push_back(atoi(s2.substr(i - 9, 9).c_str())); if (minus % 2 != 0) { if (minus == 1) { if (isBigger(s1, s2)) { } else { minus = 0; swap(a, b); } } else { if (isBigger(s2, s1)) { minus = 1; swap(a, b); } } int carry = 0; for (size_t i = 0; i < b.size() || carry; ++i) { a[i] -= carry + (i < b.size() ? b[i] : 0); carry = a[i] < 0; if (carry) a[i] += base; } while (a.size() > 1 && a.back() == 0) a.pop_back(); } else { int carry = 0; for (size_t i = 0; i < max(a.size(), b.size()) || carry; ++i) { if (i == a.size()) a.push_back(0); a[i] += carry + (i < b.size() ? b[i] : 0); carry = a[i] >= base; if (carry) a[i] -= base; } } string tmp = ""; if (a.empty()) { return "0"; } else tmp += to_string(a.back()); for (int i = (int)a.size() - 2; i >= 0; --i) { string gg = to_string(a[i]); while (gg.size() < 9) gg = "0" + gg; tmp = tmp + gg; } if (minus == 1 || minus == 4) return "-" + tmp; else return tmp; } void inc_1(int i, llong x) { for (; i <= MX; i = (i | (i + 1))) t[i] += x; } void inc_2(int i, llong x) { for (; i <= MX; i = (i | (i + 1))) t2[i] += x; } llong sum_1(int r) { llong result = 0; for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r]; return result; } llong sum_2(int r) { llong result = 0; for (; r >= 0; r = (r & (r + 1)) - 1) result += t2[r]; return result; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; m[x]++; sum += x; ans += (x * (i - m[x] - m[x - 1] - m[x + 1]) - (sum - m[x] * x - m[x - 1] * (x - 1) - m[x + 1] * (x + 1))); } cout << fixed << setprecision(0); cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int calc(int n, int m, int rc, int cc, int rd, int cd) { int sec = 0; int dr = 1, dc = 1; while (rc != rd && cc != cd) { if (rc + dr > n) dr *= -1; if (cc + dc > m) dc *= -1; rc += dr; cc += dc; sec++; } return sec; } int main() { int k; cin >> k; for (int i = 0; i < k; ++i) { int n, m, rc, cc, rd, cd; cin >> n >> m >> rc >> cc >> rd >> cd; cout << calc(n, m, rc, cc, rd, cd) << endl; } }
7
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("avx2") using namespace std; int n, m, a[1005000], l[1005000], r[1005000], c[1005000], d[1005000], ans[1005000]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) cin >> l[i] >> r[i]; for (int i = 1; i <= 1e6; i++) c[i] = c[i - 1] ^ i; for (int i = 1; i <= n; i++) { d[i] = a[i]; for (int j = i + 1; j <= n; j++) { if (a[i] <= a[j]) d[j] = c[a[j]] ^ c[a[i] - 1]; else d[j] = c[a[i]] ^ c[a[j] - 1]; d[j] = max(d[j], d[j - 1]); } for (int j = 1; j <= m; j++) if (l[j] <= i && i <= r[j]) ans[j] = max(ans[j], d[r[j]]); } for (int i = 1; i <= m; i++) cout << ans[i] << '\n'; return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int BB = 100; const int MX = N / BB + 100; const int INF = 1e9 + 10; const long long BINF = 1e18 + 10; struct line { long long k, b; line() : k(-INF), b(-BINF){}; line(long long _k, long long _b) : k(_k), b(_b){}; long long get(int x) { return k * 1ll * x + b; } long long intersect(line l) { return (b - l.b) / (l.k - k); } }; long long from[MX][BB + 1]; int Ptr[MX], Sz[MX]; line arr[MX][BB + 1]; void init(int id) { Sz[id] = 1; from[id][1] = -INF; arr[id][1] = line(); Ptr[id] = 1; } void add_line(int id, line nl) { while (Sz[id] > 1) { if (nl.k == arr[id][Sz[id]].k || arr[id][Sz[id]].intersect(nl) < from[id][Sz[id]]) { Sz[id]--; } else { break; } } from[id][Sz[id] + 1] = arr[id][Sz[id]].intersect(nl); arr[id][Sz[id] + 1] = nl; Sz[id]++; } long long get(int id, int x) { Ptr[id] = min(Ptr[id], Sz[id]); while (Ptr[id] < Sz[id] && arr[id][Ptr[id] + 1].get(x) >= arr[id][Ptr[id]].get(x)) { Ptr[id]++; } return arr[id][Ptr[id]].get(x); } vector<int> g[N], order; int A[N], B[N], sz[N], a[N], b[N]; void dfs(int v, int sumA, int sumB) { sz[v] = 1; order.push_back(v); A[(int)(order).size() - 1] = sumA; B[(int)(order).size() - 1] = abs(sumB); for (auto u : g[v]) { dfs(u, sumA + a[u], sumB + b[u]); sz[v] += sz[u]; } } int mx[N], L[N], R[N], ptr[N]; pair<long long, long long> lines[N]; void rebuild(int block_id) { int cur = 0; mx[block_id] = -INF; for (int i = L[block_id]; i <= R[block_id]; i++) { if (A[i] + ptr[block_id] >= 0) { lines[cur++] = {B[i], A[i] * 1ll * B[i]}; } else { mx[block_id] = max(mx[block_id], A[i]); lines[cur++] = {-B[i], -A[i] * 1ll * B[i]}; } } sort(lines, lines + cur); init(block_id); for (int i = 0; i < cur; i++) { add_line(block_id, line(lines[i].first, lines[i].second)); } } int pos[N], need_rebuild[N]; bool block_begin[N], block_end[N]; signed main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; for (int i = 2; i <= n; i++) { int p; cin >> p; g[p].push_back(i); } for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; } dfs(1, a[1], b[1]); for (int i = 0; i < n; i++) { pos[order[i]] = i; } int cnt_blocks = (n + BB - 1) / BB; for (int i = 0; i < cnt_blocks; i++) { L[i] = BB * i; R[i] = min(BB * (i + 1), n) - 1; block_begin[L[i]] = true; block_end[R[i]] = true; ptr[i] = 0; rebuild(i); } int C = 0; for (int i = 1; i <= q; i++) { int type; cin >> type; if (type == 1) { int v, x; cin >> v >> x; int l = pos[v], r = pos[v] + sz[v] - 1; int fr = l / BB, to = r / BB; need_rebuild[fr] = need_rebuild[to] = i; while (l <= r && !block_begin[l]) { A[l] += x; l++; } while (r >= l && !block_end[r]) { A[r] += x; r--; } if (l <= r) { for (int j = l / BB; j <= r / BB; j++) { ptr[j] += x; if (mx[j] + ptr[j] >= 0) { need_rebuild[j] = i; } } } for (int j = fr; j <= to; j++) { if (need_rebuild[j] == i) { C++; rebuild(j); } } } else { int v; cin >> v; int l = pos[v], r = pos[v] + sz[v] - 1; long long ans = 0; while (l <= r && !block_begin[l]) { ans = max(ans, abs(A[l] + ptr[l / BB]) * 1ll * B[l]); l++; } while (r >= l && !block_end[r]) { ans = max(ans, abs(A[r] + ptr[r / BB]) * 1ll * B[r]); r--; } if (l <= r) { for (int j = l / BB; j <= r / BB; j++) { ans = max(ans, get(j, ptr[j])); } } cout << ans << '\n'; } } cerr << "TIME :: " << clock() * 1.0 / CLOCKS_PER_SEC << '\n'; cerr << "REBUILDS " << C << '\n'; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100100; int n, res[N]; pair<int, int> a[N]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i].first); a[i].second = i; a[i].first = n - a[i].first; } sort(a, a + n); reverse(a, a + n); for (int i = 0, j = 1; i < n;) { int k = i + a[i].first - 1; if (k >= n) { puts("Impossible"); exit(0); } for (int l = i; l <= k; l++) { if (a[l].first != a[i].first) { puts("Impossible"); exit(0); } res[a[l].second] = j; } j++; i = k + 1; } puts("Possible"); for (int i = 0; i < n; i++) { printf("%d ", res[i]); } puts(""); return 0; }
8
CPP
// Mivik 2021.4.12 #include <cstdio> #include <cstring> #include <iostream> #define d isdigit(c) #define g c=t.get(); #define L return #define K break #define A(c,a,b)if(c)a else b; #define I(c,a)if(c)a; #define Y goto E struct MI{private:char bb[1 << 14];FILE*f;char*bs,*be;char e;bool o,l;public:MI():f(stdin),bs(0),be(0){} #ifdef __linux__ #endif inline char get(){if(o){o=0;L e;} #ifdef MIVIK char c=fgetc(f);I(c==-1,l=1)L c; #else I(bs==be,be=(bs=bb)+fread(bb,1,sizeof(bb),f))I(bs==be,{l=1;L-1;})L*bs++; #endif }inline void unget(char c){o=1;e=c;}template<class T>inline T read(){T r;*this>r;L r;}template<class T>inline MI&operator>(T&);};template<class T>struct Q{const static bool U=T(-1)>=T(0);inline void operator()(MI&t,T&r)const{r=0;char c;bool y=0;A(U,for(;;){g I(c==-1,Y)I(d,K)},for(;;){g I(c==-1,Y)A(c=='-',{g I(d,{y=1;K;})},I(d,K))})for(;;){I(c==-1,Y)A(d,r=r*10+(c^48);,K)g}t.unget(c);E:;I(y,r=-r)}};template<>struct Q<char>{};template<class T>inline MI&MI::operator>(T&t){Q<T>()(*this,t);L*this;} #undef d #undef g #undef L #undef K #undef A #undef I #undef Y template<class T>std::ostream& operator<(std::ostream&out,const T&t){return out<<t;} #define endl ('\n') #define P(x) cout<#x" = "<(x)<endl #define R (cin.read<int>()) using std::cout; #include <algorithm> #include <cassert> #include <vector> #include <queue> #include <cmath> MI cin; typedef long long qe; const int $n = 1000005; template<typename T> inline bool gmin(T &a, const T &b) { return b < a && (a = b, true); } int n; qe C, v[$n], off, ans[$n], o[$n]; bool rv, nb[$n]; struct range { qe l, r; inline range real() const { if (rv) return { std::max<qe>(off - r, 0), off - l }; return { l + off, r + off }; } }; std::deque<range> w; inline qe imag(qe v) { v = v - off; return rv ? -v : v; } int main() { cin > n > C; const int L = n - 2; w.push_back({ 0, C }); for (int i = 1; i <= L; ++i) { const qe t = v[i] = cin.read<qe>(); auto it = std::lower_bound(w.begin(), w.end(), imag(t), [](const range &t, qe v) { return t.r < v; }); if (it != w.end() && it->real().l <= t && t <= it->real().r) { // cout < "(1)\n"; rv = off = 0; w.clear(); w.push_back({ 0, t }); nb[i] = 1; } else { // cout < "(2)\n"; const bool meow = !w.empty() && (rv ? w.back() : w.front()).real().l <= t; rv = !rv; off = t - off; // for (const auto &p : w) cout < p.real() < ' '; cout < endl; if (rv) while (!w.empty() && w.back().real().r < 0) w.pop_back(); else while (!w.empty() && w.front().real().r < 0) w.pop_front(); // for (const auto &p : w) cout < p.real() < ' '; cout < endl; if (meow && (w.empty() || (rv ? w.front() : w.back()).real().r != t)) { const range ame = { imag(t), imag(t) }; if (rv) w.push_front(ame); else w.push_back(ame); } } if (w.empty()) return cout < "NO\n", 0; ans[i] = (rv ? w.back() : w.front()).real().l; // cout < i < ": "; for (const auto &p : w) cout < p.real() < ' '; cout < endl; } cout < "YES\n"; o[L] = ans[L]; for (int i = L; i; --i) { if (nb[i]) o[i - 1] = v[i]; else if (o[i] == v[i]) o[i - 1] = ans[i - 1]; else o[i - 1] = v[i] - o[i]; } for (int i = 1; i <= L; ++i) if (std::max(std::abs(o[i - 1] + o[i]), std::max(std::abs(o[i - 1]), o[i])) != v[i]) o[i] = -o[i]; qe d = 0; for (int i = 0; i <= L; ++i) gmin(d, ans[i + 1] = ans[i] + o[i]); for (int i = 0; i < n; ++i) cout < (ans[i] - d) < " \n"[i == n - 1]; for (int i = 1; i <= L; ++i) { assert(std::max(std::max(ans[i - 1], ans[i]), ans[i + 1]) - std::min(std::min(ans[i - 1], ans[i]), ans[i + 1]) == v[i]); } }
12
CPP
#include <bits/stdc++.h> using namespace std; long long a, b, c, i; char e; string p; int main() { cin >> a; c = 1; i = a; while (a != 0) { a /= 10; b++; c *= 10; } c = c / 10; cout << (i / c + 1) * c - i; }
7
CPP
#include <bits/stdc++.h> using namespace std; char s[100005]; int k; vector<int> cur; int main() { scanf("%s", s + 1); scanf("%d", &k); int n = strlen(s + 1); if (1ll * n * (n + 1) / 2 < k) { puts("No such line."); return 0; } for (int i = 0; i < n; i++) cur.push_back(i); while (!cur.empty()) { vector<int> sub; long long sum; char ch; int ncur = cur.size(); for (ch = 'a'; ch <= 'z'; ch++) { sum = 0; for (int i = 0; i < ncur; i++) if (s[cur[i] + 1] == ch) sum += n - cur[i]; if (sum < k) k -= sum; else break; } putchar(ch); int cnt = 0; for (int i = 0; i < ncur; i++) if (s[cur[i] + 1] == ch) { cnt++; if (cnt == k) return 0; sub.push_back(cur[i] + 1); } k -= cnt; cur = sub; } return 0; }
8
CPP
import sys lines = [line.strip() for line in sys.stdin.readlines()] n, k = [int(x) for x in lines[0].split()] days = [int(x) for x in lines[1].split()] arya = 0 bran = 0 cont = 0 for i in range(len(days)): arya += days[i] x = min(arya, 8) arya -= x bran += x cont += 1 if bran >= k: break if bran >= k: print(cont) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("inline", 2) using namespace std; const long long N = 5005; const long long inf = 1e18; long long n, hd[N], nxt[N << 1], to[N << 1], w[N << 1], tot = 1; long long sz[N << 1]; long long aa[N << 1], eg[N << 1]; void add(long long a, long long b, long long c) { nxt[++tot] = hd[a], to[tot] = b, w[tot] = c; hd[a] = tot; } long long dfs1(long long u, long long fe, long long d) { long long re = d; for (long long i = hd[u]; i; i = nxt[i]) if (i != fe) re += dfs1(to[i], i ^ 1, d + w[i]); return eg[fe] = re; } long long dfs2(long long u, long long fe) { long long re = 1; for (long long i = hd[u]; i; i = nxt[i]) if (i != fe) re += dfs2(to[i], i ^ 1); sz[fe ^ 1] = n - re; return sz[fe] = re; } void solve(long long rt) { memset(eg, 0, sizeof eg); long long sum = dfs1(rt, 0, 0); for (long long i = 2; i <= tot; ++i) aa[i] = min(aa[i], sum - eg[i]); } long long zz[N]; long long wi[N], wo[N], bb[N << 1]; void dfs3(long long u, long long fe) { zz[u] = 1; wi[u] = 0, wo[u] = 0; for (long long i = hd[u]; i; i = nxt[i]) if (i != fe) { dfs3(to[i], i ^ 1); zz[u] += zz[to[i]]; wi[u] += wi[to[i]]; wo[u] += wo[to[i]] + zz[to[i]] * w[i]; wi[u] -= (wo[to[i]] + zz[to[i]] * w[i]) * zz[to[i]]; } wi[u] += wo[u] * zz[u]; bb[fe] = wi[u]; } signed main() { memset(aa, 0x3f, sizeof aa); cin >> n; for (long long i = 1; i <= n - 1; ++i) { long long x, y, z; cin >> x >> y >> z; add(x, y, z); add(y, x, z); } for (long long i = 1; i <= n; ++i) dfs3(i, 0); for (long long i = 1; i <= n; ++i) solve(i); dfs2(1, 0); long long ans = 1e18; for (long long i = 1; i <= n - 1; ++i) { long long r = i << 1, s = i << 1 | 1; ans = min(ans, 1LL * w[r] * sz[r] * sz[s] + aa[r] * sz[r] + aa[s] * sz[s] + bb[r] + bb[s]); } cout << ans << endl; }
11
CPP
n, m = map(int, input().split()) d = dict() for i in range(m): a, b = input().split() d[a] = a if len(a) <= len(b) else b arr = list(input().split()) for i in arr: print(d[i], end=' ')
8
PYTHON3
#include <bits/stdc++.h> int n; int biao[] = { 0, 1, 9, 245, 126565, 54326037, 321837880, 323252721, 754868154, 328083248, 838314395, 220816781, 893672292, 166441208, 251255697, 114256285, 118775501, 482714697, 11784725, 460862131, 550384565, 106742050, 425241115, 626692854, 674266678, 320014275, 345949512, 527320049, 897822749, 137190263, 491039182, 810384961, 482023334, 658099864, 886790989, 845381174, 371433224, 278969124, 420088324, 696766322, 388302635, 141033366, 46387851, 932125021, 278342766, 371131134, 922501918, 110778457, 506223573, 806353719, 391845991, 923507761, 780307355, 109951115, 830090230, 605558495, 344686604, 988110893, 944684429, 715019947, 799898820, 384672708, 907325090, 758952329, 550672104, 368337206, 394915145, 401744167, 923781939, 831857516, 407845661, 329267374, 927004007, 891609656, 897919613, 481297880, 737337940, 651873737, 287246681, 973133651, 679864988, 784719328, 820504764, 875613823, 806512665, 164851642, 500228957, 951814419, 447763649, 273141670, 979349615, 964027956, 809510400, 276634497, 116631976, 426739449, 175282420, 885948162, 62270880, 974395255, 675165056, 759589968, 837957573, 931897605, 152352780, 585420109, 1772087, 333401718, 898833639, 745874265, 786209423, 691982338, 498790927, 473374639, 274302623, 971280670, 241671319, 13070005, 302088807, 550276351, 436592588, 631667314, 548656698, 730626984, 146295220, 674398632, 400383348, 454138904, 786220712, 118620797, 233440672, 217349271, 274853536, 310607544, 105221205, 769566615, 853585061, 800665807, 695377419, 924327065, 388199705, 551624811, 721435546, 501720515, 308465454, 825369234, 396065729, 451899519, 295058424, 142088952, 473485086, 378771634, 734511215, 462404399, 959198328, 337668263, 794122911, 38911400, 951992982, 472696081, 373904752, 105884826, 630251717, 28980684, 845136347, 353665773, 691661192, 19922354, 231463797, 757917231, 242739918, 979036950, 713722080, 234689388, 2243164, 209872853, 240808787, 539523346, 425797848, 913772061, 224613100, 421742777, 222232478, 92712941, 215137570, 949901408, 274827432, 15162482, 593145989, 274574232, 239282092, 762720192, 804146934, 500629424, 565985054, 81127381, 671811155, 655565571, 890331075, 237994348, 743647404, 667160634, 713914299, 668506729, 741341289, 277636808, 762781382, 14272789, 902864131, 567443405, 149113383, 648844381, 825489976, 933016723, 192288078, 734493315, 240985733, 861817693, 762711459, 525904609, 532463481, 377133989, 620711079, 772561562, 980733194, 227599811, 162774370, 209512798, 787116594, 3509258, 748795368, 378035466, 612938915, 802091952, 857679599, 481748937, 493370392, 358420805, 48301629, 412001241, 463126722, 509578422, 967799131, 994766554, 687287243, 863623583, 771554899, 690911527, 855314994, 923686429, 246862514, 192479791, 133487041, 703444043, 295281758, 801816257, 920762934, 749306433, 973004841, 848644684, 560026478, 952127278, 616654635, 839390326, 975154012, 409583672, 635350249, 343228425, 335331602, 223826406, 952341037, 589677800, 249747234, 555694261, 137143500, 628190328, 461598392, 431912756, 29349807, 759199489, 783281228, 781971312, 915823407, 388508707, 718062705, 27424111, 309999451, 963383322, 831185229, 132910888, 347028136, 850484840, 223055285, 142335980, 144754000, 772005560, 81796039, 167696020, 79454283, 172772542, 201056991, 484957644, 716630285, 763194701, 211505841, 903448791, 926964672, 257752668, 482951716, 411539070, 620249847, 592476107, 170473128, 814662613, 898000271, 57354872, 361106091, 488697643, 889007954, 138725767, 684860983, 36248116, 304610143, 137633385, 413715776, 99010024, 779653665, 100387568, 286328069, 564731826, 621740468, 943513219, 506666491, 249987886, 553719884, 769853086, 337485319, 702455584, 809637762, 755400257, 892290368, 502180086, 364275817, 118162370, 873374339, 261271695, 970132574, 744105500, 434447173, 117975095, 383088393, 625447969, 180281249, 545367713, 133236931, 360175662, 148087453, 806871297, 498529036, 886076476, 65645000, 465138299, 967109895, 331362616, 472283705, 796894900, 199697765, 503759892, 472807906, 187586706, 941198065, 782234442, 57693411, 18678611, 82626204, 395317191, 570588915, 152519440, 449852456, 63696518, 763741345, 878748386, 494317541, 444782633, 93316211, 929164666, 529288371, 165769871, 730546850, 955877127, 994202767, 492009567, 275683011, 415902127, 95725776, 718047399, 786963365, 73091278, 986172399, 174591541, 913259286}; signed main() { scanf("%d", &n); printf("%d", biao[n]); return 0; }
10
CPP
n, m, a = map(int, input().split()) hor_num = (n + a - 1) // a vert_num = (m + a - 1) // a print(hor_num * vert_num)
7
PYTHON3
n = int(input()) if n % 2 == 0:print('NO') else: print('YES') a = [0 for _ in range(2*n+1)] s,t = True,False for i in range(1,n+1): a[i] = 2*i-s a[i+n] = 2*i-t s,t = t,s print(*a[1:])
7
PYTHON3
w, a, b = map(int, input().split()) print(max(a-b-w, 0, b-a-w))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t, n, r; cin >> t; while (t--) { cin >> n >> r; vector<int> vec(n); for (int i = 0; i < n; i++) cin >> vec[i]; sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); n = vec.size(); if (n == 1) cout << "1" << endl; else { int flag = 1; for (int k = 1; k < n; k++) { if (vec[n - k - 1] <= k * r) { cout << k << endl; flag = 0; break; } } if (flag) cout << n << endl; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void init() { freopen("a.in", "r", stdin); cout.precision(9); } const double EPS = 1e-9; const int INF = (int)1e9 + 41; const int N = (int)1e6 + 34; int rnk[N], parent[N], a[N], b[N]; void make_set(int v) { parent[v] = v; rnk[v] = 0; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rnk[a] < rnk[b]) swap(a, b); parent[b] = a; if (rnk[a] == rnk[b]) ++rnk[a]; } } void solve() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j < m; j++) cin >> b[j]; vector<pair<int, pair<int, int> > > e; long long ans = 0; for (int i = 0; i < n; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int x; cin >> x; x--; e.push_back({-a[i] - b[x], {i, n + x}}); } } for (int i = 0; i < n + m; i++) make_set(i); sort(e.begin(), e.end()); for (auto ed : e) { int w = -ed.first; int f = ed.second.first; int s = ed.second.second; if (find_set(f) == find_set(s)) { ans += w; } union_sets(f, s); } cout << ans; } int main() { solve(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; int ans = 101; cin >> n >> m >> k; for (int i = 1; i <= n; i++) { int u; cin >> u; if (u != 0 && u <= k) { int dis = fabs(i - m); ans = min(ans, dis); } } cout << ans * 10; return 0; }
7
CPP
a,b=map(float,input().split()) s=a*b/3.305785 print(s)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; namespace io { const int SI = 1 << 21 | 1; char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100]; int f, t; inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; } inline void pc(char x) { *OS++ = x; if (OS == OT) flush(); } template <class I> inline void rd(I &x) { for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++); c < '0' || c > '9'; c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) if (c == '-') f = -1; for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15), c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) ; x *= f; } template <class I> inline void rda(I *a, int &n) { for (int i = 1; i <= n; i++) rd(a[i]); } inline void rds(char *s, int &x) { for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++); c < 33 || c > 126; c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) ; for (x = 0; c >= 33 && c <= 126; s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) ; s[x + 1] = '\0'; } template <class I> inline void print(I x, char k = '\n') { if (!x) pc('0'); if (x < 0) pc('-'), x = -x; while (x) ch[++t] = x % 10 + '0', x /= 10; while (t) pc(ch[t--]); pc(k); } inline void prints(string s) { int x = s.length(); while (t < x) pc(s[t++]); pc('\n'), t = 0; } struct Flush { ~Flush() { flush(); } } flusher; } // namespace io using io::print; using io::prints; using io::rd; using io::rda; using io::rds; const double eps = 1e-10, PI = acos(-1); struct P { double x, y; inline P() {} inline P(double x, double y) : x(x), y(y) {} inline P &operator+=(P o) { return x += o.x, y += o.y, *this; } inline P &operator-=(P o) { return x -= o.x, y -= o.y, *this; } inline P &operator*=(double o) { return x *= o, y *= o, *this; } inline P &operator/=(double o) { return x /= o, y /= o, *this; } inline friend P operator+(P a, P b) { return a += b; } inline friend P operator-(P a, P b) { return a -= b; } inline friend P operator*(P a, double b) { return a *= b; } inline friend P operator/(P a, double b) { return a /= b; } inline friend bool operator<(P a, P b) { return fabs(a.x - b.x) < eps ? a.y < b.y : a.x < b.x; } inline friend bool operator==(P a, P b) { return !(a < b) && !(b < a); } inline friend double operator*(P a, P b) { return a.x * b.x + a.y * b.y; } inline friend double operator%(P a, P b) { return a.x * b.y - a.y * b.x; } inline friend double operator^(P a, P b) { return a * b / a.l() / b.l(); } inline double a() { return atan2(y, x); } inline double l() { return sqrt(*this * *this); } inline void r(double o) { double s = sin(o), c = cos(o), X = x * c - y * s, Y = x * s + y * c; x = X, y = Y; } }; struct L { P a, b; double c; inline L() {} inline L(P a, P b) : a(a), b(b) { c = b.a(); } inline friend P operator*(L a, L b) { return a.a + a.b * (b.b % (a.a - b.a) / (a.b % b.b)); } inline friend double operator/(L a, P b) { return fabs(a.b % (b - a.a)) / a.b.l(); } inline friend bool operator<(L a, L b) { return fabs(a.c - b.c) < eps ? (b.a - a.a) % a.b < 0 : a.c < b.c; } }; inline deque<P> half_plane(vector<L> l) { sort(l.begin(), l.end()); deque<P> r; deque<L> q; for (L o : l) { while (r.size() && (r.back() - o.a) % o.b > 0) q.pop_back(), r.pop_back(); while (r.size() && (r.front() - o.a) % o.b > 0) q.pop_front(), r.pop_front(); if (q.size() && fabs(o.b % q.back().b) < eps) { if (fabs(o.b.a() - q.back().b.a()) > eps) return r.clear(), r; q.pop_back(); if (r.size()) r.pop_back(); } if (q.size()) r.push_back(q.back() * o); q.push_back(o); } while (r.size() && (r.back() - q.front().a) % q.front().b > 0) q.pop_back(), r.pop_back(); if (q.size() < 3u) r.clear(); else r.push_back(q.front() * q.back()); return r; } int n; vector<L> l, q; P p1, p2, ans1, ans2; inline bool pd(int i, int j, double r, P &t) { vector<L> s = q; for (int k = i; k <= j; k++) { L o = l[k]; P p = o.b; p.r(PI / 2); p *= r / p.l(); o.a += p, o.b *= -1, o.c = o.b.a(); s.push_back(o); } deque<P> p = half_plane(s); if (!p.size()) return 0; return t = p.back(), 1; } inline bool pd(double r) { for (int i = 0, j = 0; i < n; i++) { while (j < i + n - 2 && pd(i, j, r, p1)) ++j; if (pd(j, i + n - 1, r, p2)) return 1; } return 0; } int main() { rd(n); vector<P> p; for (int i = 1, x, y; i <= n; i++) rd(x), rd(y), p.push_back(P(x, y)); p.push_back(p[0]); for (int i = 0; i < n; i++) l.push_back(L(p[i], p[i + 1] - p[i])); p.pop_back(), q = l; for (int i = 0; i < n; i++) l.push_back(l[i]); double l = 0, r = 1e5; while (r - l > eps) { double mid = (l + r) / 2; if (pd(mid)) r = mid, ans1 = p1, ans2 = p2; else l = mid; } printf("%.10f\n", r); printf("%.10f %.10f\n", ans1.x, ans1.y); printf("%.10f %.10f\n", ans2.x, ans2.y); return 0; }
12
CPP
N = int(input()) ABs = [tuple(map(int, input().split())) for _ in range(N)] ABs.sort() print(ABs[-1][0] + ABs[-1][1])
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 305; int a[maxn][maxn]; int main() { int i, j, n, t; scanf("%d", &n); t = 0; for (j = 0; j < n; j++) { if (j & 1) for (i = 0; i < n; i++) a[i][j] = ++t; else for (i = n - 1; i >= 0; i--) a[i][j] = ++t; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) printf("%d%c", a[i][j], j < n - 1 ? ' ' : '\n'); return 0; }
9
CPP
#include <bits/stdc++.h> int n, l[200005], r[200005], rk[200005], rk1[200005], p[200005]; struct cmp { bool operator()(int a, int b) { return r[a] > r[b]; } }; int cmp1(int a, int b) { return l[a] < l[b]; } int cmp2(int a, int b) { return p[a] < p[b]; } std::priority_queue<int, std::vector<int>, cmp> q; std::vector<int> G[200005]; struct segmentTree { int min[200005 << 2]; int gmin(int a, int b) { if (r[a] > r[b]) return a; return b; } void modify(int l, int r, int p, int v, int rt) { if (l == r) { min[rt] = gmin(v, min[rt]); return; } int mid = (l + r) >> 1; if (p <= mid) modify(l, mid, p, v, rt << 1); else modify(mid + 1, r, p, v, rt << 1 | 1); min[rt] = gmin(min[rt << 1], min[rt << 1 | 1]); } int query(int l, int r, int L, int R, int rt) { if (l > R || r < L) return 0; if (l <= L && R <= r) return min[rt]; return gmin(query(l, r, L, (L + R) >> 1, rt << 1), query(l, r, ((L + R) >> 1) + 1, R, rt << 1 | 1)); } } sgt; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d%d", &l[i], &r[i]); rk[i] = i; rk1[i] = i; G[l[i]].push_back(i); } for (int i = 1; i <= n; ++i) { for (int v : G[i]) q.push(v); int u = q.top(); q.pop(); p[u] = i; } std::sort(rk1 + 1, rk1 + n + 1, cmp1); std::sort(rk + 1, rk + n + 1, cmp2); int p1 = 0; for (int i = 1; i <= n; ++i) { while (p1 < n && l[rk1[p1 + 1]] <= p[rk[i]]) { p1++; sgt.modify(1, n, p[rk1[p1]], rk1[p1], 1); } int d = sgt.gmin(sgt.query(l[rk[i]], p[rk[i]] - 1, 1, n, 1), sgt.query(p[rk[i]] + 1, r[rk[i]], 1, n, 1)); if (r[d] < r[rk[i]]) continue; printf("NO\n"); for (int i = 1; i <= n; ++i) printf("%d ", p[i]); puts(""); std::swap(p[rk[i]], p[d]); for (int i = 1; i <= n; ++i) printf("%d ", p[i]); return 0; } printf("YES\n"); for (int i = 1; i <= n; ++i) printf("%d ", p[i]); return 0; }
12
CPP
integer = input() print('YES' if integer.count('4') + integer.count('7') == 7 or integer.count('4') + integer.count('7') == 4 else 'NO')
7
PYTHON3
def call(): n = int(input()) ecoN,odcoN=0,0 l=[int(x) for x in input().split()] for i in l: if(i&1): odcoN+=1 else: ecoN+=1 if(odcoN%2!=ecoN%2): print("NO") return if(ecoN%2==0): print("YES") return else: for i in range(0,n): for j in range(i+1,n): if(l[i]%2!=l[j]%2 and abs(l[i]-l[j])==1): print('YES') return print('NO') for _ in range(int(input())): call()
9
PYTHON3
#include <iostream> #include <algorithm> #include <vector> #include <queue> typedef long long ll; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; for(int i = 1; i < n; i++) { for(int j = i + 1; j <= n; j++) { int ijxor = (i ^ j); int ans = 1; while((ijxor & 1) == 0) { ans++; ijxor = ijxor >> 1; } cout << ans << " "; } cout << "\n"; } return 0; }
0
CPP
a,b,c=map(int,input().split()) ans=min(a//3,b//2,c//2) a-=ans*3 b-=ans*2 c-=ans*2 ans*=7 m=0 ta,tb,tc=a,b,c for i in range(7): a,b,c=ta,tb,tc temp=0 for j in range(i,i+7): if j%7==0 or j%7==3 or j%7==6: if a>0: a-=1 temp+=1 else: break if j%7==1 or j%7==5: if b>0: temp+=1 b-=1 else: break if j%7==2 or j%7==4: if c>0: temp+=1 c-=1 else: break m=max(m,temp) print(ans+m)
9
PYTHON3
#include<iostream> #include<string> using namespace std; typedef unsigned int uint; int main() { int n, idx; string s; cin >> n; cin.ignore(); while(n--) { getline(cin, s); idx = -7; while((idx=s.find("Hoshino", idx+7))!=string::npos) s.replace(idx, 7, "Hoshina"); cout << s << endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 typedef long long ll; typedef pair<ll,ll>pll; typedef long double ld; ll bin_pow(ll a,ll b){ if(b==0)return 1; if(b%2==0){ ll t=bin_pow(a,b/2); return t*t%MOD; } else return a*bin_pow(a,b-1)%MOD; } vector<ll>graph[200000]; ll p[200000],sz[200000]; bool used[200000]; ll pred(ll x){ if(x==p[x])return x; return p[x]=pred(p[x]); } bool same(ll x,ll y){return pred(x)==pred(y);} void unite(ll x,ll y){ x=pred(x); y=pred(y); if(sz[x]>sz[y])swap(x,y); sz[y]+=sz[x]; p[x]=y; } ll comp; void dfs(ll x){ used[x]=1; comp++; for(auto u : graph[x]){ if(used[u])continue; dfs(u); } } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); ios::sync_with_stdio(false); cin.tie(0); ll t=1,n,m,k=0,sum=0,l=0,r=0,x=0,y=0,z=0,ans=0,mn=LLONG_MAX,mx=LLONG_MIN; cin>>t; while(t--){ ans=z=0; cin>>n; for(int i=0;i<n;i++)graph[i].clear(),p[i]=i,sz[i]=1; vector<pll>edges; for(int i=0;i<n;i++){ cin>>x>>y; x--,y--; edges.push_back({x,y}); } for(int i=0;i<n;i++)used[i]=0; for(int i=0;i<n;i++){ x=edges[i].first,y=edges[i].second; if(same(x,y)){ l=x; r=y; break; } unite(x,y); } for(int i=0;i<n;i++){ x=edges[i].first,y=edges[i].second; if(x==l&&y==r)continue; graph[x].push_back(y); graph[y].push_back(x); } queue<ll>q; q.push(l); for(int i=0;i<n;i++)p[i]=-1; while(!q.empty()){ x=q.front(); used[x]=1; q.pop(); for(auto u : graph[x]){ if(used[u])continue; q.push(u); p[u]=x; } } vector<ll>b,c; y=0; for(int i=0;i<n;i++)used[i]=0; while(r!=-1){ used[r]=1; c.push_back(r); y++; r=p[r]; } for(int i=0;i<y;i++){ used[c[i]]=0; comp=0; dfs(c[i]); b.push_back(comp); } //while(b.size()<y)b.push_back(1); ans=n*(n-1)/2; x=0; for(auto u : b){ //cout<<u<<endl; x+=u*(n-u); } ans+=x/2; cout<<ans; cout<<"\n"; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 150, S = 150 * 150, K = 150; const int inf = (int)1e9; int main() { int n, k, s; scanf("%d%d%d", &n, &k, &s); s += k * (k - 1) / 2; s = min(s, n * (n - 1) / 2); static int a[N]; for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } static int dp[S][2][K], added[S][2][K]; for (int i = 0; i < S; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < K; ++k) { dp[i][j][k] = inf; } } } queue<pair<int, int> > q[N]; q[0].push({0, 0}); q[0].push({0, 1}); dp[0][0][0] = 0; dp[0][0][1] = a[0]; for (int i = 0; i < n - 1; ++i) { while (!q[i % 2].empty()) { int cur_s = q[i % 2].front().first, cur_k = q[i % 2].front().second; added[cur_s][i % 2][cur_k] = 0; q[i % 2].pop(); if (dp[cur_s][(i + 1) % 2][cur_k] > dp[cur_s][i % 2][cur_k]) { dp[cur_s][(i + 1) % 2][cur_k] = dp[cur_s][i % 2][cur_k]; if (!added[cur_s][(i + 1) % 2][cur_k]) { q[(i + 1) % 2].push({cur_s, cur_k}); added[cur_s][(i + 1) % 2][cur_k] = 1; } } if (cur_s + i + 1 <= s && cur_k <= k) { if (dp[cur_s + i + 1][(i + 1) % 2][cur_k + 1] > dp[cur_s][i % 2][cur_k] + a[i + 1]) { dp[cur_s + i + 1][(i + 1) % 2][cur_k + 1] = dp[cur_s][i % 2][cur_k] + a[i + 1]; if (!added[cur_s + i + 1][(i + 1) % 2][cur_k + 1]) { q[(i + 1) % 2].push({cur_s + i + 1, cur_k + 1}); added[cur_s + i + 1][(i + 1) % 2][cur_k + 1] = 1; } } } dp[cur_s][i % 2][cur_k] = inf; } } int ans = inf; for (int i = 0; i <= s; ++i) { ans = min(ans, dp[i][(n - 1) % 2][k]); } printf("%d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100010; long long op[N]; int f[N][20], dp[N][20]; string ss; int n; long long query(int l, int r) { int s = log(r - l + 1); return 1ll * max(f[l][s], f[r - (1 << s) + 1][s]) * min(dp[l][s], dp[r - (1 << s) + 1][s]); } int main() { int T; cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) cin >> op[i]; long long ans = -0x3f3f3f3f3f3f3f3f; for (int i = 2; i <= n; i++) ans = max(ans, op[i] * op[i - 1]); cout << ans << "\n"; } return 0; }
7
CPP
#include <bits/stdc++.h> const double PI = 3.141592653589793238460; using namespace std; long long pows(long long a, long long n, long long m) { a = a % m; long long res = 1; while (n) { if (n % 2 != 0) { res = (res * a) % m; n--; } else { a = (a * a) % m; n = n / 2; } } return res % m; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } bool isprime(long long n) { if (n == 1 || n == 0) { return false; } for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } bool istrue(string s) { int i = 0; int j = s.size() - 1; while (i < j) { if (s[i] == s[j]) { i++; j--; } else { return false; } } return true; } const int N = 2000005; long long l[N]; long long r[N]; long long a[N]; long long ans[N]; long long n; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long TT = clock(); TT = clock(); cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; } stack<long long> st; for (long long i = 1; i <= n; i++) { while (!st.empty() && a[st.top()] >= a[i]) { st.pop(); } if (st.empty()) { l[i] = 0; } else { l[i] = st.top(); } st.push(i); } while (!st.empty()) { st.pop(); } for (long long i = n; i >= 1; i--) { while (!st.empty() && a[st.top()] >= a[i]) { st.pop(); } if (st.empty()) { r[i] = n + 1; } else { r[i] = st.top(); } st.push(i); } for (long long i = 1; i <= n; i++) { long long len = r[i] - l[i] - 1; ans[len] = max(a[i], ans[len]); } for (long long i = n - 1; i >= 1; i--) { ans[i] = max(ans[i], ans[i + 1]); } for (long long i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << "\n"; cerr << "\n\nTIME: " << (long double)(clock() - TT) / CLOCKS_PER_SEC << " sec\n"; return 0; }
8
CPP
#include <bits/stdc++.h> template <typename T> void read(T &); template <typename T> void write(const T &); const int iinf = 2147483647; const long long llinf = 9223372036854775807ll; const int N = 105; int s[N][N][N]; int dp[N][N]; int n, m; int main() { read(n), read(m); for (int i = 1; i <= n; ++i) { int k; read(k); for (int j = 1; j <= k; ++j) { int l, r; read(l), read(r); for (int k = l; k <= r; ++k) { ++s[l][r][k]; } } } for (int k = 1; k <= m; ++k) { for (int r = 1; r <= m; ++r) { for (int l = r - 1; l >= 1; --l) { s[l][r][k] += s[l + 1][r][k]; } } for (int l = 1; l <= m; ++l) { for (int r = l + 1; r <= m; ++r) { s[l][r][k] += s[l][r - 1][k]; } } } for (int l = m; l >= 1; --l) { for (int r = l; r <= m; ++r) { dp[l][r] = -iinf; for (int k = l; k <= r; ++k) { dp[l][r] = std::max( dp[l][r], dp[l][k - 1] + dp[k + 1][r] + s[l][r][k] * s[l][r][k]); } } } write(dp[1][m]), putchar('\n'); return 0; } template <typename T> void read(T &Re) { T k = 0; char ch = getchar(); int flag = 1; while (!(ch >= '0' && ch <= '9')) { if (ch == '-') flag = -1; ch = getchar(); } while ((ch >= '0' && ch <= '9')) k = (k << 1) + (k << 3) + ch - '0', ch = getchar(); Re = flag * k; } template <typename T> void write(const T &Wr) { if (Wr < 0) { putchar('-'), write(-Wr); } else { if (Wr < 10) { putchar(Wr + '0'); } else { write(Wr / 10), putchar((Wr % 10) + '0'); } } }
11
CPP
#include <bits/stdc++.h> using namespace std; long long add[100001], sub[100001], num[100001]; void dfs(int pa, vector<int>* A, int s); int main() { int n, a, b; cin >> n; vector<int> A[n + 1]; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; A[b].push_back(a); A[a].push_back(b); } for (int i = 1; i <= n; ++i) cin >> num[i]; memset(add, 0, sizeof(add)); memset(sub, 0, sizeof(sub)); dfs(-1, A, 1); cout << add[1] + sub[1] << endl; return 0; } void dfs(int pa, vector<int>* A, int s) { vector<int>::iterator pos; for (pos = A[s].begin(); pos != A[s].end(); ++pos) { if (*pos != pa) { dfs(s, A, *pos); add[s] = max(add[s], add[*pos]); sub[s] = max(sub[s], sub[*pos]); } } num[s] += add[s] - sub[s]; if (num[s] > 0) sub[s] += num[s]; else add[s] -= num[s]; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 500001; int n, m, k, l, pv; set<int> s; queue<int> q; vector<int> g[MAXN], jj[MAXN], co; void bfs(int v) { q.push(v); s.erase(v); jj[pv].push_back(v); int x; while (!q.empty()) { x = q.front(); q.pop(); for (int itt : s) if (lower_bound(g[x].begin(), g[x].end(), itt) == g[x].end()) { q.push(itt); co.push_back(itt); jj[pv].push_back(itt); } else if (*lower_bound(g[x].begin(), g[x].end(), itt) != itt) { q.push(itt); co.push_back(itt); jj[pv].push_back(itt); } for (int i = 0; i < co.size(); i++) s.erase(co[i]); co.clear(); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) s.insert(i); for (int i = 0; i < m; i++) { scanf("%d%d", &k, &l); g[k].push_back(l), g[l].push_back(k); } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); while (s.begin() != s.end()) { bfs(*s.begin()); pv++; } printf("%d\n", pv); for (int i = 0; i < pv; i++) { printf("%d ", jj[i].size()); for (int j = 0; j < jj[i].size(); j++) printf("%d ", jj[i][j]); printf("\n"); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long n, m, k, A[200016], b[200016], co, okay, taken[200016]; bool bsearch(long long x) { memset(b, 0, sizeof b); memset(taken, 0, sizeof taken); co = x - 1, okay = 0; for (long long i = 0; i < m; i++) { for (long long j = co + 1; j < n; j++) { if (b[i] + A[j] <= k) { b[i] += A[j]; taken[j] = 1; co = j; } else break; } } for (long long i = x; i < n; i++) if (!taken[i]) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (long long i = 0; i < n; i++) cin >> A[i]; long long st = -1, en = n, mid = 0; while (en - st > 1) { mid = ((st + en) / 2); if (bsearch(mid)) en = mid; else st = mid; } cout << n - en << "\n"; }
10
CPP
N = int(input()) lis = [int(input()) for i in range(N)] a=1 for i in range(1,N+1): a=lis[a-1] if a == 2: print(i) break if a !=2: print(-1)
0
PYTHON3
n=int(input()) arr=list(map(int,input().split())) count=0 for i in range(1,n-1): if arr[i]==0 and arr[i-1]==1 and arr[i+1]==1: arr[i+1]=0 count+=1 if count==0: print(count) else: print(count)
8
PYTHON3
#include<bits/stdc++.h> using namespace std; string s,t; int main(){ cin>>s>>t; if(t.substr(0,t.size()-1)==s)cout<<"Yes"; else cout<<"No"; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; vector<int> e[200001]; long long dp0[200001], s[200001]; long long a[200001]; void dfs0(int x, int lt) { s[x] = a[x]; for (int y : e[x]) { if (y == lt) continue; dfs0(y, x); dp0[x] += dp0[y] + s[y]; s[x] += s[y]; } } long long an; void dfs(int x, int lt, long long v) { if (v + dp0[x] > an) an = v + dp0[x]; for (int y : e[x]) { if (y == lt) continue; dfs(y, x, v + dp0[x] - dp0[y] - s[y] * 2 + s[1]); } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); e[u].push_back(v); e[v].push_back(u); } dfs0(1, 1); dfs(1, 1, 0); printf("%lld\n", an); return 0; }
12
CPP
def sieve(n): prime = [True for i in range(n+1)] prime[0] = False prime[1] = False p = 2 while (p*p<=n): if prime[p] == True: for i in range(p*p,n+1,p): prime[i] = False p += 1 return prime list1 = sieve(10**6+1) k = input() num = [int(x) for x in input().split()] for j in num: if (j**0.5) == int(j**0.5) and list1[int(j**0.5)]: print('YES') else: print('NO')
8
PYTHON3
import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools,itertools,operator,bisect,fractions,statistics from collections import deque,defaultdict,OrderedDict,Counter from fractions import Fraction from decimal import Decimal from sys import stdout from heapq import heappush, heappop, heapify ,_heapify_max,_heappop_max def primeFactors(n): pf=[] # Print the number of two's that divide n while n % 2 == 0: pf.append(2) n = n / 2 # n must be odd at this point # so a skip of 2 ( i = i + 2) can be used for i in range(3,int(math.sqrt(n))+1,2): # while i divides n , print i ad divide n while n % i== 0: pf.append(int(i)) n = n /i # Condition if n is a prime # number greater than 2 if n > 2: pf.append(int(n)) return pf def main(): mod=1000000007 # InverseofNumber(mod) # InverseofFactorial(mod) # factorial(mod) starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") tc=ri() # zz=0 for _ in range(tc): p, q = ria() if p%q: print(p) else: pf=primeFactors(q) # print(pf) pf=sorted(pf) d=Counter(pf) val=[] pw=p for i in d: pw=p while pw%q==0: pw=pw//i val.append(pw) val=sorted(val) print(val[-1]) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[25][25]; int n, m, f[2][45][45][45][45]; int cal(int odd, int ls, int rs, int lm, int rm) { if (ls > rs || lm > rm) return 0; int &res = f[odd][ls + 1][rs + 1][lm + 21][rm + 21]; if (res != -1) return res; res = 0; vector<int> a; for (int i = (0); i <= (n - 1); i++) for (int j = (0); j <= (m - 1); j++) if (((i + j) & 1) == odd) if (i + j >= ls && i + j <= rs && i - j >= lm && i - j <= rm) { if (s[i][j] == 'L') a.push_back(cal(odd, ls, i + j - 1, lm, rm) ^ cal(odd, i + j + 1, rs, lm, rm)); else if (s[i][j] == 'R') a.push_back(cal(odd, ls, rs, lm, i - j - 1) ^ cal(odd, ls, rs, i - j + 1, rm)); else a.push_back(cal(odd, ls, i + j - 1, lm, i - j - 1) ^ cal(odd, ls, i + j - 1, i - j + 1, rm) ^ cal(odd, i + j + 1, rs, lm, i - j - 1) ^ cal(odd, i + j + 1, rs, i - j + 1, rm)); } sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); while (res < ((int)((a).size())) && a[res] == res) ++res; return res; } int main() { scanf("%d%d", &n, &m); memset(f, -1, sizeof(f)); for (int i = (0); i <= (n - 1); i++) scanf("%s", s[i]); int t = cal(0, 0, n - 1 + m - 1, 0 - (m - 1), n - 1 - 0) ^ cal(1, 0, n - 1 + m - 1, 0 - (m - 1), n - 1 - 0); if (t) printf("WIN"); else printf("LOSE"); return 0; }
10
CPP
#include <iostream> #include <string> #include <sstream> #include <vector> #include <queue> #define repi(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,a) repi(i,0,a) #define all(a) (a).begin(), (a).end() using Flag = std::pair<int, std::pair<int, int> >; struct State { int mask[3], dist, prv; }; int n, m; int cup[3]; int back( int mask ) { for( int i = n-1; i >= 0; --i ) if( mask >> i & 1 ) return i; return -1; } void quePush( std::queue<State> &que, int cup[], int dist, int prv ) { int x = back( cup[0] ), y = back( cup[1] ), z = back( cup[2] ); //printf( "(x,y,z)=(%d,%d,%d)\n", x, y, z ); if( prv ) { if( x > y ) que.push( { { cup[0]^(1<<x), cup[1]|(1<<x), cup[2] }, dist+1, 0 } ); else que.push( { { cup[0]|(1<<y), cup[1]^(1<<y), cup[2] }, dist+1, 0 } ); } if( prv != 1 ) { if( y > z ) que.push( { { cup[0], cup[1]^(1<<y), cup[2]|(1<<y) }, dist+1, 1 } ); else que.push( { { cup[0], cup[1]|(1<<z), cup[2]^(1<<z) }, dist+1, 1 } ); } return; } int main() { while( std::cin >> n >> m, n|m ) { std::queue<State> que; rep( i, 3 ) cup[i] = 0; rep( i, 3 ) { int lc; std::cin >> lc; int x; rep( j, lc ) std::cin >> x, cup[i] |= 1<<(x-1); } //printf( "(%d,%d,%d)\n", cup[0], cup[1], cup[2] ); if( cup[0] == (1<<n)-1 || cup[2] == (1<<n)-1 ) { printf( "%d\n", 0 ); goto end; } quePush( que, cup, 0, -1 ); while( !que.empty() ) { State st = que.front(); que.pop(); if( st.dist > m ) continue; //printf( "(%d,%d,%d,%d)\n", st.mask[0], st.mask[1], st.mask[2], st.dist ); if( st.mask[0] == (1<<n)-1 || st.mask[2] == (1<<n)-1 ) { printf( "%d\n", st.dist ); goto end; } quePush( que, st.mask, st.dist, st.prv ); } puts("-1"); end:; } return 0; }
0
CPP
import math import datetime import collections import statistics import itertools def input_list(): ll = list(map(int, input().split(" "))) return ll ### Start of Main Code def find_maxi_gcd(x) : if x%2 == 0: return x//2 else: return (x-1)//2 t = int(input()) while t: x = int(input()) print(find_maxi_gcd(x)) t-=1
7
PYTHON3
N = int(input()) L = list(map(int, input().split())) L.sort() ans = 0 from itertools import combinations LC = list(combinations(L, 3)) for l in LC: if l[2] < l[0] + l[1]: s = set(l) if len(s) == 3: ans += 1 print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int N; int mDif; vector<int> A; multiset<int> s; int i, iBack; vector<int> best1, best2; int szBest = -1; int iFirst = 0; int count = 0; cin >> N >> mDif; for (i = 0; i < N; i++) { A.push_back(0); cin >> A[i]; } int *high, low; for (i = 0, iBack = 0; i < N; i++) { s.insert(A[i]); while ((*(--s.end()) - *s.begin()) > mDif) s.erase(s.find(A[iBack++])); if ((i - iBack) >= szBest) { if ((i - iBack) > szBest) count = 0; count++; szBest = i - iBack; best1.push_back(iBack); best2.push_back(i); } } cout << szBest + 1 << " " << count << endl; i = best1.size() - 1; while ((i >= 0) && ((best2[i] - best1[i]) == szBest)) { cout << best1[i] + 1 << " " << best2[i] + 1 << endl; i--; } }
11
CPP
N = int(input()) A = list(map(int,input().split())) S = sum(A) S2 = sum(map(lambda x: x*x,A)) print((S*S - S2) // 2 % 1000000007)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 220000; const int MAXS = 256; const int MAXB = 20; int n, q; char s[MAXN]; int a[MAXN], b[MAXN], c[2 * MAXN]; bool d[2 * MAXN]; int sa[MAXN], rnk[2 * MAXN], rnk1[2 * MAXN]; int ct[MAXS], cnt[MAXN], tmp[MAXN]; int height[MAXN], lg[MAXN], h[MAXN][MAXB]; long long sum[2 * MAXN]; struct Node { int id, pos; Node(int i = 0, int p = 0) : id(i), pos(p) {} } sta[2 * MAXN]; int top; inline bool cmp(int a, int b) { return rnk[a] < rnk[b]; } inline int query(int l, int r) { int k = lg[r - l + 1]; return min(h[l][k], h[r - (1 << k) + 1][k]); } inline int lcp(int i, int j) { if (i == j) return n - i + 1; if (rnk[i] > rnk[j]) swap(i, j); return query(rnk[i] + 1, rnk[j]); } int main() { scanf("%d%d%s", &n, &q, s + 1); memset(ct, 0, sizeof ct); memset(rnk, 0, sizeof rnk); for (int i = 1; i <= n; i++) ct[s[i]] = 1; for (int i = 1; i < MAXS; i++) ct[i] += ct[i - 1]; for (int i = 1; i <= n; i++) rnk[i] = ct[s[i]]; for (int k = 0, p = 1; k != n; p <<= 1) { memset(cnt, 0, sizeof cnt); for (int i = 1; i <= n; i++) cnt[rnk[i + p]]++; for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; i--) tmp[cnt[rnk[i + p]]--] = i; memset(cnt, 0, sizeof cnt); for (int i = 1; i <= n; i++) cnt[rnk[i]]++; for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; i--) sa[cnt[rnk[tmp[i]]]--] = tmp[i]; memcpy(rnk1, rnk, sizeof rnk1); rnk[sa[1]] = k = 1; for (int i = 2; i <= n; i++) { if (rnk1[sa[i]] != rnk1[sa[i - 1]] || rnk1[sa[i] + p] != rnk1[sa[i - 1] + p]) k++; rnk[sa[i]] = k; } } for (int i = 1, k = 0; i <= n; i++) { if (rnk[i] == 1) { height[rnk[i]] = k = 0; continue; } if (--k < 0) k = 0; while (s[i + k] == s[sa[rnk[i] - 1] + k]) k++; height[rnk[i]] = k; } lg[0] = 0; for (int i = 1; i <= n; i++) lg[i] = lg[i - 1] + (1 << lg[i - 1] + 1 == i); for (int i = 1; i <= n; i++) h[i][0] = height[i]; for (int j = 1; 1 << j <= n; j++) for (int i = 1; i + (1 << j) - 1 <= n; i++) h[i][j] = min(h[i][j - 1], h[i + (1 << j - 1)][j - 1]); while (q--) { int k, l; scanf("%d%d", &k, &l); for (int i = 1; i <= k; i++) scanf("%d", &a[i]); for (int i = 1; i <= l; i++) scanf("%d", &b[i]); sort(a + 1, a + k + 1, cmp); sort(b + 1, b + l + 1, cmp); for (int i = 1, j = 1, t = 1; i <= k || j <= l; t++) { if (i <= k && (j > l || rnk[a[i]] <= rnk[b[j]])) c[t] = a[i++], d[t] = 0; else if (j <= l && (i > k || rnk[a[i]] > rnk[b[j]])) c[t] = b[j++], d[t] = 1; } long long ans = 0; sum[0] = 0; top = 0; for (int i = 1; i <= k + l; i++) sum[i] = sum[i - 1] + d[i]; for (int i = 1; i <= k + l; i++) { int t = i; while (top > 0 && lcp(c[i], c[i - 1]) < lcp(c[sta[top - 1].id], c[sta[top - 1].id - 1])) { t = min(t, sta[--top].pos); ans += (1ll * (sum[sta[top].id - 1] - sum[sta[top].pos - 2]) * ((i - sta[top].id) - (sum[i - 1] - sum[sta[top].id - 1])) + 1ll * ((sta[top].id - sta[top].pos + 1) - (sum[sta[top].id - 1] - sum[sta[top].pos - 2])) * (sum[i - 1] - sum[sta[top].id - 1])) * lcp(c[sta[top].id], c[sta[top].id - 1]); } sta[top++] = Node(i, t); } while (top > 0) { top--; ans += (1ll * (sum[sta[top].id - 1] - sum[sta[top].pos - 2]) * ((k + l - sta[top].id + 1) - (sum[k + l] - sum[sta[top].id - 1])) + 1ll * ((sta[top].id - sta[top].pos + 1) - (sum[sta[top].id - 1] - sum[sta[top].pos - 2])) * (sum[k + l] - sum[sta[top].id - 1])) * lcp(c[sta[top].id], c[sta[top].id - 1]); } printf("%I64d\n", ans); } return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; if (n == 1) { cout << 1 << endl; continue; } if (n == 2) { cout << 2 << endl; continue; } if (n & 1) { cout << 1 + (n - 1) / 2 << endl; } else { cout << 2 + (n - 2) / 2 << endl; } } }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int q, x; cin >> q >> x; int i; vector<int> y(q); for (i = 0; i < q; i++) cin >> y.at(i); for (i = 0; i < q; i++) y.at(i) %= x; vector<int> nums(x, 0); int now = 0; int req = 0; for (i = 0; i < q; i++) { nums.at(y.at(i))++; while (nums.at(now % x) > req) { now++; if (now % x == 0) req++; } cout << now << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; struct point { int x, y; int k; point(int x, int y, int k) : x(x), y(y), k(k) {} double dist(point p) { return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y); } }; int main() { map<int, vector<point> > S; int n, pop; cin >> n >> pop; for (int i = 0; i < n; ++i) { int x, y, k; cin >> x >> y >> k; S[point(0, 0, 0).dist(point(x, y, k))].push_back(point(x, y, k)); } map<int, vector<point> >::iterator it = S.begin(); int rad = 0; while (pop < 1000000) { if (it == S.end()) { printf("-1\n"); return 0; } rad = it->first; for (vector<point>::iterator j = it->second.begin(); j != it->second.end(); ++j) pop += j->k; it++; } printf("%.7lf\n", sqrt(1.0 * rad)); return 0; }
8
CPP
#include<bits/stdc++.h> using namespace std; using ll = long long; //INSERT ABOVE HERE signed main(){ string s; cin>>s; reverse(s.begin(),s.end()); auto add= [&](string s,string t){ int n=max(s.size(),t.size()); s.resize(n,'0'); t.resize(n,'0'); string a(n,'0'); int u=0; for(int i=0;i<n;i++){ int x=s[i]-'0',y=t[i]-'0'; a[i]+=(x+y+u)%10; u=(x+y+u)/10; } if(u) a.push_back('0'+u); return a; }; string b=s; b=add(b,b); b=add(b,b); b=add(b,b); s=add(s,b); auto check= [&](ll x){ string t=to_string(x*9); reverse(t.begin(),t.end()); string a=add(s,t); ll res=0; for(int i=0;i<(int)a.size();i++) res+=a[i]-'0'; return res<=x*9; }; ll l=0,r=s.size()+10; while(l+1<r){ ll m=(l+r)>>1; if(check(m)) r=m; else l=m; } cout<<r<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int mx = 1e6 + 123; int p[mx], a[mx], b[mx]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> p[i]; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; priority_queue<int, vector<int>, greater<int>> pq[5][5]; for (int i = 1; i <= n; i++) pq[a[i]][b[i]].push(p[i]); int m; cin >> m; for (int i = 1; i <= m; i++) { int c; cin >> c; int ans = INT_MAX; int f, b; for (int i = 1; i <= 3; i++) { if (!pq[c][i].empty() and pq[c][i].top() < ans) { ans = pq[c][i].top(); f = c; b = i; } } for (int i = 1; i <= 3; i++) { if (!pq[i][c].empty() and pq[i][c].top() < ans) { ans = pq[i][c].top(); f = i; b = c; } } if (ans == INT_MAX) ans = -1; else pq[f][b].pop(); cout << ans << " "; } cout << '\n'; }
8
CPP
import re n,m=input().split();print('YNeos'[not re.match(r'[0-9]{'+n+'}-[0-9]{'+m+'}',input())::2])
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> odd, even; long long int sum = 0; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); sum += x; if (x & 1) odd.push_back(x); else even.push_back(x); } sort(odd.rbegin(), odd.rend()); sort(even.rbegin(), even.rend()); int sz = min(odd.size(), even.size()); sum -= accumulate(odd.begin(), odd.begin() + sz, 0); sum -= accumulate(even.begin(), even.begin() + sz, 0); if (odd.size() > sz) sum -= odd[sz]; else if (even.size() > sz) sum -= even[sz]; printf("%lld\n", sum); return 0; }
8
CPP
#include<stdio.h> #include<vector> #include<algorithm> using namespace std; typedef long long ll; ll mod; ll dp[6666][2222]; int main() { int num; scanf("%d%lld", &num, &mod); dp[0][0] = 1; for (int i = 0; i < num * 3; i++) { int t = num * 3 - i; for (int j = 0; j <= num; j++) { dp[i + 3][j + 1] = (dp[i + 3][j + 1] + dp[i][j] * (t - 1)*(t - 2)) % mod; dp[i + 2][j + 1] = (dp[i + 2][j + 1] + dp[i][j] * (t - 1)) % mod; dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod; } } ll ans = 0; for (int i = 0; i <= num; i++)ans += dp[num * 3][i]; printf("%lld\n", ans%mod); }
0
CPP
#include <bits/stdc++.h> constexpr int P = 998244353; int power(int a, int b) { int res = 1; for (; b; b >>= 1, a = 1ll * a * a % P) if (b & 1) res = 1ll * res * a % P; return res; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, m; std::cin >> n >> m; std::vector<int> fac(n + m + 2), invFac(n + m + 2); fac[0] = 1; for (int i = 1; i <= n + m + 1; ++i) fac[i] = 1ll * fac[i - 1] * i % P; invFac[n + m + 1] = power(fac[n + m + 1], P - 2); for (int i = n + m + 1; i; --i) invFac[i - 1] = 1ll * invFac[i] * i % P; int s1 = fac[m - 1], s2 = 0, s3 = 0, dp = 0; for (int s = 1; s <= n; ++s) { s1 = (s1 + 1ll * fac[m - 1 + s] * invFac[s]) % P; s2 = (s2 + 1ll * fac[m - 1 + s] * (P - invFac[s - 1])) % P; int x = 1ll * m * fac[s] % P; dp = (1ll * (s + 1) * s1 + s2) % P * (n + m + 1) % P * invFac[m + s + 1] % P; dp = (dp + 1ll * s3 * invFac[m + s]) % P; dp = 1ll * dp * x % P * (m + s) % P * invFac[s] % P * fac[s - 1] % P; s3 = (s3 + 1ll * dp * fac[m - 1 + s] % P * invFac[s]) % P; } std::cout << dp << "\n"; return 0; }
14
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1234, M = 26; char s[N]; int w[M]; int main() { int ans = 0, max_w = 0, k; scanf("%s%d", s, &k); for (int i = 0; i < M; ++i) scanf("%d", w + i), max_w = max(max_w, w[i]); for (int i = 0, n = strlen(s); i < n; ++i) ans += (i + 1) * w[s[i] - 'a']; for (int i = 0, n = strlen(s); i < k; ++i) ans += (i + n + 1) * max_w; printf("%d\n", ans); return 0; }
8
CPP
input() print(''.join(str(len(d)) for d in input().split('0')))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m; const int N = 123457; vector<int> a[N]; bool siege[N]; int d[3][N]; int v[3][N]; int to[N]; int go(int x, int parent) { vector<int> yy; vector<pair<int, int> > cost[3]; for (int i = 0; i < a[x].size(); i++) { int y = a[x][i]; if (y == parent) continue; if (!go(y, x)) continue; d[0][x] += d[0][y] + 2 * to[y]; yy.push_back(y); cost[1].push_back( make_pair(d[1][y] + to[y] - (d[0][y] + 2 * to[y]), v[1][y])); cost[2].push_back( make_pair(d[2][y] + 2 * to[y] - (d[0][y] + 2 * to[y]), v[2][y])); } if (yy.size() == 1 && !siege[x]) { int y = yy[0]; to[x] = to[y] + 1; d[0][x] = d[0][y]; d[1][x] = d[1][y]; v[1][x] = v[1][y]; d[2][x] = d[2][y]; v[2][x] = v[2][y]; return to[x]; } v[1][x] = x; v[2][x] = x; to[x] = (siege[x] || yy.size() > 0) ? 1 : 0; if (yy.size() == 0) return to[x]; sort(cost[1].begin(), cost[1].end()); d[1][x] = d[0][x] + cost[1][0].first; v[1][x] = cost[1][0].second; sort(cost[2].begin(), cost[2].end()); d[2][x] = d[0][x] + cost[2][0].first; v[2][x] = cost[2][0].second; if (yy.size() > 1) { int candidate = d[0][x] + cost[1][0].first + cost[1][1].first; if (candidate == d[2][x]) { v[2][x] = min(v[2][x], cost[1][0].second); v[2][x] = min(v[2][x], cost[1][1].second); } if (candidate < d[2][x]) { d[2][x] = candidate; v[2][x] = cost[1][0].second; v[2][x] = min(v[2][x], cost[1][1].second); } } if (d[2][x] == d[1][x]) { v[2][x] = min(v[2][x], v[1][x]); } if (d[2][x] > d[1][x]) { d[2][x] = d[1][x]; v[2][x] = v[1][x]; } return to[x]; } int main() { cin >> n >> m; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; x--; y--; a[x].push_back(y); a[y].push_back(x); } for (int i = 0; i < m; i++) { int x; cin >> x; x--; siege[x] = true; } int last = n; for (int i = n - 1; i >= 0; i--) { if (siege[i]) { last = i; break; } } a[n].push_back(last); go(n, -1); cout << v[2][n] + 1 << endl; cout << d[2][n] << endl; return 0; }
10
CPP
#include<bits/stdc++.h> #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(v) (v).begin(),(v).end() #define INF 1e9 typedef long long llong; using namespace std; int main(){ int n; cin>>n; vector<int> a(n);//mmm. REP(i,n) cin>>a[i]; sort(ALL(a)); int cnt=0; while(a.size()>1){ int want=1; while(want<=*(a.end()-1)) want*=2; want-=*(a.end()-1); auto itr=lower_bound(ALL(a),want);//二分探索. if(a.begin()<=itr&&itr<a.end()-1&&*(itr)==want){ cnt++; a.erase(itr); } a.erase(a.end()-1); } cout<<cnt<<endl; }//解答解説参考.
0
CPP
#include <bits/stdc++.h> using namespace std; long long n, all; struct dot { long long x, y; dot(long long x = 0, long long y = 0) : x(x), y(y){}; friend istream &operator>>(istream &in, dot &a) { in >> a.x >> a.y; return in; } }; bool ok(dot a, dot b, dot p) { dot n(b.y - a.y, a.x - b.x); dot pp(p.x - a.x, p.y - a.y); return n.x * pp.x + n.y * pp.y > 0; } dot pnt[100001]; long long count(const dot &p) { long long cnt = 0; for (int a = 0; a < n; a++) { long long l = 1, r = n - 1; while (l != r - 1) { int mid = (l + r) / 2; int b = (a + mid) % n; if (ok(pnt[a], pnt[b], p)) l = mid; else r = mid; } cnt += (l * (l - 1)) / 2; } return all - cnt; } bool vect(dot p, dot a, dot b) { dot aa(a.x - p.x, a.y - p.y); dot bb(b.x - p.x, b.y - p.y); return aa.x * bb.y - aa.y * bb.x < 0; } bool inn(const dot &p) { for (int i = 0; i < n; i++) { int i1 = i, i2 = (i + 1) % n; if (!vect(p, pnt[i1], pnt[i2])) return false; } return true; } int main() { cin >> n; all = n * (n - 1) * (n - 2) / 6; for (int i = 0; i < n; i++) cin >> pnt[i]; int t; cin >> t; while (t--) { dot p; cin >> p; cout << (inn(p) ? count(p) : 0) << '\n'; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << " "; return *this; } } dbg; int check(string s) { if (s == "no") return 0; if (s == "cool" || s == "not bad" || s == "great!") { return 1; } else if (s == "don't think so") { return 1; } else if (s == "don't touch me!") { return 1; } else if (s == "don't even") return 2; else if (s == "terrible") return 2; else if (s == "worse") return 2; else if (s == "go die in a hole") return 2; else if (s == "no way") return 2; else if (s == "are you serious?") return 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string ans; int cnt = 0; int f = 0; for (int i = 0; i < 10; i++) { cout << i << endl; cout << flush; string s; getline(cin, s); int xx = check(s); if (xx == 1) { ans = "normal"; break; } else if (xx == 2) { ans = "grumpy"; break; } } cout << ans << endl; cout << flush; return 0; }
8
CPP
steps = 0 dest = int(input()) if dest >= 5: steps += dest//5 dest %=5 if dest>=4: steps += dest//4 dest %=4 if dest >=3: steps += dest//3 dest %=3 if dest >= 2: steps += dest//2 dest %=2 steps += dest print(steps)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; #define ll long long int int main() { int t; cin>>t; while(t--) { ll n; cin>>n; ll a[n]; ll i; ll sum=0; ll max=INT_MIN; for(i=0;i<n;i++) { cin>>a[i]; if(a[i]>max) { max=a[i]; } sum+=a[i]; } ll d=max*(n-1); if(d>=sum) { ll ans=d-sum; cout<<ans<<endl; } else { double s=sum; ll k=ceil(s/(n-1)); ll p=k*(n-1); ll ans=p-sum; cout<<ans<<endl; } } }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { long n, m, i, j, mn, mx; scanf("%ld %ld", &n, &m); long a[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%ld", &a[i][j]); } } mx = 0; for (i = 0; i < n; i++) { mn = LONG_MAX; for (j = 0; j < m; j++) { mn = min(a[i][j], mn); } mx = max(mn, mx); } printf("%ld", mx); }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[1001], i; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); if (a[0] == 1) printf("-1\n"); else printf("1\n"); }
7
CPP
a = input() b = input() c=[] for i in range(len(a)): z=(int(a[i])+int(b[i]))%2 c.append(z) c=[str(x) for x in c] c=''.join(c) print(c)
7
PYTHON3
n=int(input()) p=[int(k) for k in str(n)] i=1 while sum(p)%4!=0: n=n+1 p=[int(k) for k in str(n)] p=[str(k) for k in str(n)] print(''.join(p))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, ans, p[200001], b[200001]; bool mark[200001], need = 1; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", p + i); for (int i = 1; i <= n; i++) scanf("%d", b + i), need ^= b[i]; for (int i = 1; i <= n; i++) if (!mark[i]) { mark[i] = true; ans++; int v = i; while ((v = p[v]) != i) mark[v] = true; } if (ans == 1) ans--; printf("%d", ans + need); }
7
CPP
import math for _ in range(int(input())): n,m = map(int,input().split()) mat = [] for i in range(n): temp = list(map(int,input().split())) mat.append(temp) for i in range(n): for j in range(m): if mat[i][j]%2 != (i+j)%2: mat[i][j] +=1 for i in range(n): print(*mat[i])
9
PYTHON3
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll n; double dist[1005],x[1005],y[1005],r[1005]; vector< pair<ll,double> >adj[1005]; int main(){ ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>x[0]>>y[0]>>x[1]>>y[1]; cin>>n;n+=2; for(ll i=2;i<n;i++)cin>>x[i]>>y[i]>>r[i]; for(ll i=0;i<n;i++){ for(ll j=i+1;j<n;j++){ double dist = hypot(x[i]-x[j],y[i]-y[j]) - r[i] - r[j]; dist=max(dist , 0.0); adj[i].push_back({j,dist}); adj[j].push_back({i,dist}); } dist[i]=(1.0e+12); } priority_queue< pair<double,ll> > pq; pq.push({0,0}); dist[0]=0; while(!pq.empty()){ ll u = pq.top().second; pq.pop(); for(auto &ele:adj[u]){ if(dist[ele.first] > dist[u] + ele.second ){ dist[ele.first] = dist[u] + ele.second; pq.push({-dist[ele.first] , ele.first}); } } } cout<<fixed<<setprecision(18)<<dist[1]; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int r; r = s[5] * 10 + s[6]; cout << r % 2 << endl; return 0; }
10
CPP
r,c = map(int, input().split()) r0=[];c0=[] for i in range(r): s = input() for j in range(c): if s[j]=="S": r0.append(i) c0.append(j) r0=set(r0);c0=set(c0) print((r*c)-(len(r0)*len(c0)))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int counter = 2; int arr[100000]; int main() { if (fopen("file.in", "r")) { freopen("file.in", "r", stdin); freopen("file.out", "w", stdout); } cin >> n; for (int i = 0; i < 100000; i++) { arr[i] = (i % n) + 1; } int index = 1; for (int i = 0; i < n - 2; i++) { cout << arr[index] << " "; index += counter; counter++; } cout << arr[index] << "\n"; return 0; }
7
CPP
n=int(input()) l=set() for i in range(1,int(n**(1/2))+1): if n%i==0: l.add(n//i+i) print(min(list(l))-2)
0
PYTHON3
from math import ceil from collections import deque for _ in range(int(input())): n, k = [int(i) for i in input().split()] x = [int(i) for i in input().split()] y = [int(i) for i in input().split()] x.sort() a = [] for i in x: a.append((i,1)) a.append((i-k,0)) a.sort() s = [] sav = 0 for i in a: if i[1]: sav -= 1 else: sav += 1 s.append((i[0],sav)) s.sort() mx = [[i[0], 0] for i in s] t = 0 for i in range(len(s)-1, -1, -1): t = max(s[i][1], t) mx[i][1] = t if t == n: print(n) continue mx.append((9999999999, 0)) ans = mx[0][1] j = 0 for i in range(len(s)): while mx[j][0] <= s[i][0]+k and j<len(s): j += 1 ans = max(ans, s[i][1]+mx[j][1]) print(ans)
11
PYTHON3
#include<iostream> using namespace std; #define MOD 1000000007 int h,w; char a[1005][1005]; int dp[1005][1005]; int main(){ cin>>h>>w; for(int i=1;i<=h;i++) cin>>(a[i]+1); dp[0][1]=1; for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) if(a[i][j]=='#') dp[i][j]=0; else dp[i][j]=(dp[i-1][j]+dp[i][j-1])%MOD; cout<<dp[h][w]; }
0
CPP
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int>a(n); vector<int>f(1000005); int mx = 0; for (int i = 0; i < n; i++) { cin >> a[i]; f[a[i]]++; mx = max(mx, a[i]); } int ans = 0; sort(a.begin(), a.end()); for (int i = 0; i < n; i++) { int x = a[i]; if (f[x] == 1)ans++; for (int j = 2; x * j <= mx; j++) f[x * j] = 0; } cout << ans; }
0
CPP
#!/usr/bin/pypy3 # import sys # sys.stdin = open("/home/vaibhav/python/input.txt", "r") # sys.stdout = open("/home/vaibhav/python/output.txt", "w") for _ in range(int(input())): n = int(input()) s = str(input()) ans = "" for i in range(0, 2 * n - 1, 2): ans += s[i] print(ans)
7
PYTHON3
s=input() k=0 if len(s)<7: print("NO") else: for i in range(len(s)-6): sum=int(s[i])+int(s[i+1])+int(s[i+2])+int(s[i+3])+int(s[i+4])+int(s[i+5])+int(s[i+6]) if sum%7==0: k=1 if k==0: print("NO") else: print("YES")
7
PYTHON3
#include <bits/stdc++.h> const int MAXN = 1000 + 5; int n, m; long long c[4][MAXN][MAXN]; void modify(int x, int y, long long k) { int obs = 0; if (x & 1) obs++; if (y & 1) obs++, obs++; for (int i = x; i < MAXN; i += i & (-i)) { for (int j = y; j < MAXN; j += j & (-j)) { c[obs][i][j] ^= k; } } } long long query(int x, int y) { int obs = 0; if (x & 1) obs++; if (y & 1) obs++, obs++; long long res = 0; for (int i = x; i > 0; i -= i & (-i)) { for (int j = y; j > 0; j -= j & (-j)) { res ^= c[obs][i][j]; } } return res; } long long rd() { long long x; scanf("%I64d", &x); return x; } int main() { n = rd(), m = rd(); while (m--) { long long k; int _1x, _2x, _1y, _2y, _3x, _3y, _4x, _4y, opt; scanf("%d", &opt); if (opt == 2) { scanf("%d%d%d%d%I64d", &_1x, &_1y, &_2x, &_2y, &k); modify(_1x, _1y, k); modify(_2x + 1, _2y + 1, k); modify(_1x, _2y + 1, k); modify(_2x + 1, _1y, k); } else if (opt == 1) { scanf("%d%d%d%d", &_1x, &_1y, &_2x, &_2y); long long res1 = query(_1x - 1, _1y - 1); long long res2 = query(_2x, _2y); long long res3 = query(_1x - 1, _2y); long long res4 = query(_2x, _1y - 1); printf("%I64d\n", res1 ^ res2 ^ res3 ^ res4); } } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; long long dp[200007][3]; long long n, l, r; unordered_map<long long, long long> m; long long solve(long long ind, long long sum) { if (ind == n) { if (sum == 0) return 1; else return 0; } if (dp[ind][sum] != -1) return dp[ind][sum]; long long res = 0; for (long long i = 0; i <= 2; i++) res = (res % mod + ((solve(ind + 1, (sum + i) % 3) % mod) * (m[i] % mod)) % mod) % mod; return dp[ind][sum] = res % mod; } int main() { cin >> n >> l >> r; m[l % 3] = (r - l); m[(l + 1) % 3] = (r - l - 1); m[(l + 2) % 3] = (r - l - 2); for (int i = 0; i <= 2; i++) { if (m[i] >= 0) m[i] = m[i] / 3 + 1; else m[i] = 0; } memset(dp, -1, sizeof dp); cout << solve(0, 0) << endl; return 0; }
9
CPP
n,k=map(int,input().split()) a=list(map(int,input().split())) d1={} for i in a: d1[i]=1 if i not in d1 else d1[i]+1 d2={} ans=0 for i in a: d1[i]-=1 if i%k==0: if i//k in d2 and i*k in d1: ans+=(d2[i//k]*d1[i*k]) d2[i]=1 if i not in d2 else d2[i]+1 print(ans)
9
PYTHON3