cf2067A
原题链接:https://codeforces.com/contest/2067/problem/A
题目背景:
给定x,y,判读是否存在 n 满足S(n) = x,S(n + 1) = y。定义 S(a) 等于 a 的十进制位数之和。
思路:
不难发现一般 n 和 n + 1 的位数之和相差为 1,这种情况我们先判断一下即可;但是当 n 以 9结尾时,他在+1时就会进位,所有我们可以推出 S(n) = S(n+1) + k * 9 - 1( k 为 n 中有几个 9 )通过公式变形可得(S(n) - S(n +1) +1)/9 = k,带入 x,y 得( x - y + 1 ) / 9 = k,所以只需判断 (x - y + 1)% 9 是否等于 0 即可。
数据范围:
1 <= x,y <= 1000
时间复杂度:
O(1)。
ac代码:
#include <bits/stdc++.h>#define ioscc ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define me(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define sz(a) ((int)(a).size())
#define pb(a) push_back(a)
using namespace std;typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<bool> vb;const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAX = (1ll << 31) - 1;
const int MIN = 1 << 31;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cout << a[i] << ' ';return os;
}template <class T>
istream &operator>>(istream &in, vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cin >> a[i];return in;
}/* 有乘就强转,前缀和开ll */void solve()
{int x, y;cin >> x >> y;if (x == y)cout << "NO" << endl;else if (x + 1 == y)cout << "YES" << endl;else if (x > y && (x - y + 1) % 9 == 0)cout << "YES" << endl;elsecout << "NO" << endl;
}int main()
{ioscc;int T;cin >> T;while (T--)solve();return 0;
}