公式

A - Find Multiple 解説 by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.


Let \(X\) be the quotient of \(B\) divided by \(C\), with the remainder rounded down, that is, \(X=\lfloor \frac{B}{C} \rfloor\).

Let \(Y=X \times C\).
Then \(Y\) is the minimum multiple of \(C\) not exceeding \(B\).

Therefore, if \(Y \geq A\), then \(Y\) is an answer.
If \(Y \lt A\), then the answer is \(-1\).

A sample code in C++ follows.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

signed main(){
    ll a,b,c;
    cin>>a>>b>>c;
    ll Y = b / c * c; // Y is a multiplt of C not exceeding B
    if(a <= Y){
        cout<<Y<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}

投稿日時:
最終更新: