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.

The meaning of problems: that is to give you a number n of array a [], can be determined whether [] array transformed from the full v 0 of the same size.

You can i operations on the array.

The conversion rule is:

1. When the i-th operation, you can add value to the array v k ^ i in an arbitrary position

2. At times i do nothing

Solution: k is a problem hex conversion, see meaning of the questions, we know that the key is operating rule 1, we use k ^ i used only once, for any integer a [i], it can be split into 'k' system.

If a [i] is converted to k-ary n-digit number, a [i] then there must be: a [i] = x0 * k ^ 0 + x1 * k ^ 1 + x2 * k ^ 2 + ...... + x (n-1) * k ^ n-1

We want to make a final array all zeros, and you can only subtract k ^ i, and i is from 0 constantly on the rise, which called for k ^ i used only once!

So we just split each a [i], take a look at its composition, frequency of use statistics k ^ i can, over one that is "NO".

See details Code:

#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 ^ II can each i occurs once coefficient i ++ and k ^ i is. 1 
        for ( int I = 0 ; I <n-; I ++ ) {
             // demolition A [I] 
            int C = 0 ;
             the while (A [I]) 
            { 
                C ++ ; 
                CNT [C] + = (A [I]% K ); // coefficients 
                A [I] / K =; // K-band 
                IF (CNT [C]>. 1 ) 
                In Flag = 0 ; 
            } // binary conversion 
        }
         IF (In Flag) the printf ( " YES \ n- " );
         the else the printf ( " NO \ n- " ); 
    } 
    return  0 ; 
}
View Code

 

Guess you like

Origin www.cnblogs.com/xuanmaiboy/p/12460021.html