Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- template<class integer>
- inline integer to_int(const string& s, size_t* idx = 0, int base = 10) {
- size_t n = s.size(), i = idx ? *idx : 0; bool sign = false; integer x = 0;
- if (s[i] == '-')
- ++i, sign = true;
- function<int(char)> char_to_digit = [&](char c) {
- static const int d[] = {'a'-10,'0'};
- return tolower(c)-d[isdigit(c)]; };
- while (i < n)
- x *= base, x += char_to_digit(s[i++]);
- if (idx)
- *idx = i;
- return sign ? -x : x; }
- template<class integer>
- inline string to_string(integer x, int base = 10) {
- bool sign = false; string s;
- if (x < 0)
- x = -x, sign = true;
- function<char(int)> digit_to_char = [](int d) {
- static const char c[] = {'a'-10,'0'};
- return c[d < 10]+d; };
- do
- s += digit_to_char(x%base), x /= base;
- while (x > 0);
- if (sign)
- s += '-';
- reverse(s.begin(),s.end());
- return s; }
- template<class integer>
- inline istream& read(istream& is, integer& x) {
- string s; is >> s, x = to_int<integer>(s); return is; }
- template<class integer>
- inline ostream& write(ostream& os, integer x) { return os << to_string(x); }
- using lll = signed __int128;
- using ulll = unsigned __int128;
- inline istream& operator>>(istream& is, lll &x) { return read(is,x); }
- inline istream& operator>>(istream& is, ulll &x) { return read(is,x); }
- inline ostream& operator<<(ostream& os, lll x) { return write(os,x); }
- inline ostream& operator<<(ostream& os, ulll x) { return write(os,x); }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(NULL);
- const ulll one=(ulll)1;///using 1 as ulll 1
- const ulll zero=(ulll)0;///using 0 as ulll 0
- const ulll two=(ulll)2;///using 2 as ulll 2
- ///This is being used to prevent errors in comparison between 2 different data types
- int n;
- cin>>n;
- while(n--)
- {
- ulll x;
- cin>>x;
- ulll fact=one;
- ulll i;///since i is being used in calculations such as division by i*(i+1)
- ///if i is of a different data type dividing ulll data with int may result in error
- if(x==one)
- {
- cout<<one;///ostream(output stream) is of ull constant to represent 1, unnecessary edit
- cout<<'\n';///this endline works for when 1 is to be printed
- continue;
- }
- 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
- {
- x=x/(i*(i+one));///this is the main reason for using i as ulll
- if(x==one)
- {
- cout<<i+one;///one used here for the same reason to prevent errors due to performing arithmetic operations between 2 different data types
- break;
- }
- if(x==zero)
- {
- cout<<i;
- break;
- }
- }
- cout<<'\n';///this endline works for everyone except when 1 is to be printed
- }
- }
Add Comment
Please, Sign In to add comment