0%

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
ID: wcr19961
PROG: lamps
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 110;
int sta[maxn];
string ans[20];

int main() {
freopen("lamps.in", "r", stdin);
freopen("lamps.out", "w", stdout);
int n, c;
scanf("%d%d", &n, &c);
int x;
while (~scanf("%d", &x) && ~x) {
sta[x - 1] = 1;
}
while (~scanf("%d", &x) && ~x) {
if (sta[x - 1] == 1) {
puts("IMPOSSIBLE");
return 0;
}
sta[x - 1] = -1;
}
int S = 1 << 4, cnt = 0;
for (int i = 0; i < S; ++i) {
int k = __builtin_popcount(i);
if (k == 4 && (c < 4 || c & 1)) {
continue;
}
if (k == 3 && (c < 3 || (c & 1) == 0)) {
continue;
}
if (k == 2 && (c < 2 || (c & 1))) {
continue;
}
if (k == 1 && (c & 1) == 0) {
continue;
}
bool ok = true;
for (int j = 0; j < n; ++j) {
x = 1;
if (i & 1) {
x ^= 1;
}
if ((i & 2) && (j & 1)) {
x ^= 1;
}
if ((i & 4) && (j & 1) == 0) {
x ^= 1;
}
if ((i & 8) && j % 3 == 0) {
x ^= 1;
}
if ((sta[j] == -1 && x == 1) || (sta[j] == 1 && x == 0)) {
ok = false;
}
ans[cnt] += char('0' + x);
}
if (ok) {
++cnt;
} else {
ans[cnt] = "";
}
}
if (!cnt) {
puts("IMPOSSIBLE");
}
sort(ans, ans + cnt);
for (int i = 0; i < cnt; ++i) {
puts(ans[i].c_str());
}
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
ID: wcr19961
PROG: runround
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 10;
int b[maxn];
bool vis[maxn];

bool check(int n) {
int len = 0;
memset(vis, 0, sizeof vis);
while (n) {
b[len] = n % 10;
if (n % 10 == 0 || vis[n % 10]) {
return false;
}
vis[b[len]] = true;
++len;
n /= 10;
}
int p = len - 1;
for (int i = 0; i < len; ++i) {
vis[b[p]] = false;
p = ((p - b[p]) % len + len) % len;
}
if (p != len - 1) {
return false;
}
for (int i = 0; i < 10; ++i) {
if (vis[i]) {
return false;
}
}
return true;
}

int main() {
freopen("runround.in", "r", stdin);
freopen("runround.out", "w", stdout);
int n;
scanf("%d", &n);
for (;;) {
if (check(++n)) {
printf("%d\n", n);
return 0;
}
}
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
ID: wcr19961
PROG: subset
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = (1 << 20) + 10;
int sum[maxn];

int main() {
freopen("subset.in", "r", stdin);
freopen("subset.out", "w", stdout);
int n;
scanf("%d", &n);
int aim = (n * (n + 1)) >> 1;
if (aim & 1) {
puts("0");
return 0;
}
aim >>= 1;
int p = (n + 1) >> 1, S = 1 << p, num = S;
for (int i = 0; i < S; ++i) {
for (int j = 0; j < p; ++j) {
if (i & (1 << j)) {
sum[i] += j + 1;
}
}
}
sort(sum, sum + num);
p = n - p, S = 1 << p;
LL ans = 0;
for (int i = 0; i < S; ++i) {
int tmp = 0;
for (int j = 0; j < p; ++j) {
if (i & (1 << j)) {
tmp += n - j;
}
}
ans += int(upper_bound(sum, sum + num, aim - tmp) - lower_bound(sum, sum + num, aim - tmp));
}
printf("%d\n", int(ans >> 1));
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
ID: wcr19961
PROG: preface
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const char s[] = "IVXLCDM";
const string base[4][10] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
int cnt[4][10][7], ans[7];

int main() {
freopen("preface.in", "r", stdin);
freopen("preface.out", "w", stdout);
for (int i = 0; i < 4; ++i) {
for (int j = 1; j <= 9; ++j) {
for (int k = 0; k < base[i][j].length(); ++k) {
char c = base[i][j][k];
if (c == 'I') {
++cnt[i][j][0];
} else if (c == 'V') {
++cnt[i][j][1];
} else if (c == 'X') {
++cnt[i][j][2];
} else if (c == 'L') {
++cnt[i][j][3];
} else if (c == 'C') {
++cnt[i][j][4];
} else if (c == 'D') {
++cnt[i][j][5];
} else if (c == 'M') {
++cnt[i][j][6];
}
}
}
}
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
int k = 0, x = i;
while (x) {
int tmp = x % 10;
for (int j = 0; j < 7; ++j) {
ans[j] += cnt[k][tmp][j];
}
x /= 10;
++k;
}
}
for (int i = 0; i < 7; ++i) {
if (ans[i] > 0) {
printf("%c %d\n", s[i], ans[i]);
}
}
return 0;
}

链接

传送门

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;

const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int maxn = 10010;
const int maxm = 60010;
int x[maxn], y[maxn], z[maxn];
int u[maxm], v[maxm], dif[maxm];
double len[maxm >> 1];

struct Edge {
int from, to;
double dist;
Edge(int u, int v, double d): from(u), to(v), dist(d) {}
};

struct Dijkstra {
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
double d[maxn];
int p[maxn];

void init(int n) {
this->n = n;
for (int i = 0; i <= n; ++i) {
G[i].clear();
}
edges.clear();
}

void AddEdge(int from, int to, double dist) {
edges.push_back(Edge(from, to, dist));
m = int(edges.size());
G[from].push_back(m - 1);
}

struct HeapNode {
double d;
int u;
HeapNode(double d, int u): d(d), u(u) {}
bool operator < (const HeapNode& rhs) const {
return d > rhs.d;
}
};

void dijkstra(int s) {
priority_queue<HeapNode> Q;
for (int i = 0; i < n; ++i) {
d[i] = inf;
}
d[s] = 0;
memset(done, 0, sizeof done);
Q.push(HeapNode(0, s));
while (!Q.empty()) {
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if (done[u]) {
continue;
}
done[u] = true;
for (int i = 0; i < G[u].size(); ++i) {
Edge& e = edges[G[u][i]];
if (d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to], e.to));
}
}
}
}
} a, b;

int Dif(int i, int j) {
if (z[j] <= z[i]) {
return 0;
}
return floor(100.0 * (z[j] - z[i]) / sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
}

double Dis(int i, int j) {
return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j]));
}

int main() {
int n, m;
while (~scanf("%d%d", &n, &m) && (n || m)) {
a.init(n + 1), b.init(n + 1);
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &x[i], &y[i], &z[i]);
}
m <<= 1;
for (int i = 0; i < m; i += 2) {
scanf("%d%d", &u[i], &v[i]);
v[i + 1] = --u[i], u[i + 1] = --v[i];
len[i >> 1] = Dis(u[i], v[i]);
}
int s, t, d;
scanf("%d%d%d", &s, &t, &d);
--s, --t;
for (int i = 0; i < m; ++i) {
dif[i] = Dif(u[i], v[i]);
double l = len[i >> 1];
if (dif[i] <= d) {
a.AddEdge(u[i], v[i], l);
b.AddEdge(v[i], u[i], l);
}
}
a.dijkstra(s), b.dijkstra(t);
double ans = inf;
for (int i = 0; i < m; ++i) {
if (dif[i] == d) {
double l = a.d[u[i]] + b.d[v[i]] + len[i >> 1];
if (l < ans) {
ans = l;
}
}
}
if (fabs(ans - inf) < eps) {
puts("None");
} else {
printf("%.1f\n", ans);
}
}
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
ID: wcr19961
PROG: hamming
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 65;
int a[maxn];

int main() {
freopen("hamming.in", "r", stdin);
freopen("hamming.out", "w", stdout);
int n, b, d, p = 0, cnt = 0;
scanf("%d%d%d", &n, &b, &d);
putchar('0');
for (int i = 1; i < n; ++i) {
bool ok = false;
while (!ok) {
bool flag = true;
for (int j = 0; j < i; ++j) {
if (__builtin_popcount(p ^ a[j]) < d) {
flag = false;
break;
}
}
if (flag) {
ok = true;
break;
}
++p;
}
a[i] = p;
++cnt;
printf("%c%d", cnt % 10 ? ' ' : '\n', p++);
}
puts("");
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
ID: wcr19961
PROG: holstein
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 25;
const int maxm = 20;
int p[maxn], n, m;
int a[maxm][maxn];

bool better(int a, int b) {
int ca = __builtin_popcount(a), cb = __builtin_popcount(b);
if (ca < cb) {
return true;
}
if (ca > cb) {
return false;
}
for (int i = 0; i < m; ++i) {
if ((a & (1 << i)) != (b & (1 << i))) {
return a & (i << i);
}
}
return true;
}

int main() {
freopen("holstein.in", "r", stdin);
freopen("holstein.out", "w", stdout);
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &p[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%d", &a[i][j]);
}
}
int S = 1 << m, ans = -1;
for (int i = 0; i < S; ++i) {
if (!better(i, ans)) {
continue;
}
bool ok = true;
for (int j = 0; j < n; ++j) {
int sum = 0;
for (int k = 0; k < m; ++k) {
if (i & (1 << k)) {
sum += a[k][j];
}
}
if (sum < p[j]) {
ok = false;
break;
}
}
if (ok) {
ans = i;
}
}
printf("%d", __builtin_popcount(ans));
for (int i = 0; i < m; ++i) {
if (ans & (1 << i)) {
printf(" %d", i + 1);
}
}
puts("");
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
ID: wcr19961
PROG: sort3
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 1010;
int a[maxn], p[5];

int main() {
freopen("sort3.in", "r", stdin);
freopen("sort3.out", "w", stdout);
int n, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
++p[a[i]];
}
p[2] += p[1];
for (int i = 0; i < p[2]; ++i) {
if (i < p[1]) {
if (a[i] == 3) {
for (int j = n - 1; j >= p[1]; --j) {
if (a[j] == 1) {
swap(a[i], a[j]);
break;
}
}
++ans;
} else if (a[i] == 2) {
for (int j = p[1]; j < n; ++j) {
if (a[j] == 1) {
swap(a[i], a[j]);
break;
}
}
++ans;
}
} else {
if (a[i] == 3) {
++ans;
}
}
}
printf("%d\n", ans);
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
ID: wcr19961
PROG: frac1
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 165;
struct Frac {
int a, b;
bool operator < (const Frac& rhs) const {
return a * rhs.b < rhs.a * b;
}
} f[maxn * maxn];

int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}

int main() {
freopen("frac1.in", "r", stdin);
freopen("frac1.out", "w", stdout);
int n, num = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= i; ++j) {
int g = gcd(i, j);
if (g == 1) {
f[num].a = j, f[num].b = i;
++num;
}
}
}
sort(f, f + num);
for (int i = 0; i < num; ++i) {
printf("%d/%d\n", f[i].a, f[i].b);
}
return 0;
}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
ID: wcr19961
PROG: castle
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 55;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {-1, 0, 1, 0};
int a[maxn][maxn], n, m;
int p[maxn][maxn], num[maxn * maxn];
bool vis[maxn][maxn];

void dfs(int i, int j, int id) {
vis[i][j] = true;
p[i][j] = id;
for (int k = 0; k < 4; ++k) {
if ((a[i][j] & (1 << k)) == 0) {
int x = i + dx[k], y = j + dy[k];
if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y]) {
dfs(x, y, id);
}
}
}
}

int main() {
freopen("castle.in", "r", stdin);
freopen("castle.out", "w", stdout);
scanf("%d%d", &m, &n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf("%d", &a[i][j]);
}
}
int cnt = 0, Max = 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!vis[i][j]) {
dfs(i, j, ++cnt);
}
Max = max(Max, ++num[p[i][j]]);
}
}
printf("%d\n%d\n", cnt, Max);
int ansx = 0, ansy = 0;
char ch = 0;
for (int j = 0; j < m; ++j) {
for (int i = n - 1; i >= 0; --i) {
for (int k = 1; k <= 2; ++k) {
if (a[i][j] & (1 << k)) {
int x = i + dx[k], y = j + dy[k];
if (x >= 0 && x < n && y >= 0 && y < m) {
if (p[i][j] != p[x][y]) {
int tmp = num[p[i][j]] + num[p[x][y]];
if (tmp > Max) {
ansx = i, ansy = j;
ch = k == 1 ? 'N' : 'E';
Max = tmp;
}
}
}
}
}
}
}
printf("%d\n%d %d %c\n", Max, ansx + 1, ansy + 1, ch);
return 0;
}