【CF】Powers Of Two

 


 惭愧啊 菜到DIV3 C都又T又WA得

题目大意给出n和k,让你判断n能否由k个2的幂的和组成,并输出。

正确思路:把n转二进制存数组,因为,所以不断从高位向低位转换并判断就好了.


//#include <bits\stdc++.h>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
const long double pi = acos(-1);
const ll maxn = 2*1e5+100;
const ll inf = 1e18;
const ll mod = 998244353;

ll n,k,arr[maxn],numone,len;

void two(ll x)
{
    while(x)
    {
        arr[len] = x%2;
        if(arr[len] == 1)
            numone++;
        len++;
        x = x/2;
    }
    len--;

    return ;
}

void slove()
{
    if(k < numone)
    {
        cout << "NO" << endl;
        return ;
    }
    else
    {
        for(ll i = len; i >= 0; i--)
        {
            while(arr[i] != 0)
            {
                //cout << i << " " << arr[i] << endl;
                if(numone == k)
                {
                    //cout << numone << endl;
                    cout << "YES" << endl;
                    ll temp = 1;
                    for(ll i = 0; i <= len; i++)
                    {
                        while(arr[i] != 0)
                        {
                            arr[i]--;
                            cout << temp << " ";
                        }

                        temp = temp*2;
                    }
                
                    return ;
                }

                if(i == 0)
                    break;

                arr[i]--;
                numone++;
                arr[i-1]+=2;

            }
        }
    }

    cout << "NO" << endl;
    return ;
}

int main()
{
    ios::sync_with_stdio(false);

    cin >> n >> k;

    two(n);

    /*for(ll i = len; i >= 0; i--)
        cout << arr[i] << " ";
    cout << endl;*/

    slove();

    return 0;
}



猜你喜欢

转载自blog.csdn.net/Whyckck/article/details/86578930