URAL 1153. Supercomputer 二分求根
2015-03-23 22:26
239 查看
1153. Supercomputer
Time limit: 2.0 secondMemory limit: 64 MB
To check the speed of JCN Corporation new supercomputer it was decided to figure out the sum of first N (N < 10600)
positive integers. Unfortunately, by the time the calculation was finished the Chief Programmer forgot the value of N he entered. Your task is to write the program (for personal computer), which would determine the value of N by the result
calculated on supercomputer.
Note: JCN Corporation manufactures only reliable computers, and its programmers write only correctly working programs.
Input
One line containing the result of calculations on the supercomputer.Output
Выведите N, the number entered by Chief Programmer.Sample
input | output |
---|---|
28 | 7 |
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round
Tags: none (
hide tags for unsolved problems
)
题意
给你1-n个数的和,求出n多大。
做法:
根据等差数列公式, 得 (1+n)*n/2 =(n^2+n)/2;
设给的和 为ans。
n^2+n-2ans=0
然后根据 公式 (-b+根号(b^2-4ab))/2a 就可以算出n是多少了。
因为ans很大,所以要用大数。java大数也没有现成的开根号函数,所以要用二分来求 根。
import java.math.*; import java.util.*; import java.io.*; public class Main{ static BigInteger li=BigInteger.ZERO; static BigInteger yi=BigInteger.ONE; static BigInteger er=BigInteger.valueOf(2); public static BigInteger sqrtt(BigInteger n) { BigInteger l=yi,r=n,mid; while(l.compareTo(r)<=0) { mid=l.add(r).divide(er); if(mid.multiply(mid).compareTo(n)<0) l=mid.add(yi); else r=mid.subtract(yi); //System.out.println(l+" "+r); } return l; } public static void main(String[] args) { Scanner input = new Scanner(System.in); BigInteger a; while(input.hasNext()) { a=input.nextBigInteger(); a=a.multiply(BigInteger.valueOf(8)); a=a.add(BigInteger.ONE); a=sqrtt(a); System.out.println(a.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2))); } } }
相关文章推荐
- ural 1153. Supercomputer
- URAL 1156 Two Rounds (DFS二分染色 + DFS枚举 + 剪枝)
- URAL - 1066 Garland(二分)
- 二分求根及牛顿迭代求根分析
- URAL - 1486 Equal Squares 哈希、二维hash、二分、卡大素数
- 二分法求根与二分次数
- ural 1080. Map Coloring(二分染色bfs)
- URAL 1949 The Best Picture in the Galaxy 二分匹配 求字典序最小解
- ural 1056. Computer Net 树的重心
- URAL 1056 Computer net [DFS求树的中心]
- URAL 1998 The old Padawan 二分
- Codeforces Round #280 (Div. 2)D. Vanya and Computer Game(二分)
- ural 1056 Computer Net
- URAL1917_Titan Ruins: Better late than killed_二分搜索
- ural1156. Two Rounds(二分染色+dfs)
- URAL 1849|Rabbit Hunt 2|计算几何|二分|Set
- URAL 1196. History Exam (二分)
- ural 1209. 1, 10, 100, 1000... 二分
- Codeforces 492D - Vanya and Computer Game (二分)
- URAL 1196. History Exam (二分)