dengxinyi 6 éve
szülő
commit
325cdd7304
1 módosított fájl, 3 hozzáadás és 4 törlés
  1. 3 4
      leetcode/hard/829.consecutive-numbers-sum.cpp

+ 3 - 4
leetcode/hard/829.consecutive-numbers-sum.cpp

@@ -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++;
         }