-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightOJ__1104.c
executable file
·75 lines (59 loc) · 1.7 KB
/
lightOJ__1104.c
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
#include <stdio.h>
#include <math.h>
#define SIZE 100005
double power( double base, long long n ){
double res;
if ( n == 0 ) return 1;
res = power(base, n>>1);
res *= res;
if ( n & 1 ) res *= base;
return res;
}
double exactBDayParadox(long n, long long p){
long i;
double res = 1;
for ( i = 1 ; i <= p ; i++ ) res *= (( n - i + 1 ) *1.0) / n;
return 1 - res;
}
double funcBDayParadox( long n, long long p ){
long long pC2 = p;
long i;
double res = 1;
pC2 *= p - 1;
pC2 >>= 1;
return 1 - power(1 - 1.0/n, pC2);
}
long cache[SIZE];
long f( long n ){
double res = 0;
long low = 0, mid, high = n + 1;
if ( cache[n] ) return cache[n];
while ( low + 1 < high ){
mid = ( low + high ) >> 1;
/*printf("low %d high %ld mid %ld...... res previously %lf\n", low, high, mid, res);
getchar();*/
if ( (res = funcBDayParadox(n, mid)) < 0.5 ) low = mid;
else high = mid;
}
/*printf("res now %lf, func(%ld) = %lf\n", res, mid + 1, funcBDayParadox(n, mid + 1));*/
while ( exactBDayParadox(n, low) >= 0.5 ) low--;
return cache[n] = low;
}
int main(){
long n;
int test, kase;
double res;
cache[1] = 1, cache[2] = 1;
/*for ( n = 1 ; n <= 100000 ; n++ ){
res = funcBDayParadox(n, f(n));
if ( res < 0.5 ) printf("%ld %ld\n", n, f(n));
}*/
while ( scanf("%d", &test ) == 1 ){
for ( kase = 1 ; kase <= test ; kase++ ){
scanf("%ld", &n);
/*n = 100000 - kase;*/
printf("Case %d: %ld\n", kase, f(n));
}
}
return 0;
}