• B. DIV + MOD


    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

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

    • fa(x)=⌊xa⌋+xmodafa(x)=⌊xa⌋+xmoda, where ⌊xa⌋⌊xa⌋ is xaxa, rounded down, xmodaxmoda — 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=5f3(11)=⌊113⌋+11mod3=3+2=5.

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

    Input

    The first line of input data contains an integer tt (1≤t≤1041≤t≤104) — the number of input test cases.

    This is followed by tt lines, each of which contains three integers lili, riri and aiai (1≤li≤ri≤109,1≤ai≤1091≤li≤ri≤109,1≤ai≤109) — the left and right boundaries of the segment and the fixed value of aa.

    Output

    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.

    Example

    input

    Copy

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

    output

    Copy

    2
    4
    5
    999999999
    5
    

    Note

    In the first sample:

    • f3(1)=⌊13⌋+1mod3=0+1=1f3(1)=⌊13⌋+1mod3=0+1=1,
    • f3(2)=⌊23⌋+2mod3=0+2=2f3(2)=⌊23⌋+2mod3=0+2=2,
    • f3(3)=⌊33⌋+3mod3=1+0=1f3(3)=⌊33⌋+3mod3=1+0=1,
    • f3(4)=⌊43⌋+4mod3=1+1=2f3(4)=⌊43⌋+4mod3=1+1=2

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

    解题说明:此题是一道数学题,找规律求解即可。

    1. #include
    2. int main()
    3. {
    4. int t, l, r, i, sum;
    5. scanf("%d", &t);
    6. while (t--)
    7. {
    8. scanf("%d%d%d", &l, &r, &i);
    9. if (l / i == r / i || (r + 1) % i == 0)
    10. {
    11. sum = r / i + r % i;
    12. }
    13. else
    14. {
    15. sum = (r / i - 1) + i - 1;
    16. }
    17. printf("%d\n", sum);
    18. }
    19. return 0;
    20. }

  • 相关阅读:
    【Spring.。。】Day17
    Hudi数据湖技术引领大数据新风口(四)核心概念
    IP地址、子网掩码、网络地址之间相关的计算
    脆而不倒谷草兼用脆秆水稻被发掘 国稻种芯百团计划行动
    关于蓝绿发布(Blue-Green Deployment)
    OkHttp网络框架深入理解-SSL握手与加密
    JS for循环语句的用法
    ubuntu基础操作(1)-个人笔记
    Echart 知识图谱--连接线段
    鸿蒙harmony天气预报Demo
  • 原文地址:https://blog.csdn.net/jj12345jj198999/article/details/126336529