-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathUVa12520.cc
78 lines (74 loc) · 1.73 KB
/
UVa12520.cc
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
// UVa12520 Square Garden
// 陈锋
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _zero(D) memset((D), 0, sizeof(D))
#define _init(D, v) memset((D), (v), sizeof(D))
#define _ri1(x) scanf("%d", &(x))
#define _ri2(x, y) scanf("%d%d", &(x), &(y))
#define _ri3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z))
#define _ri4(a, b, c, d) scanf("%d%d%d%d", &(a), &(b), &(c), &(d))
typedef long long LL;
LL solve(LL L, LL N) {
LL g = (L * L + 1) / 2;
if (N <= g) return 4 * N;
if (L % 2) {
LL tN = N - g, ans1 = 4 * g, ans2 = 4 * (g - 1);
/*
*_*
_*_
*_*
*/
LL e = min(tN, L / 2 * 4);
ans1 -= 2 * e, tN -= e;
assert(tN <= (L - 2) * (L - 2) / 2 + 1);
ans1 -= tN * 4;
/*
_*_
*_*
_*_
*/
tN = N - (g - 1);
tN -= min(4LL, tN);
e = min((L / 2 - 1) * 4, tN);
ans2 -= 2 * e, tN -= e;
ans2 -= 4 * tN;
// printf("ans1 = %lld, ans2 = %lld\n", ans1, ans2);
return max(ans1, ans2);
}
/*
_*_*
*_*_
_*_*
*_*_
*/
LL tN = N - g, ans = g * 4;
tN -= min(tN, 2LL); // 先放两个对角
LL e = min(tN, L * 2 - 4);
ans -= 2 * e, tN -= e; // 放四边上
ans -= 4 * tN; // 中间
return ans;
}
int main() {
LL l, n;
while (scanf("%lld%lld", &l, &n) == 2 && l) {
LL ans = solve(l, n);
printf("%lld\n", ans);
}
return 0;
}
// 19903491 12520 Square Garden Accepted C++11 0.000 2017-08-23 11:42:51