#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cnt_p2(ll n)
{
ll p_2=2,cnt=0;
while(p_2<=n)
{
cnt+=n/p_2;
p_2*=2;
}
return cnt;
}
ll cnt_p5(ll n)
{
ll p_5=5,cnt=0;
while(p_5<=n)
{
cnt+=n/p_5;
p_5*=5;
}
return cnt;
}
ll cnt_div(ll n,ll ret_val)
{
ll i=2,j,k,l,m,cnt2=0,cnt5=0;
while(1)
{
if(n%2!=0)
break;
else
{
cnt2++;
n=n/2;
}
}
while(1)
{
if(n%3!=0)
break;
else
{
n=n/3;
}
}
while(1)
{
if(n%5!=0)
break;
else
{
cnt5++;
n=n/5;
}
}
//cout<<cnt2<<" "<<cnt5<<endl;
if(ret_val==2)
return cnt2;
else
return cnt5;
}
int main()
{
//cout<<cnt_div(10,5)<<endl;
ll i,j,k,l,m,n,r,p,q,test,cn,cr,cnr,cp;
cin>>test;
for(ll in=1;in<=test;in++)
{
cin>>n>>r;
cin>>p>>q;
i=cnt_p2(n);
j=cnt_p5(n);
k=cnt_p2(r);
l=cnt_p5(r);
ll tmp=n-r;
m=cnt_p2(tmp);
n=cnt_p5(tmp);
ll x,y;
x=cnt_div(p,2)*q;
y=cnt_div(p,5)*q;
//cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<m<<" "<<n<<" "<<x<<" "<<y<<endl;
ll ans=min(i-k-m+x,j-l-n+y);
cout<<"Case "<<in<<": "<<ans<<endl;
}
return 0;
}
using namespace std;
typedef long long ll;
ll cnt_p2(ll n)
{
ll p_2=2,cnt=0;
while(p_2<=n)
{
cnt+=n/p_2;
p_2*=2;
}
return cnt;
}
ll cnt_p5(ll n)
{
ll p_5=5,cnt=0;
while(p_5<=n)
{
cnt+=n/p_5;
p_5*=5;
}
return cnt;
}
ll cnt_div(ll n,ll ret_val)
{
ll i=2,j,k,l,m,cnt2=0,cnt5=0;
while(1)
{
if(n%2!=0)
break;
else
{
cnt2++;
n=n/2;
}
}
while(1)
{
if(n%3!=0)
break;
else
{
n=n/3;
}
}
while(1)
{
if(n%5!=0)
break;
else
{
cnt5++;
n=n/5;
}
}
//cout<<cnt2<<" "<<cnt5<<endl;
if(ret_val==2)
return cnt2;
else
return cnt5;
}
int main()
{
//cout<<cnt_div(10,5)<<endl;
ll i,j,k,l,m,n,r,p,q,test,cn,cr,cnr,cp;
cin>>test;
for(ll in=1;in<=test;in++)
{
cin>>n>>r;
cin>>p>>q;
i=cnt_p2(n);
j=cnt_p5(n);
k=cnt_p2(r);
l=cnt_p5(r);
ll tmp=n-r;
m=cnt_p2(tmp);
n=cnt_p5(tmp);
ll x,y;
x=cnt_div(p,2)*q;
y=cnt_div(p,5)*q;
//cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<m<<" "<<n<<" "<<x<<" "<<y<<endl;
ll ans=min(i-k-m+x,j-l-n+y);
cout<<"Case "<<in<<": "<<ans<<endl;
}
return 0;
}
No comments:
Post a Comment