Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <stdio.h>
- #include <string.h>
- #include <unordered_set>
- #ifndef N
- const int N = 4;
- #endif
- static char S[N+4];
- template<int n> long conv(const char* p) { return p[n-1] + 10 * conv<n-1>(p); }
- template<> long conv<0>(const char* p) { return 0; }
- template<int n> int unconv(char* p, long x) { *p = x % 10; return unconv<n-1>(p+1, x / 10); }
- template<> int unconv<0>(char* p, long x) { return x; }
- constexpr long pow10(int n) { return n ? pow10(n-1) * 10 : 1; }
- void save(long a, long b) {
- if (b < pow10(N-1)) return;
- printf ("%ld + %ld = %ld\n", a, b, a+b);
- }
- void solve(int id, int m = 0) {
- if (id) {
- for (S[id-1]=S[id]; S[id-1]<=9; S[id-1]++) {
- solve (id-1, m + S[id-1]);
- }
- } else if (m % 9 == 0) { // fprintf (stderr, "* %d%d%d%d\n", S[0], S[1], S[2], S[3]);
- char S1[N], S2[N];
- std::unordered_set<long> R;
- memcpy(S1, S, N);
- fprintf (stderr, "%ld\r", conv<N>(S));
- do {
- memcpy(S2, S1, N);
- long A1 = conv<N>(S1);
- R.insert(A1);
- while (std::prev_permutation(S2, S2+N)) {
- long A2 = conv<N>(S2);
- if (R.count(A1+A2)) save(A1, A2);
- }
- } while (std::prev_permutation(S1, S1+N));
- }
- }
- int main() {
- S[N] = 0;
- solve (N);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement