PDF (English) | Statistics | Forum |
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 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).
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 |
#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; }