Find the number of trailing zeroes for the following function:
nCr * pq
where n, r, p, q are given. For example, if n = 10, r = 4, p = 1, q = 1, then the number is 210 so, number of trailing zeroes is 1.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains four integers: n, r, p, q (1 ≤ n, r, p, q ≤ 106, r ≤ n).
Output
For each test case, print the case number and the number of trailing zeroes.
Sample Input |
Output for Sample Input |
2 10 4 1 1 100 5 40 5 |
Case 1: 1 Case 2: 6 |
题意:求组合数C(n,r)乘p的q次方末尾0个数。
分析:大家都做过n!末尾0的个数这一题吧,这题类似。C(n,r)=n!/((n-r)!×r!);
由末尾0,来自最基本因子2×5,分别求出式子中因子2,因子5的个数,取两者最小值即可
#include<stdio.h> #include<string.h> #include<algorithm> #define LL long long using namespace std; int sum5, sum2; void five(int n)//计算n!中包含因子5的个数 { while(n!=0) sum5+=n/5,n/=5; } void two(int n)//计算n!中包含因子2的个数 { while(n!=0) sum2+=n/2,n/=2; } int main() { int T, t=1, n, r, p, q; scanf("%d", &T); while(T--) { sum2=sum5=0; scanf("%d%d%d%d", &n, &r, &p, &q); int s=p; while(s%5==0)//求p中因子5的个数 sum5++,s/=5; while(p%2==0)//求p中因子2的个数 sum2++,p/=2; sum5*=q;sum2*=q; five(-r);two(-r); five(n);two(n); five(r-n);two(r-n); printf("Case %d: %d ", t++, min(sum5, sum2)); } }