codeforces1328A

个人博客链接: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) ab(1a,b109),对于一组 a , b a,b a,b,问你至少使 a a a增大多少,才可以被 b b b整除。

思路

  • 思路 1 1 1:若 a ≤ b a\leq b ab直接输出 ( b − a ) (b-a) (ba)否则考虑 ( 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)=nb,所以 a n s = n ∗ b − a ans=n*b-a ans=nba,要求的是至少的次数,又于是倍数递增,所以遍历最小的 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;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuoyanli

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值