Codeforces 934.D A Determined Cleanup
1 second
256 megabytes
standard input
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).
The only line of input contains two space-separated integers p and k (1 ≤ p ≤ 1018, 2 ≤ k ≤ 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 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.
46 2
7
0 1 0 0 1 1 1
2018 214
3
92 205 1
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的更多相关文章
- 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 934D - A Determined Cleanup
934D - A Determined Cleanup 思路: 找规律,和k进制的求法差不多,答案的奇数位是p%k,偶数位如果p%k!=0,那么答案是k-p%k,否则为0. 代码: #include& ...
- 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 ...
- [codeforces934D]A Determined Cleanup
[codeforces934D]A Determined Cleanup 试题描述 In order to put away old things and welcome a fresh new ye ...
- [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 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 934 A.Compatible Pair
http://codeforces.com/contest/934 A. A Compatible Pair time limit per test 1 second memory limit p ...
- Codeforces 934.C A Twisty Movement
C. A Twisty Movement time limit per test 1 second memory limit per test 256 megabytes input standard ...
随机推荐
- django+xadmin在线教育平台(十)
剩余app model注册 courses注册 新建courses/adminx.py: # encoding: utf-8 __author__ = 'mtianyan' __date__ = '2 ...
- 《Redis设计与实现》- RDB持久化
Redis RDB持久化功能可以将Redis内存中的数据库状态保存到磁盘里面,避免数据意外丢失. 1. 手动生成 RDB 文件 有两个Redis命令可以用于生成RDB文件: SAVE,该命令会阻塞Re ...
- 转-Spark编程指南
Spark 编程指南 概述 Spark 依赖 初始化 Spark 使用 Shell 弹性分布式数据集 (RDDs) 并行集合 外部 Datasets(数据集) RDD 操作 基础 传递 Functio ...
- PAT (Basic Level) Practice 1006 换个格式输出整数
个人练习 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为 ...
- POJ 2836 状压DP
Rectangular Covering Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2727 Accepted: 7 ...
- [BZOJ2427][HAOI2010]软件安装(tarjan+树形DP)
如果依赖关系出现环,那么对于一个环里的点,要么都选要么都不选, 所以每个环可以当成一个点,也就是强连通分量 然后就可以构造出一颗树,然后树形背包瞎搞一下就行了 注意要搞一个虚拟节点当根节点 Code ...
- No module named appium
在脚本中会有:from appium import webdriver 第一次运行时可能会遇到这样的error:No module named appium 之所以会报这样的error是因为没有装cl ...
- Eclipse EE 运行 Myeclipse Web 应用
第一步:进入项目所在目录,打开.project文件,找到<natures>...</natures>标签,在中间添加如下子标签内容 <nature>org.ecli ...
- MyEclipse - MyEclipse优化
1.去除不需要的启动加载项 选择菜单:Window --> Preferences -->General --> Startup and Shutdown, 可以关掉的启动项有: J ...
- C#正则表达式引发的CPU跑高问题以及解决方法
3月23日(周日)下午16:30左右,博客园主站负载均衡中的2台Web服务器CPU玩起了爬楼梯的游戏(见上图),一直爬到了接近100%.发现这个状况后,我们立即将这2台阿里云临时磁盘云服务器从负载均衡 ...