您的位置:首页 > 大数据 > 人工智能

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

2016-03-30 21:45 726 查看
1090 - Trailing Zeroes (II)



 


PDF (English)StatisticsForum
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: