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的结果最后有多少个零,肯定不能直接求出,所以打表打出1~x相乘的结果里有多少个因子2,5,这两个因子中较少的那个的数量就是后面零的数量。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct fact
{
int x,y;
}a[1000010];
void fun()
{
int x = 0,y = 0;
for(int i=2;i<=1000000;i++)
{
int num = i;
while(num % 2 == 0)
{
x++;
num /= 2;
}
num = i;
while(num % 5 == 0)
{
y++;
num /= 5;
}
a[i].x = x;
a[i].y = y;
}
}
int main(void)
{
int T,n,r,p,q;
fun();
scanf("%d",&T);
int cas = 1;
while(T--)
{
scanf("%d%d%d%d",&n,&r,&p,&q);
int ans = min(a[n].x - a[r].x - a[n-r].x + (a[p].x - a[p-1].x) * q,
a[n].y - a[r].y - a[n-r].y + (a[p].y - a[p-1].y) * q);
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}