|
@@ -1,15 +1,14 @@
|
|
|
-#include <cmath>
|
|
|
-
|
|
|
class Solution {
|
|
|
public:
|
|
|
int consecutiveNumbersSum(int N) {
|
|
|
// Assume that we pick n elements start from x, then
|
|
|
// for (x + (n - 1) * x) * n / 2 == N,
|
|
|
- // ie. n * x + n * (n - 1) / 2 == N.
|
|
|
+ // ie. n * x + n * (n - 1) / 2 == N
|
|
|
// Just check for some n, we have valid x.
|
|
|
int cnt = 0;
|
|
|
for (int n = 1; ; n++) {
|
|
|
- int nx = N - n * (n - 1) / 2;
|
|
|
+ // n * x = N - blabla
|
|
|
+ int nx = N - (n * (n - 1) >> 1);
|
|
|
if (nx < n) break;
|
|
|
if (nx % n == 0) cnt++;
|
|
|
}
|