1. 程式人生 > >Minimum Integer CodeForces - 1101A (思維+公式)

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

pre .org integer void 但是 tor air positive not

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 (思維+公式)