Under construction

Still being built. Things may change

Operator Party

Time limit2sMemory limit512 MB

Summary
Starting from X = 0, apply the operations for multiples of A to F at each step i from 1 to N, in the given priority order, and print the final X.
Level

Hard8 of 10

Topics
Math, Bit manipulation, Simulation
Solved
No attempts yet

Problem

A variable XX that holds an integer starts at 00.

For each ii from 11 to NN, increasing by 11 each time, perform the operations that match the conditions below. After finishing all operations up to NN, print the final value of XX.

  • If ii is a multiple of AA, do XX = XX + ii. (+ is the addition operator.)
  • If ii is a multiple of BB, do XX = XX % ii. (% is the remainder operator.)
  • If ii is a multiple of CC, do XX = XX & ii. (& is the bitwise AND operator.)
  • If ii is a multiple of DD, do XX = XX ⊕ ii. (⊕ is the bitwise XOR operator.)
  • If ii is a multiple of EE, do XX = XX | ii. (| is the bitwise OR operator.)
  • If ii is a multiple of FF, do XX = XX >> ii. (>> is the bitwise right shift operator.)
  • When several operations apply at once, perform them in the priority order +, %, &, ⊕, |, >>.

Input

The first line contains NN.

The second line contains AA, BB, CC, DD, EE, and FF, separated by spaces.

Output

Print the final value of XX.

Constraints

  • 1≤N≤10121 \le N \le 10^{12}
  • 1≤A≤B≤C≤D≤E≤F≤1 000 0001 \le A \le B \le C \le D \le E \le F \le 1\,000\,000
  • NN, AA, BB, CC, DD, EE, and FF are positive integers.

Hint

Bitwise operators work on each bit separately.

Bitwise AND

For each pair of bits, the result bit is 11 only when both bits are 11. Otherwise it is 00.

Example: 01102=6&  11002=12────01002=4\begin{aligned} 0110_{2} &= 6 \\ \text{\&} \ \ 1100_{2} &= 12 \\ \text{────} \\ 0100_{2} &= 4 \end{aligned}

Bitwise XOR

For each pair of bits, the result bit is 11 when the two bits differ. Otherwise it is 00.

Example: 01102=6⊕  11002=12────10102=10\begin{aligned} 0110_{2} &= 6 \\ \text{⊕} \ \ 1100_{2} &= 12 \\ \text{────} \\ 1010_{2} &= 10 \end{aligned}

Bitwise OR

For each pair of bits, the result bit is 11 when at least one of the bits is 11. Otherwise it is 00.

Example: 01102=6|  11002=12────11102=14\begin{aligned} 0110_{2} &= 6 \\ \text{|} \ \ 1100_{2} &= 12 \\ \text{────} \\ 1110_{2} &= 14 \end{aligned}

Bitwise right shift

For aa >> bb, the bits of aa move bb places to the right. The bits that open up on the left are filled with 00.

Example: 01102 >> 2↓00012\begin{aligned} & 0110_{2} \ \text{>>} \ 2 \\ & \downarrow \\ & 0001_{2} \end{aligned}

Examples4

  1. Example 1

    Input
    3
    1 1 1 1 2 2
    
    Expected output
    3
    
  2. Example 2

    Input
    5
    1 2 3 4 5 6
    
    Expected output
    13
    
  3. Example 3

    Input
    2023
    1 2 3 5 7 11
    
    Expected output
    4079
    
  4. Example 4

    Input
    1234567895
    1 2 4 8 16 32
    
    Expected output
    2469135770