FJNU-1155 Fat Brother’s prediction

Description

Fat Brother is a famous prophet, One day he get a prediction that disaster will come after X days. He is too nervous that sudden die. Fortunately he leave a note about number X after he died:

X is a number of integer which can divided exactly by Y range in [L, R]

You want to know what X is.

 

Input

There are multiple test cases. The first line of input contains an integer T (T <= 10000) indicating the number of test cases. For each test case:

Contain 3 integer L, R, Y (1 <= L <= R <= 2^63 - 1, 1 <= Y <= 2^63 - 1)

 

There are multiple test cases. The first line of input contains an integer T (T <= 10000) indicating the number of test cases. For each test case:

Contain 3 integer L, R, Y (1 <= L <= R <= 2^63 - 1, 1 <= Y <= 2^63 - 1)

 

Output

Each case print a number X.

 

Sample Intput

2

1 3 2

3 10 3

Sample Output

1

3


即区间[L, R]中能被Y整除的数的数量。

直接用 R/Y 减去 L/Y 即可,也就是1到R中能被Y整除的数的数量减去1到L中能被Y整除的数的数量,因为是闭区间,所以要注意L本身是否能被Y整除,如果可以,结果要+1.

#include <iostream>
using namespace std;

int main(void)
{
    int t;
    
    while(cin >> t)
    {
        while(t--)
        {
            long long l, r, y, num = 0;
            
            cin >> l >> r >> y;
            num += r/y-l/y;
            if(l%y == 0)
            {
                num++;
            }
            cout << num << endl;
        }
    }
    return 0;
}

当给出的数的范围很大的时候要记得用 long long,如果是水题,这很有可能是一道找规律的题目,如本题。

原文地址:https://www.cnblogs.com/limyel/p/6600582.html