LightOJ 1090 - Trailing Zeroes (II) (求式子结果0的个数)

1090 - Trailing Zeroes (II)
Time Limit: 2 second(s)Memory Limit: 32 MB

Find the number of trailing zeroes for the followingfunction:

nCr * pq

where n, r, p, q are given. For example, if n =10, r = 4, p = 1, q = 1, then the number is210 so, number oftrailing 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 oftrailing 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)中2的个数和5的个数,怎么求呢,设F2(x)表示x!中2的个数,那么C(n,r)中2的个数就是
                                                               [F2(n)-F2[n-r]]/F2(r)
同理可以求出5的个数,然后就是求p^q中2和5的个数了,直接分解p然后结果乘q就行了,最后取cnt2和cnt5的最小值



ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 101000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int main()
{
    int n,r,p,q;
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&r,&p,&q);
        int nn=n;
        int cnt1=0,cnt2=0;
        int k=r;
        while(nn)
        cnt1+=nn/5,nn/=5;
        nn=n-r;
        while(nn)
        cnt1-=nn/5,nn/=5;
        nn=n;
        while(nn)
        cnt2+=nn/2,nn/=2;
        nn=n-r;
        while(nn)
        cnt2-=nn/2,nn/=2;
        while(k)
        cnt1-=k/5,k/=5;
        k=r;
        while(k)
        cnt2-=k/2,k/=2;
        while(p%2==0)
        p/=2,cnt2+=q;
        while(p%5==0)
        p/=5,cnt1+=q;
        printf("Case %d: %d\n",++cas,min(cnt1,cnt2));
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值