Problem:
Score : 600 points

Problem Statement

Given are a prime number p and a sequence of p integers a_0, \ldots, a_{p-1} consisting of zeros and ones.

Find a polynomial of degree at most p-1, f(x) = b_{p-1} x^{p-1} + b_{p-2} x^{p-2} + \ldots + b_0, satisfying the following conditions:

For each i (0 \leq i \leq p-1), b_i is an integer such that 0 \leq b_i \leq p-1.

For each i (0 \leq i \leq p-1), f(i) \equiv a_i \pmod p.

Constraints

2 \leq p \leq 2999

p is a prime number.

0 \leq a_i \leq 1

Input

Input is given from Standard Input in the following format:

p
a_0 a_1 \ldots a_{p-1}

Output

Print b_0, b_1, \ldots, b_{p-1} of a polynomial f(x) satisfying the conditions, in this order, with spaces in between.

It can be proved that a solution always exists. If multiple solutions exist, any of them will be accepted.

Sample Input 1

2
1 0

Sample Output 1

1 1

f(x) = x + 1 satisfies the conditions, as follows:

f(0) = 0 + 1 = 1 \equiv 1 \pmod 2

f(1) = 1 + 1 = 2 \equiv 0 \pmod 2

Sample Input 2

3
0 0 0

Sample Output 2

0 0 0

f(x) = 0 is also valid.

Sample Input 3

5
0 1 0 1 0

Sample Output 3

0 2 0 1 3

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

//! ----------------------------------------------
//! Framework <https://github.com/vain0x/procon>
//!
//! See the bottom of file for solution.
//! ----------------------------------------------

#![allow(unused_imports)]
#![allow(non_snake_case)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Display, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;

/// Print values to standard error if debug mode.
#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), stderr());
            writeln!(err, "\x1B[33m{}\x1B[0m = {:?}", e, $e).unwrap()
        })*
    };
}

/// Read from standard input and parse each word.
/// - `read!(T, U, ..)` parses a line as a tuple of words.
/// - `read![[T]]` parses a line as an array of words.
/// - `read![..; N]` parses `N` lines, using `read!(..)` repeatedly.
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

/// Read a line from standard input.
#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();

    #[allow(deprecated)]
    buf.trim_right().to_owned()
}

// -----------------------------------------------
// Solution
// -----------------------------------------------

pub fn pow(x: i64, n: i64, P: i64) -> i64 {
    let (mut x, mut y, mut n) = (x % P, 1_i64, n);

    // P^4 < 10^18 だから余りを取るのは4ステップに1回でいい
    'a: loop {
        for _ in 0..4 {
            if n <= 0 {
                break 'a;
            }
            if n % 2 != 0 {
                y *= x;
                n -= 1;
            }

            x *= x;
            n /= 2;
        }

        y %= P;
        x %= P;
    }
    y % P
}

fn main() {
    let P = read!(i64);
    let Pz = P as usize;
    let A = read![[i64]];

    let mut fact = vec![0; Pz];
    fact[0] = 1;
    fact[1] = 1;
    for n in 2..Pz {
        fact[n] = n as i64 * fact[n - 1] % P;
    }

    let mut fact_inv = vec![0; Pz];
    for n in 0..Pz {
        fact_inv[n] = pow(fact[n], P - 2, P);
    }

    let combo = |n: i64, k: i64| {
        // P^3≤10^12 だから余りを取るのは1回でいい
        let mut z = fact[n as usize];
        z *= fact_inv[k as usize];
        z *= fact_inv[(n - k) as usize];
        z % P
    };

    // f(x) = Σi. a(i)・(1 - (x-i)^(p-1))
    // (x-i)^(p-1) = Σk=0,p-1. C(p-1,k) x^k (-i)^(p-1-k)

    let mut B = vec![0; Pz];
    for i in 0..Pz {
        if A[i] == 0 {
            continue;
        }

        for k in 0..Pz {
            let mut b = combo(P - 1, k as i64);
            b *= pow((Pz - i) as i64, (Pz - 1 - k) as i64, P);
            // |b|≤10^8 の増減を 10^8 回行なってもオーバーフローしないので余りは取らなくていいはず
            B[k] += b;
        }

        B[0] += 1;
    }

    println!(
        "{}",
        B.into_iter()
            .map(|b| ((b % P + P) % P).to_string())
            .collect::<Vec<_>>()
            .join(" ")
    )
}