?原題鏈接: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;
}