A - Infinite Sequence
Problem description
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 1to 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 longinteger 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
解题思路:简单推导:假设k表示为第k(k>=1)个序列(1,...,k),那么前k(k>=1)个序列一共有n=(k+1)*k/2个数,则第n个数在第floor(√(2*n+0.25)-0.5)(向下取整)个序列中。因为当n必须刚好为前k个序列数字的总个数时,k才会是那个精确的第k个序列。因此,此时只需判断m=(k+1)*k/2是否大于n。如果n大于m,说明第n个数实际在第k+1个序列里面,则这个数为n-m;否则第n个数就在第k个序列里面,则这个数为k-(m-n)=k+n-m。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n,k,m;
cin>>n;
k=sqrt(2.0*n+0.25)-0.5;
m=k*(k+)/;
if(n>m)cout<<n-m<<endl;
else cout<<k+n-m<<endl;
return ;
}
A - Infinite Sequence的更多相关文章
- CodeForces 622 A.Infinite Sequence
A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 7 A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...
- codeforces 675A A. Infinite Sequence(水题)
题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...
- codeforces 622A Infinite Sequence
A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【arc071f】Infinite Sequence(动态规划)
[arc071f]Infinite Sequence(动态规划) 题面 atcoder 洛谷 题解 不难发现如果两个不为\(1\)的数连在一起,那么后面所有数都必须相等. 设\(f[i]\)表示\([ ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...
- codeforces 622A A. Infinite Sequence (二分)
A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its fi ...
- 【CodeForces 622A】Infinite Sequence
题意 一个序列是, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5....这样排的,求第n个是什么数字. 分析 第n个位置属于1到k,求出k,然后n-i*(i-1)/ ...
随机推荐
- vue02 过滤器、计算和侦听属性、vue对象的生命周期、阻止事件冒泡和刷新页面
3. Vue对象提供的属性功能 3.1 过滤器 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 定义过滤器的方式有两种. 3.1.1 使用Vue.fil ...
- vino-server服务是啥服务
近期接手一个项目,开始梳理服务器,突然发现有个进程是开启5900远程桌面端口的, 在不知情的情况下怕被人给利用了,啥也不说,先干掉再说. server端开启vino-server,允许别人查看自己的桌 ...
- mysql cpu 100% 满 优化方案 解决MySQL CPU占用100%的经验总结
下面是一些经验 供参考 解决MySQL CPU占用100%的经验总结 - karl_han的专栏 - CSDN博客 https://blog.csdn.net/karl_han/article/det ...
- cvpr2016论文
http://openaccess.thecvf.com/ICCV2017.py http://openaccess.thecvf.com/CVPR2017.py http://www.cv-foun ...
- Python网络爬虫之Scrapy框架(CrawlSpider)
目录 Python网络爬虫之Scrapy框架(CrawlSpider) CrawlSpider使用 爬取糗事百科糗图板块的所有页码数据 Python网络爬虫之Scrapy框架(CrawlSpider) ...
- Serial Fluent UDF on Windows
test test Table of Contents 1. Serial UDF on Windows OS 1 Serial UDF on Windows OS Note: Udf has to ...
- SSL/TLS 协议介绍
SSL/TLS 协议(RFC2246 RFC4346)处于 TCP/IP 协议与各种应用层协议之间,为数据通讯提供安全支持. 从协议内部的功能层面上来看,SSL/TLS 协议可分为两层: 1. SSL ...
- [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]
AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...
- 同开三本DJANGO,需要提升一下本职工作的能力啦
慢慢将一些概念固化到基因内,才有可能和SPRING MVC,MEAN之类的好好作比较吧. 全都是基于1.8版本的教材,爽!!!
- BZOJ——T 2097: [Usaco2010 Dec]Exercise 奶牛健美操
http://www.lydsy.com/JudgeOnline/problem.php?id=2097 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: ...