-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1006_clumsy.cc
More file actions
40 lines (39 loc) · 917 Bytes
/
1006_clumsy.cc
File metadata and controls
40 lines (39 loc) · 917 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class Solution2 {
public:
int clumsy(int N) {
if (N==0) return 0;
if (N==1) return 1;
if (N==2) return 2;
if (N==3) return 6;
function<int(int)> helper=[&](int n) {
if (n==0) return 0;
if (n==1||n==2||n==3) return 1;
if (n==4) return -2;
if (n==5) return 0;
return helper((n-2)%4+2);
};
return N*(N-1)/(N-2)+helper(N-3); // * / + ...
}
};
class Solution {
public:
int clumsy(int N) {
if (N==0) return 0;
if (N==1) return 1;
if (N==2) return 2;
if (N==3) return 6;
function<int(int)> helper=[&](int n) {
if (n==0) return 0;
if (n==1||n==2||n==3) return 1;
return n-(n-1)*(n-2)/(n-3)+helper(n-4); // - * / +
};
return N*(N-1)/(N-2)+helper(N-3); // * / + ...
}
};
int main(){
Solution2 s;
cout << s.clumsy(4) << endl;
cout << s.clumsy(10) << endl;
}