题目链接:CodeForces 1992E 【Novice's Mistake】
思路
直接对a,b枚举肯定会超时,因为a,b数数字过大,但是通过结果a * n - b可以发现结果最多为6位数,所以对结果的位数进行枚举,然后枚举a,来计算出b并判断是否符合题意,同时需要去掉b不符合题目的范围的情况。
代码
#include <functional>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
#define ll long long
const int N = 50090 + 10;
struct ab {
ll a, b;
} res[N];
void solve(){
ll n, a, b, ans = 1;
cin >> n;
string s1 = to_string(n);
for (int i = 2; i <= 10000; i++) {
string s2 = "";
while (s2.length() <= 10)
s2 += s1;
for (int j = 1; j <= 6; j++) {
ll b = s1.size() * i - j;
if (b < 1 || b > min(10000ll, n * i))
continue;
if (to_string(i * n - b) == s2.substr(0, j)) {
res[ans++] = {i, b};
}
}
}
cout << ans - 1 << endl;
for (int i = 1; i < ans; i++) {
cout << res[i].a << " " << res[i].b << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
标签:string,ll,Novice,CodeForces,1992E,include,Mistake
From: https://www.cnblogs.com/againss/p/18305010