传送门: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=egin{cases} kb_0+p,i=0\kb_i+b_{i-1},1le i<n\b_{n-1},i=nend{cases}$$
由于0≤ai<k,于是,
$$b_i=egin{cases} leftlceil-frac{p}{k} ight ceil,i=0\leftlceil-frac{b_{i-1}}{k} ight ceil,1le i<nend{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 ", d); for (int i = 0; i < d; i++) printf("%d ", a[i]); return 0; }