题目:SHOI 2002
若当前已经有了 x 种,再买一个买到不一样的概率为 (n-x)/n,要使这个概率变成 1,则至少再买 n/(n-x) 瓶。
1 #include <cstdio> 2 #include <string> 3 4 long long max(long long x, long long y) { 5 return y < x ? x : y; 6 } 7 8 long long gcd(long long x, long long y) { 9 return y ? gcd(y, x % y) : x; 10 } 11 12 int main() { 13 int n; scanf("%d", &n); long long x = 1, y = 1, t; 14 for (register int i = 2; i <= n; ++ i) { 15 t = y * i / gcd(y, i), x = t / y * x + t / i, y = t; 16 } 17 x *= n, t = gcd(x, y), x /= t, y /= t, t = x / y, x %= y; 18 if (x == 0) { 19 printf("%lld\n", t); 20 } else { 21 int a = 0, b = 0; long long r = t; 22 while (r) r /= 10, ++ a; 23 for (register int i = 1; i <= a; ++ i) putchar(‘ ‘); 24 printf("%lld\n%lld", x, t); 25 r = max(x, y); 26 while (r) r /= 10, ++ b; 27 for (register int i = 1; i <= b; ++ i) putchar(‘-‘); 28 puts(""); 29 for (register int i = 1; i <= a; ++ i) putchar(‘ ‘); 30 printf("%lld\n", y); 31 } 32 return 0; 33 }