1+ /*
2+
3+ Problem : Rudolf and Snowflakes 1846E2
4+
5+ Approach : sum = 1+k+k2+k3+...kd : ki = k^i
6+ so we need to find a pair of k,d for n such {sum = n}
7+ 2^60>1e18 so we can iterate for each d and using binary search we can also find k;
8+
9+ Time Complexity : O(nlogn)
10+ Space Complexity : O(n)
11+
12+ Submission Link : https://codeforces.com/problemset/submission/1846/356008118
13+ */
14+ #include < bits/stdc++.h>
15+ using namespace std ;
16+
17+ #define fast_io ios::sync_with_stdio (false ); cin.tie(nullptr );
18+ #define int long long
19+ #define ull unsigned long long
20+ #define ld long double
21+
22+ #define vi vector<int >
23+ #define vll vector<long long >
24+ #define vb vector<bool >
25+ #define vs vector<string>
26+ #define pii pair<int , int >
27+ #define pll pair<long long , long long >
28+ #define vpi vector<pii>
29+ #define vpl vector<pll>
30+ #define vvi vector<vi>
31+ #define vvll vector<vll>
32+ #define mii map<int , int >
33+ #define mll map<long long , long long >
34+ #define si set<int >
35+ #define sll set<long long >
36+
37+ #define loop (i, n ) for (int i = 0 ; i < n; i++)
38+ #define rloop (i, n ) for (int i = n - 1 ; i >= 0 ; i--)
39+ #define loop1 (i, a, b ) for (int i = a; i <= b; i++)
40+ #define rloop1 (i, a, b ) for (int i = a; i >= b; i--)
41+
42+ #define pb push_back
43+ #define ppb pop_back
44+ #define mp make_pair
45+ #define F first
46+ #define S second
47+ #define all (x ) (x).begin(), (x).end()
48+ #define rall (x ) (x).rbegin(), (x).rend()
49+ #define sz (x ) ((int )(x).size())
50+ #define endl ' \n '
51+ #define print1 cout<<" YES" <<endl
52+ #define print0 cout<<" NO" <<endl
53+
54+ const int MOD = 1e18 ;
55+ const int INF = 1e18 ;
56+
57+ #ifndef ONLINE_JUDGE
58+ #define debug (x ) cerr << #x << " = " ; _print(x); cerr << endl;
59+
60+ template <typename T> void _print (const T &x) { cerr << x; }
61+
62+ template <typename T, typename U> void _print (const pair<T, U> &p) {
63+ cerr << " (" ; _print (p.first ); cerr << " , " ; _print (p.second ); cerr << " )" ;
64+ }
65+
66+ template <typename T> void _print (const vector<T> &v) {
67+ cerr << " [" ;
68+ for (size_t i = 0 ; i < v.size (); ++i) {
69+ _print (v[i]);
70+ if (i != v.size () - 1 ) cerr << " , " ;
71+ }
72+ cerr << " ]" ;
73+ }
74+
75+ template <typename T> void _print (const set<T> &s) {
76+ cerr << " {" ;
77+ for (auto it = s.begin (); it != s.end (); ++it) {
78+ _print (*it);
79+ if (next (it) != s.end ()) cerr << " , " ;
80+ }
81+ cerr << " }" ;
82+ }
83+
84+ template <typename T, typename U> void _print (const map<T, U> &m) {
85+ cerr << " {" ;
86+ for (auto it = m.begin (); it != m.end (); ++it) {
87+ _print (it->first ); cerr << " : " ; _print (it->second );
88+ if (next (it) != m.end ()) cerr << " , " ;
89+ }
90+ cerr << " }" ;
91+ }
92+
93+ #else
94+ #define debug (x )
95+ #endif
96+
97+
98+ int binpow (int a, int n) {
99+ int result = 1 ;
100+ a %= MOD;
101+ while (n > 0 ) {
102+ if (n & 1 )
103+ result = (result * a);
104+ a = (a * a);
105+ n >>= 1 ;
106+ }
107+ return result;
108+ }
109+
110+ /*
111+
112+ s(k) = [k^(d+1) - 1]/[k-1]
113+
114+ */
115+
116+ int cal (int k, int d, int n){
117+ int sum = 1 , term = 1 ;
118+ loop (i,d){
119+ if (term>n/k || sum>n) return 2 ;
120+ term *= k;
121+ sum += term;
122+ }
123+ if (sum == n) return 0 ;
124+ return 1 ;
125+ }
126+
127+ void solve (){
128+ int n;
129+ cin>>n;
130+
131+ if (n<7 ) {print0; return ;}
132+
133+ loop1 (d,2 ,60 ){
134+ int l = 2 , h = n;
135+ while (l <= h)
136+ {
137+ int k = l + (h-l)/2 ;
138+ int res = cal (k,d,n);
139+ if (res==0 ){
140+ print1;
141+ return ;
142+ }
143+ else if (res==1 ) l = k + 1 ;
144+ else h = k - 1 ;
145+ }
146+ }
147+ print0;
148+ }
149+
150+ int32_t main () {
151+ fast_io;
152+ int t = 1 ;
153+ cin >> t;
154+ while (t--) {
155+ solve ();
156+ }
157+ return 0 ;
158+ }
0 commit comments