FCTRL - Editorial

WHY IS THIS ANSWER WRONG ?

#include <stdio.h>
#include <math.h>
main()
{
int s,x,i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
s=x/5+x/25+x/125+x/625+x/3125+x/15625+x/pow(5,7)+x/pow(5,8)+x/pow(5,9)+x/pow(5,10)+x/pow(5,11)+x/pow(5,12)+x/pow(5,13);
printf("%d\n",s);
}
return 0;
}

Hello I am doing it with python. All of test cases are passing but My solution is not accepted .Can someone help please.
Here is my code :

  def fact(x):
    count=0
    while x/5>0:
        count+=x/5
        x=x/5
    #print("hello")
    print (count)
t=int(input())

for i in range(t):
    fact(int(input()))

import java.util.;
import java.io.
;

public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int noftimes=sc.nextInt();
for (int i = 0; i < noftimes; i++) {
int value=sc.nextInt();
int count=getValue(value/5);
System.out.println(String.valueOf(count));
}

}
private static int getValue(int n) {
if (n <= 5) {
return n;
}
return getValue(n / 5) + n;
}
}

#include<stdio.h>
int main()
{
unsigned n, mul;
unsigned t=0,i,z=0,temp,c2=0,c5=0;
scanf ( “%u”, &t );
while ( t-- ) {
c5=0;
c2=0;
scanf ( “%u”, &n );
for ( i = 1 ;i <= n ; i++){
mul *= i;
temp = i;
while ( temp % 5 == 0 && temp > 0){
c5++;
temp = temp / 5 ;
}
while ( temp % 2 == 0 && temp > 0){
c2++;
temp = temp / 2 ;
}
}
printf("%u\n",c5);
}
return 0;
}

why is this not running in console? why are judges facing infiniteloop here.

#include<stdio.h>
int fact(int);
int zeroes(int);
main()
{
int p,i,j,a,b;
printf(“enter the number of numbers to be entered”);scanf("%d",&p);
for(i=0;i<p;i++){scanf("%d",&j);a=fact(j);b=zeroes(a);printf("%d",b);}}
int fact(int j)
{int f;if(n==1)f=1;else f=f*fact(j-1);return f;}
int zeroes(int a);
{int y=0,z;while(z==0){z=a%10;if(z==0){y=y+1;a=a%10}}return y;}

#include<stdio.h>
int fact(int);
int zeroes(int);
main()
{
int p,i,j,a,b;
printf(“enter the number of numbers to be entered”);scanf("%d",&p);
for(i=0;i<p;i++){scanf("%d",&j);a=fact(j);b=zeroes(a);printf("%d",b);}}
int fact(int j)
{int f;if(n==1)f=1;else f=f*fact(j-1);return f;}
int zeroes(int a);
{int y=0,z;while(z==0){z=a%10;if(z==0){y=y+1;a=a%10}}return y;}

#include
#include
using namespace std;
int main()
{
unsigned long long int t, n, i, maxpo, sum;
cin>>t;
while(t–)
{
cin>>n;
maxpo = sum = 0;
maxpo = (int)(log(n)/log(5));
for(i = 1;i <= maxpo;i++)
sum += n/pow(5,i);
cout<<sum<<endl;
}
}

instead of finding the factors why can’t we actually count the no of zero’s of that number using “number modulo 10” which gives us the last digit in that number

can we solve this by actually counting the trailing zero’s and not by the factor method? Won’t that be much simpler?

#include<stdio.h>
int calc (int entr);
int main(void)
{
long num;
scanf("%ld",&num);
long entr[num];
long fact[num];
for(int i=0;i<num;i++)
{
scanf("%ld",&entr[i]);
fact[i]=calc(entr[i]);

}
for(int i=0;i<num;i++)
    printf("%ld\n",fact[i]);
return 0;    

}
int calc (int entr)
{
int fin= entr/5;
if(fin==0)
return 0;
else
return fin+calc(fin);

}

#include<stdio.h>
int calc (int entr);
int main(void)
{
long num;
scanf("%ld",&num);
long entr[num];
long fact[num];
for(int i=0;i<num;i++)
{
scanf("%ld",&entr[i]);
fact[i]=calc(entr[i]);

}
for(int i=0;i<num;i++)
    printf("%ld\n",fact[i]);
return 0;    

}
int calc (int entr)
{
int fin= entr/5;
if(fin==0)
return 0;
else
return fin+calc(fin);

}

#include<stdio.h>
#include<math.h>
long int zeroes(long int a);
int main()
{
int n,i=0;
long int k[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%ld\n",&k[i]);
}
for(i=0;i<n;i++)
{
zeroes(k[i]);
}
return 0;
}
long int zeroes(long int a)
{
int z=1,b=0,i=0;
while(a>5*z)
{
z=pow(5,i+1);
b+=a/z;
i+=1;
}
printf("%ld\n",b);
}

why wrong?

#include
using namespace std;
int main()
{int x,t,i;
cin>>t;
if(t<=10000)
{for(int j=0;j<t;j++)
{int count=0;
cin>>x;
if(x<=1000000000 && x>=1)
{for(i=5;x/i>=1;i=i*5)
{count=count+(x/i);}
cout<<count<<"\n";}
else
break;
}}

}

Could someone please tell me what is wrong with this code.
Basic idea behind my logic is i calculate the factorial by multiplying all numbers from 1 to N(N being the number itself) and at each juncture i check the condition whether the factorial at that particular instant is divisible by 10 leaving a remainder of 0 and if it is i am calculalating the sum at the end.
here goes the code.

#include

using namespace std;

int main()
{
int a,b,c,fact=1,sum=0,e=0;
cin>>a;//for taking in the total number of input
int f=0;
for(b=0;b<a;b++)
{
cin>>c;//selecting each input value

for(int d=1;d<=c;d++)
{f=0;
    fact=fact*d;
    e=fact;
    while(f==0)
    {
    if(fact%10==0)
    {
        sum++;
        fact=fact/10;
        f=0;
    }
    else{f=1;}}
    fact=e;
}
cout<<sum<<endl;
sum=0;
fact=1;}}

@dark_pharoah Your logic is correct but it takes more time, quite cumbersome and is prone to mistakes. The logic is 0’s are formed by combination of 5’s and 2’s(since 5x2=10) Agree? For example 30=2x3x5(0 is formed by 5 and 2). So, we have to find min(number of 2’s,number of 5’s) in n! so that they can combine to form 0’s. That can be found by:-

number of 2’s->[n/2]+[n/2^2]+… until it is 0

number of 5’s->[n/5]+[n/5^2]+… until it is 0, Here [] is floor

The reason for this is explained clearly in the above editorial. Have a look at it.

We can observe that number of 5’s will be less as it’s a big number(so just calculate number of 5’s)

So, the code goes as follows:- Click Here

1 Like

#include
using namespace std;

int mmofive( long int t)
{ int n=0, pof=5;
while ( t/pof>0)
{pof*=5; n++;}
return n-1;
}//max divisible multiple of 5 (power)

int main()
{
long int n,t; int i,j;
int trailingzero=0;
cin>>n;

const long int f=n;
int A[f];
for ( i=0; i<n; i++)
{cin>>t;
for(j=5;j<=5^mmofive(t);j*=5)
{trailingzero+=t/j;}
A[i]=trailingzero;
trialingzero=0;
}

for (long int i=0; i<n; i++)
{cout<<A[i]<<endl;}

return 0;
}

WHY NOT WORKING? :((
i devised the same logic as expected.
but it says it exceeds time limit.
i believe, even if the number is as large as 100000000 , on dividing by 5 continuously, the final output should take quite less time.

#include <stdio.h>

int main()
{
int n = 4617, troll = 0, i=1, num = 5,quotient;

while(quotient > 1)
{
    i = num * i;
    //printf("%d",i);
    quotient = n/i;
    troll = troll + quotient;
}
printf("%d\n",troll);

return 0;

}

Help in C#!!
My Code is Running successfully, giving desires o/p
But on submit getting error as Wrong Answer.

using System;
public class Test
{
public static void Main()
{// your code goes here
int n = Convert.ToInt32(Console.ReadLine());
int[] N = new int[n];
for(int i=0;i<N.Length;i++)
{
N[i] = Convert.ToInt32(Console.ReadLine());
}
for(int i=0;i<N.Length;i++)
{
int fact = 5;
int trailingzeros = 0;
while(N[i]>fact)
{
trailingzeros = trailingzeros + (N[i]/fact);
fact = fact*5;
}
Console.WriteLine(trailingzeros);
}
}
}

Why doesn’t this work?

#include <iostream>
using namespace std;
int main()
{
	// your code goes here
int f=9;
	do
    {
	    int n;
	    cin>>n;
	    int fac=1;
	    for(int i=1; i<=n; i++)
	    {
	        fac= fac*i;
	    }
	        int cont=0;
            while(fac%10==0)
            {
                cont++;
                fac= fac/10;
            }
	        cout<<cont<<endl;

	} while(f==9);
	return 0;
}

Why can’t I simply find the answer by cout<<(input/5)+(input/25); please explain it to me somebody? Why do I need to divide it if I can find the answer by doing the above method. But if I am trying to do (input/5)+(input/25) I am getting WA. Please help!