MirzaMdAzwad

CodingParina.cpp

Aug 10th, 2021 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. template<class integer>
  6. inline integer to_int(const string& s, size_t* idx = 0, int base = 10) {
  7.     size_t n = s.size(), i = idx ? *idx : 0; bool sign = false; integer x = 0;
  8.     if (s[i] == '-')
  9.         ++i, sign = true;
  10.     function<int(char)> char_to_digit = [&](char c) {
  11.         static const int d[] = {'a'-10,'0'};
  12.         return tolower(c)-d[isdigit(c)]; };
  13.     while (i < n)
  14.         x *= base, x += char_to_digit(s[i++]);
  15.     if (idx)
  16.         *idx = i;
  17.     return sign ? -x : x; }
  18.  
  19. template<class integer>
  20. inline string to_string(integer x, int base = 10) {
  21.     bool sign = false; string s;
  22.     if (x < 0)
  23.         x = -x, sign = true;
  24.     function<char(int)> digit_to_char = [](int d) {
  25.         static const char c[] = {'a'-10,'0'};
  26.         return c[d < 10]+d; };
  27.     do
  28.         s += digit_to_char(x%base), x /= base;
  29.     while (x > 0);
  30.     if (sign)
  31.         s += '-';
  32.     reverse(s.begin(),s.end());
  33.     return s; }
  34.  
  35. template<class integer>
  36. inline istream& read(istream& is, integer& x) {
  37.     string s; is >> s, x = to_int<integer>(s); return is; }
  38.  
  39. template<class integer>
  40. inline ostream& write(ostream& os, integer x) { return os << to_string(x); }
  41.  
  42. using  lll =   signed __int128;
  43. using ulll = unsigned __int128;
  44.  
  45. inline istream& operator>>(istream& is,  lll &x) { return  read(is,x); }
  46. inline istream& operator>>(istream& is, ulll &x) { return  read(is,x); }
  47. inline ostream& operator<<(ostream& os,  lll  x) { return write(os,x); }
  48. inline ostream& operator<<(ostream& os, ulll  x) { return write(os,x); }
  49.  
  50.  
  51. int main()
  52. {
  53.     ios_base::sync_with_stdio(0);
  54.     cin.tie(NULL);
  55.     const ulll one=(ulll)1;///using 1 as ulll 1
  56.     const ulll zero=(ulll)0;///using 0 as ulll 0
  57.     const ulll two=(ulll)2;///using 2 as ulll 2
  58.     ///This is being used to prevent errors in comparison between 2 different data types
  59.     int n;
  60.     cin>>n;
  61.     while(n--)
  62.     {
  63.         ulll x;
  64.         cin>>x;
  65.         ulll fact=one;
  66.         ulll i;///since i is being used in calculations such as division by i*(i+1)
  67.         ///if i is of a different data type dividing ulll data with int may result in error
  68.         if(x==one)
  69.         {
  70.             cout<<one;///ostream(output stream) is of ull constant to represent 1, unnecessary edit
  71.             cout<<'\n';///this endline works for when 1 is to be printed
  72.             continue;
  73.  
  74.         }
  75.         for(i=two;;i+=two)///the loop variable is ulll so it starts and increments with an ulll value so as to prevent errors due to combining 2 different data types for an arithmetic operation
  76.         {
  77.  
  78.             x=x/(i*(i+one));///this is the main reason for using i as ulll
  79.             if(x==one)
  80.             {
  81.                 cout<<i+one;///one used here for the same reason to prevent errors due to performing arithmetic operations between 2 different data types
  82.                 break;
  83.             }
  84.             if(x==zero)
  85.             {
  86.                 cout<<i;
  87.                 break;
  88.             }
  89.  
  90.         }
  91.        cout<<'\n';///this endline works for everyone except when 1 is to be printed
  92.  
  93.     }
  94.  
  95.  
  96. }
  97.  
Add Comment
Please, Sign In to add comment