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 l≤x≤rl≤x≤r.

Input

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

Then qq lines follow, each containing a query given in the format lili riri didi (1≤li≤ri≤1091≤li≤ri≤109, 1≤di≤1091≤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‘;
        }
    }
}
 

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

时间: 2024-08-29 03:09:47

Minimum Integer CodeForces - 1101A (思维+公式)的相关文章

Minimum Integer sequence HDU - 3522(扩展KMP)

Minimum Integer sequence HDU - 3522 题意: 几行代码看了一个多小时!!吐血!! 明天再来补题~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=100010; 4 char s[maxn],t[maxn]; 5 int lens,lent; 6 int nex[maxn],ex[maxn]; 7 int best; 8 9 void getnex(char* t){ 10

CodeForces - 417A(思维题)

Elimination Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The finalists of the "Russian Code Cup" competition in 2214 will be the participants who win in one of the elimination rounds.

Doors Breaking and Repairing CodeForces - 1102C (思维)

You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move. There are nn doors, the ii-th door ini

CF1101A Minimum Integer 模拟

题意翻译 题意简述 给出qqq组询问,每组询问给出l,r,dl,r,dl,r,d,求一个最小的正整数xxx满足d∣x d | x\ d∣x 且x?∈[l,r] x \not\in [l,r]x?∈[l,r] 输入格式 第一行一个正整数q(1≤q≤500)q(1 \leq q \leq 500)q(1≤q≤500) 接下来qqq行每行三个正整数l,r,d(1≤l≤r≤109,1≤d≤109)l,r,d(1 \leq l \leq r \leq 10^9 , 1 \leq d \leq 10^9)l

CodeForces 1131B(思维题)

You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai: bibi". It is known that if the current score

E Minimum Array ( Codeforces Round #555 (Div. 3) )

You are given two arrays aa and bb, both of length nn. All elements of both arrays are from 00 to n−1n−1. You can reorder elements of the array bb (if you want, you may leave the order of elements as it is). After that, let array cc be the array of l

CodeForces - 417B (思维题)

Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description During the "Russian Code Cup" programming competition, the testing system stores all sent solutions for each participant. We know th

Queue CodeForces - 353D (思维dp)

https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为位置$i$处的$F$复位所花费时间, 有 $dp[i] = max(dp[i-1]+1,cnt_i)$, $cnt_i$为前$i$位$M$的个数 $dp$最大值即为答案 #include <iostream> #include <algorithm> #include <cstd

Codeforces 1270C 思维题

C. Make Good time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's call an array a1,a2,…,ama1,a2,…,am of nonnegative integer numbers good if a1+a2+?+am=2⋅(a1⊕a2⊕?⊕am)a1+a2+?+am=2⋅(a1⊕a2⊕?⊕