Nim game
Practice
3.4 (5 votes)
Algorithms
Dynamic programming
Medium
Problem
40% Success 387 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Alice and Bob are playing the classic Nim game. In this version of the game, there are \(N\) heaps of stones. The \(i^{th}\) heap consists of \(i\) stones. In a single move, a player can select any non-empty heap and remove a positive number of stones from it. Alice starts the game, and both players move alternately. The player who cannot make a move in his or her turn loses the game.

Bob and Alice believe that the game is highly unfair to the second player. They both decide to give Bob an advantage. Before the game begins, Bob must select \(2\) distinct heaps \( i, j\) \((1\le i < j \le N)\) and combine them to form a single heap (the total number of heaps now will be \(N-1\) and the combined heap will contain \(i+j\)  stones). The game, then begins as usual, with Alice making the first move. Both the players play optimally in the game.

Your task is to determine the number of ways in which Bob can select two distinct heaps such that after combining them, Bob has a winning strategy. Since the answer may be large, you have to calculate the answer modulo \(10^{9} + 7\).

Note: If in two ways, one of the selected heaps is different, then these two ways are considered different.

Input format

  • First line: A single integer \(T\) denoting the number of test cases
  • For each test case:
    • First line: A single integer \(N\) denoting the number of heaps

Output format

For each test case, print a single integer, on a new line, that represents the number of ways of winning for Bob modulo \(1000000007\).

Constraints

\(1 \le T \le 10\)

\(2 \le N \le 10^{18}\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
Dynamic ProgrammingDynamic Programming and Bit MaskingIntroduction to Dynamic Programming 1AlgorithmsBitmask1-D
Points:30
31 votes
Tags:
AlgorithmsApprovedBit ManipulationDynamic ProgrammingMediumOpen
Points:30
14 votes
Tags:
AlgorithmsApprovedBit ManipulationDynamic ProgrammingMediumOpen