zoukankan      html  css  js  c++  java
  • Codeforces 934.D A Determined Cleanup

    D. A Determined Cleanup
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    In order to put away old things and welcome a fresh new year, a thorough cleaning of the house is a must.

    Little Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this...

    Given two integers p and k, find a polynomial f(x) with non-negative integer coefficients strictly less than k, whose remainder is p when divided by (x + k). That is, f(x) = q(x)·(x + k) + p, where q(x) is a polynomial (not necessarily with integer coefficients).

    Input

    The only line of input contains two space-separated integers p and k (1 ≤ p ≤ 1018, 2 ≤ k ≤ 2 000).

    Output

    If the polynomial does not exist, print a single integer -1, or output two lines otherwise.

    In the first line print a non-negative integer d — the number of coefficients in the polynomial.

    In the second line print d space-separated integers a0, a1, ..., ad - 1, describing a polynomial  fulfilling the given requirements. Your output should satisfy 0 ≤ ai < k for all 0 ≤ i ≤ d - 1, and ad - 1 ≠ 0.

    If there are many possible solutions, print any of them.

    Examples
    input
    46 2
    output
    7
    0 1 0 0 1 1 1
    input
    2018 214
    output
    3
    92 205 1
    Note

    In the first example, f(x) = x6 + x5 + x4 + x = (x5 - x4 + 3x3 - 6x2 + 12x - 23)·(x + 2) + 46.

    In the second example, f(x) = x2 + 205x + 92 = (x - 9)·(x + 214) + 2018.

     题目大意:给定k和p,要求一个多项式f(x) = q(x)(x+k) + p,其中f(x)的每个系数都是非负的,并且小于k.

    分析:这道题看着有点难,其实分析出规律来以后挺简单的.

       考虑构造q(x).先让常数项小于k,也就是让相乘后的常数项+p小于k.q(x)的常数项就是p / (-k).这样会产生一次项,那么继续消一次项.直到最后的p = 0.

       原理其实就是q(x)的第i次项与k相乘,使得第i-1次项与k项乘的结果加上它以后小于k.

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    
    ll p,k,ans[100010],cnt;
    
    ll get()
    {
        ll x = p % k;
        if (x < 0)
            x += k;
        return x % k;
    }
    
    int main()
    {
        cin >> p >> k;
        while (p)
        {
            ans[++cnt] = get();
            p -= get();
            p /= (-k);
        }
        cout << cnt << endl;
        for (int i = 1; i <= cnt; i++)
            cout << ans[i] << " ";
    
        return 0;
    }

     

  • 相关阅读:
    log4j 悟寰轩
    初学PHP 悟寰轩
    java基本语法注意问题 悟寰轩
    HTTP协议基础 悟寰轩
    SSHweb.xml详解 悟寰轩
    windows和linux查找被入侵后留下的后门文件 悟寰轩
    MFC Link problem
    Manually Walking a Stack
    linux试用(2)安装 mplayer, g++
    子集和问题
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8449289.html
Copyright © 2011-2022 走看看