CF1650B.DIV + MOD

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Not so long ago, Vlad came up with an interesting function:

  • fa(x)=xa+xmodaf_a(x)=\left\lfloor\frac{x}{a}\right\rfloor + x \bmod a , where xa\left\lfloor\frac{x}{a}\right\rfloor is xa\frac{x}{a} , rounded down, xmodax \bmod a — the remainder of the integer division of xx by aa .

For example, with a=3a=3 and x=11x=11 , the value f3(11)=113+11mod3=3+2=5f_3(11) = \left\lfloor\frac{11}{3}\right\rfloor + 11 \bmod 3 = 3 + 2 = 5 .

The number aa is fixed and known to Vlad. Help Vlad find the maximum value of fa(x)f_a(x) if xx can take any integer value from ll to rr inclusive ( lxrl \le x \le r ).

输入格式

The first line of input data contains an integer tt ( 1t1041 \le t \le 10^4 ) — the number of input test cases.

This is followed by tt lines, each of which contains three integers lil_i , rir_i and aia_i ( 1liri109,1ai1091 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9 ) — the left and right boundaries of the segment and the fixed value of aa .

输出格式

For each test case, output one number on a separate line — the maximum value of the function on a given segment for a given aa .

输入输出样例

  • 输入#1

    5
    1 4 3
    5 8 4
    6 10 6
    1 1000000000 1000000000
    10 12 8

    输出#1

    2
    4
    5
    999999999
    5

说明/提示

In the first sample:

  • f3(1)=13+1mod3=0+1=1f_3(1) = \left\lfloor\frac{1}{3}\right\rfloor + 1 \bmod 3 = 0 + 1 = 1 ,
  • f3(2)=23+2mod3=0+2=2f_3(2) = \left\lfloor\frac{2}{3}\right\rfloor + 2 \bmod 3 = 0 + 2 = 2 ,
  • f3(3)=33+3mod3=1+0=1f_3(3) = \left\lfloor\frac{3}{3}\right\rfloor + 3 \bmod 3 = 1 + 0 = 1 ,
  • f3(4)=43+4mod3=1+1=2f_3(4) = \left\lfloor\frac{4}{3}\right\rfloor + 4 \bmod 3 = 1 + 1 = 2

As an answer, obviously, f3(2)f_3(2) and f3(4)f_3(4) are suitable.

首页