Minimum Integer CodeForces - 1101A (思维+公式)

Posted qieqiemin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Minimum Integer CodeForces - 1101A (思维+公式)相关的知识,希望对你有一定的参考价值。

You are given qq queries in the following form:

Given three integers lili, riri and didi, find minimum positive integer xixi such that it is divisible by didi and it does not belong to the segment [li,ri][li,ri].

Can you answer all the queries?

Recall that a number xx belongs to segment [l,r][l,r] if lxrl≤x≤r.

Input

The first line contains one integer qq (1q5001≤q≤500) — the number of queries.

Then qq lines follow, each containing a query given in the format lili riri didi (1liri1091≤li≤ri≤109, 1di1091≤di≤109). lili, riri and didi are integers.

Output

For each query print one integer: the answer to this query.

Example

Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10

题目链接:CodeForces - 1101A 
水题一个,但是数据量略大不足以让我们暴力随便过。
那么便思考一下找公式就行了,
观察可知,当d小于L的时候,答案就是d
否则,答案是
(r/d+1)*d;

我的AC代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int q;
int l,r;
int d;
int main()
{
    gbtb;
    cin>>q;
    while(q--)
    {
        cin>>l>>r>>d;
        int flag=0;
        if(d<l)
        {
            cout<<d<<endl;
            continue;
        }else
        {
            int ans=(r/d+1)*d;
            cout<<ans<<endl;
        }
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch ==   || ch == 
);
    if (ch == -) {
        *p = -(getchar() - 0);
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 - ch + 0;
        }
    }
    else {
        *p = ch - 0;
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 + ch - 0;
        }
    }
}

 

 

以上是关于Minimum Integer CodeForces - 1101A (思维+公式)的主要内容,如果未能解决你的问题,请参考以下文章

Minimum Integer CodeForces - 1101A (思维+公式)

Minimum Integer sequence HDU - 3522(扩展KMP)

Minimum Height Trees

310. Minimum Height Trees

CF1101A Minimum Integer 模拟

[leetcode]599. Minimum Index Sum of Two Lists