1864C - Divisor Chain - CodeForces Solution


bitmasks constructive algorithms math number theory

Please click on ads to support us..

C++ Code:

//                                                                                                  san
//                                                                                                narayan
//                                                                                              narayanraja
//                                                                                                narayan
//                                                            narayannarayan                        san                      narayannarayannan
//                                                                narayannarayannar                                      narayannarayannan
//                                                                     narayannarayannarayan                      narayannarayannarayan
//                                                                          narayannarayannarayan            narayannarayannarayan
//                                                                                 narayannarayannarayannarayannarayannayan
//                                                                                         narayannarayannarayann
//                                                    narayannarayannan
//                                                narayannarayannarayannarayannan
//                                            narayannarayannarayannarayannarayan
//                                        narayannarayannarayannarayannarayan
//                                    narayannarayannarayannarayannarayannarayan
//                                 narayannarayannan                    narayan
//                                                                       narayan
//                                                                       narayan
//                                                                      narayan                            narayannarayannan
//                                                                     narayan                        narayannarayannarayannarayan
//                                                                    narayan                     narayannarayannarayannarayannarayannarayannarayannan
//                                                                   narayan                  narayannarayannarayannarayannarayannarayannarayannan
//                                                                  narayan                 narayannarayannarayan                  narayannarayannan
//                                                                narayan                 narayannarayannan                         narayannarayannan
//                                                              narayan                narayannarayannan                                narayannarayann
//                                                           narayannan              narayannarayannarayann                                    narayannarayann
//                                                        narayannar              narayannarayannarayan                                        narayannarayann
//                                                     narayannarayan              narayannarayannarayan                                           narayannayan
//                                                  narayannarayannarayannarayannarayannarayannarayan                                              narayannar
//                                                  narayannarayannarayannarayannarayannarayannarayan                                                 narayan
//                                                     narayannayannarayannarayannarayannarayannarayan                                                    narayan
//                                                       narayannarnarayannarayannarayannarayan                                                      narayan
//                                                          narayannannarayannarayannarayannan                                                          narayan
//                                                             narayan                                                                            narayannarayan
//                                                               narayan                     nar                                                 narayannarayan
//                                                                 narayan                   nar                                                narayannarayan
//                                                                   narayan                 nar                                               narayannarayan
//                                                                      narayan              narayan                                             narayannarayan
//                                                                        narayan             narayann                                          narayannarayan
// narayan                                                                 narayan             narayan                                        narayannarayan
//  narayan                                                                  narayan            narayan                                      narayannarayan
//   narayan                                                                narayan             narayan                                   narayannarayan
//     narayannan                                                              narayan             narayan                                 narayannarayan
//       narayannan                                                             narayan              narayannan                            narayannarayan
//         narayannan                                                          narayan                 narayannarayan                      narayannarayan
//           narayannan                                                       narayan                    narayannarayan                narayannarayan
//              narayannan                                                  narayan                        narayannarayannarayannarayannarayannarayannarayannarayannayan
//                 narayannarayan                                          narayannan                            narayannarayannarayannarayannarayannarayannarayannarayannan
//                    narayannarayan                                   narayannan                                 narayannarayannarayannarayannan
//                       narayannarayannan                            narayannan                                       narayannarayannarayannan
//                          narayannarayannayan                   narayannarayan
//                              narayannarayannarayannarayannayannarayannarayann
//                                  narayannarayannarayannarayannarayannayan
//                                      narayannarayannarayannayan
//                                           narayannarayannan
//
//
// ॐ भूर्भुव: स्व: तत्सवितुर्वरेण्यं भर्गो देवस्य धीमहि धियो यो न: प्रचोदयात्।
// ॐ त्र्यम्बकं यजामहे सुगन्धिं पुष्टिवर्धनम्। उर्वारुकमिव बन्धनान् मृत्योर्मुक्षीय मामृतात्॥
/*
    कर्मण्येवाधिकारस्ते मा फलेषु कदाचन ।
    मा कर्मफलहेतुर्भुर्मा ते संगोऽस्त्वकर्मणि ॥
अर्थ:- तेरा कर्म करने में अधिकार है इनके फलो में नही.
     तू कर्म के फल प्रति असक्त न हो या कर्म न करने के प्रति प्रेरित न हो.
*/
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mod 1000000007

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t;
    cin >> t;
    while (t--)
    {

        ll n;
        cin >> n;
        // ll val=1;

        ll temp = n;

        vector<ll> bits;

        while (temp != 0)
        {
            bits.push_back(temp % 2);
            temp /= 2;
        }

        ll cnt = bits.size();

        vector<ll> ans;
        ll val = 1;

        ans.push_back(n);
        for (ll i = 0; i < cnt - 1; i++)
        {
            if (bits[i] == 1)
            {
                ans.push_back(n - val);
                n -= val;
            }
            val *= 2;
        }

        while (n != 1)
        {
            ans.push_back(n / 2);
            n /= 2;
        }

        cout << ans.size() << endl;
        for (auto i : ans)
        {
            cout << i << " ";
        }

        cout << endl;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation