码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-12 14:17:46      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:can   fine   eof   space   color   oid   visible   mes   targe   

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), ‘\0‘, 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 == \n);
    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 (思维+公式)

标签:can   fine   eof   space   color   oid   visible   mes   targe   

原文地址:https://www.cnblogs.com/qieqiemin/p/10259423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!