Description
Gives a line a x − c y + b ( a , b ∈ N , c ∈ N ∗ ) ax-cy+b\space(a,b\in\N,c\in\N^*) ax−cy+b (a,b∈N,c∈N∗) and an integer n n n, count the points ( x 0 , y 0 ) ( x 0 , y 0 ∈ N ) (x_0,y_0)\space(x_0,y_0\in\N) (x0,y0) (x0,y0∈N) following the two conditions below:
- Under the specified line (including the ones on the line);
- x 0 ≤ n x_0\le n x0≤n
Input Format
Test includes
t
t
t groups of data.
For one group of data, inputs a line including 4 integers
a
a
a,
b
b
b,
c
c
c and
n
n
n.
Output Format
A line of one integer for each group of data, i.e. the count result.
Data Scale
As for 100% inputs,
1
≤
t
≤
1
0
5
1\le t\le10^5
1≤t≤105,
a
,
b
,
c
,
n
≤
1
0
7
a,b,c,n\le 10^7
a,b,c,n≤107
Solution
The count of points is in fact finding the summation expression:
∑
i
=
0
n
⌊
a
i
+
b
c
⌋
\sum^n_{i=0}\lfloor\frac{ai+b}c\rfloor
i=0∑n⌊cai+b⌋
=
∑
i
=
0
n
(
⌊
a
i
m
o
d
c
+
b
m
o
d
c
c
⌋
+
⌊
a
c
⌋
i
+
⌊
b
c
⌋
)
=\sum^n_{i=0}(\lfloor\frac{ai\mod c+b\mod c}c\rfloor+\lfloor\frac ac\rfloor i+\lfloor\frac bc\rfloor)
=i=0∑n(⌊caimodc+bmodc⌋+⌊ca⌋i+⌊cb⌋)
Construct a function
f
(
a
,
b
,
c
,
n
)
=
∑
i
=
0
n
⌊
a
i
+
b
c
⌋
f(a,b,c,n)=\sum^n_{i=0}\lfloor\frac{ai+b}c\rfloor
f(a,b,c,n)=∑i=0n⌊cai+b⌋, therefore:
f
(
a
,
b
,
c
,
n
)
=
f
(
a
i
m
o
d
c
+
b
m
o
d
c
,
c
,
n
)
+
∑
i
=
0
n
⌊
a
c
⌋
i
+
∑
i
=
0
n
⌊
b
c
⌋
f(a,b,c,n)=f(ai\mod c+b\mod c,c,n)+\sum^n_{i=0}\lfloor\frac ac\rfloor i+\sum^n_{i=0}\lfloor\frac bc\rfloor
f(a,b,c,n)=f(aimodc+bmodc,c,n)+i=0∑n⌊ca⌋i+i=0∑n⌊cb⌋So that, the answer can be found by recursions. Here is given the recursive expression of function
f
(
a
,
b
,
c
,
n
)
f(a,b,c,n)
f(a,b,c,n) below:
f
(
a
,
b
,
c
,
n
)
=
∑
i
=
0
n
⌊
a
i
+
b
c
⌋
f(a,b,c,n)=\sum^n_{i=0}\lfloor\frac{ai+b}c\rfloor
f(a,b,c,n)=i=0∑n⌊cai+b⌋
=
∑
i
=
0
n
∑
j
=
0
⌊
a
i
+
b
c
⌋
−
1
1
=\sum^n_{i=0}\sum_{j=0}^{\lfloor\frac{ai+b}c\rfloor-1}1
=i=0∑nj=0∑⌊cai+b⌋−11 Obviously, the inner summation cycles
⌊
a
i
+
b
c
⌋
−
1
\lfloor\frac{ai+b}c\rfloor-1
⌊cai+b⌋−1 times, which depends on the outer summation loop. So the inner condition is extracted into the expression:
=
∑
i
=
0
n
∑
j
=
0
⌊
a
n
+
b
c
⌋
−
1
[
j
<
⌊
a
i
+
b
c
⌋
−
1
]
=\sum^n_{i=0}\sum_{j=0}^{\lfloor\frac{an+b}c\rfloor-1}[j<\lfloor\frac{ai+b}c\rfloor-1]
=i=0∑nj=0∑⌊can+b⌋−1[j<⌊cai+b⌋−1]
Thereinto, the bracket expressions
[
…
]
[\dots]
[…] means checking a condition. It has value
1
1
1 if the condition is true, or
0
0
0 otherwise.
Besides, the summation expressions can exchange the range when they are all independent:
=
∑
j
=
0
⌊
a
n
+
b
c
⌋
−
1
∑
i
=
0
n
[
j
<
⌊
a
i
+
b
c
⌋
−
1
]
=\sum^{\lfloor\frac{an+b}c\rfloor-1}_{j=0}\sum^n_{i=0}\space [j<\lfloor\frac{ai+b}c\rfloor-1]
=j=0∑⌊can+b⌋−1i=0∑n [j<⌊cai+b⌋−1]
Please pay attention to the condition expression inside. The inner summation cycles with variable
i
i
i, but the condition judges variable
j
j
j. So we do something to make it work on
j
j
j:
j
<
⌊
a
i
+
b
c
⌋
−
1
j<\lfloor\frac{ai+b}c\rfloor-1
j<⌊cai+b⌋−1
∴
j
+
1
<
⌊
a
i
+
b
c
⌋
≤
a
i
+
b
c
\therefore j+1<\lfloor\frac{ai+b}c\rfloor \le \frac{ai+b}c
∴j+1<⌊cai+b⌋≤cai+b And then solve the equation of
i
i
i, considering variable
j
j
j as a parameter:
i
>
c
j
+
c
−
b
a
i>\frac{cj+c-b}a
i>acj+c−b For
i
i
i is an integer, in the summation loop it has a starting value:
i
>
c
j
+
c
−
b
a
≥
⌊
c
j
+
c
−
b
a
⌋
+
1
i>\frac{cj+c-b}a\ge\lfloor\frac{cj+c-b}a\rfloor+1
i>acj+c−b≥⌊acj+c−b⌋+1 So the original expression of
f
(
a
,
b
,
c
,
n
)
f(a,b,c,n)
f(a,b,c,n) turns out to be:
=
∑
j
=
0
⌊
a
n
+
b
c
⌋
−
1
∑
i
=
⌊
c
j
+
c
−
b
a
⌋
+
1
n
1
=\sum^{\lfloor\frac{an+b}c\rfloor-1}_{j=0}\sum^n_{i=\lfloor\frac{cj+c-b}a\rfloor+1}1
=j=0∑⌊can+b⌋−1i=⌊acj+c−b⌋+1∑n1
=
∑
j
=
0
⌊
a
n
+
b
c
⌋
−
1
(
n
−
⌊
c
j
+
c
−
b
a
⌋
)
=\sum^{\lfloor\frac{an+b}c\rfloor-1}_{j=0}(n-\lfloor\frac{cj+c-b}a\rfloor)
=j=0∑⌊can+b⌋−1(n−⌊acj+c−b⌋)
=
∑
j
=
0
⌊
a
n
+
b
c
⌋
−
1
n
−
∑
j
=
0
⌊
a
n
+
b
c
⌋
−
1
⌊
c
j
+
c
−
b
a
⌋
=\sum^{\lfloor\frac{an+b}c\rfloor-1}_{j=0}n-\sum^{\lfloor\frac{an+b}c\rfloor-1}_{j=0}\lfloor\frac{cj+c-b}a\rfloor
=j=0∑⌊can+b⌋−1n−j=0∑⌊can+b⌋−1⌊acj+c−b⌋
=
n
⌊
a
n
+
b
c
⌋
−
f
(
c
,
c
−
b
−
1
,
a
,
⌊
a
n
+
b
c
⌋
−
1
)
=n\lfloor\frac{an+b}c\rfloor-f(c,c-b-1,a,\lfloor\frac{an+b}c\rfloor-1)
=n⌊can+b⌋−f(c,c−b−1,a,⌊can+b⌋−1) In the function variables
a
a
a and
c
c
c is defined as divident and divisor, whose positions in parameter list are exchanged, taking after Euclid’s algorithm. In fact it can be proved that, this algorithm has the time complexity in
O
(
log
2
n
)
O(\log_2n)
O(log2n). So that this algorithm can be designed as recursive calls. The input data scale doesn’t beyond
1
0
9
10^9
109 (below
2
30
2^{30}
230), and the program would process data scale
O
(
t
log
2
n
)
<
3
E
6
O(t\log_2n)<3E^6
O(tlog2n)<3E6, using no more than 0.1s.