个人博客链接:https://blog.nuoyanli.com/2020/03/27/codeforces1328a/
题目链接
http://codeforces.com/contest/1328/problem/A
题意
t t t组数据,每次给你两个数 a , b ( 1 ≤ a , b ≤ 1 0 9 ) a,b(1 \leq a,b \leq 10^9) a,b(1≤a,b≤109),对于一组 a , b a,b a,b,问你至少使 a a a增大多少,才可以被 b b b整除。
思路
- 思路 1 1 1:若 a ≤ b a\leq b a≤b直接输出 ( b − a ) (b-a) (b−a)否则考虑 ( a + a n s ) % b = 0 (a+ans)\%b=0 (a+ans)%b=0,即 ( a + a n s ) = n ∗ b (a+ans)=n*b (a+ans)=n∗b,所以 a n s = n ∗ b − a ans=n*b-a ans=n∗b−a,要求的是至少的次数,又于是倍数递增,所以遍历最小的 n n n即可,时间复杂度是允许的。
- 思路 2 2 2:若 a = = b a==b a==b输出 0 0 0,否则输出 b − ( a − ( a / b ) ∗ b ) b-(a-(a/b)*b) b−(a−(a/b)∗b)即可。
参考代码
- 参考代码 1 1 1:
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define PB push_back
#define FI first
#define SE second
#define m_p(a, b) make_pair(a, b)
const double pi = acos(-1.0);
const double eps = 1e-9;
typedef long long LL;
const int N = 1e6 + 10;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double f = 2.32349;
void solve() {
IOS;
int t;
cin >> t;
while (t--) {
LL a, b;
cin >> a >> b;
if (b >= a) {
cout << b - a << endl;
} else {
int k = a / b;
while (k * b < a) {
k++;
}
cout << k * b - a << endl;
}
}
}
signed main() {
solve();
return 0;
}
- 参考代码 2 2 2:
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define PB push_back
#define FI first
#define SE second
#define m_p(a, b) make_pair(a, b)
const double pi = acos(-1.0);
const double eps = 1e-9;
typedef long long LL;
const int N = 1e6 + 10;
const int M = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double f = 2.32349;
void solve() {
IOS;
int t;
cin >> t;
while (t--) {
LL a, b;
cin >> a >> b;
if (a == b) {
cout << 0 << endl;
} else {
cout << b - (a - (a / b) * b) << endl;
}
}
}
signed main() {
solve();
return 0;
}