QOJ.ac

QOJ

时间限制: 1 s 内存限制: 256 MB 总分: 100

#6226. Taking Stones

统计

Two players, A and B, are playing a stone-taking game. There is a pile of $n$ stones. Player A goes first, and they take turns removing stones from the pile. In the first turn, Player A can remove at most $k$ stones. In each subsequent turn, a player can remove at most twice the number of stones removed by the previous player. Each player must remove at least one stone per turn. The player who takes the last stone loses, and the other player wins. Given $k$, find the number of integers $n$ in the range $1$ to $N$ such that Player A has a winning strategy.

Input

The input consists of multiple test cases.

The first line contains a positive integer $T$, representing the number of test cases.

Each of the next $T$ lines contains two space-separated integers $k$ and $N$, representing a query.

Output

Output $T$ lines, each containing an integer representing the answer for the corresponding query in the order they were provided.

Examples

Input 1

3
1 5
2 5
1 10

Output 1

2
3
4

Note 1

When $k=1$:

  • If $n=1$, Player A must take the only stone and loses.
  • If $n=2$, Player A takes one stone, Player B must take the last stone, so Player A wins.
  • If $n=3$, Player A must take one stone, Player B takes one stone, Player A must take the last stone and loses.
  • If $n=4$, Player A must take one stone, Player B takes two stones, Player A must take the last stone and loses.
  • If $n=5$, Player A takes one stone, Player B can take one or two stones, and Player A can always leave the last stone for Player B to win.

Subtasks

For all test cases, $1\le T\le 10^5, k, N\le 10^{18}$.

  • For $10\%$ of the data, $T, N\le 500$.
  • For another $20\%$ of the data, $T, N\le 10^5$.
  • For another $20\%$ of the data, $T\le 3, N\le 3\times 10^6$.
  • For another $20\%$ of the data, $k=1$.
  • For the remaining $30\%$ of the data, there are no special restrictions.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.