Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)
阿新 • • 發佈:2018-04-18
gui bits 水平 base dfs contest mes code end
A.Alternating Sum
等比求和
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e5 + 10; 5 const LL mod = 1e9 + 9; 6 char s[maxn]; 7 8 LL qpow(LL a, LL b) 9 { 10 LL ret = 1LL; 11 while(b) 12 { 13 if(b & 1) ret = ret * a % mod;Aguin14 a = a * a % mod; 15 b >>= 1; 16 } 17 return ret; 18 } 19 20 LL inv(LL x) 21 { 22 return qpow(x, mod - 2); 23 } 24 25 int main(){ 26 LL n, a, b, k; 27 scanf("%I64d %I64d %I64d %I64d %s", &n, &a, &b, &k, s); 28 LL base = qpow(a, n), sum = 0, r = (n + 1) / k, ans; 29 for(int i = 0; i < k; ++i){ 30 if(s[i] == ‘+‘) sum = (sum + base) % mod; 31 else sum = (sum - base + mod) % mod; 32 base = base * b % mod * inv(a) % mod; 33 } 34 LL tmp = qpow(b, k) * inv(qpow(a, k)) % mod; 35 if(tmp == 1) ans = r * sum % mod;36 else ans = sum * (1 - qpow(tmp, r) + mod) % mod * inv((1 - tmp + mod) % mod) % mod; 37 printf("%I64d\n", ans); 38 return 0; 39 }
B.Destruction of a Tree
大家的貪心水平都很高阿
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 2e5 + 10; 4 vector<int> G[maxn], ans; 5 6 vector<int> son[maxn][2][2]; // d/nd y/n 7 int df[maxn], ndf[maxn]; 8 void dfs1(int x, int f){ 9 for(int i = 0; i < G[x].size(); ++i){ 10 int to = G[x][i]; 11 if(to == f) continue; 12 dfs1(to, x); 13 son[x][df[to]][ndf[to]].push_back(to); 14 } 15 if(son[x][0][0].size() || son[x][1][0].size() % 2 == 1 && son[x][1][1].size() == 0) df[x] = 0; 16 else df[x] = 1; 17 if(son[x][0][0].size() || son[x][1][0].size() % 2 == 0 && son[x][1][1].size() == 0) ndf[x] = 0; 18 else ndf[x] = 1; 19 } 20 21 void dfs2(int x, int del_f){ 22 int cnt = 0; 23 for(int i = 0; i < son[x][0][1].size(); ++i){ 24 dfs2(son[x][0][1][i], 0); 25 } 26 if(del_f && son[x][1][0].size() % 2 == 1 || !del_f && son[x][1][0].size() % 2 == 0){ 27 int tmp = son[x][1][1][son[x][1][1].size()-1]; 28 son[x][1][1].pop_back(); 29 dfs2(tmp, 0); 30 } 31 ans.push_back(x); 32 for(int i = 0; i < son[x][1][1].size(); ++i){ 33 dfs2(son[x][1][1][i], 1); 34 } 35 for(int i = 0; i < son[x][1][0].size(); ++i){ 36 dfs2(son[x][1][0][i], 1); 37 } 38 } 39 40 int main(){ 41 int n; 42 scanf("%d", &n); 43 for(int i = 1; i <= n; ++i){ 44 int p; 45 scanf("%d", &p); 46 if(p) G[i].push_back(p), G[p].push_back(i); 47 } 48 dfs1(1, 0); 49 if(df[1] == 0) puts("NO"); 50 else{ 51 dfs2(1, 1); 52 puts("YES"); 53 for(int i = 0; i < n; ++i) printf("%d\n", ans[i]); 54 } 55 return 0; 56 }Aguin
C.Cutting Rectangle
D.Frequency of String
蛤習天下無敵
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e5 + 10; 5 const int INF = 1e9; 6 char s[maxn], T[maxn]; 7 vector<int> pos[maxn], id[maxn]; 8 int k[maxn], ans[maxn]; 9 10 // Hash 11 const int seed = 131; 12 LL mod = 1000000007; 13 LL po[maxn], P[maxn], PT[maxn], t0[maxn]; 14 15 void init(int len) 16 { 17 P[0] = 0, po[0] = 1; 18 for(int i = 1; i <= len; i++) 19 P[i] = (P[i-1] * seed + s[i]) % mod, po[i] = po[i-1] * seed % mod; 20 } 21 map<LL, int> M; 22 23 int main(){ 24 scanf("%s", s + 1); 25 int len = strlen(s + 1); 26 init(len); 27 int n; 28 scanf("%d", &n); 29 for(int kase = 1; kase <= n; ++kase) { 30 scanf("%d%s", k + kase, T + 1); 31 int lt = strlen(T + 1); 32 for (int t = 0; t <= 0; t++) { 33 PT[0] = 0; 34 for (int i = 1; i <= lt; i++) 35 PT[i] = (PT[i - 1] * seed + T[i]) % mod; 36 } 37 t0[kase] = PT[lt]; 38 ans[kase] = INF; 39 id[lt].push_back(kase); 40 } 41 for(int i = 1; i <= len; ++i) { 42 if(id[i].size() == 0) continue; 43 M.clear(); 44 for(int j = 0; j < id[i].size(); ++j){ 45 int x = id[i][j]; 46 M[t0[x]] = x; 47 } 48 for(int j = i; j <= len; ++j){ 49 LL T0 = (P[j] - P[j-i] * po[i] % mod + mod) % mod; 50 if(M.find(T0) == M.end()) continue; 51 int x = M[T0]; 52 pos[x].push_back(j); 53 if(pos[x].size() >= k[x]) ans[x] = min(pos[x][pos[x].size() - 1] - pos[x][pos[x].size() - k[x]] + i, ans[x]); 54 } 55 } 56 for(int i = 1; i <= n; ++i) 57 printf("%d\n", ans[i] == INF ? -1 : ans[i]); 58 return 0; 59 }Aguin
E.Circles of Waiting
Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)