#P3996. 失败的竞猜游戏

失败的竞猜游戏

Description

The game rules are as follows: The player gives three integers A0A_0, aa, bb, which define a linear recurrence: Ai=a×Ai1+bA_i=a \times A_{i-1} +b It defines an infinite sequence { A1A_1, A2A_2, A3A_3, ... }. The game system randomly generates a number nn. If nn can be represented as the sum of several distinct terms from this sequence (excluding A0A_0), the player wins; otherwise, the player loses. Now Daning has forced the operator to hand over a batch of recent game testdata, but he is too lazy to compute them one by one. Please help determine how many times the player won in the testdata.

Input Format

The first line contains a positive integer TT, the number of games played. Each of the next TT lines contains four integers, describing one game: A0A_0, aa, bb, nn.

Output Format

Output one line with AnsAns, the number of times the player won among the TT games.

7
3 1 5 16
10 1 0 5
2 1 0 3
2 1 0 10
3 1 5 59
1 2 0 998
0 1 0 0

4

Hint

Sample explanation: In games 131 \sim 3 the player loses; in games 474 \sim 7 the player wins.

Test point ID Constraints Special properties
121 \sim 2 T=10T=10, n103n \leq 10^3 a=1a=1
343 \sim 4 T=103T=10^3, n104n \leq 10^4 a=2a=2
55 T=104T=10^4, n109n \leq 10^9 a3a \leq 3
66 T=105T=10^5, n109n \leq 10^9 b=0b=0
7107 \sim 10 T=2×105T=2 \times 10^5, n109n \leq 10^9 None

For all testdata, 1a101 \leq a \leq 10, 0b1040 \leq b \leq 10^4, 0n1090 \leq n \leq 10^9, 0A01000 \leq A_0 \leq 100.

In fact, the player's win rate in this game is negligible.

Translated by ChatGPT 5