-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path448.cpp
108 lines (96 loc) · 2.61 KB
/
448.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <vector>
#include <type_traits>
using namespace std;
class Solution
{
public:
vector<int> findDisappearedNumbers(vector<int> &nums)
{
int n = nums.size();
for (auto &num : nums)
{
int x = (num - 1) % n;
nums[x] += n;
}
vector<int> ret;
for (int i = 0; i < n; i++)
{
if (nums[i] <= n)
{
ret.push_back(i + 1);
}
}
return ret;
}
};
template <int... Ints>
using int_sequence = std::integer_sequence<int, Ints...>;
template <int I, int... N>
void print(int_sequence<I, N...>)
{
cout << I << " ";
if constexpr (sizeof...(N) == 0)
cout << endl;
else
print(int_sequence<N...>());
}
template <int I, int... N>
constexpr auto cat(int_sequence<N...>)
{
return int_sequence<I, N...>();
}
template <int n, int index, int Itop, int... i, int... I>
constexpr auto changeaddN(int_sequence<i...>, int_sequence<Itop, I...>)
{
if constexpr (sizeof...(i) == index)
return int_sequence<i..., Itop + n, I...>();
else
return changeaddN<n,index>(int_sequence<i..., Itop>(), int_sequence<I...>());
}
template <int n>
constexpr int_sequence<> callwithreturn(int_sequence<>) { return int_sequence<>(); }
template <int n, int Itop, int... I>
constexpr auto callwithreturn(int_sequence<Itop, I...>)
{
if constexpr (Itop > n)
return callwithreturn<n>(int_sequence<I...>());
else
return cat<n - sizeof...(I)>(callwithreturn<n>(int_sequence<I...>()));
}
template <int index, int Itop, int... i, int... I>
constexpr auto getvalue(int_sequence<i...>, int_sequence<Itop, I...>)
{
if constexpr (sizeof...(i) == index)
return Itop;
else
return getvalue<index>(int_sequence<i..., Itop>(), int_sequence<I...>());
}
template <int n, int Iindex, int... I>
constexpr auto CounterAdd(int_sequence<I...> _)
{
if constexpr (Iindex < n)
return CounterAdd<n, Iindex + 1>(changeaddN<n, (getvalue<Iindex>(int_sequence<>(), _) - 1) % n>(int_sequence<>(), _));
else
return _;
}
template <int... I>
constexpr auto findDisappearedNumbers(int_sequence<I...> _)
{
return callwithreturn<sizeof...(I)>(CounterAdd<sizeof...(I), 0>(_));
}
int main()
{
vector<int> a = {4, 3, 2, 7, 8, 2, 3, 1};
Solution S;
vector<int> b = S.findDisappearedNumbers(a);
for (auto x : b)
{
cout << x << " ";
}
cout << endl;
auto template_a = int_sequence<4, 3, 2, 7, 8, 2, 3, 1>();
auto template_b = findDisappearedNumbers(template_a);
print(template_b);
return 0;
}