POJ 1844 Sum
题意:给一个整数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的更多相关文章
- OpenJudge/Poj 1844 Sum
1.链接地址: http://bailian.openjudge.cn/practice/1844 http://poj.org/problem?id=1844 2.题目: Sum Time Limi ...
- POJ 1844 Sum【简单数学】
链接: http://poj.org/problem?id=1844 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29256#probl ...
- ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法
POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS Memory Limit:65536KB 64bit IO Fo ...
- POJ.2739 Sum of Consecutive Prime Numbers(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- POJ 1844:Sum ”滚动“数组
Sum Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10494 Accepted: 6895 Description ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- poj 2739 Sum of Consecutive Prime Numbers 解题报告
题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...
- 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 ...
随机推荐
- ffplay 中filter的使用
添加字幕:ffplay -vf drawtext="fontfile=arial.ttf: text='Test Text': x=100: y=300: \ fontsize=48: fo ...
- React组件生命周期-初始化阶段的函数执行顺序
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...
- Crypto++编译使用
简述 Crypto++库是一个用c++ 编写的密码类库,是一个自由软件.有关它的信息可以访问以下两个网站: Crypto++® Library Wiki-Crypto++® Library 简述 下载 ...
- jenkins集成自动化部署插件(一) deploy-plugin
在实际情况中项目构建成功,特别是web项目构建成功是需要将war放到对应的服务上面,进行运行(测试的阶段可能就是发布到测试服务器上面)这样只需要指定构建的触发策略就可以自动构建以及部署,省去不少人工的 ...
- dojo 资源库
文档 :http://wenku.baidu.com/link?url=Nnek_tAjIC-Q3t3e9zHQmsh4LhU3f0ncC1QH8WD_U9-I8-fJ7mMisscFpgfuS8nU ...
- [HDOJ5510]Bazinga(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 普通集合会tle,换高贵的并查集. #include <algorithm> #in ...
- HeadFirst jsp 03 (MVC)
创建一个小的 web 应用, mvc, 麻雀虽小, 五脏俱全 补1: servlet没有main()方法, 他们受控与另外一个Java应用, 这个Java应用称为 容器, tomcat就是这么一个容器 ...
- JAVA设计模式之【工厂方法模式】
看例子 1.TV产品接口,负责播放 public interface TV // TV接口 { public void play(); } 2.TV工厂接口,负责生产产品 public interfa ...
- SQL注入实验,PHP连接数据库,Mysql查看binlog,PreparedStatement,mysqli, PDO
看到有人说了判断能否sql注入的方法: 简单的在参数后边加一个单引号,就可以快速判断是否可以进行SQL注入,这个百试百灵,如果有漏洞的话,一般会报错. 下面内容参考了这两篇文章 http://blog ...
- radio checked不起作用的原因
<table id="approveTable"> <tr> <td> <input type="radio" nam ...