dengxinyi %!s(int64=6) %!d(string=hai) anos
pai
achega
325cdd7304
Modificáronse 1 ficheiros con 3 adicións e 4 borrados
  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++;
         }