0
点赞
收藏
分享

微信扫一扫

iOS 使用coreData存贮页面的模型数据中的字典

泠之屋 2023-09-01 阅读 12

Problem - C - Codeforces

#include <bits/stdc++.h>
//using int_max = 0x3f3f3f3f;
#define long_max 9223372036854775807;
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
using VI = vector<int>;
typedef unsigned long long ull;
const int MAXN = 1e6;

int bit(int x,int pos){
    return (x >> pos) & 1;
}


void solve(){
    int x;
    cin>>x;
    VI a;
    a.push_back(x);
    int p ;
    for(int i=0;;i++){
        if(bit(x,i)) {
            if (x == (1 << i)) {
                p = i;
                break;
            }
            x -= (1 << i);
            a.push_back(x);
        }
    }
    while(p){
        x /= 2;
        a.push_back(x);
        p--;
    }
    cout<<a.size()<<"\n";
    for(auto u : a){
        cout<<u<<" ";
    }
    cout<<"\n";



}

int main() {
   int t;
   cin>>t;
    while (t--){
        solve();
    }

}

如何把一个数变成2的n次幂

从最低位开始枚举,如果第 i 位为 1 ,若x == (1 << i )就为2^i ,否则就减去(1<< i)

然后每次减去一半 2^k - 2^(k-1) = 2^(k-1)

举报

相关推荐

0 条评论