-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrailing Zeroes (II).cpp
58 lines (52 loc) · 1.18 KB
/
Trailing Zeroes (II).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
solving: nCr = n!/(r!(n-r)!). find the number of 2 and 5 in this as you know the method.
now p^k = find number of 2 and 5 id p then multiply them by k . ex: 10^2 = factorize(10,2) = 1,factorize(10,5) = 1,factorize(100,2) = 2,factorize(100,5) = 2.
**/
#include<bits/stdc++.h>
using namespace std;
int factorial(int n,int x)
{
int cnt = 0;
while(n>0)
{
cnt += n/x;
n /= x;
}
return cnt;
}
int power(int n,int x)
{
int cnt = 0;
while(n%x == 0)
{
cnt++;
n /= x;
}
return cnt;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,n,r,p,q,cnt = 1,a,b,c,d,e,f,g,h,i;
cin >> t;
while(t--)
{
cin >> n >> r >> p >> q;
a = n-r;
b = factorial(n,2);
c = factorial(n,5);
d = factorial(r,2);
e = factorial(r,5);
f = factorial(a,2);
g = factorial(a,5);
h = power(p,2)*q;
i = power(p,5)*q;
int mn = b+h-d-f;
int mx = c+i-e-g;
int res = min(mn,mx);
cout << "Case " << cnt++ << ": ";
cout << res << "\n";
}
return 0;
}