HDOJ 1554

You are to find all pairs of integers such that their sum is equal to the given integer number N and the second number results from the first one by striking out one of its digits. The first integer always has at least two digits and starts with a non-zero digit. The second integer always has one digit less than the first integer and may start with a zero digit.
Input
The input file consists of several test cases.
Each test case contains single integer N (10 ≤ N ≤ 10^9), as decribed above
Output
The output consists of several blocks, one for each test case.
On the first line of a block write the total number of different pairs of integers that satisfy the problem statement. On the following lines write all those pairs. Write one pair on a line in ascending order of the first integer in the pair. Each pair must be written in the following format: X + Y = N Here X, Y, and N, must be replaced with the corresponding integer numbers. There should be exactly one space on both sides of ‘+’ and ‘=’ characters.
Sample Input
302
Sample Output
5
251 + 51 = 302
275 + 27 = 302
276 + 26 = 302
281 + 21 = 302
301 + 01 = 302

hdu题解

试了一下,用哈希表去重在排序比set有序插入要快,插入查找哈希表得期望还是要比红黑树好一些。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include<vector>
#include <unordered_set>
using namespace std;
int N;

unordered_set<int>Ans;

#define Verify verify(a,b,c,i)

inline bool verify(const int& a, const int& b, const int& c,const int&i) {
return ((11 * a + b) * i + 2 * c) == N;
}

inline void UpdateAns(const int&X) {
if (Ans.find(N - X) != Ans.end()) {
return;
}
Ans.insert(X);
}

void Compute() {
for (long long i = 1; i <= N; i *= 10) {
int a = (N / i) / 11;
int b = (N / i) % 11;
int c;
if (b < 10) {
c = (N - N / i * i) / 2;
if (Verify) {
UpdateAns(10 * a * i + b * i + c);
}
}
--b;
if (a + b && b >= 0) {
c = (N - N / i * i + i) / 2;
if (Verify) {
UpdateAns(10 * a * i + b * i + c);
}
}
}
}

inline void OutputZero(int X) {

int Tail = 0;
int Y = N - X;
while (X) {
X /= 10;
++Tail;
}
if (!Y) {
--Tail;
}
while (Y) {
Y /= 10;
--Tail;
}
while (--Tail) {
putchar('0');
}
}

void Output(const vector<int>&Ans) {
printf("%d\n", Ans.size());
for (auto it = Ans.cbegin(); it != Ans.cend(); ++it) {
printf("%d + ", *it);
OutputZero(*it);
printf("%d = %d\n", N - *it, N);
}
}

int main(){

while (cin >> N) {
Compute();
vector<int> ve;
for (auto it = Ans.begin(); it != Ans.end(); ++it) {
ve.push_back(*it);
}
sort(ve.begin(), ve.end(), [](int& x,int& y) {return x < y; });
Output(ve);
Ans.clear();
}

return 0;

}