题意:给一个整数n,求当n由1到k的连续整数加或减组成时的最小的k。

解法:一开始觉得dp……后来觉得复杂度太大了……GG……百度了一下是个数学题orz。

如果n全部由加法组成,那么k可以组成k(k+1)/2,设减掉的部分为s,则有k(k+1)/2-2s=n,所以n和k(k+1)/2mod2同余。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i = (int)sqrt(n); ; i++)
{
int tmp = (i * i + i) / 2;
if(tmp < n) continue;
if((tmp & 1) == (n & 1))
{
cout << i << endl;
break;
}
}
}
return 0;
}

  

POJ 1844 Sum的更多相关文章

  1. OpenJudge/Poj 1844 Sum

    1.链接地址: http://bailian.openjudge.cn/practice/1844 http://poj.org/problem?id=1844 2.题目: Sum Time Limi ...

  2. POJ 1844 Sum【简单数学】

    链接: http://poj.org/problem?id=1844 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29256#probl ...

  3. ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法

    POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS     Memory Limit:65536KB     64bit IO Fo ...

  4. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  5. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  6. POJ 1844:Sum ”滚动“数组

    Sum Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10494   Accepted: 6895 Description ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. poj 2739 Sum of Consecutive Prime Numbers 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...

  9. poj 1564 Sum It Up

    题目连接 http://poj.org/problem?id=1564 Sum It Up Description Given a specified total t and a list of n ...

随机推荐

  1. 多线程包:java.util.concurrent,

    Java1.5提供了一个非常高效实用的多线程包:java.util.concurrent,

  2. ArcGIS Engine -- 常用方法

    空间关系 计算两点间距离 feature平移 计算范围 得到点集合的n倍Envelope范围 查询 查询要素,返回多个要素union后的Geometry 查找图层 得到地图上图层列表 根据名称在地图上 ...

  3. Head First HTML5 Programming笔记--chapter1 认识HTML5

    升级到HTML5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 //EN" "http://www.w3.org/TR/ ...

  4. 270. Closest Binary Search Tree Value

    题目: Given a non-empty binary search tree and a target value, find the value in the BST that is close ...

  5. Linux配置自建 YUM 软件存储库

    yum软件仓库的搭建方式有三种,分别是本地yum源,网络yum源,第三方软件仓库. 以下示例演示了搭建本地yum仓库的方法: 1. 删除 /etc/yum.repos.d/dvd.repo 这个仓库文 ...

  6. Linux系统安装MySQL步骤及支持远程操作配置方法

    一.数据库安装(安装在/usr/local目录) 1. 压缩包拷贝到/users/lengyufang/tools 2. groupadd mysql3. useradd -r -g mysql -s ...

  7. C# Path.Combine 方法的用法

    C#   Path.Combine 方法的用法 *.注意: string filePath3= Path.Combine(string path1,string path2): 情况一: path2中 ...

  8. json 字符串转换成对象,对象转换成json字符串

    json   字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法:   [注意jquery版本问题] var str = '{"name":&qu ...

  9. Eclipse插件安装与使用 —— Properties Editor

    一.下载     首先当然是下载插件啦! 下载地址:http://sourceforge.jp/projects/propedit/downloads/40156/jp.gr.java_conf.us ...

  10. R之批处理

    在linux下如何编写脚本调用R语言写的程序呢? R语言进行批处理有2种方式: R CMD BATCH --options scriptfile outputfile Rscript --option ...