Lightoj 1090 - Trailing Zeroes (II)

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

2

10 4 1 1

100 5 40 5

Sample Output

Case 1: 1

Case 2: 6

题目大意:给你n, r, p, q四个数,问带入C(n,r)*p^q尾部有多少个0;

思路:尾部有多少个0.主要看5的个数,当5和偶数结合就会出现0;

formula

所以为n的5的个数减去m的5的个数还有(n-m)的5的个数,同样2也是,然后p^q中5和2的个数取决于p,然后再*q就好了。

最后比较5和2的最小值,即尾部0的个数。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxx=pow(10,6)+1;
int b[1000010];//5
int c[1000010];//2
void solve()
{
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for(int i=2; i<=maxx; i++)
    {
        int k=i;
        while(k%5==0)//5
        {
            k/=5;
            b[i]++;
        }
        while(k%2==0)
        {
            k/=2;
            c[i]++;
        }
        b[i]+=b[i-1];
        c[i]+=c[i-1];
    }
}
int cn(int n,int r,int p,int q)
{
    int t5=0,t2=0;
    t5=b[n]-b[r]-b[n-r];//5
    t2=c[n]-c[r]-c[n-r];//2

    t5+=q*(b[p]-b[p-1]);//5
    t2+=q*(c[p]-c[p-1]);
    int tt=min(t2,t5);
    return tt;
}
int main()
{
    int t;
    int o=1;
    solve();
    scanf("%d",&t);
    while(t--)
    {
        int n,r,p,q;
        scanf("%d%d%d%d",&n,&r,&p,&q);
        int ans=cn(n,r,p,q);
        printf("Case %d: %d\n",o++,ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值