LightOJ - 1090 Trailing Zeroes (II)
nCr * pq
求该表达式末尾0的个数。
显然至于2 5 的因子个数有关。
考虑预处理出每个数的2,5的因子个数,并统计前缀和(题个的组合数其实是阶乘)
int _2[maxn], _5[maxn]; ll _s2[maxn], _s5[maxn]; int _count(int x, int y) { int res = 0; while (x % y == 0) x /= y, res++; return res; } void init() { for (int i = 2; i < maxn; i++) { _2[i] = _count(i, 2); _5[i] = _count(i, 5); _s2[i] = _s2[i - 1] + _2[i]; _s5[i] = _s5[i - 1] + _5[i]; } } int main() { init(); int T = readint(); int n, r, p, q; int kase = 1; while (T--) { n = readint(), r = readint(), p = readint(), q = readint(); int c1 = _s2[n], c2 = _s5[n]; c1 -= _s2[r] + _s2[n - r]; c2 -= _s5[r] + _s5[n - r]; int c3 = _2[p] * q; int c4 = _5[p] * q; printf("Case %d: %d\n", kase++, min((c1 + c3), (c2 + c4))); } }