Problem · Math

City Infection Number

Learn this problem
MediumArcesium logoArcesiumINTERNOA

Problem statement

An infinite row of cities is indexed from 0. Each city is either healthy (0) or infected (1).

On day 1, only city 0 is infected. For every later day, the state of city i is the XOR of the previous-day states of cities i - 1 and i. Treat city -1 as always healthy.

After day n, interpret the city states as a binary number with city 0 as the least significant bit. Return that number modulo 10^9 + 7.

Function

infectedCitiesValue(n: int) → int

Examples

Example 1

n = 6return = 51

The states from city 0 through city 5 are 1, 1, 0, 0, 1, 1. With city 0 as the least significant bit, the value is 1 + 2 + 16 + 32 = 51.

Example 2

n = 1return = 1

Only city 0 is infected on day 1.

Constraints

  • 1 <= n <= 10^6.

More Arcesium problems

drafts saved locally
public int infectedCitiesValue(int n) {
  // Write your code here.
}
n6
expected51
checking account