Header Ad

HackerEarth The furious five problem solution

In this HackerEarth The furious five problem-solution You are given an integer N.

Write a program to find a minimum number P such that 1 <= X <= P, Sigma F(X) >= N ( where F(X) represents the number of times X can be divided by 5 ).


HackerEarth The furious five problem solution


HackerEarth The furious five problem solution.

#include <bits/stdc++.h>
#define ll long long

ll minm(ll a, ll b)
{
return (a < b) ? a : b;
}

ll get(ll num)
{
ll ret = 0;
while(num != 0) {
ret += num/5;
num /= 5;
}
return ret;
}

int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
ll low, mid, high;
scanf("%d", &n);
low = 0, high = 1e18;
ll ans = high;
while(low <= high) {
mid = (low+high)/2;
ll val = get(mid);
if(val >= n) {
high = mid-1;
ans = minm(ans, mid);
} else {
low = mid+1;
}
}
printf("%lld\n", ans);
}
return 0;
}

Second solution

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(i= a ; i < b ; ++i)
#define rep(i,n) FOR(i,0,n)
#define INF INT_MAX
#define pb push_back
#define si(n) scanf("%d",&n)
#define pln(n) printf("%lld\n",n)
#define pl(n) printf("%lld ",n)
#define sl(n) scanf("%lld",&n)
#define mod (int)(1e9 + 7)
#define ll long long int

inline ll helper(ll n)
{
ll ans=0,calc;
ans+=n;
calc=5;
while(n/calc!=0)
{
ans+=(n/calc);
calc*=5;
}
return ans;
}
int main()
{
int t,i;
ll calc,s=0,n,cnt,low,high,mid,c1,c2;
si(t);
assert(t>=1 && t<=100000);
while(t--)
{
sl(n);
assert(n>=1 && n<=1000000000);
low=0;
high=mod;
while(low<=high)
{
mid=(low+high)/2;
c1=helper(mid);
c2=helper(mid+1);
if(c1<n && c2>=n)
{
printf("%lld\n",5*(mid+1));
break;
}
else if(n>c1)
low=mid+1;
else
high=mid-1;
}
}
return 0;
}


Post a Comment

0 Comments