help with FCTRL algorithm

why is this code rejected ,

#include <iostream>
using namespace std;
int main ()
{
    unsigned int T, result, N, count , i;
    cin  >> T;
    while (T--)
    {
    	cin >> N;
        result = count = 0;
        for ( i = 5; i < N ; i = i * 5)//i (5 and its powers) is supposed to be less than N
        {
                result = result+N/i;
        }
    cout << result << endl;
    }
    return 0; 
 }

and this one is accepted :

#include <iostream>
using namespace std;
int main ()
{
    unsigned int T, result, N, count , i;
    cin  >> T;
    while (T--)
    {
    	cin >> N;
        result = count = 0;
        for ( i = 5; N/i >= 1 ; i = i * 5)
        {
                result = result+N/i;
        }
    cout << result << endl;
    }
    return 0;
} 

To me both conditions seem same.
In first code I am checking that the 5 and its powers are within the range.
In second code I am checking the division output of N! and 5 (and its powers).

why is my first code wrong ?

in your first code replace for ( i = 5; i < N ; i = i * 5) by for ( i = 5; i <= N ; i = i * 5) then you will get AC.

1 Like

Thank you so much !!

@vijju123 the increment condition in the for loop is i = i * 5, so it is logarithmic with base 5. It won’t get TLE.

oh sorry, lol, i thought its i++. My mistake :stuck_out_tongue:

1 Like