Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers?

Example 1:

Input: 5
Output: 2
Explanation: 5 = 5 = 2 + 3

Example 2:

Input: 9
Output: 3
Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4

Example 3:

Input: 15
Output: 4
Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5

分析:https://leetcode.com/problems/consecutive-numbers-sum/discuss/209317/topic

这道题的要求的另一种说法: 把N表示成一个等差数列(公差为1)的和

我们不妨设这个数列的首项是x,项数为m,则这个数列的和就是[x + (x + (m-1))]m / 2 = mx + m(m-1)/2 = N

接下来,一个很自然的想法就是,枚举m,通过上式判断对于相应的m是否存在合法的x。

x = ((N - m(m-1)/2)) / m

显然枚举的复杂度是O(sqrt(N))。因为m能取到的最大值显然是sqrt(n)数量级的

 class Solution {
int consecutiveNumbersSum(int N) {
int ans = ;
for (int m = ; ; m++) {
int mx = N - m * (m-) / ;
if (mx <= )
break;
if (mx % m == )
ans++;
}
return ans;
}
}

Consecutive Numbers Sum的更多相关文章

  1. 829. Consecutive Numbers Sum

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  2. [Swift]LeetCode829. 连续整数求和 | Consecutive Numbers Sum

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  3. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  4. 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...

  5. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  6. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  7. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  8. 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers

    题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  9. [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers

    SQL架构: Create table If Not Exists Logs (Id int, Num int) Truncate table Logs insert into Logs (Id, N ...

随机推荐

  1. learning armbian steps(7) ----- armbian 源码分析(二)

    从compile.sh开始入手: SRC="$(dirname "$(realpath "${BASH_SOURCE}")")" # fal ...

  2. 20190908 NOIP 模拟40

    考试过程: 刚看完题,发现T1是个类lis 问题,但要求$O(nlogn)$,应该是个数据结构优化dp,T2应该是个数据结构,T3是个字符串?没有匹配,不会是后缀数组吧,这是NOIP模拟啊,可能是个d ...

  3. 解决vscode打开空白的问题

    环境 :win7,最新vscode 问题:打开后窗口全黑,但是原按钮对应位置还有触摸手势,显示tag等,卸载重装等无效,如上图 最终方案: 启动方式后加 --disable-gpu 解决思路(其余参考 ...

  4. html,css,js实现的一个钟表

    效果如图: 实现代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  5. centos与debian网卡

    debian    /etc/network/interfaces # This file describes the network interfaces available on your sys ...

  6. mysql的count和sum使用条件表达式

    count函数条件不为null的时候显示结果.即使为false也也会显示结果. 可以是使用if条件或者case when语句.如果条件不为null即需要的结果. 使用count()函数实现条件统计的基 ...

  7. 使用<bind>元素创建变量

    在使用模糊查询sql时,如果使用${}进行字符拼接,无法防止sql诸如问题,如果使用concat函数则只对mysql有效果,用Oracle则需要用连接符||,这样在数据库变的时候需要修改,不利于移植. ...

  8. mysql与Oracle的区别:

    1.  Oracle是大型数据库而Mysql是中小型数据库,Oracle市场占有率达40%,Mysql只有20%左右,同时Mysql是开源的而Oracle价格非常高. 2. Oracle支持大并发,大 ...

  9. LeetCode 复原IP地址(探索字节跳动)

    题目描述 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135&qu ...

  10. 使用arcpy.mapping 更新和修复数据源

    来自:https://blog.csdn.net/gisinfo/article/details/6675390 在许多情况下,您都可能需要修复数据源或重定向数据源至其他位置.然而,如果是在每个相关的 ...