CF 622 A. Infinite Sequence【找无限数列中第i个数是几】
2016-03-17 16:41
501 查看
A. Infinite Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first
the number 1 is written out, then the numbers from 1 to 2,
then the numbers from 1 to 3,
then the numbers from 1 to 4 and
so on. Note that the sequence contains numbers, not digits. For example number 10 first appears in the sequence in position 55 (the
elements are numerated from one).
Find the number on the n-th position of the sequence.
Input
The only line contains integer n (1 ≤ n ≤ 1014)
— the position of the number to find.
Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you
can use the long long integer type and in Java you can
use long integer type.
Output
Print the element in the n-th position of the sequence (the elements are numerated from one).
Examples
input
output
input
output
input
output
input
output
input
output
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first
the number 1 is written out, then the numbers from 1 to 2,
then the numbers from 1 to 3,
then the numbers from 1 to 4 and
so on. Note that the sequence contains numbers, not digits. For example number 10 first appears in the sequence in position 55 (the
elements are numerated from one).
Find the number on the n-th position of the sequence.
Input
The only line contains integer n (1 ≤ n ≤ 1014)
— the position of the number to find.
Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you
can use the long long integer type and in Java you can
use long integer type.
Output
Print the element in the n-th position of the sequence (the elements are numerated from one).
Examples
input
3
output
2
input
5
output
2
input
10
output
4
input
55
output
10
input
56
output
1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#define Wi(a) while((a)--)
#define Si(a) scanf("%d", &a)
#define Sl(a) scanf("%I64d", &a)
#define Pi(a) printf("%d\n", (a))
#define Pl(a) printf("%I64d\n", (a))
#define CLR(a, b) memset(a, (b), sizeof(a))
#define INF 1e9
#define LL __int64using namespace std;
int main()
{
LL n;
while(Sl(n)==1)
{
LL l, r, mid;
l = 0, r = INF;
while(r-l > 1)
{
mid = (l+r)>>1;
if(mid*(mid+1)>>1 <= n) l = mid;
else r = mid;
}
if((l+1)*l>>1 == n) Pl(l);
else
{
r = (1+l)*l>>1;
Pl(n-r);
}
}
return 0;
}
相关文章推荐
- UI设计与GUI设计的区别
- iOS --UIScrollView的学习(二)
- @Response与@Request
- UI Test自动化测试查看覆盖率注意事项
- setValue:forUndefinedKey this class is not key value coding-compliant for the key
- Script with Login & UPC & Requests vs Urllib2
- iOS7 UI兼容 导航栏按钮边框 UINavigationItem left and right padding
- 82. Remove Duplicates from Sorted List II My Submissions Question
- TableView详细解释
- iOS --UIScrollView的学习(一)
- KVC中setValuesForKeysWithDictionary 崩溃问题
- UITableView刷新某一行或某一部分,并记录做项目时的一小心得
- 为什么子线程不能做ui操作
- ant之build.xml基本属性详解
- POJ2431 运用queueq优先队列
- android之handler messagequene looper threadlocal 之间的详细解读
- uipickerview用法
- @RequestBody, @ResponseBody 注解详解(转)
- AS-->如何更高效的使用 Gradle, 快速build apk
- phpQuery对数据信息的采集进一步学习