#P10084. [GDKOI2024 提高组] 计算

[GDKOI2024 提高组] 计算

Problem Description

Define F(x,a,b)=gcd⁡(xa−1,xb−1)+1,x>0F(x, a, b) = \gcd(x^a - 1, x^b - 1) + 1, x > 0.

In particular, if a=0a = 0 or b=0b = 0, then F(x,a,b)=0F(x, a, b) = 0.

Now you are given five non-negative integers m,a,b,c,dm, a, b, c, d.

Let L=F(m,a,b)+1L = F(m, a, b) + 1 and R=F(m,c,d)R = F(m, c, d).

Ask how many subsets of the set {L,L+1,L+2,…,R−2,R−1,R}\{L, L + 1, L + 2, \dots, R - 2, R - 1, R\} have a subset sum that is a multiple of mm.

Since the answer may be very large, you only need to output the result modulo 998244353998244353.

Because the third batch of testdata is incorrect, in particular, if L>RL > R, output 11.

Input Format

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

The next TT lines each contain five non-negative integers m,a,b,c,dm, a, b, c, d.

Output Format

For each test case, output the answer.

3
5 0 0 2 1
4 1 2 2 4
8 3 2 4 6
8
1024
527847872

Hint

[Sample Explanation]

After calculation, we have L=1L=1 and R=5R=5. The set is 1,2,3,4,51,2,3,4,5. There are 88 subsets whose subset sums satisfy the condition:

{}\{\}, {5}\{5\}, {2,3}\{2, 3\}, {1,4}\{1, 4\}, {1,2,3,4}\{1, 2, 3, 4\}, {2,3,5}\{2, 3, 5\}, {1,4,5}\{1, 4, 5\}, {1,2,3,4,5}\{1, 2, 3, 4, 5\}.

[Constraints]

::cute-table{tuack}

Test Point ID mm L,RL,R a,ba,b c,dc,d TT Special Properties
11 =2=2 L=1,R=2L=1,R=2 =0=0 ≤10\leq 10 ≤5\leq 5 None
22 ≤10\leq 10 L=1,R=mL=1,R=m ^ ^ ^ ^
33 ≤5\leq 5 ≤103\leq 10^3 ≤10\le 10 11
4∼64\sim 6 ≤20\leq 20 ≤2×103\leq 2\times 10^3 ^ None
77 ^ ≤105\leq 10^5 ≤102\leq 10^2 22
8,98,9 ≤80\leq 80 ≤109\leq 10^9 ^ None
10∼1310\sim 13 ≤2×103\leq 2\times 10^3 ≤1018\leq 10^{18} ≤103\leq 10^3 ^
14∼1714\sim 17 ≤105\leq 10^5 ^
18∼2018\sim 20 ≤107\leq 10^7 ≤104\leq 10^4
  • Special Property 1: R−L+1≤20R - L + 1 \leq 20.
  • Special Property 2: R−L+1≤2000R - L + 1 \leq 2000.

For all data, it is guaranteed that L<RL < R and m>0m > 0.

Translated by ChatGPT 5