TOTALODD - Số lẻ hoàn toàn

Tác giả: ladpro98

Ngôn ngữ: C++

#include <bits/stdc++.h>
const int M = 1000006;
using namespace std;
int a[M];
int m;

int main() {
    ios :: sync_with_stdio(0); 
    for(int i = 1; i <= 9; i += 2) a[++m] = i;
    int cut = 1;
    for(int i = 2; i <= 8; i++) {
        int tmp = m;
        for(int j = cut; j <= tmp; j++)
            for(int k = 1; k <= 9; k += 2) 
                a[++m] = 10 * a[j] + k;
        cut = tmp;
    }
    string ins; int x;
    while (1) {
        cin >> ins;
        if (ins[1] == 'E') break;
        cin >> x;
        int j = lower_bound(a + 1, a + 1 + m, x) - a;
        int res = a[m] + a[m];
        for(int i = 1; i <= j; i++) {
            while (j > i && a[i] + a[j - 1] >= x) j--;
            if (a[i] + a[j] >= x) res = min(res, a[i] + a[j]);
        }
        cout << res << endl;
    }
    return 0;
}

Download