Codeforces 1312C Adding Powers

时间:2020-03-12
本文章向大家介绍Codeforces 1312C Adding Powers,主要包括Codeforces 1312C Adding Powers使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目链接

考虑二进制, 若满足条件, 那么二进制位数每一位都\(≤1\), 例如0 1 3, 对应的二进制为0, 1, 11, 这样末尾有2个1, 不满足条件, 若为0 1 2, 对应的二进制为0, 1, 10, 满足条件, 使用短除法很容易扩展到k进制

#include<bits/stdc++.h>
using namespace std;
#define ms(x,y) memset(x, y, sizeof(x))
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii;


void run_case() {
    int n, k; 
    cin >> n >> k;
    vector<LL> a(n);
    for(auto &x: a) {
        cin >> x;
    }
    bool flag = true;
    vector<int> ans(100);
    for(auto x: a) {
        vector<int> bit;
        bit.clear();
        while(x) {
            if(x%k == 1 || x%k == 0) bit.push_back(x%k);
            else flag = false;
            x /= k;
        }
        for(int i = bit.size()-1; i >= 0; --i)
            if(bit[i]) ans[i]++;
    }
    for(auto x: ans) {
        if(x > 1) flag = false;
    }
    if(flag) cout << "YES\n";
    else cout << "NO\n";
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cout.flags(ios::fixed);cout.precision(2);
    int t; cin >> t;
    while(t--)
    run_case();
    cout.flush();
    return 0;
}

原文地址:https://www.cnblogs.com/GRedComeT/p/12482643.html