FCTRL - Editorial

This a really simple solution to it in Java

  public static void main (String[]args) throws java.lang.Exception
  {
    // your code goes here
    Scanner in = new Scanner (System.in);
    int t = in.nextInt ();
    for (int i = 0; i < t; i++)
      {
          int n=in.nextInt();
          int sum=0;
        while (n/5>0)
        {
         sum+=n/5;
         n/=5;
        }
        System.out.println(sum);
      }
  }

it should be (input/5)+(input/25)+(input/125)+(input/625)+(input/3125)...

Create a while loop like this:

   int n=in.nextInt();
          int sum=0;
        while (n/5>0)
        {
         sum+=n/5;
         n/=5;
        }
        System.out.println(sum);

l=[]
n=int(input())
for i in range(n):
l.append(int(input()))
for i in l:
k=5
r=0
while(k<i):
r+=i//k
k*=5
print( r )

//this code works fine in other editor but gives error in codechef why??
///sorry for indentation in comments

1 Like

the actual formula is n/5 + n/5^2 + n/5^3+… up until n becomes 0.So you need to cut off that j loop and start calculation with N
you can check my code here : CodeChef: Practical coding for everyone

1 Like

I am using this code for the problem.

# cook your dish here

for _ in range(int(input())):
    num = int(input())
    zeroes = 0
    power_div = 5
    while num / power_div > 1:
        zeroes += int((num / power_div))
        power_div *= 5
    print(zeroes)

I tested in my machine for the numbers in the editorial, it is working fine for those inputs

I get

4
24
249
1151

for the inputs

23
101
1000
4617

But the submission is returning Wrong answer. Can someone point out the error? Thanks in advance :pray:

just change
while num / power_div > 1:
to
while num // power_div > 0:

It worked but I do not understand why

Since we have to stop below 1. (Read the editorial)
And previously you are doing > 1(Means excluding 1)
Even >=1 also works here (that’s why maybe testcase with 5 fails previously)

I get output as per sample input given on problem statement but submission says solution is wrong, please help pointing mistake with the solution

#include

using namespace std;

int main()
{
int intinput;
cin >> intinput;
int zeros=0;
do
{
intinput = intinput/5;
zeros = intinput+zeros;
} while (intinput != 0);
cout << zeros;
}

Can someone kindly explain what’s wrong in this code?

#include<stdio.h>
int main()
{
int t,n,i,j;
scanf("%d",&t);
for(i=0;i<t;i++)
{

    scanf("%d",&n);
    int c=0;
    for(j=5;n/j>0;j*=5)
    {
        c+=n/j;
    }
    printf("%d",c);
}
return 0;

}

I cant understand the problem. what we have to find here .can someone explain in an easy language.

I cant understand the problem. what we have to find here .can someone explain in an easy language.

I cant understand the problem. what we have to find here .can someone explain in an easy language. why taking 25 , 125 … if they are already multiple of 5

t=int(input())
for i in range(t):
b=0
i=1
num=int(input())
while i>0:
if num/(5i)>=1:
b+=num//(5
i)
i+=1
else:
break
print(b)

I like how they put this in the EASY category but it’s really not

HEY CAN SOMEONE TELL WHY MY CODE IS NOT WORKING , i m getting the right sample output though
#include <stdio.h>
long int gi(float n);
unsigned long int no5(unsigned long int x);
int main(void) {
unsigned int t;
unsigned long int k;
scanf("%d",&t);

while(t–)
{
scanf("%lu “,&k);
printf(”%lu \n",no5(k));
}

return 0;
}
long int gi(float n){
unsigned long x;
x=(unsigned long)n;
return x;
}
unsigned long int no5(unsigned long int x){
unsigned long int z;
int cnt=0;
z=x;
while(1)
{
z=z/5;
if(gi(z)>0)
{
cnt=cnt+gi(z);
}
else
{
return cnt;
break;
}
}
}

Thanks for your wonderfull explanation

#include<bits/stdc++.h>
#define lli long long int
using namespace std;

int main()
{
lli t;
cin>>t;//Testcase
while(t–)
{

lli N;
cin>>N;//N!

lli X  = N;
lli NumOfZeroes = 0;
while(X!=0)
{
        X = X/5;
        NumOfZeroes+=X;}

    cout<<NumOfZeroes<<"\n";

}

return 0;
}

#include
using namespace std;

int main()
{
int t;
cin >> t;
 
for (int k = 0; k < t; k++)
{
int n;
scanf("%d", &n);
 
int c = 0;
while (n >= 5)
{
n /= 5;
c += n;
}
 
printf("%d\n", c);
}
 
return 0;
}

its time complexity is more , than while you do with powers of 5
while (t–)
{
long long n;
cin>>n;

    long long power5=5;
    long long ans=0;
    while (n/power5 >= 1 )
    {
        ans += n / power5;
        power5*=5;
    }
    cout<<ans<<endl;
}