fork(1) download
  1. #include<bits/stdc++.h>
  2. #define REP(i,n) for (int i = 1; i <= n; i++)
  3. #define mod 1000000007
  4. #define pb push_back
  5. #define ff first
  6. #define ss second
  7. #define ii pair<int,int>
  8. #define vi vector<int>
  9. #define vii vector<ii>
  10. #define lli long long int
  11. #define endl '\n'
  12.  
  13. using namespace std;
  14. int b;
  15. int fact[10];
  16.  
  17. void initFactorial()
  18. {
  19. fact[0] = 1;
  20. for(int i=1;i<10;i++)
  21. fact[i] = fact[i-1] * i;
  22. }
  23.  
  24. bool isFactorian(int N)
  25. {
  26. int value = 0;
  27. int temp = N;
  28.  
  29. while(temp)
  30. {
  31. int current_digit = temp % b;
  32. temp /= b;
  33.  
  34. value += fact[current_digit];
  35. }
  36.  
  37. return value == N;
  38. }
  39.  
  40. int getBiggestFactorian(int N)
  41. {
  42. for(int i=N-1;i>=1;i--)
  43. if(isFactorian(i)) return i;
  44.  
  45. return -1;
  46. }
  47.  
  48. int main()
  49. {
  50. initFactorial();
  51. cin>>b;
  52. cout<<getBiggestFactorian(1500000);
  53. }
  54.  
  55.  
  56.  
  57.  
  58. /*
  59.  
  60. it is more of an implementation problem , the toughest part of it will be to convert a given number in base 10 to base b (so you can understand this is not that difficult problem).
  61.  
  62. so here is how we can solve this problem.
  63.  
  64. 1. pre calculate factorials from 0 to 9 (because we only need factorial of digits).
  65.  
  66. 2. run a loop from 1500000 - 1 to 0 and check whether the current number is factorian number or not.
  67.  
  68. 3. if the current number is factorian number stop the loop and print current number.
  69.  
  70. step 2 requires to check whether the given number is factorian number or not , for that you need to convert the current number from base 10 to base b after that you can easily check that.
  71.  
  72. I am providing the code , you can check the implementation details there.
  73.  
  74. */
Success #stdin #stdout 0.03s 4384KB
stdin
11
stdout
40472