# 1006. Clumsy Factorial

Normally, the factorial of a positive integer `n` is the product of all positive integers less than or equal to `n`.  For example, `factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1`.

We instead make a  clumsy factorial:  using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.

For example, `clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1`.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.

Additionally, the division that we use is  floor division  such that `10 * 9 / 8` equals `11`.  This guarantees the result is an integer.

`Implement the clumsy` function as defined above: given an integer `N`, it returns the clumsy factorial of `N`.

Example 1:

``````Input: 4
Output: 7
Explanation: 7 = 4 * 3 / 2 + 1
``````

Example 2:

``````Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
``````

Note:

1. `1 <= N <= 10000`
2. `-2^31 <= answer <= 2^31 - 1`  (The answer is guaranteed to fit within a 32-bit integer.)

``````class Solution {
public:
int clumsy(int N) {
int res = 0, cur = N, j = 0;
vector<char> ops{'*', '/', '+', '-'};
for (int i = N - 1; i >= 1; --i) {
if (ops[j] == '*') {
cur *= i;
} else if (ops[j] == '/') {
cur /= i;
} else if (ops[j] == '+') {
res += i;
} else {
res += (i == N - 4) ? cur : -cur;
cur = i;
}
j = (j + 1) % 4;
}
return res + ((N <= 4) ? cur : -cur);
}
};
``````

``````class Solution {
public:
int clumsy(int N) {
int res = 0;
for (int i = 0; i * 4 < N; ++i) {
int num = N - i * 4, t = num;
if (num >= 3) t = num * (num - 1) / (num - 2);
res += (i == 0) ? t : -t;
if (num > 3) res += (num - 3);
}
return res;
}
};
``````

``````N * N - 3 * N = N * N - 3 * N + 2 - 2
N * (N - 3) = (N - 1) * (N - 2) - 2。（factorization）
N = (N - 1) * (N - 2) / (N - 3) - 2 / (N - 3) （Divide N - 3 on both side）
N - (N - 1) * (N - 2) / (N - 3) = - 2 / (N - 3)
- 2 / (N - 3) = 0, If N - 3 > 2.
So when N > 5, N - (N - 1) * (N - 2) / (N - 3) = 0
N + 1 - N * (N - 1) / (N - 2) = 0  (Replace N with N + 1)
N + 1 = N * (N - 1) / (N - 2)
``````

``````i * (i-1) / (i-2) + (i-3) - (i-4) * (i-5) / (i-6) + (i-7) - (i-8) * .... + rest elements
=   (i+1) + "(i-3)" - "(i-4) * (i-5) / (i-6)" + "(i-7)" - "(i-8) * " .... + rest elements
=   (i+1) + "(i-3) - (i-3)" + "(i-7) - (i-7)" +  ....  + rest elements
=   (i+1) + rest elements

when 0 element left: final result is (i+1) + ... + 5 - (4*3/2) + 1, which is i+1
when 1 element left: final result is (i+1) + ... + 6 - (5*4/3) + 2 - 1, which is i+2
when 2 element left: final result is (i+1) + ... + 7 - (6*5/4) + 3 - 2 * 1, which is i+2
when 3 element left: final result is (i+1) + ... + 8 - (7*6/5) + 4 - 3 * 2 / 1, which is i-1
``````

``````class Solution {
public:
int clumsy(int N) {
int a[5] = {-1, 1, 2, 6, 7};
int b[4] = {1, 2, 2, -1};
return N < 5 ? a[N] : N + b[N % 4];
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1006

https://leetcode.com/problems/clumsy-factorial/

https://leetcode.com/problems/clumsy-factorial/discuss/252257/C%2B%2B-3-lines.

https://leetcode.com/problems/clumsy-factorial/discuss/252247/C%2B%2BJava-Brute-Force

https://leetcode.com/problems/clumsy-factorial/discuss/252279/You-never-think-of-this-amazing-O(1)-solution

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation