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).
Input
The only line of input contains two space-separated integers p and k (1 ≤ p ≤ 1018, 2 ≤ k ≤ 2 000).
Output
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.
Examples
Input
46 2
Output
7
0 1 0 0 1 1 1
Input
2018 214
Output
3
92 205 1
Note
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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to find a polynomial f(x) with non-negative integer coefficients strictly less than k, such that f(x) ≡ p (mod (x+k)). That is, when evaluating f at x = -k, we get f(-k) = p (since f(x) = q(x)(x+k) + p, plugging x=-k gives p). So the condition is f(-k) = p. However careful: The statement: "remainder is p when divided by (x + k)". In polynomial division, the remainder is a constant (since divisor is degree 1). So indeed f(x) = q(x)(x+k) + p. For integer polynomials, if we allow q(x) with rational coefficients? The statement says "q(x) is a polynomial (not necessarily with integer coefficients)". So the division is in the ring of polynomials over reals? But they want f(x) with non-negative integer coefficients less than k. The remainder is p (constant). So we require f(-k)=p. So we need to find a representation of p in base (-k) but with digits (coefficients) non-negative and less than k. Usually representation in base b uses digits 0..b-1. But here base is negative: -k, with 0 ≤ ai < k. That is exactly a representation of p in base (-k) using digits from 0 to k-1. But is that always possible? For any integer p and base b (|b| ≥ 2) there is a unique representation with digits between 0 and |b|-1 if we allow negative base? Actually, representation of integers in negative base is possible: e.g., base -2. For base -k, we can represent any integer as sum_{i=0}^{n-1} a_i (-k)^i, with 0 ≤ a_i < k. That's a mixed radix system. The typical algorithm: repeatedly divide by -k, but since division with negative base can produce remainders in [0, k-1]. For a given p, we can attempt to compute the digits by the following: while p != 0, compute remainder r = p mod k? But careful: The division algorithm: For base b = -k, we want p = a0 + b * q, where a0 in [0, k-1]. So we need to find q such that (p - a0) / b = integer. That is q = (p - a0)/(-k). For this to be integer, we need p ≡ a0 (mod -k)? But mod with negative base is tricky. Alternatively, we can set a0 = p mod k? But p mod k yields a remainder in [0, k-1] when dividing by k (positive). But we need p = a0 + (-k)*q. So p ≡ a0 (mod k) actually? Because p = a0 + (-k)*q => p ≡ a0 (mod k). Indeed modulo k, (-k)*q ≡ 0, so p ≡ a0 (mod k). So we can set a0 = p mod k (the least non-negative remainder). Then q = (a0 - p)/k. Let's compute: p = a0 + (-k)*q => p = a0 - kq => kq = a0 - p => q = (a0 - p)/k. Since a0 ≡ p (mod k), (a0 - p) is divisible by k, and q = (a0 - p)/k = -(p - a0)/k. So q is integer, possibly negative. Then we repeat with p = q? Actually we want f(x) = Σ a_i x^i, with condition f(-k)=Σ a_i (-k)^i = p. The process: p = a0 + (-k) * p1, where p1 = Σ_{i=1}^{n-1} a_i (-k)^{i-1}. Then we can compute a0 as p mod k (ensuring 0 ≤ a0 < k). Then set p1 = (a0 - p)/k (which is integer). Then we need to represent p1 similarly (with digits a1, a2, ...). So this is exactly the conversion to base -k. So we can iteratively compute digits until p becomes 0. But we must ensure that all digits are non-negative and < k, which they will be by construction. However, will the process always terminate? Possibly not? Let's examine: p1 = (a0 - p)/k = - (p - a0)/k. Since a0 = p mod k, we have p = k * t + a0, where t = floor(p/k). Then p1 = (a0 - (k t + a0))/k = -t. So p1 = - floor(p/k). So after first step, the new "p" is -⌊p/k⌋. Next step, we compute a1 = p1 mod k? But careful: p1 might be negative. The algorithm for negative base representation typically handles negative numbers by taking remainder in [0, k-1] and then adjusting quotient accordingly. However, the recurrence we derived: p = a0 + (-k) * p1. We set a0 = p mod k (non-negative remainder when dividing by k). For p negative, p mod k (as typically defined) is non-negative, but the quotient floor division may be different. But let's see: p negative: Write p = k * q + r, with 0 ≤ r < k, where q = floor(p/k) (which may be negative). Then a0 = r, and p1 = (a0 - p)/k = (r - (kq + r))/k = -q. So p1 = -q. Since q = floor(p/k), p1 = - floor(p/k). For negative p, floor(p/k) is negative, so p1 becomes positive? Example: p = -1, k=2. floor(-1/2) = -1? Actually floor(-0.5) = -1? In integer division, floor(-0.5) = -1. But typically in programming, p // k is floor division? But we need to be careful: We want an algorithm that always yields digits 0 ≤ a_i < k and terminates (i.e., eventually p becomes 0). This is known as representation in base -k. It is known that every integer has a unique representation in base -k with digits 0..k-1. The conversion algorithm: While n != 0: r = n mod k (but mod operation should return a non-negative remainder, e.g., in Python: r = n % k gives 0 <= r < k). Then n = -(n - r)//k? Actually from p = a0 + (-k)*p1 => p - a0 = -k p1 => p1 = (a0 - p)/k. Since a0 = p mod k (non-negative), we can compute p1 = -(p - a0)/k. But (p - a0) is divisible by k. So p1 = -(p - a0)//k, using integer division that truncates towards zero? Actually if we compute (p - a0)//k, since p-a0 is divisible by k, exact division yields an integer, but its sign is sign(p). Then p1 = -(p - a0)//k. If we compute integer division with floor? However, using standard integer division where // truncates towards zero? But if we want to be safe, we can compute p1 = (a0 - p) // k. Since a0-p is divisible by k, it's exact. So p1 = (a0 - p) // k. In Python, // with negative numbers floors, but since a0-p is divisible by k, the result is exact anyway. So we can compute p1 = (a0 - p) // k. This works for any sign of p.
Let's test: p=46, k=2. p mod 2 = 0 => a0=0. p1 = (0-46)//2 = -23. Next p=-23. p mod 2: -23 % 2 =? In Python, -23 % 2 = 1 (since -23 = -12*2 + 1). So a1 = 1. p1 = (1 - (-23))//2 = (24)//2 = 12. Next p=12: a2 = 12%2=0, p1=(0-12)//2=-6. p=-6: a3=(-6)%2=0? -6%2=0, p1=(0-(-6))//2=3. p=3: a4=3%2=1, p1=(1-3)//2 = (-2)//2 = -1. p=-1: a5=(-1)%2=1, p1=(1-(-1))//2= (2)//2=1. p=1: a6=1%2=1, p1=(1-1)//2=0. Done. Digits: a0=0, a1=1, a2=0, a3=0, a4=1, a5=1, a6=1. That's exactly the example output: 7 coefficients: 0 1 0 0 1 1 1.
Second example: p=2018, k=214. Compute digits. Let's test quickly: p=2018. p mod 214: 2149=1926, remainder 92 => a0=92. p1 = (92-2018)//214 = (-1926)//214 = -9. (since -1926/214 = -9 exactly). p=-9. p mod 214: -9 % 214 = 205 (since -9 = -1214 + 205). a1=205. p1 = (205 - (-9))//214 = 214//214=1. p=1. a2 = 1 % 214 =1, p1 = (1-1)//214 =0. Done. Digits: 92,205,1. That matches example.
Thus the algorithm works: Represent p in base -k with digits in [0,k-1].
But does this always succeed? For any p >=1 and k>=2, the process should terminate because |p| decreases? Let's check magnitude: p1 = (a0 - p)/k. We can bound |p1| = | - (p - a0)/k | = |p - a0|/k. Since 0 ≤ a0 < k, |p - a0| is approximately |p|, but dividing by k may reduce magnitude if |p|>k? However, can |p1| be larger than |p|? For p positive, a0 = p % k, so p = kt + a0, t>=0. Then p1 = -t. So |p1| = t = floor(p/k) which is less than p unless p<k? For p<k, t=0, so p1=0, process ends. For p positive, the next p1 is negative and its absolute value is at most p/k. So magnitude reduces. For p negative, p = kt + a0 where t is floor(p/k) (which is negative, e.g., p = -9, k=214: floor(-9/214) = -1 because -9/214 = -0.042, floor = -1; then a0 = p - kt = -9 - 214(-1) = -9+214=205). So t is negative. Then p1 = -t, which is positive. And |t| = floor(|p|/k) maybe? Actually if p negative, t = floor(p/k) (floor division) which is <= p/k, and |t| is approximately |p|/k but at least 1 if |p|>=k? For p=-9, |t|=1, which is less than 9. For p=-1, k=2: floor(-1/2) = -1, so |t|=1, which is greater than 1? Actually |t|=1 equals |p|. That's a problem: p=-1, |p|=1, t=-1, p1 = 1, so |p1| = 1 = |p|. That doesn't reduce magnitude. Could the process get stuck in a loop? Let's test p=-1, k=2. We already did: digits: p=-1: a0 = (-1) % 2 = 1 (since -1 = -12 +1). Then p1 = (1 - (-1)) // 2 = 2//2 =1. p=1: a1 = 1%2 =1, p2 = (1-1)//2=0. So after two steps we get zero. So it terminated. So even though |p1| = |p| for p=-1, eventually the sign flips and magnitude reduces. In general, after each step, p alternates sign? For p negative, p1 = -t where t = floor(p/k). Since p negative, t is ≤ p/k (more negative or equal). But t is negative, so -t is positive. So p1 positive. Then for p1 positive, next p2 = - floor(p1/k) (negative). So magnitude reduces when p positive and > k? But when p positive but < k, p1 becomes 0. When p negative but |p| < k? Example p=-1,k=2: t = floor(-0.5)= -1, so p1 = 1 which is positive <k? Actually 1<2, so next step will yield p2=0. So it terminates. For p=-k+1, say k=3, p=-2. Then a0 = (-2)%3 = 1 (since -2 = -13 +1). Then p1 = (1 - (-2))//3 = 3//3=1. Then p=1: a1=1%3=1, p2=0. So terminates. For p=-k, p=-3, k=3: a0 = (-3)%3 = 0; p1 = (0 - (-3))//3 = 3//3=1; p=1 => terminates. So seems always terminates.
But is it always the case? There is known fact: For any integer base b with |b|>1, every integer has a unique representation with digits 0..|b|-1. This is standard. So the answer always exists? The problem statement says "If the polynomial does not exist, print -1". So is there any case where it doesn't exist? Possibly if p is negative? But p is given as 1 ≤ p ≤ 10^18, so p is positive. So p is always positive. For positive p, the algorithm yields digits and terminates, as we saw. So it seems always exists. But maybe there is nuance: The polynomial coefficients must be non-negative integers < k. That's exactly digits. So representation always exists for any integer p. But is it always possible for positive p? Yes, because base -k representation yields digits 0..k-1, and the number is expressed as Σ a_i (-k)^i. For positive p, the highest index will be such that the leading coefficient is positive. However, is it guaranteed that the leading coefficient is non-zero? Yes, unless p=0. But p≥1, so representation has at least one digit, and the highest digit is >0. So we always have a solution.
But wait: The problem states: "Your output should satisfy 0 ≤ ai < k for all 0 ≤ i ≤ d - 1, and ad - 1 ≠ 0." That's exactly the digits from base -k representation. So why would they ask to output -1 if polynomial does not exist? Possibly there are cases where no such polynomial exists? Since p is positive and k>=2, always exists. Unless there is a hidden condition: The polynomial coefficients must be non-negative integers, but also the polynomial f(x) must have integer coefficients. That's satisfied. The division condition: f(x) = q(x)·(x + k) + p, with q(x) polynomial (not necessarily integer coefficients). But if f has integer coefficients, then dividing by (x+k) gives quotient with rational coefficients in general. That's allowed. So no issue.
Maybe there is constraint that coefficients are strictly less than k, which we already have. So existence seems guaranteed. Then why -1? Possibly they consider that d (the number of coefficients) must be non-negative integer. But if p=0? But p ≥ 1, so not. However, what about p = 0? Not allowed. So maybe -1 never needed. But perhaps there is scenario where the base -k representation yields a coefficient a_i >= k? But by construction, we enforce a_i in [0,k-1]. So it's fine.
Wait, maybe the condition includes that the polynomial f(x) must have integer coefficients, and the division yields remainder p, but q(x) is not necessarily integer coefficient. That is fine.
Could there be a case where the representation yields leading zero? We will trim.
Thus solution is simply: Convert p to base -k, output the digits a0, a1, ... (least significant first). That yields the polynomial f(x)= Σ a_i x^i.
Now we need to implement for p up to 1e18 and k up to 2000, which is fine. The number of digits may be up to something like log_{k}(p) maybe times some factor because base is negative, but roughly O(log p / log k). With p up to 1e18, k min 2, max 2000, so digits at most about log_2(1e18) ~ 60, maybe slightly more because negative base might cause alternating signs leading to more digits? Actually the representation length is about log_k(p) + something. For k=2, representation length of 46 was 7, which is about log_2(46) ~ 5.5, but 7 is a bit larger. For p=1e18, base 2, worst-case digits: maximum number representable with n digits in base -2: The range of representable numbers is something like? Actually we can compute: For base -2, the maximum positive number with n digits (a_{n-1}..a0, each 0 or 1) is when digits are 1 at even positions? Not sure. But length is O(log p). So fine.
Implementation: While p != 0:
a = p % k (Python's % yields non-negative remainder)
Append a to list.
p = (a - p) // k (since p = a + (-k) * new_p => new_p = (a - p)/k)
Alternatively, p = -(p - a) // k, but careful with integer division: In Python, // does floor division, but since (p - a) is divisible by k, (p - a) // k yields exact integer, but sign? Actually (p - a) // k, if (p - a) is negative, floor division might produce a number that is one less? But because (p - a) is exactly divisible by k, the floor division yields exact quotient. Example: p=-1, a=1, p-a = -2, -2//2 = -1 exactly. Then p = -(-1) =? Not that. Let's just use p = (a - p) // k. For p=-1,a=1: (1 - (-1))//2 = 2//2=1, correct. For p=46,a=0: (0-46)//2 = -46//2 = -23, correct. For p=-23,a=1: (1-(-23))//2 = 24//2=12, correct. So p = (a - p) // k works for both positive and negative p as long as (a-p) is divisible by k, which it is because p ≡ a (mod k). In Python, integer division // of a negative number that is exactly divisible yields correct integer because there is no rounding needed. So we can safely use p = (a - p) // k.
But note: In Python, if we do (-46)//2, that gives -23, correct. So all good.
We need to ensure we stop when p=0. The list of digits will be from least significant to most. Then output d = len(list), and the list in order a0, a1, ...? The output expects: "print d space-separated integers a0, a1, ..., ad-1" which are the coefficients of the polynomial: f(x)= a0 + a1 x + ... + a_{d-1} x^{d-1}. So indeed that matches listing digits starting from constant term.
Thus we can implement.
But wait: Are there any edge cases? p=1, k=2. Then p=1: a0=1%2=1, p=(1-1)//2=0. Digits: [1]. Output d=1, "1". Check: f(x)=1. f(-2)=1, remainder is 1, correct. coefficients <2, non-negative.
p=1, k=1000: a0=1, p=0 => [1]. Good.
p=k? p=2, k=2: p=2: a0=2%2=0, p=(0-2)//2 = -1. p=-1: a1=(-1)%2=1 (since -1%2=1), p=(1-(-1))//2 = 1. p=1: a2=1%2=1, p=(1-1)//2=0. Digits: [0,1,1]. So polynomial: f(x)= x^2 + x? Actually a0=0, a1=1, a2=1 => f(x)= x + x^2? But careful: order: a0 constant, a1 x, a2 x^2 => f(x)= x + x^2? Actually that's x^2 + x. f(-2)=4 -2 =2, correct. coefficients less than 2: 0,1,1 okay. So works.
Now, what about p huge up to 1e18 and k up to 2000: conversion is O(log_{k} p) steps, safe.
Thus answer always exists. So why mention -1? Could be a red herring or perhaps some obscure case where no such polynomial exists? Let's think deeper.
The condition: f(x) = q(x)·(x + k) + p, with q(x) not necessarily integer coefficients. That's always possible if we treat polynomials over reals. But maybe they require f(x) to have integer coefficients? That's given. So we need integer polynomial f such that f(-k)=p. But any integer polynomial evaluated at -k yields some integer. So the question reduces to: given p, can we find integer coefficients a_i ∈ [0, k-1] such that Σ a_i (-k)^i = p. This is a representation in base -k. As argued, it's always possible for any integer p.
But wait: There is a subtlety: The coefficients are non-negative and < k. The base -k representation always yields digits in that range, but is it always valid for any integer? Yes, because the representation is essentially the unique representation in the numeral system with negative base. It is known that every integer (positive, negative, or zero) has such a representation. The algorithm terminates because the absolute value of the number decreases after two steps if not after one? More formally, we can prove termination. So for positive p, it exists. So -1 never needed.
But the problem statement includes possibility of -1. Could there be a case where the leading coefficient becomes zero? But we can always ensure it's non-zero by trimming. That's fine.
Perhaps the polynomial must have coefficients strictly less than k, but also the polynomial f(x) must have integer coefficients, and the division yields remainder p when divided by (x+k). In polynomial division over integers, if we divide an integer polynomial by (x+k), the remainder is a constant, specifically f(-k). So it's the same condition. So fine.
Thus solution is straightforward.
But I'm aware of some Codeforces problem "Little Pony and Sort by Shift" or something? Actually this problem is likely from Codeforces: "Polynomial and Mod" or something. I recall a problem: "Given p and k, find polynomial with coefficients in [0, k-1] such that f(k) = p"? Wait, typical problem is: "Given p and k, find a polynomial with coefficients between 0 and k-1 such that f(k) = p". That is representation in base k. But there, existence is not guaranteed if p is not representable? Actually if coefficients are 0..k-1, then f(k) yields a number in base k representation, so any p can be represented in base k with digits 0..k-1? Actually any non-negative integer can be represented in base k with digits 0..k-1. So that also always exists. But they often ask to find the polynomial such that f(k)=p. That is simply base-k representation. But here it's x+k, i.e., evaluate at -k. So it's negative base representation.
But why -1? Possibly they consider if p=0? But p>=1. So maybe for some p and k the only representation would require leading coefficient zero? Not possible because representation yields non-zero leading coefficient. Or maybe they want the polynomial to have integer coefficients, but the division by (x+k) must produce a polynomial q(x) with integer coefficients? The statement says "q(x) is a polynomial (not necessarily with integer coefficients)". So that is allowed.
Wait, maybe the condition is that f(x) is cleaned by taking it modulo another. It says: "Little Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this..." So he originally had polynomial f, then took f mod (x+k) to get p? That is, he computed f(x) mod (x+k) and got p. Then he wants to recover f. So f must have coefficients in [0,k-1]. But there could be many f. So we need to output any.
But why -1? Perhaps for some p and k, there is no polynomial f with coefficients in that range such that f mod (x+k) = p. But we argued that f(x) mod (x+k) = f(-k). So we need f(-k)=p. Could it be that for some p and k, it's impossible to represent p as Σ a_i (-k)^i with 0 ≤ a_i < k? Is there a theorem that every integer has a representation in negative base? Yes, it's a known fact. But is it always possible with non-negative digits? Let's think: For base b negative, we want digits in [0, |b|-1]. This is standard "negabinary" for base -2. For general base -k, it works. The algorithm we described is exactly the conversion algorithm. So existence is guaranteed.
But maybe there is a catch: The polynomial coefficients must be integers, but also we require that the polynomial is "cleaned up" by taking modulo another? Hmm.
Let's double-check the condition: "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)." They emphasize that q(x) may not have integer coefficients. That means the division is in ℝ[x] or ℚ[x] but not necessarily integer polynomial division. So the remainder is defined as usual polynomial division: when dividing f by (x+k), we get quotient and remainder (degree less than 1) which is constant. That constant equals f(-k). So indeed.
Thus the problem reduces to representing p in base -k. So solution always exists for p>0.
But maybe there is a condition: The polynomial must have integer coefficients. That is satisfied.
I suspect the -1 case might be for p=0? But p>=1. Or maybe if k is 0? But k>=2. So always exists.
Let's search memory: There's a Codeforces problem "C. Little Pony and Sort by Shift"? No. There's "A. Little Pony and Expected Maximum"? Not.
Maybe it's from a contest: "Polynomial and Mod" could be from AtCoder? I recall a problem from AtCoder ABC? Actually AtCoder ABC 192 D? Not.
Anyway, it's safe to implement the conversion.
But wait: Let's verify for some random values: p=100, k=10. Base -10 representation? Let's compute: p=100: 100%10=0 => a0=0, p=(0-100)//10 = -10. p=-10: a1=(-10)%10 = 0? -10%10=0 (since -10 = -110 + 0). Actually Python: -10 % 10 = 0. So a1=0, p=(0-(-10))//10 = 1. p=1: a2=1%10=1, p=(1-1)//10=0. Digits: [0,0,1] => polynomial: f(x)=1x^2? Actually a0=0,a1=0,a2=1 => f(x)=x^2. f(-10)=100, correct. Coeffs <10. So works.
Now what about p=5, k=10: a0=5, p=0 => [5] => f(x)=5. f(-10)=5. Works.
So seems fine.
But wait, what about p=1, k=2? Already did.
Now, could there be a case where the conversion yields leading zero? The loop stops when p=0. If the last digit we appended is zero? But we only append when p != 0. So the last appended digit is always from a non-zero p? Not necessarily: If p becomes zero after some step, we stop, and we do not append a zero. So the list ends with the last non-zero digit from the previous step. For example, if we had p such that after computing a, we set new p to zero, the loop ends, and we have a as the last digit. That a is non-zero? It could be zero if p was a multiple of k? Let's test: p=2, k=2: p=2, a0=0, new p = -1 (non-zero), so we continue. So zero can be appended when p non-zero. The last step: p becomes 0 only after setting p to (a - p)//k; if that yields 0, then the a that gave that p was from previous iteration, and that a could be zero? Let's see: Suppose p=0 initially? Not possible. In iteration, we have current p, we compute a = p % k, then compute new p = (a - p) // k. If new p = 0, we exit loop. So the digit a from that iteration is included. Could a be zero? Example: p such that (a-p)//k = 0 => a-p = 0 => a = p. And p % k = a = p, and (a-p)=0 => new p=0. That means p = a, and a = p % k. So p = p % k => p < k. So for p < k, a = p, new p = 0. So a = p which is >0 because p≥1. So a is non-zero. So the last digit is always non-zero because p >=1 and 0. If p >= k, then new p is non-zero (since floor division yields >=1 in magnitude). So the last digit is from a step where p < k? Actually eventually p will become less than k in absolute value? Possibly when p becomes positive and less than k, then a=p>0, new p=0. Or p could become negative with absolute less than k? But p negative could also lead to new p=0? Let's check: p negative but absolute less than k? Example: p=-1, k=2: a = (-1)%2 = 1, new p = (1 - (-1))//2 = 1, not zero. So negative p doesn't directly give zero; it flips to positive. Then eventually positive p less than k yields termination. So the final digit is from a positive p less than k, thus non-zero. So the leading coefficient (the last appended) is always positive. So condition ad-1 ≠ 0 is satisfied.
Thus everything fine.
Now why would they include -1? Possibly the problem originally had constraints that p could be 0? But p≥1, so no.
Maybe there is hidden condition: f(x) must have integer coefficients, and q(x) must also have integer coefficients? The statement says "q(x) is a polynomial (not necessarily with integer coefficients)". That implies it's allowed not to have integer coefficients, so that condition is relaxed. So no.
Alternatively, perhaps the polynomial must have non-negative integer coefficients strictly less than k, but also the polynomial must be "cleaned up by taking it modulo another" meaning that originally f(x) had integer coefficients maybe not restricted, and he took f(x) mod g(x) where g(x)=? Hmm.
But I'll trust the algorithm.
Now, edge case: p=0? Not in constraints.
Thus implement:
def solve():
read p, k
if p == 0? (but p>=1) -> handle maybe.
digits = []
while p != 0:
a = p % k # Python's % yields non-negative
digits.append(a)
p = (a - p) // k
d = len(digits)
print(d)
print(' '.join(map(str, digits)))
But careful: In Python, for negative p, p % k yields result in [0,k-1], which is correct.
Test on given examples.
Example1: p=46,k=2 -> digits computed as [0,1,0,0,1,1,1]? Let's test:
But note order: we appended in order a0, a1, a2, ... So digits list is [a0, a1, a2, ...] which is what output expects. So print as is.
Example2: p=2018,k=214 -> digits = [92,205,1] as earlier.
All good.
Now consider p huge: 10^18, k=2. Let's compute approximate digits: In base -2, representation length might be up to about 2*log2(p) maybe? Actually max number with n digits in negabinary? Let's approximate: The magnitude grows like roughly k^{n-1}? For base -2, the largest positive with n digits (n odd/even) is something like (2^{n+1}-2)/3? That is ~ (2/3)*2^n, so n ≈ log2(1.5p). For p=1e18, log2~60, so n ~ 60-70. Fine.
So algorithm is efficient.
Thus final answer: Just implement.
But wait, is there any catch about "the number of coefficients" d? They want d as non-negative integer. For p>=1, d will be at least 1.
Thus solution is straightforward.
But I'm still thinking about -1. Could it be that for some p and k, the base -k representation yields a coefficient equal to k? No because we use mod.
Maybe there is scenario where the representation would require infinite digits? But the algorithm terminates.
Thus answer is never -1. But to be safe, we could still include a check: if after conversion we get empty list? That would only happen if p==0, which is not in input. So not needed.
But perhaps the problem originally had p can be 0? Possibly they allowed p=0? But constraints say 1 ≤ p ≤ 10^18, so not.
Thus we print the representation.
Now, confirm that the polynomial f(x) indeed satisfies f(-k)=p. Since we constructed using the base representation, it's guaranteed.
Now, is there any alternative representation that might have fewer digits? Possibly, but any representation works.
Thus final solution.
But before finalizing, let's re-check the problem statement: "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)." This implies we want f(x) such that when you do polynomial division (over reals) by (x+k), the remainder is constant p. That's f(-k)=p. So correct.
Thus solution is as above.
But wait: The note in example 1 says: f(x) = x^6 + x^5 + x^4 + x = (x^5 - x^4 + 3x^3 - 6x^2 + 12x - 23)·(x + 2) + 46. Indeed the f they gave is 0 + 1x + 0x^2 + 0x^3 + 1x^4 + 1x^5 + 1x^6? Actually they wrote "0 1 0 0 1 1 1" which corresponds to f(x)=0 + 1x + 0x^2 + 0x^3 + 1x^4 + 1x^5 + 1x^6 = x + x^4 + x^5 + x^6, but they wrote in note "x^6 + x^5 + x^4 + x". Yes that matches. So they listed coefficients from a0 to a6.
All good.
Thus answer.
But I recall a Codeforces problem "C. Little Elephant and Function" but no.
Maybe there is a nuance: The polynomial f must have non-negative integer coefficients less than k. That's the same as digits. But we derived the condition f(-k)=p. However, is it possible that f(-k)=p but f(x) mod (x+k) yields p? Because polynomial division remainder is p means f(x) = q(x)(x+k) + p. That implies f(-k) = p. Conversely, if f(-k)=p, does it guarantee that the remainder upon division by (x+k) is p? Over the reals, yes: Since (x+k) is linear, the remainder is constant r, and evaluating at x=-k gives f(-k)=r. So r = p. So equivalence holds.
Thus the problem is solved.
Now, why -1? Possibly the problem originally had p could be any integer (including negative) and k any positive integer, and for some combos representation might not exist because the algorithm might not terminate? But it always does. However, there is a subtle point: The coefficients must be non-negative and strictly less than k. For negative p, representation also exists (with digits in [0,k-1]) and yields a polynomial that when evaluated at -k gives p. But is that always possible? For negative p, the base -k representation yields a polynomial that evaluates to p, but the coefficients are still non-negative. So representation exists for any integer p (positive, negative, zero). So -1 would never be needed. Unless they require that the polynomial f(x) must have integer coefficients and also that the polynomial q(x) must be a polynomial with integer coefficients? The statement says "q(x) is a polynomial (not necessarily with integer coefficients)". So they explicitly say it may not have integer coefficients. So they allow rational coefficients. So even if we had a representation, does it guarantee that q(x) exists as a polynomial (with possibly rational coefficients)? Yes, because we can perform polynomial division; the quotient will have rational coefficients if f(x) is integer. But it's always possible. So nothing else.
Thus answer.
But I'll double-check: In the example, they show q(x) with integer coefficients? Actually for first example, q(x) = x^5 - x^4 + 3x^3 - 6x^2 + 12x - 23, which has integer coefficients. That's interesting. Could it be that they require q(x) to have integer coefficients? The statement says not necessarily, but maybe they just gave an example where it happens to be integer. It's not required.
Thus fine.
Thus I'll implement solution.
Now, implementation details: Use Python. Input: p, k = map(int, input().split()). While p != 0: r = p % k; digits.append(r); p = (r - p) // k. Output len(digits) and digits.
But careful: In some languages, modulo for negative numbers might give negative remainder. In Python it's fine.
We can also implement in C++ if needed, but we can provide solution in Python.
Edge case: If p=0 (though not allowed), then digits would be empty; we would output d=0? But they require d non-negative integer; but we could handle by printing 0 and empty line? But not needed.
Thus final.
Now, I think we should also consider that p might be up to 10^18, which fits in 64-bit. Python int is arbitrary precision.
Now, I'm going to think about if there is any possibility that the algorithm might produce an infinite loop. Consider p=0? breaks. Good.
Thus finalize.
But before final answer, let's test more cases with a script mentally.
Now, is there any scenario where the algorithm might produce a digit equal to k? No, because p%k yields value in [0,k-1].
Thus final.
I will write the solution explanation and code.
But given the problem likely expects to handle -1 case, maybe there is something else: The polynomial must have coefficients less than k, non-negative integer. But also f(x) must be a polynomial (finite degree). Our representation gives finite degree. So fine.
Maybe there is constraint that the polynomial must have integer coefficients and that q(x) must be a polynomial (could be any), but also the polynomial division must be exact with remainder p. That's same.
Thus final answer: Represent p in base -k.
Now I'll produce a comprehensive reasoning.
Proof of correctness:
The condition f(x) = q(x)(x+k) + p is equivalent to f(-k) = p.
For f(x) = Σ_{i=0}^{d-1} a_i x^i, we have f(-k) = Σ a_i (-k)^i.
We need Σ a_i (-k)^i = p, with 0 ≤ a_i < k.
This is exactly the representation of p in base -k with digits a_i.
The conversion algorithm (repeated division by -k) yields such a representation for any integer p, and terminates because after each step the absolute value of the number decreases or becomes zero within two steps.
Since p > 0, the algorithm produces at least one digit, and the last digit is non-zero.
Thus the constructed list of digits gives a valid polynomial.
Complexity: O(log_k(p)) time, O(log_k(p)) memory.
Now produce final code.
Potential pitfalls: In languages where % with negative numbers yields negative remainder, we must adjust. But we'll use Python.
Code:
code
def solve():
import sys
p, k = map(int, sys.stdin.readline().split())
digits = []
while p != 0:
r = p % k
digits.append(r)
p = (r - p) // k
print(len(digits))
print(' '.join(map(str, digits)))
But maybe they want to output -1 if no polynomial exists? Since it always exists, we never output -1. But we could include a check: if after loop digits empty, print -1. But not needed.
Thus final. ```python
import sys
def main() -> None:
data = sys.stdin.readline().strip()
if not data:
return
p, k = map(int, data.split())
code
coeff = []
while p != 0:
r = p % k # remainder in [0, k-1]
coeff.append(r)
p = (r - p) // k # exact division, moves to next "digit"
# p >= 1 guarantees at least one coefficient
print(len(coeff))
print(' '.join(map(str, coeff)))