#P3277. [SCOI2011] 飞镖

[SCOI2011] 飞镖

Description

Darts is a popular sport in Europe. The dartboard is divided into 20 sector areas labeled 1 to 20. Each sector has single, double, and triple rings; hitting a ring yields the sector value multiplied by the corresponding multiplier.

For example, hitting the triple ring of 18 scores 54 points.

In addition, at the center of the board there are the outer bull and the inner bull, worth 25 and 50 points respectively.

A common darts rule is that you must finish on a double with your last dart to win. That is, if 12 points remain, you must hit double 6 to win; hitting single 12 or triple 4 does not count.

In particular, the inner bull also counts as a double (double 25). Under these rules, the maximum score that can be finished in 3 darts is 170 (two triple 20s and finishing on the inner bull).

Now, lxhgww changes the sector values from 1–20 to 1–KK, and changes the outer bull to be worth MM points (the inner bull is twice that). lxhgww wants to know whether a total of XX points can be finished within 3 darts (you may use fewer than 3 darts). Likewise, the last dart must be a double (including the inner bull).

Input Format

The first line contains an integer TT, the number of test cases.

The second line contains 5 integers A1,B1,C1,D1,K1A_1,B_1,C_1,D_1,K_1, indicating that in the first test case the board has sectors from 1 to K1K_1. For subsequent test cases, the board parameter is determined by Ki=(A1Ki12+B1Ki1+C1)modD1+20K_i=(A_1K^2_{i-1}+B_1K_{i-1}+C_1)mod D_1 + 20, and in the ii-th test case (1<iT1<i\le T) the board has sectors from 1 to KiK_i.

The third line contains 5 integers A2,B2,C2,D2,M1A_2,B_2,C_2,D_2,M_1, indicating that in the first test case the outer bull is worth M1M_1 points. For subsequent test cases, the value is determined by Mi=(A2Mi12+B2Mi1+C2)modD2+20M_i=(A_2M^2_{i-1}+B_2M_{i-1}+C_2)mod D_2 + 20, and in the ii-th test case (1<iT1<i\le T) the outer bull is worth MiM_i points.

The fourth line contains 5 integers A3,B3,C3,D3,X1A_3,B_3,C_3,D_3,X_1, indicating that in the first test case the required total is X1X_1 points. For subsequent test cases, the target is determined by Xi=(A3Xi12+B3Xi1+C3)modD3+20X_i=(A_3X^2_{i-1}+B_3X_{i-1}+C_3)mod D_3 + 20, and in the ii-th test case (1<iT1<i\le T) the required total is XiX_i points.

Output Format

Output one line containing a single integer, the number of test cases among the TT that are solvable.

5
1 2 2 10 20
1 3 2 15 25
2 2 5 200 170
4

Hint

For 30% of the testdata, it is guaranteed that 1T201 \le T \le 20, 20K1,M1,X1,D1,D2,D3100020 \le K_1,M_1,X_1,D_1,D_2,D_3 \le 1000.

For 100% of the testdata, it is guaranteed that 1T1061 \le T \le 10^6, 20K1,M1,X1,D1,D2,D310920 \le K_1,M_1,X_1,D_1,D_2,D_3 \le 10^9.

For all testdata, it is guaranteed that 0A1,B1,A2,B2,C2,A3,B3,C31090 \le A_1,B_1,A_2,B_2,C_2,A_3,B_3,C_3 \le 10^9.

Translated by ChatGPT 5