Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Archives
Today
Total
관리 메뉴

PS 부수기

Project Euler #32 : Pandigital products 본문

Project Euler

Project Euler #32 : Pandigital products

jyheo98 2020. 8. 8. 11:37
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
llong solve(VLL p, int index1, int index2) {
    int pow = 1, prod1 = 0, prod2 = 0, prod3 = 0;
    for (int i = index1; i >= 0; i--) {
        prod1 += pow * p[i];
        pow *= 10;
    }
    pow = 1;
    for (int i = index2; i > index1 ; i--) {
        prod2 += pow * p[i];
        pow *= 10;
    }
    pow = 1;
    for (int i = 8; i > index2; i--) {
        prod3 += pow * p[i];
        pow *= 10;
    }
    return (prod1 * prod2 == prod3) ? prod3 : 0;
}
 
int main() {
    llong sum = 0;
    set<llong> a;
    VLL p = { 123456789 };
    do {
        for (int i = 0; i < 8; i++) {
            for (int j = i + 1; j < 8; j++) {
                int x = solve(p, i, j);
                if (x != 0 && a.count(x) == 0) {
                    sum += x;
                    a.insert(x);
                }
            }
        }
    } while (next_permutation(p.begin(), p.end()));
    cout << sum;
}
cs

 

Comments