1810B - Candies - CodeForces Solution


constructive algorithms math

Please click on ads to support us..

C++ Code:

//  **********Mr HGA उर्फ ऋतिक गडरियाल आनंद************
//                       ¯\_(ツ)_/¯ 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
#define F first
#define S second
#define vvi vector<vector<int>>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define vl vector<long long>
#define mii map<int, int>
#define pqb priority_queue<int>
#define IOS \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);\
cout.tie(NULL);
#define rsort(a) sort(a.rbegin(), a.rend())
#define get_sum(a) accumulate(a.begin(), a.end(), 0LL)
#define get_max(a) *max_element(a.begin(), a.end())
#define get_min(a) *min_element(a.begin(), a.end())
#define setbits(x) __builtin_popcountll(x)
#define int long long int
using namespace std;
#define mod 1e5+7
#define w(t) \
int t;     \
cin >> t;  \
    while(t--)

#define goog(t) \
int t;     \
cin >> t;  \
for(int i=1;i<=t;i++){ \
cout << "Case #" << i << ": "; solve(); \
}

#define f(i, x,y) for (int i = x; i < y; i++)
//*************************DSU Snippets****************************
// vi par(mod),rnk(mod);
// void make_set(int n)
// {
//     for(int i=1; i<=n; i++)
//     {
//         par[i]=i;
//         rnk[i]=1;
//     }
// }
// int find_set(int x)
// {
//     if(par[x]==x)
//     {
//         return x;
//     }
//     par[x]=find_set(par[x]);
//     return par[x];
// }
// void union_set(int a, int b)
// {
//     int p1=find_set(a);
//     int p2=find_set(b);
//     if(p1==p2)
//         return;
//     if(rnk[p1]>=rnk[p2])
//     {
//         par[p2]=p1;
//         rnk[p1]+=rnk[p2];
//     }
//     else
//     {
//         par[p1]=p2;
//         rnk[p2]+=rnk[p1];
//     }
// }
//************************************************************//


void solve(){
int n; cin>>n;
if(n%2==0) {cout<<-1<<nl; return;}
vi ans;
while(n!=1){
    if(((n+1)/2)%2!=0){
       ans.pb(1);
       n = (n+1)/2;
    }
    else {
      ans.pb(2);
      n=(n-1)/2;
    }
}
ans.pb(1);
reverse(all(ans));
cout<<ans.size()<<nl;
for(auto i: ans) cout<<i<<" ";
cout<<nl;
}
 



int32_t main(){
IOS 


   w(t)
{
solve();
}
// goog(t)
}


Comments

Submit
0 Comments
More Questions

1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array