Problems > Narayana's Cows Modulo

Narayana's Cows Modulo

Difficulty: 1750 4000 ms 128 MB

math matrix-exponentiation

The sequence obeys N(0)=N(1)=N(2)=1 and N(n)=N(n−1)+N(n−3). Print term N modulo 109+7.

Input: one integer N.

Output: term N mod 109+7.

Constraints:

  • 0 ≤ N ≤ 1018

Sample Tests

Input 1
5
Output 1
4
Input 2
0
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.