One Mathematic Problem

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^*) axcy+b (a,bN,cN) 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,y0N) following the two conditions below:

  1. Under the specified line (including the ones on the line);
  2. x 0 ≤ n x_0\le n x0n

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 1t105,
a , b , c , n ≤ 1 0 7 a,b,c,n\le 10^7 a,b,c,n107

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=0ncai+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=0n(caimodc+bmodc+cai+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=0ncai+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=0ncai+i=0ncbSo 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=0ncai+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=0nj=0cai+b11 Obviously, the inner summation cycles ⌊ a i + b c ⌋ − 1 \lfloor\frac{ai+b}c\rfloor-1 cai+b1 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=0nj=0can+b1[j<cai+b1]
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=0can+b1i=0n [j<cai+b1]
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+b1 ∴ 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+bcai+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+cb 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+cbacj+cb+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=0can+b1i=acj+cb+1n1 = ∑ 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=0can+b1(nacj+cb) = ∑ 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=0can+b1nj=0can+b1acj+cb = 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) =ncan+bf(c,cb1,a,can+b1) 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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值