[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-1} = a_d \\ 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;
}

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/8453548.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值