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

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值