Educational Codeforces Round 83 (Rated for Div. 2) 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.

Example
Input
Copy
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
Copy
YES 
YES 
NO 
NO 
YES 
to the effect that a given target sequence, Q sequence from the start (all 0) can step through a number of operations reaches the target sequence. For the i-th operation, consequently quit can be coupled to the i th power of a number k.
Taking into account what can not do, and each increase in the number of different from each other, can think: every attempt to split the power of the number of target sequence into a plurality of k (different from each other), and the form of (similar to the binary resolution) as a [i] = k ^ p1 + k ^ p2 + .... k ^ pn (p1! = p2! = ... pn), if not successfully resolved, then it can be judged not directly Arrivals. Then for each number to which p1 ... pn Sort into a vector and then, if there are two numbers are equal to each other can not reach (the number of each operation applied is not the same), else.

For details, see Notes.
#include <bits/stdc++.h>
using namespace std;
int n,k;
long long ori[35],a[35];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        memset(a,0,sizeof(a));
        int i;
        vector<int>v;
        for(i=1;i<=n;i++)
        {
            Scanf ( " % LLD " , & ori [I]); 
        } 
        BOOL In Flag = . 1 ;
         for (I = . 1 ; I <= n-; I ++ ) 
        { 
            Long  Long TEMP = ori [I];
             int CNT = 0 ;
             IF ( ori [I] == 0 ) // 0 no action 
            {
                 Continue ; 
            } 
            the while (TEMP) // resolved 
            {
                 IF (TEMP% = K! 0 && (temp- . 1 !)% k = 0 ) // Representative of power can not be split into k and form 
                { 
                    In Flag = 0 ;
                     BREAK ; 
                } 
                IF ((temp- . 1 )% k == 0 ) // there is a k 0 th 
                { 
                    v.push_back (CNT); // the variable current accumulated thrown to 
                    temp--; // subtracted. 1 
                    CNT ++ ; 
                    TEMP / = K; 
                } 
                the else  IF (TEMP% K == 0) 
                { 
                    CNT ++ ; 
                    TEMP / = K; 
                }         
            } 
            IF (In Flag)! BREAK ; 
        } 
        IF ! (In Flag) // must first determine whether this 
        { 
            COUT << " NO " << endl;
             Continue ; 
        } 
        IF (! v.size ()) // in Flag 0 is not the case if the vector is empty 
        { 
            COUT << " YES " << endl;
            continue;
        }
        sort(v.begin(),v.end());
        for(i=0;i<v.size()-1;i++)
        {
            if(v[i]==v[i+1])//判重复 
            {
                flag=0;
                break;
            }
        }
        if(!flag)
        {
            cout<<"NO"<<endl;
            continue;
        }
        else cout<<"YES"<<endl;
    }
    
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12453200.html