F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Rikka with Proper Fractions

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 41    Accepted Submission(s): 2


Problem Description
Rikka is a lovely girl. Although she was not good at math before, she has made great progress with the help of her boyfriend Yuta. And Rikka has been able to do math research by herself now.

Today, Rikka is reading some materials about rational number approximation. Rikka is interested in the algorithm continuous fractional expansion which can find a closest fraction $\frac{a}{b}$ to approximate a given number $x$ among all the fractions with the denominator less than $n$.

Rikka wants to estimate the difficulty of this problem. She picks up a fraction $\frac{a}{b}$ which is less than $x$ and picks up a fraction $\frac{c}{d}$ which is larger than $x$. And she wants to find the number of rational numbers in the interval $[\frac{a}{b},\frac{c}{d}]$ which can be represented by fraction numbers with the denominator less than $n$. Formally, given $\frac{a}{b},\frac{c}{d},n$, Rikka wants to find out the number of proper fractions $\frac{e}{f}(1 \leq e < f \leq n, \text{gcd}(e,f)=1)$ which satisfy $\frac{a}{b} \leq \frac{e}{f} \leq \frac{c}{d}$.

This task seems too hard for Rikka. So, could you please help her to find out the answer?
 

Input
The first line contains a single integer $t(1 \leq t \leq 10^3)$, the number of the testcases.

For each testcase, the first line contains five numbers $n,a,b,c,d(0 < \frac{a}{b} < \frac{c}{d} < 1, 1 \leq a,b,c,d \leq 10^8)$.

The input guarantees that both $\frac{a}{b}$ and $\frac{c}{d}$ are proper fraction numbers, $1 \leq n \leq 10^{10}$ and there are at most $3$ testcases with $n > 10^6$.
 

Output
For each testcase, output a single line with a single integer, the answer modulo $998244353$.
 

Sample Input
3 5 1 2 3 4 10 1 2 7 9 1000000 2 13 10000 10001
 

Sample Output
4 10 620740490
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 08:27:43, Gzip enabled