CodeForces - 1312C. Adding Powers
Original title address:
http://codeforces.com/contest/1312/problem/C
Basic questions:
Give you an array a of n elements and a number k, and ask if you can add an array of all zeros to k ^ i at a time, so that this array becomes an array a, and each i can be used at most once, that is, cannot Use an i multiple times.
The basic idea:
This problem is essentially a hexadecimal replacement. We convert each number in the array a to k hexadecimal, because each i is used at most once, so each bit is enumerated, and the sum of each bit is not greater than 1. Otherwise, it will not work.
Reference Code:
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define INF 0x3f3f3f3f
const int maxn = 35;
int n,k,a[maxn];
int memo[maxn][64];
void judge(int pos,int m){
int x = a[pos];
int top = 0;
while (x > 0){
memo[pos][top++] = x % k;
x /= m;
}
}
signed main() {
IO;
int t;
cin >> t;
while (t-- > 0) {
cin >> n >> k;
memset(memo,0, sizeof(memo));
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
judge(i, k);
}
bool v = true;
for (int i = 63; i >= 0; i--) {
int sum = 0;
for (int j = 0; j < n; j++) {
sum += memo[j][i];
}
if (sum > 1) v = false;
}
if (v) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}