# Codeforces Round #369 (Div. 2) ABCDE题解

A. Bus to Udayland

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e3 + 50;
const int MAXM = 5e3 + 50;

int n, m;
char s[MAXN][10];

int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d", &n)) {
bool ok = false;
for (int i = 0; i < n; i++) {
scanf("%s", s[i]);
if (!ok) {
if (s[i][0] == 'O' && s[i][1] == 'O') {
s[i][0] = s[i][1] = '+';
ok = true;
}
if (!ok) {
if (s[i][3] == 'O' && s[i][4] == 'O') {
s[i][3] = s[i][4] = '+';
ok = true;
}
}
}
}
if (!ok)
puts("NO");
else {
puts("YES");
for (int i = 0; i < n; i++)
printf("%s\n", s[i]);
}
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}

B. Chris and Magic Square

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 500 + 50;
const int MAXM = 5e3 + 50;

int n, num[MAXN][MAXN];
LL c[MAXN], r[MAXN], d1, d2;

int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d", &n)) {
memset(c, 0, sizeof(c));
memset(r, 0, sizeof(r));
d1 = d2 = 0;
int x, y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &num[i][j]);
if (num[i][j] == 0) {
x = i; y = j;
continue;
}
r[i] += num[i][j];
c[j] += num[i][j];
if (i == j) d1 += num[i][j];
if (i + j == n - 1) d2 += num[i][j];
}
}
if (n == 1) {
puts("1");
continue;
}
LL ans = 0;
if (x > 0)
ans = r[x - 1] - r[x];
else
ans = r[x + 1] - r[x];
r[x] += ans;
c[y] += ans;
if (x == y)
d1 += ans;
if (x + y == n - 1)
d2 += ans;
bool ok = true;
LL flag = r[0];
for (int i = 0; i < n; i++) {
if (r[i] != flag || c[i] != flag) {
ok = false;
break;
}
}
if (d1 != flag || d2 != flag)
ok = false;
if (!ok || ans <= 0)
puts("-1");
else
printf("%I64d\n", ans);
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}

C. Coloring Trees

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 100 + 50;
const int MAXM = 5e3 + 50;

int n, m, k, c[MAXN], cost[MAXN][MAXN];
LL dp[MAXN][MAXN][MAXN];

int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d%d%d", &n, &m, &k)) {
memset(dp, INFLL, sizeof(dp));
memset(cost, 0, sizeof(cost));
for (int i = 1; i <= n; i++)
scanf("%d", &c[i]);
if (!c[1])
c[0] = 101;
else
c[0] = c[1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &cost[i][j]);
}
}
for (int i = 1; i <= m; i++) {
dp[0][i][0] = 0;
}
for (int i = 1; i <= m; i++) {
if (c[1])
dp[1][c[1]][1] = 0;
else
dp[1][i][1] = cost[1][i];
}
for (int i = 1; i <= n; i++) {
if (c[i]) {
if (c[i - 1]) {
for (int l = 1; l < i; l++) {
if (c[i] == c[i - 1])
dp[i][c[i]][l] = min(dp[i][c[i]][l], dp[i - 1][c[i - 1]][l]);
else
dp[i][c[i]][l + 1] = min(dp[i][c[i]][l + 1], dp[i - 1][c[i - 1]][l]);
}
} else {
for (int j = 1; j <= m; j++) {
for (int l = 1; l < i; l++) {
if (c[i] == j)
dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l]);
else
dp[i][c[i]][l + 1] = min(dp[i][c[i]][l + 1], dp[i - 1][j][l]);
}
}
}
} else {
if (c[i - 1]) {
for (int j = 1; j <= m; j++) {
for (int l = 1; l < i; l++) {
if (j == c[i - 1])
dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][j]);
else
dp[i][j][l + 1] = min(dp[i][j][l + 1], dp[i - 1][c[i - 1]][l] + cost[i][j]);
}
}
} else {
for (int j1 = 1; j1 <= m; j1++) {
for (int j2 = 1; j2 <= m; j2++) {
for (int l = 1; l < i; l++) {
if (j1 == j2)
dp[i][j1][l] = min(dp[i][j1][l], dp[i - 1][j1][l] + cost[i][j1]);
else
dp[i][j1][l + 1] = min(dp[i][j1][l + 1], dp[i - 1][j2][l] + cost[i][j1]);
}
}
}
}
}
}
LL ans = INFLL;
if (c[n])
ans = min(ans, dp[n][c[n]][k]);
else
for (int i = 1; i <= m; i++) {
ans = min(ans, dp[n][i][k]);
}
printf("%I64d\n", ans == INFLL ? -1 : ans);
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 50;
const int MAXM = 2e5 + 50;

int n;
struct edge {
int to, nxt;
} E[MAXM];
void edge_init() {
tot = 0;
}
void edge_add(int u, int v) {
E[tot].to = v;
}

/*
for (int i = 1; i <= n; i++)
if (!DFN[i])
Tarjan(i);
*/
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN], num[MAXN];
int Index, top, scc;
bool InStack[MAXN];
void Tarjan_init() {
memset(DFN, 0, sizeof(DFN));
memset(num, 0, sizeof(num));
memset(InStack, false, sizeof(InStack));
Index = scc = top = 0;
}
void Tarjan(int u) {
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
InStack[u] = true;
for(int i = Head[u]; i != -1; i = E[i].nxt) {
v = E[i].to;
if( !DFN[v] ) {
Tarjan(v);
if( Low[u] > Low[v] )Low[u] = Low[v];
} else if(InStack[v] && Low[u] > DFN[v])
Low[u] = DFN[v];
}
if(Low[u] == DFN[u]) {
scc++;
do {
v = Stack[--top];
InStack[v] = false;
Belong[v] = scc;
num[scc]++;
} while( v != u);
}
}

LL fastpow(LL a, LL c) {
LL res = 1;
while (c) {
if (c & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
c >>= 1;
}
return res;
}

int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d", &n)) {
edge_init();
Tarjan_init();
for (int i = 1; i <= n; i++) {
int t;
scanf("%d", &t);
}
for (int i = 1; i <= n; i++)
if (!DFN[i])
Tarjan(i);
LL ans = 1;
int tmp = n;
for (int i = 1; i <= scc; i++) {
if (num[i] > 1) {
ans = (ans * (fastpow(2, num[i]) - 2)) % MOD;
tmp -= num[i];
}
}
if (tmp)
ans = (ans * fastpow(2, tmp)) % MOD;
printf("%I64d\n", ans);
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}

E. ZS and The Birthday Paradox

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
#define bitcnt(x) __builtin_popcount(x)
#define bitcntll(x) __builtin_popcountll(x)
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e6 + 3;
const int MAXN = 2e5 + 50;
const int MAXM = 2e5 + 50;

LL n, k;

LL fastpow(LL a, LL c) {
LL res = 1;
while (c) {
if (c & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
c >>= 1;
}
return res;
}

int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%I64d%I64d", &n, &k)) {
if (n <= 62 && k > (1LL << n)) {
printf("1 1\n");
continue;
}
LL cnt = 0;
LL p = fastpow(2, n);
for (LL i = k - 1; i >= 1; i >>= 1) {
cnt += i / 2;
}
//        for (LL i = tmp - 1; i >= tmp - k + 1; i--) {
//            LL ttmp = i & (-i);
//            cnt += (LL)log2(ttmp);
//        }
LL inv = fastpow(fastpow(2, cnt), MOD - 2);
LL up = 1, down = fastpow(p, k - 1);
for (LL i = 1; i <= k - 1; i++) {
LL tmp = (p - i + MOD) % MOD;
up = (up * tmp) % MOD;
if (tmp <= 0) //！！！
break;
}
up = up * inv % MOD;
down = down * inv % MOD;
up = (down - up + MOD) % MOD;
printf("%I64d %I64d\n", up, down);
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}

• 本文已收录于以下专栏：

## Codeforces Round #369 (Div. 2) D. Directed Roads （dfs+组合数学 图论）

• guhaiteng
• 2016年08月30日 19:06
• 373

## 解题报告：Codeforces Round #433 (Div. 2) E. Boredom ( 离线处理+树状数组）

• qq_32570675
• 2017年09月07日 09:56
• 332

## Codeforces Round #460 (Div. 2)题目题解

A.Supermarket (水题) #include #include #include #include #include using namespace std; int mai...
• wyxeainn
• 2018年01月31日 23:17
• 72

## Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作，都会拿走一个横行，一个竖行。 所以一共会操作min（横行，竖行）次。 #include #include #include #include ...
• rowanhaoa
• 2014年07月25日 14:39
• 1470

## 【解题报告】Codeforces Round #401 (Div. 2)

• TRiddle
• 2017年02月25日 17:55
• 408

## Codeforces Round #395 (Div. 2)(ABCD)

ps:打完这场cf才知道自己真的很菜，还是停留在AB题的水平，有时候CD其实很简单，但就是想不到，别人一眼看出而我就是想不到，有时候想到了点子上但就是突破不了 题目链接：  Codeforc...
• qq_34731703
• 2017年02月03日 12:50
• 486

## Codeforces Round #304 (Div. 2) A,B,C题解

A. Soldier and Bananas time limit per test 1 second memory limit per test 256 megabytes input ...
• qq_24489717
• 2015年05月27日 13:48
• 999

## Codeforces Round #256 (Div. 2) E. Divisors

E. Divisors time limit per test 2 seconds memory limit per test 256 megabytes input...
• modiz
• 2014年07月18日 23:37
• 586

## Codeforces Round #456 (Div. 2) D. Fishes

D. Fishes time limit per test 1 second memory limit per test 256 megabytes input standard input ...
• Haipai1998
• 2018年01月20日 14:06
• 34

## Codeforces Round #459 (Div. 2)The Monster[匹配问题]

C. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard...
• Haipai1998
• 2018年01月30日 15:20
• 122

举报原因： 您举报文章：Codeforces Round #369 (Div. 2) ABCDE题解 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)