PIBO2 - Fibonacci vs Polynomial (HARD)

no tags 

Define a sequence Pib(n) as following

  • Pib(0) = 1
  • Pib(1) = 1
  • otherwise, Pib(n) = Pib(n-1) + Pib(n-2) + P(n)

Here P is a polynomial.

Given n and P, find Pib(n) modulo 1,111,111,111.

Maybe you should solve PIBO before this task, it has lower constraints.

Input

First line of input contains two integer n and d (0 ≤ n ≤ 109, 0 ≤ d ≤ 10000), d is the degree of polynomial.

The second line contains d+1 integers c0,c1 … cd, represent the coefficient of the polynomial (Thus P(x) can be written as Σcixi). 0 ≤ ci < 1,111,111,111 and cd ≠ 0 unless d = 0.

Output

A single integer represents the answer.

Example

Input:
10 0
0

Output:
89

Input:
10 0
1

Output:
177

Input:
100 1
1 1

Output:
343742333

hide comments
eugenio: 2014-11-18 19:39:03

I've learnt so many things to tackle this one (even though I haven't used all of them)... Beautiful problem :)

Last edit: 2014-11-18 19:40:07
(Tjandra Satria Gunawan)(曾毅昆): 2014-11-13 13:25:55

Last Position! :p
Any hint for opti?

Federico Lebrón: 2013-05-27 21:48:38

Hrm, this seems pretty hard. I thought with a 0.01s runtime in PIBO and O(d) memory it'd be good enough, but apparently it's not :s
--ans--> You are the first who uses the special form of mod. Try to solve it without modular division or inverse.
EDIT: Hah, thanks, I had done that as an "optimization", but I was working 4 times as much as I needed :p Cut the time in half now. Still, I'll try to think of a better approach - this clearly too slow! Thanks for the problem :)

Last edit: 2013-05-30 03:53:40
Francky: 2013-03-04 19:21:22

It was great to solve this hard edition, I did my best with O(d) memory print. ;-)
I don't think a python solution will be possible, as it is ~ ×20 to ×100 slower on that kind of tasks.
Many thanks for this PIBO2.


Added by:Michael Kharitonov
Date:2013-02-28
Time limit:0.100s-10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64