Codeforces C. Adding Powers

C. Adding Powers

Suppose you are performing the following algorithm. There is an array v1,v2,,vnv1,v2,…,vn filled with zeroes at start. The following operation is applied to the array several times — at ii-th step (00-indexed) you can:

  • either choose position pospos (1posn1≤pos≤n) and increase vposvpos by kiki;
  • 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 vv equal to the given array aa (vj=ajvj=aj for each jj) after some step?

Input

The first line contains one integer TT (1T10001≤T≤1000) — the number of test cases. Next 2T2T lines contain test cases — two lines per test case.

The first line of each test case contains two integers nn and kk (1n301≤n≤30, 2k1002≤k≤100) — the size of arrays vv and aa and value kk used in the algorithm.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai10160≤ai≤1016) — the array you'd like to achieve.

Output

For each test case print YES (case insensitive) if you can achieve the array aa after some step or NO (case insensitive) otherwise.

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 00-th step, or don't choose any position several times and stop the algorithm.

In the second test case, you can add k0k0 to v1v1 and stop the algorithm.

In the third test case, you can't make two 11 in the array vv.

In the fifth test case, you can skip 9090 and 9191, then add 9292 and 9393 to v3v3, skip 9494 and finally, add 9595 to v2v2.

题意:就是给你一个个数为n的数组a[],判断是否可以由相同大小的全0数组v[]变换而来。

你可以对这个数组进行i次操作。

变换规则为:

1.在第i次操作时,你可以给数组v任意位置的值加上k^i

2.在第i次什么也不做

题解:就是一个k进制转化的问题,看题意就知道,关键在于操作规则1,我们使用k^i只能使用一次,对于任意一个整数a[i],它都可以拆分为k进制。

假如a[i]转化为k进制数有n位,a[i]那么一定有:a[i]= x0* k^0 +x1* k^1 +x2* k^2 +……+x(n-1)* k^n-1

我们要使最后的a数组全为0,并且每次只能减去k^i,且i是从0不断递增的,即要求k^i只能使用一次!

那么我们只需拆分每一个a[i],看看它的组成,统计k^i使用次数即可,超过1就是“NO”。

详细请看代码:

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll a[35];
ll cnt[64];
int main(void)
{
    int t,n;
    ll k;
    scanf("%d",&t);
    while(t--)
    {
        memset(cnt,0,sizeof(cnt));
        int flag=1;
        scanf("%d %lld",&n,&k);
        for(int i=0;i<n;i++){
            scanf("%lld",&a[i]);
        }
        //k^i i只能每个i出现一次  i++ 且k^i的系数为1
        for(int i=0;i<n;i++){
            //拆a[i]
            int c=0;
            while(a[i])
            {
                c++;
                cnt[c]+=(a[i]%k);//系数 
                a[i]/=k;//k进制
                if(cnt[c]>1)
                flag=0;
            }//进制转换 
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/xuanmaiboy/p/12460021.html
今日推荐