Educational Codeforces Round 83 (Rated for Div. 2)C. Adding Powers

C. Adding Powers

题目链接-C. Adding Powers
在这里插入图片描述
在这里插入图片描述
题目大意
给一个长度为 n 的数列 a 和一个全零序列 v ,以及一个正整数 k ,第 i 次操作可以给任意位置加上0或k^i问能否在若干秒后,对于任意 i 都有 vi=ai ,但是不能有重复加k的i次方的存在

解题思路
进制转换

  • 把每个数拆成k 进制(类似二进制拆分),每一位至多是1,否则输出NO
  • 用map记录数列a中K进制下每一位的出现次数 ,处理完扫一遍,若有map中有元素value值大于1 ,则输出NO ,否则输出YES 。

附上代码

#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
int n,k,a[110];
map<int,int> mp;
bool judge(int x){
	int tmp=0;
	while(x){
        if(x%k>1) 
			return 0;
        if(x%k==1) 
			mp[tmp]++;
        x/=k;
        tmp++;
    }
    return 1;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int t;
	cin>>t;
	while(t--){
		mp.clear();
		bool flag=0;
		cin>>n>>k;
		for(int i=0;i<n;i++){
			cin>>a[i];
			if(!judge(a[i]))
				flag=1;
		}
		if(flag){
			cout<<"NO"<<endl;
			continue;
		}
		for(int i=0;i<=100;i++){
			if(mp[i]>1){
				cout<<"NO"<<endl;
				flag=1;
				break;
			}
		}
		if(!flag)
			cout<<"YES"<<endl;
	}
	return 0;
}

发布了123 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/104786344