[codeforces934D]A Determined Cleanup
[codeforces934D]A Determined Cleanup
试题描述
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) \cdot (x + k) + p\), where \(q(x)\) is a polynomial (not necessarily with integer coefficients).
给定两个整数 \(p\) 和 \(k\),构造一个满足下列条件的多项式 \(f(x)\):
- 每项系数严格小于 \(k\) 且非负;
- \(f(x) = g(x) \cdot (x+k) + p\),其中 \(g(x)\) 是个多项式,系数没有任何要求。
输入
The only line of input contains two space-separated integers \(p\) and \(k\) \((1 \le p \le 10^{18}, 2 \le k \le 2 000)\).
输出
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 \(a_0, a_1, \cdots , a_{d - 1}\), describing a polynomial fulfilling the given requirements. Your output should satisfy \(0 \le a_i < k\) for all \(0 \le i \le d - 1\), and \(a_{d - 1} \ne 0\).
If there are many possible solutions, print any of them.
输入示例1
46 2
输出示例1
7
0 1 0 0 1 1 1
输入示例2
2018 214
输出示例2
3
92 205 1
数据规模及约定
见“输入”
题解
我们假设 \(f(x) = \sum_{i=0}^d a_i x^i\),然后做一下 \(\frac{f(x)}{(x+k)}\) 的大除法,并将得到的 \(g(x)\) 的系数写出来(假设 \(g(x) = \sum_{i=0}^{d-1} b_i x^i\)),会发现如下规律:
b_{d-2} = a_{d-1} - k a_d \\
b_{d-3} = a_{d-2} - k a_{d-1} + k^2 a_d \\
\cdots \\
b_0 = a_1 - k a_2 + k^2 a_3 - \cdots \\
p = a_0 - k a_1 + k^2 a_2 - \cdots = \sum_{i=0}^d (-k)^i a_i
\]
于是发现 \((a_0a_1a_2 \cdots)_{-k}\) 就是 \(p\) 的 \(-k\) 进制表示,上面的过程证明了它是 \(p\) 的 \(-k\) 进制表示是满足题目要求的必要条件;由于 \(g(x)\) 没有任何约束,即 \(b_i\) 可以是任意实数,充分性也显然。
负进制的转化也是同样的过程,只不过除法要做到严格的向下取整,而不是用 C++ 中默认的朝零取整。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define rep(i, s, t) for(int i = (s), mi = (t); i <= mi; i++)
#define dwn(i, s, t) for(int i = (s), mi = (t); i >= mi; i--)
#define LL long long
LL read() {
LL x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
#define maxn 65
int cnt, A[maxn];
int main() {
LL p = read(), k = read();
while(p) {
LL div = p / -k;
if(-k * div > p) div++;
A[cnt++] = p - (-k * div);
p = div;
}
printf("%d\n", cnt);
rep(i, 0, cnt - 1) printf("%d%c", A[i], i < cnt - 1 ? ' ' : '\n');
return 0;
}
[codeforces934D]A Determined Cleanup的更多相关文章
- Codeforces 934D - A Determined Cleanup
934D - A Determined Cleanup 思路: 找规律,和k进制的求法差不多,答案的奇数位是p%k,偶数位如果p%k!=0,那么答案是k-p%k,否则为0. 代码: #include& ...
- Codeforces 934.D A Determined Cleanup
D. A Determined Cleanup time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 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 ...
- [Codeforces 933B]A Determined Cleanup
Description 题库链接 给你两个正整数 \(p,k\) ,询问是否能够构造多项式 \(f(x)=\sum\limits_{i=0}^{d-1}a_ix^i\) ,使得存在多项式 \(q(x) ...
- Codeforces Round #464 (Div. 2) A Determined Cleanup
A. Love Triangle time limit per test1 second memory limit per test256 megabytes Problem Description ...
- 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 ...
- 【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 用多项式除法除 ...
- codeforces 462div.2
A A Compatible Pair standard input/output 1 s, 256 MB x1916 B A Prosperous Lot standard input/out ...
- SVN:Previous operation has not finished; run 'cleanup' if it was interrupted
异常处理汇总-开发工具 http://www.cnblogs.com/dunitian/p/4522988.html cleanup failed to process the following ...
随机推荐
- 如何将一个div水平垂直居中
方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; backg ...
- Scott Young-《如何高效学习》
1.如果你只用一种方式了解某样事物,那么你就没有真正了解它.事情真正含义的秘密取决于我们如何将其与我们所了解的其他事情相联系.很好联系的内容可使你将想法融于脑中,从各种角度看问题,直至你找到合适自己的 ...
- 一张思维导图带你梳理HashMap相关知识
HashMap可以说是java中最常见也是最重要的key-value存储结构类,很多程序员可能经常用,但是不一定清楚这个类背后的数据结构和相关操作原理,为了复习HashMap相关的知识,今天花了一天的 ...
- C#爬虫实践
忘了什么时候加的,iPad上的人人视频追剧了<我的天才女友>,没事的时候看了下,感觉还不错,进一步了解到原著那不勒斯四部曲,感觉视频进度有些慢,就想找找书看看,一时没找到[PS:购买实体书 ...
- JS之执行上下文
执行上下文(execution context),是JS中的一个很重要的概念.它对于我们理解函数定义,执行时都做了什么有着很大的意义.理解它我们才能明白我们常说的函数声明提升,作用域链,闭包等原理. ...
- html中table,tr,td
table表格,tr表格中的行,tr表格中的列,等级关系是table>tr>td, 当然表格中还包括thead,tbody,tfoot,th,但由于浏览器支持缘故很少使用.另外table在 ...
- PHP 面向对象 static 和 self 的区别
一.前言 php是世界上最好的语言 php从面向过程走到现在成熟的面向对象体系, 在php面向对象中,静态变量的调用我们可以用这两个self::method和 static::method, 但是很多 ...
- BZOJ 1441: Min(裴蜀定理)
BZOJ 1441:Min Description 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 Input 第一行给出数 ...
- [BZOJ3631][JLOI2014]松鼠的新家(树链剖分)
[BZOJ3631] 树剖模板题了, Code #include <cstdio> #include <algorithm> #define MID int mid=(l+r) ...
- PHP.19-验证码生成
生成验证码 思路:先定义验证码函数getCode() //绘制验证码 $num = 4; //字符长度 getCode($num, 2); 1.创建画布,分配颜色 imagecreatetruecol ...