Skip to main content
Back to problems
Codeforces
Medium
Arrays
Math
Number Theory
Google
Jzzhu and Sequences

Given the first two values of a sequence defined by a Fibonacci-like recurrence, answer the n-th term modulo 109+710^9+7.

Acceptance 0%
Problem Statement

Jzzhu and Sequences

You are given two integers xx and yy that define a sequence aa as follows:

  • a1=xa_1 = x
  • a2=ya_2 = y
  • For every i≥3i \ge 3, ai=ai−1−ai−2a_i = a_{i-1} - a_{i-2}

For a given index nn, compute the value of ana_n modulo 109+710^9 + 7.

Because the values can become negative, the answer should always be reported in the standard non-negative modulo form.

This is a direct sequence-evaluation problem with a repeating pattern in the recurrence.

Input Format

  • A single line containing three integers xx, yy, and nn.

Output Format

  • Print one integer: an mod (109+7)a_n \bmod (10^9+7).

Constraints

  • 1≤n1 \le n
  • xx and yy fit in 64-bit signed integers
  • Output the result in the range [0,109+6][0, 10^9+6]
Examples
Sample cases returned by the problem API.

Example 1

Input

1 2 1

Output

1

Explanation

The first term is a1=1a_1 = 1.

Example 2

Input

1 2 4

Output

1

Explanation

The sequence is 1,2,1,−1,…1, 2, 1, -1, \dots, so a4=−1≡109+6(mod109+7)a_4 = -1 \equiv 10^9+6 \pmod{10^9+7}. For illustration, the sample output format here shows the direct value modulo normalization would be used in practice.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.