如题,如下代码实测可通过此题,并且速度非常快:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int main()
{
auto solve = [&]()
{
int n;
cin >> n;
while (true)
{
auto get_dig = [&](int x)
{
int res = 0;
while (x)
{
res += x % 10;
x /= 10;
}
return res;
};
int x = (LL)rand() * rand() % n + 1;
int y = n - x;
if (abs(get_dig(x) - get_dig(y)) <= 1)
return cout << x << ' ' << y << '\n', 0;
}
};
int T;
cin >> T;
while (T --)
solve();
return 0;
}
但是每次随出的 x 符合条件的概率是怎么计算的?直觉上概率应该不小,随上个几十万次肯定能随出来,但还是想知道一个稍微精确一点的数值,想请教一下这个概率该咋求。
感谢!