EASYMATC - EASY MATH (Challenge)

You will be given 4 numbers: n m a d.

find count of numbers between n and m (inclusive) not divisible by (a) or (a+d) or (a+2d) or (a+3d) or (a+4d).

Input

first line has number t - number of test cases.

each test case has 4 numbers n m a d.

Output

Single line having single number giving the count.

Constraints

1 <= n <= m <= 2^32

1 <= a <= 2^32

1 <= d <= 2^32

2 <= t <= 100

Example

Input:
3
1 10 2 2
20 100 3 3
100 1000 4 5

Output:
5
54
543

Added by:Devil D
Date:2012-04-23
Time limit:1s
Source limit:3000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:own

hide comments
2024-04-16 15:33:54
<snip>
[Simes]: no thanks.

Last edit: 2024-04-16 19:47:24
2016-12-27 14:07:48
TLE in C++
2014-06-06 12:03:49 Sanjay Singh
its working for n<=10^7.but above that showing TLE
2014-05-16 23:18:42 pvkcse
also TLE in python 3.2.3
2014-04-24 15:37:47 Mitch Schwartz
@Nadav Chernin: Problem description and example are fine. We have S = {a, a+d, ... , a+4d} and the predicate on integer x in [n,m] is: there does not exist s in S such that s divides x.
2014-04-24 12:20:23 nadavishe
Something wrong in problem definition
How for input 1 10 2 2 will be output 5? All numbers between 1 to 10 not divisible by (a) or (a+d) or (a+2d) or (a+3d) or (a+4d).
Please advice
2012-10-06 19:34:42 Aditya Pande
its not the language but its your naive algorithm
2012-07-08 10:36:35 Bharath Reddy
TLE in Perl..
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.