0
int main()
{
unsigned long long int i,t,N;
cin>>t;                        //INPUT NUMBER OF TEST CASE
while(t--)        
{
    count=0;
    cin>>N;
    if(N==0 || N==1)          //IF FACTORIAL=0 OR 1
        cout<<"1"<<endl;
    else
    {
        i=1;
        while(N!=1)            
        {
            i++;                   
            N/=i;            //REDUCING THE NUMBER
        }
        cout<<i<<endl;
    }
}
return 0;
}

I coded this solution, which gives the correct output for small integers.

I am getting the partially correct output. (Time Limit Exceeded is 2nd case). I think that there can be another approach to solve this question of dynamic programming. Please correct me if I'm wrong.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
confused_
  • 1,133
  • 8
  • 10
  • What are the values passed in the second (faied) case? Are all numbers in input *exactly* factorials or there can be non-factorial (think about this code's behaviour in case of, say, N = 5)? – Bob__ Jun 23 '20 at 08:48
  • According to the given question details : the inputted number is only factorial. and the test case is hidden so I can't provide you with the inputted test cases . – confused_ Jun 23 '20 at 10:16
  • 3
    In the title you mention 10^36, which is far too big to fit into a 64-bit unsigned integer type. Is `unsigned long long` a 128-bit wide type in the testing environment? – Bob__ Jun 23 '20 at 13:35
  • No **unsigned long long int is 64bit** only in the testing environment. It helped me passing the first test case . I am currently working on the large input (10^36) – confused_ Jun 24 '20 at 02:13
  • Then you should use a 128-bit type, if something like `__uint128_t` is an option and if it's enough to store the value, or an arbitrary precision arithmetic library. It would help to know the exact problem statement. – Bob__ Jun 24 '20 at 06:22
  • Thank you I a found a library in c++ to store 128 bit integers – confused_ Jun 24 '20 at 13:04
  • Does this answer your question? [Calculating factorial of large numbers in C](https://stackoverflow.com/questions/1384160/calculating-factorial-of-large-numbers-in-c) – codegorilla Jun 25 '20 at 01:49
  • No, I have seen this post. – confused_ Jun 25 '20 at 12:21

1 Answers1

0
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
uint128_t N = 1;    

After including the library in your program. This stores the input without any problem.

ps: these libraries are not supported by all online judges.

confused_
  • 1,133
  • 8
  • 10