UVa 10025: The ? 1 ? 2 ? ... ? n = k problem
这道题仔细思考后就可以得到比较快捷的解法,只要求出满足n*(n+1)/2 >= |k| ,且n*(n+1)/2-k为偶数的n就可以了。注意n==0时需要特殊判断。
我的解题代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm> using namespace std; int main()
{
long long T,K,k,n;
cin >> T;
while(T--)
{
cin >> K; if(K<0) k=-K; else k=K;
if(K==0) cout << 3 << endl;
else
{
double ans = (sqrt(1.0+8*k)-1)/2;
n=ceil(ans);
while((n*(n+1)/2-K)%2) n++;
cout << n << endl;
}
if(T) cout << endl;
}
return 0;
}
附上题目如下:
The problem
Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtain a given k
? 1 ? 2 ? ... ? n = k
For example: to obtain k = 12 , the expression to be used will be:
- 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12
with n = 7
The Input
The first line is the number of test cases, followed by a blank line.
Each test case of the input contains integer k (0<=|k|<=1000000000).
Each test case will be separated by a single line.
The Output
For each test case, your program should print the minimal possible n (1<=n) to obtain k with the above formula.
Print a blank line between the outputs for two consecutive test cases.
Sample Input
2 12 -3646397
Sample Output
7 2701
UVa 10025: The ? 1 ? 2 ? ... ? n = k problem的更多相关文章
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律
UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...
- UVA 10025(数学)
The ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operators ' ...
- uva 1567 - A simple stone game(K倍动态减法游戏)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=4342">题目链接:uva 1567 - ...
- UVa 1363 (数论 数列求和) Joseph's Problem
题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分 ...
- UVa10025-The ? 1 ? 2 ? ... ? n = k problem
分析:因为数字之间只有加减变换,所以-k和k是一样的,都可以当成整数来考虑,只要找到最小的n满足sum=n*(n+1)/2>=k:且sum和k同奇同偶即可,做法是用二分查找,然后在就近查找 因为 ...
- UVA 12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)
题目链接:https://vjudge.net/problem/UVA-12412 题目大意 略. 分析 比较大规模的模拟,注意输入输出,浮点数精度,还有排名相同的输出顺序,还有一些边界情况处理. 代 ...
- K - problem 问题
Leetcode 有几个题目, 分别是 2sum, 3sum(closest), 4sum 的求和问题和 single Number I II, 这些题目难点在于用最低的时间复杂度找到结果 2-sum ...
- 【例题 6-4 UVA - 11988】Broken Keyboard (a.k.a. Beiju Text)
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会链表的插入操作的话.这个就不难了. 放置两个哨兵节点. 然后模拟插入一个节点的过程就好. 实时修改光标就好->即下一个插入的 ...
随机推荐
- iOS国际化支持
写给自己看: 1.先创建一个国际化文件,用于描述在不同的区域环境,显示不同的value.文件名必须是Localizable.strings,文件的内容稍后再写.
- mac 卸载java
由于电脑上的jdk版本和项目组使用的版本不一致,因此需要卸载,但是作为一个新人小白加没有使用mac的过多经验,还是稍微费了一些些功夫的,从网上查的资料,终于解决这个问题,因此记录一下. 参考博客:ht ...
- 使用out来返回多个值
一般的方法中,一次只能有一个返回值.但是当我们需要一个方法给我们返回多个值得时候应该怎么做呢?这时候可以使用out来修饰参数. out介绍: static void Main(string[] arg ...
- java之迭代器
迭代这个名词对于熟悉Java的人来说绝对不陌生.我们常常使用JDK提供的迭代接口进行java collection的遍历: Iterator it = list.iterator();while(it ...
- (转)PHP模板smarty简单入门教程
转之--http://blog.163.com/zf_2011@126/blog/static/166861361201062595057962/ 如何在smarty中开始我们程序设计.PHP代码:- ...
- 配置中的address不能重复
<jaxws:endpoint implementor="com.service.imp.UserServiceImpl" address="/user" ...
- 使用Spring MVC,Mybatis框架等创建Java Web项目时各种前期准备的配置文件内容
1.pom.xml 首先,pom.xml文件,里面包含各种maven的依赖,代码如下: <project xmlns="http://maven.apache.org/POM/4.0. ...
- GetComponents和FindObjectsOfTypeAll区别
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_GetComponent.html GetComponents获得的是当 ...
- Linux导航神器-----autojump
对于命令行用户来说,频繁的cd和tab应该是日常工作中最多使用的命令了.特别对于重度用户来说,如果可以省去这么多cd和tab,将更多的时间做有意义的事该多好.其实Linux的学习过程本身就行这样.你会 ...
- 12_RHEL7.1普通用户添加sudo权限
1.关于sudo Sudo是linux系统中,非root权限的用户提升自己权限来执行某些特性命令的方式,它使普通用户在不知道超级用户的密码的情况下,也可以暂时的获得root权限. 一 ...