zoukankan      html  css  js  c++  java
  • 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
    
    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 key idea: since ks>s1x=0kx then there should be no more than one position pospos such that aposksapos≥ks and we should decrease it by ksks. Now we can decrease ss by 11 and repeat the same process.

    就是等比数列求和,前n-1项的和小于第n项;所有如果数列中某个数大于k 的i次方,那么肯定是i小了。log10^16就是一个常数,所有可以把每个数字都拿来判断一遍。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const int mod = 998244353;
    const int N = 2e5 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    map<ll, ll> mp;
    
    bool isok(ll x,ll k)
    {
        while (x > 0)
        {
            ll base = 1,cnt=0;
            while (x >= base)
            {
                cnt++;
                base *= k;
            }
            cnt--;
            mp[cnt]++;
            base /= k;
            x -= base;
            if (mp[cnt] > 1)
                return 0;
        }
        return 1;
    }
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            ll n, k;
            cin >> n >> k;
            vector<ll> a(n);
            for (int i = 0; i < n; i++)
            {
                cin >> a[i];
            }
            if (k == 1)
            {
                cout << "YES" << endl;
                continue;
            }
            int fg = 0;
            for (int i = 0; i < n; i++)
            {
                if (a[i] == 0)
                    continue;
                if (!isok(a[i], k))
                {
                    fg = 1;
                    cout << "NO" << endl;
                    break;
                }
            }
            if (!fg)
                cout << "YES" << endl;
            mp.clear();
        }
        return 0;
    }
  • 相关阅读:
    教你如何在Drcom下使用路由器上校园网(以广东工业大学、极路由1S HC5661A为例)
    selenium跳过webdriver检测并模拟登录淘宝
    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口
    使用xib制作界面有时会出现button无法点击,解决办法
    1.开博客的第一篇
    6.关于瀑布模型
    3.Freshman阶段学习内容的确定
    7.Solution的Build、Rebuild和Clean
    4.词法结构JavaScript权威指南笔记
    8.对于.NET的初步理解和介绍
  • 原文地址:https://www.cnblogs.com/dealer/p/12962027.html
Copyright © 2011-2022 走看看