-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem32.cpp
76 lines (62 loc) · 1.32 KB
/
problem32.cpp
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
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <unordered_set>
#include <algorithm>
using long_t = long;
using namespace std;
int const N = 9;
long_t cc(0);
// {1,2,3,4} -> 1234
int vec2num(vector<int> const& vec, int start, int end){
int p = pow(10, end - start -1);
int acc(0);
for (int i = start;i<end;i++){
acc += p*vec[i];
p /= 10;
}
return acc;
}
/*
* 1*2=345*6789
*/
unordered_set<int> cache;
long_t countUnusual(vector<int> const& perm){
long_t local_cc(0);
for (long_t starPos = 1;starPos<=N-2;starPos++){
for (long_t eqPos = starPos+1; eqPos <=N-1 ;eqPos++){
auto p1 = vec2num(perm, 0, starPos);
auto p2 = vec2num(perm, starPos, eqPos);
auto r = vec2num(perm, eqPos, N);
if (p1==0 || p2==0 || r==0) cout<<"error"<<endl;
if (p1*p2==r && cache.find(r)==cache.end() ){
local_cc++;
cache.insert(r);
}
}
}
return local_cc;
}
void backtrace(vector<int> perm){
if (perm.size()==size_t(N)){
cc += countUnusual(perm);
}
for(int i = 1;i<=9;i++){
if (find(perm.begin(), perm.end(), i)==perm.end()){
perm.push_back(i);
backtrace(perm);
perm.pop_back();
}
}
}
int main(){
backtrace(vector<int>());
auto ans(0);
for (auto &s : cache){
ans += s;
cout<<s<<endl;
}
// cout<<countUnusual({3,9,1,8,6,7,2,5,4})<<endl;
cout<<"ans: "<<ans<<endl;
}