## Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) (964)ABCD

A. Splits

k最大只能到n/2再加上全为1的一项。

``````#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
#include<iomanip>
#include<functional>
using namespace std;
const int maxn = (int)(1e5) + 10;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const double eps = 1e-6;
typedef long long LL;
typedef unsigned long long ull;
int main() {
int n;
while (~scanf("%d", &n)) {
printf("%d\n", n / 2 + 1);
}
return 0;
}``````

B. Messages

Vasya在收邮件，现在给出每封邮件的收到时间，并且每封邮件的价值都为A

Vasya能收到最大的价值是多少。

（笔者比赛时手滑把n写成t就结果过了pre，最后测试wa的难受到爆炸）

``````#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
#include<iomanip>
#include<functional>
using namespace std;
const int maxn = (int)(1e5) + 10;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const double eps = 1e-6;
typedef long long LL;
typedef unsigned long long ull;
int val[1111];
int main() {
int a, b, c, n, t;
while (~scanf("%d%d%d%d%d", &n, &a, &b, &c, &t)) {
for (int i = 0; i < n; i++)
scanf("%d", &val[i]);
if (b >= c)
printf("%d\n", n*a);
else {
int ans = 0;
for (int i = 0; i <n; i++)
ans += (c - b)*(t - val[i]);
printf("%d\n", ans + n*a);
}
}
return 0;
}``````

C. Alternating Sum

``````#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
#include<iomanip>
#include<functional>
using namespace std;
const int maxn = (int)(1e5) + 10;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 9;
const double eps = 1e-6;
typedef long long LL;
typedef unsigned long long ull;
char op[maxn];
LL quick_mod(LL a, LL b) {
LL ans = 1;
a = a % mod;
while (b) {
if (b & 1)ans = (ans * a) % mod;
b >>= 1;
a = (a * a) % mod;
}
return ans%mod;
}
int main() {
LL n, a, b, k;
while (~scanf("%I64d%I64d%I64d%I64d", &n, &a, &b, &k)) {
scanf("%s", op);
while (k*k < n) {//压缩数据
for (int i = 0; i < k; i++)
op[i + k] = op[i];
k = 2 * k;
}
LL tmp = 0;
for (LL i = k - 1, j = 0; j < k; i--, j++) {//因为不知道tmp的正负所以通通修正
if (op[j] == '+')
tmp = ((tmp + quick_mod(a, i)*quick_mod(b, j) % mod) % mod + mod) % mod;
else
tmp = ((tmp - quick_mod(a, i)*quick_mod(b, j) % mod) % mod + mod) % mod;
}
LL ans = 0;
LL st = n + 1 - k, ed = 0;//提取项a的指数和b的指数
for (int i = 0; i < (n + 1) / k; i++) {
ans = ((ans + quick_mod(a, st)*quick_mod(b, ed)%mod*tmp%mod) % mod + mod) % mod;
st -= k, ed += k;
}
st = n - (n + 1) / k*k, ed = n - st;//余项的a的指数和b的指数
for (int i = 0; i < n + 1 - ((n + 1) / k)*k; i++) {
if (op[i] == '+')
ans = ((ans + quick_mod(a, st)*quick_mod(b, ed)) % mod + mod) % mod;
else
ans = ((ans - quick_mod(a, st)*quick_mod(b, ed)) % mod + mod) % mod;
st--, ed++;
}
printf("%I64d\n", ans);
}
return 0;
}``````

D. Destruction of a Tree

``````#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
#include<iomanip>
#include<functional>
using namespace std;
const int maxn = 2*(int)(1e5) + 10;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 9;
const double eps = 1e-6;
typedef long long LL;
typedef unsigned long long ull;
vector<int> edge[maxn];
int sz[maxn];
void dfs1(int u) {
sz[u] = 1;
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
dfs1(v);
sz[u] += sz[v];
}
}
void dfs2(int u) {
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
if (sz[v] % 2 == 0)
dfs2(v);
}
printf("%d\n", u);
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
if (sz[v] % 2)
dfs2(v);
}
}
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++)
edge[i].clear();
int root, v;
for (int u = 1; u <= n; u++) {
scanf("%d", &v);
if (!v)
root = u;
edge[v].push_back(u);
}
if (n % 2 == 0) {
printf("NO\n");
continue;
}
else {
printf("YES\n");
dfs1(root);
dfs2(root);
}
}
return 0;
}``````

E题不存在的