CF1650B.DIV + MOD

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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

  • fa(x)=⌊xa⌋+x mod af_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, x mod ax \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⌋+11 mod 3=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 (l≤x≤rl \le x \le r).

不久之前,弗拉德提出了一个有趣的函数:

  • fa(x)=⌊xa⌋+x mod af_a(x)=\left\lfloor\frac{x}{a}\right\rfloor + x \bmod a,其中 ⌊xa⌋\left\lfloor\frac{x}{a}\right\rfloor 表示将 xa\frac{x}{a} 向下取整,x mod ax \bmod a 表示 xx 除以 aa 的整数除法所得余数。

例如,当 a=3a=3、x=11x=11 时,有 f3(11)=⌊113⌋+11 mod 3=3+2=5f_3(11) = \left\lfloor\frac{11}{3}\right\rfloor + 11 \bmod 3 = 3 + 2 = 5。

参数 aa 是固定的,且弗拉德已知其值。请帮助弗拉德找出 fa(x)f_a(x) 在 xx 取 ll 到 rr(含端点)之间任意整数值时所能达到的最大值(即 l≤x≤rl \le x \le r)。

输入格式

The first line of input data contains an integer tt (1≤t≤1041 \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 (1≤li≤ri≤109,1≤ai≤1091 \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.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示输入测试用例的数量。

接下来是 tt 行,每行包含三个整数 lil_i、rir_i 和 aia_i(1≤li≤ri≤109, 1≤ai≤1091 \le l_i \le r_i \le 10^9,\ 1 \le a_i \le 10^9)—— 分别表示区间的左端点、右端点以及固定的值 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.

对于每个测试用例,在单独一行输出一个数字——该函数在给定区间上、对于给定 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⌋+1 mod 3=0+1=1f_3(1) = \left\lfloor\frac{1}{3}\right\rfloor + 1 \bmod 3 = 0 + 1 = 1,
  • f3(2)=⌊23⌋+2 mod 3=0+2=2f_3(2) = \left\lfloor\frac{2}{3}\right\rfloor + 2 \bmod 3 = 0 + 2 = 2,
  • f3(3)=⌊33⌋+3 mod 3=1+0=1f_3(3) = \left\lfloor\frac{3}{3}\right\rfloor + 3 \bmod 3 = 1 + 0 = 1,
  • f3(4)=⌊43⌋+4 mod 3=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.

在第一个样例中:

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

显然,答案为 f3(2)f_3(2) 和 f3(4)f_3(4)。

输入解题思路,AI测评打分。不知道怎么写?

首页