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-1},且存在多项式q(x),s.t.f(x)=q(x)·(x+k)+p。求多项式f(x)。
设$f(x)=\sum_{i=0}^{n}a_i x^i$,$q(x)=\sum_{i=0}^{n-1}b_i x^i$,则:$f(x)=q(x)\cdot(x+k)+p=kb_0+p+\sum_{i=1}^{n-1}(kb_i+b_{i-1})x^i+b_{n-1}x^n$。于是,
$$a_i=\begin{cases} kb_0+p,i=0\\kb_i+b_{i-1},1\le i<n\\b_{n-1},i=n\end{cases}$$
由于0≤ai<k,于是,
$$b_i=\begin{cases} \left\lceil-\frac{p}{k}\right\rceil,i=0\\\left\lceil-\frac{b_{i-1}}{k}\right\rceil,1\le i<n\end{cases}$$
即可确定多项式系数。参考程序如下:
#include <stdio.h> #include <stdint.h> #define MAX_N 10000 int64_t a[MAX_N], b[MAX_N]; int64_t ceil_div(int64_t x, int64_t y) { int64_t res = x / y; if (x > 0 && x % y) res++; return res; } int main(void) { int64_t p, k; scanf("%I64d%I64d", &p, &k); int d = 1; b[0] = ceil_div(-p, k); a[0] = k * b[0] + p; for (int i = 1; i < MAX_N; i++) { b[i] = ceil_div(-b[i - 1], k); a[i] = k * b[i] + b[i - 1]; if (a[i]) d = i + 1; } printf("%d\n", d); for (int i = 0; i < d; i++) printf("%d ", a[i]); return 0; }
posted on 2018-02-20 17:17 SiuGinHung 阅读(277) 评论(0) 编辑 收藏 举报