【HDOJ】1099 Lottery
题意超难懂,实则一道概率论的题目。求P(n)。
P(n) = n*(1+1/2+1/3+1/4+...+1/n)。
结果如果可以除尽则表示为整数,否则表示为假分数。
1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return gcd(b, a%b); 11 } 12 13 int main() { 14 int i, j, k, n; 15 __int64 fz, fm, tmp, intr; 16 17 buf[1] = 1; 18 for (i=2; i<=22; ++i) 19 buf[i] = i*buf[i-1]/gcd(i, buf[i-1]); 20 21 while (scanf("%d", &n) != EOF) { 22 fz = 0; 23 fm = buf[n]; 24 for (i=1; i<=n; ++i) 25 fz += fm/i; 26 fz *= n; 27 tmp = gcd(fz, fm); 28 fz /= tmp; 29 fm /= tmp; 30 tmp = fz/fm; 31 fz -= tmp*fm; 32 if (fz == 0) { 33 printf("%I64d\n", tmp); 34 continue; 35 } 36 n = 0; 37 intr = tmp; 38 while (tmp) { 39 ++n; 40 tmp /= 10; 41 } 42 for (i=0; i<=n; ++i) 43 printf(" "); 44 printf("%I64d\n", fz); 45 printf("%I64d ", intr); 46 j = k = 0; 47 while (fz) { 48 ++j; 49 fz /= 10; 50 } 51 tmp = fm; 52 while (tmp) { 53 ++k; 54 tmp /= 10; 55 } 56 j = (j>k) ? j:k; 57 for (i=0; i<j; ++i) 58 printf("-"); 59 printf("\n"); 60 for (i=0; i<=n; ++i) 61 printf(" "); 62 printf("%I64d\n", fm); 63 } 64 65 return 0; 66 }