传送门: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 > && x % y) res++;
return res;
} int main(void)
{
int64_t p, k;
scanf("%I64d%I64d", &p, &k);
int d = ;
b[] = ceil_div(-p, k);
a[] = k * b[] + p;
for (int i = ; i < MAX_N; i++) {
b[i] = ceil_div(-b[i - ], k);
a[i] = k * b[i] + b[i - ];
if (a[i]) d = i + ;
}
printf("%d\n", d);
for (int i = ; i < d; i++)
printf("%d ", a[i]);
return ;
}