-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathR1B_B_s.cpp
84 lines (73 loc) · 1.32 KB
/
R1B_B_s.cpp
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
// Google Code Jam 2013 R1B
// Problem B. Falling Diamonds
// small
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;
static inline int popcount(unsigned int b)
{
#ifdef __GNUC__
return __builtin_popcount(b);
#elif _MSC_VER >= 1400
return __popcnt(b);
#else
b = (b & 0x55555555) + (b >> 1 & 0x55555555);
b = (b & 0x33333333) + (b >> 2 & 0x33333333);
b = (b & 0x0F0F0F0F) + (b >> 4 & 0x0F0F0F0F);
b += b >> 8;
b += b >> 16;
return b & 0x3F;
#endif
}
double solve(int N, int X, int Y)
{
if (Y < 0) {
return 0.0;
}
int length = abs(X) + abs(Y);
int i, j, k;
for (i = 0; i < length; i += 2) {
N -= i * 2 + 1;
}
if (N <= 0) {
// insufficient
return 0.0;
}
if (N >= (length * 2 + 1)) {
// full
return 1.0;
}
if (Y >= length) {
// topmost
return 0.0;
}
int A = 1 << N;
int C = 0;
for (i = 0; i < A; ++i) {
int c = popcount(i);
c += max(0, N - c - length);
C += c > Y;
}
return (double)C / (double)A;
}
int main(int argc, char *argv[])
{
string s;
getline(cin, s);
int T = atoi(s.c_str());
if (T <= 0) return 0;
for (int t = 0; t < T; ++t) {
int N = 0, X = 0, Y = 0;
getline(cin, s);
stringstream ss(s);
ss >> N >> X >> Y;
if (N <= 0) {
break;
}
printf("Case #%d: %.7f\n", t+1, solve(N, X, Y));
}
return 0;
}