Adding Powers
Adding Powers
Suppose you are performing the following algorithm. There is an array ,,…, filled with zeroes at start. The following operation is applied to the array several times — at step (-indexed) you can:
- either choose position pos and increase by ;
- or not choose any position and skip this step.
You can choose how the algorithm would behave on each step and when to stop it. The question is: can you make array equal to the given array a ( for each ) after some step?
Input
The first line contains one integer — the number of test cases. Next lines contain test cases — two lines per test case.
The first line contains one integer — the number of test cases. Next lines contain test cases — two lines per test case.
The second line contains integers — the array you’d like to achieve.
Output
For each test case print YES (case insensitive) if you can achieve the array a after some step or NO (case insensitive) otherwise.
Example
input
5
4 100
0 0 0 0
1 2
1
3 4
1 4 1
3 2
0 1 3
3 9
0 59049 810
output
YES
YES
NO
NO
YES
Note
In the first test case, you can stop the algorithm before the 0-th step, or don’t choose any position several times and stop the algorithm.
In the second test case, you can add to and stop the algorithm.
In the third test case, you can’t make two in the array .
In the fifth test case, you can skip and , then add and to , skip and finally, add to .
思路
题意:给出一个数列,数字k,数列中的每个数字能否由k的n次方加成,且每个次方只能用一次。很明显将每个数字的k进制表示出来,然后判断同一位次上的数字之和是否 ,这样就可以了,用一个数组统计每一个位次上的和。
代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=2e5+10;
int s[100];
ll t,n,k;
ll a[mx];
int main()
{
scanf("%d",&t);
while(t--)
{
bool flag=false;
memset(s,0,sizeof(s));
scanf("%lld%lld",&n,&k);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=1;i<=n;i++)
{
int j=0;
while(a[i])//统计每一个数字的k进制
{
s[j]+=a[i]%k;//加上同一位次的
j++;
a[i]/=k;
}
}
for(int i=0;i<100;i++)
if(s[i]>1)//只要有超过一次的就不能了
{
flag=true;
break;
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
上一篇: go语言十六进制转十进制方法
下一篇: 判断输入的字符串是否为回文
推荐阅读
-
[codeforces 1312C] Adding Powers 进制转换
-
C. Adding Powers
-
Adding Powers
-
Adding Powers
-
Adding Unit Tests to an existing iOS project with Xcode 4 博客分类: 手机开发 iosunittest
-
Oracle 11g维护分区(一)Adding Partitions
-
powers.exe是什么进程 powers进程查询
-
Password is required when adding a database to AG group if the database has a master key
-
POJ 1730 Perfect Pth Powers G++ pow未实现 素数+gcd实现 巧妙
-
Additive Powers-of-Two (APoT) Quantization:硬件友好的非均匀量化方法