• 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. }

  • 相关阅读:
    Scss--@extend--使用/实例
    力扣 138. 随机链表的复制
    uni-app配置微信开发者工具
    线性表--顺序表-1
    linux查看redis安装目录
    AI艺术创作之MidJourney 的新 V4 算法太疯狂了
    vue组件库发布到npm
    Redis事务、pub/sub、PipeLine-管道、benchmark性能测试详解
    Tauri2 mobile development traps
    辽宁2022农民丰收节 国稻种芯:4个主会场31个分会场同步
  • 原文地址:https://blog.csdn.net/jj12345jj198999/article/details/126336529