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[],cnt; ll get()
{
ll x = p % k;
if (x < )
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 = ; i <= cnt; i++)
cout << ans[i] << " "; return ;
}

Codeforces 934.D A Determined Cleanup的更多相关文章

  1. Codeforces 934D/933B - A Determined Cleanup

    传送门:http://codeforces.com/contest/934/problem/D 给定两个正整数p(p≥1).k(k>1).多项式f(x)的系数的取值集合为{0,1,2,...,k ...

  2. Codeforces 934D - A Determined Cleanup

    934D - A Determined Cleanup 思路: 找规律,和k进制的求法差不多,答案的奇数位是p%k,偶数位如果p%k!=0,那么答案是k-p%k,否则为0. 代码: #include& ...

  3. Codeforces Round #462 (Div. 2) D. A Determined Cleanup

    D. A Determined Cleanup time limit per test1 second memory limit per test256 megabytes Problem Descr ...

  4. [codeforces934D]A Determined Cleanup

    [codeforces934D]A Determined Cleanup 试题描述 In order to put away old things and welcome a fresh new ye ...

  5. [Codeforces 933B]A Determined Cleanup

    Description 题库链接 给你两个正整数 \(p,k\) ,询问是否能够构造多项式 \(f(x)=\sum\limits_{i=0}^{d-1}a_ix^i\) ,使得存在多项式 \(q(x) ...

  6. Codeforces Round #464 (Div. 2) A Determined Cleanup

    A. Love Triangle time limit per test1 second memory limit per test256 megabytes Problem Description ...

  7. 【Codeforces Round #462 (Div. 1) B】A Determined Cleanup

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设\(设f(x)=a_d*x^{d}+a_{d-1}*x^{d-1}+...+a_1*x+a_0\) 用它去除x+k 用多项式除法除 ...

  8. Codeforces 934 A.Compatible Pair

    http://codeforces.com/contest/934 A. A Compatible Pair   time limit per test 1 second memory limit p ...

  9. Codeforces 934.C A Twisty Movement

    C. A Twisty Movement time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. Mysql: pt-table-checksum 和 pt-table-sync 检查主从一致性,实验过程

    一.安装 percona 包 1.安装仓库的包 https://www.percona.com/doc/percona-repo-config/yum-repo.html sudo yum insta ...

  2. PHP关于 []

    在一个表格里,提交时,名字部分加一个[],表示数组,这样,存在多个同样名字的name.前面的value不会替代后面value,如下面 <td><input name="so ...

  3. JZOJ 5919. 逛公园

    Description            琥珀色黄昏像糖在很美的远方,思念跟影子在傍晚一起被拉长……Description      小 B 带着 GF 去逛公园,公园一共有 n 个景点,标号为 ...

  4. python——标准异常总结

    请参考此网站: Python 标准异常总结 https://fishc.com.cn/forum.php?mod=viewthread&tid=45814&extra=page%3D1 ...

  5. [CodeChef]RIN(最小割)

    Description  有m门课可以在n个学期内学习,第i门课在第j个学期的收益是\(X_{i,j}\),一个学期可以学多门课,有的课之间有依赖关系,即必须先学a再学b,求最大收益.n,m<= ...

  6. 云心出岫——Splay Tree

    (多图预警!!!建议在WI-FI下观看) 之前我们谈论过AVL树,这是一种典型适度平衡的二叉搜索树,成立条件是保持平衡因子在[-1,1]的范围内,这个条件已经是针对理想平衡做出的一个妥协了,但依然显得 ...

  7. 关于 JS 模块化的最佳实践总结

    模块化开发是 JS 项目开发中的必备技能,它如同面向对象.设计模式一样,可以兼顾提升软件项目的可维护性和开发效率. 模块之间通常以全局对象维系通讯.在小游戏中,GameGlobal 是全局对象.在小程 ...

  8. 2753: [SCOI2012]滑雪与时间胶囊

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2633  Solved: 910 Descriptio ...

  9. Python的类(一)

    类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义在类中且在函数体之外. ...

  10. Java学习关于时间操作的应用类--Date类、Calendar类及其子类

    Date类 Date类封装了当期时间和日期.与Java1.0定义的原始版的Date类相比,Date类发生了本质的变化.在Java1.1发布时,原始版Date类定义的许多功能被移进Calendar类和D ...