【HDOJ】1099 Lottery

时间:2024-12-18 11:04:44

题意超难懂,实则一道概率论的题目。求P(n)。
P(n) = n*(1+1/2+1/3+1/4+...+1/n)。
结果如果可以除尽则表示为整数,否则表示为假分数。

 #include <cstdio>
#include <cstring> #define MAXN 25 __int64 buf[MAXN]; __int64 gcd(__int64 a, __int64 b) {
if (b == ) return a;
else return gcd(b, a%b);
} int main() {
int i, j, k, n;
__int64 fz, fm, tmp, intr; buf[] = ;
for (i=; i<=; ++i)
buf[i] = i*buf[i-]/gcd(i, buf[i-]); while (scanf("%d", &n) != EOF) {
fz = ;
fm = buf[n];
for (i=; i<=n; ++i)
fz += fm/i;
fz *= n;
tmp = gcd(fz, fm);
fz /= tmp;
fm /= tmp;
tmp = fz/fm;
fz -= tmp*fm;
if (fz == ) {
printf("%I64d\n", tmp);
continue;
}
n = ;
intr = tmp;
while (tmp) {
++n;
tmp /= ;
}
for (i=; i<=n; ++i)
printf(" ");
printf("%I64d\n", fz);
printf("%I64d ", intr);
j = k = ;
while (fz) {
++j;
fz /= ;
}
tmp = fm;
while (tmp) {
++k;
tmp /= ;
}
j = (j>k) ? j:k;
for (i=; i<j; ++i)
printf("-");
printf("\n");
for (i=; i<=n; ++i)
printf(" ");
printf("%I64d\n", fm);
} return ;
}