CodeForces
来源:互联网 发布:哈工大机械考研知乎 编辑:程序博客网 时间:2024/05/21 18:46
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.
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.
Print the element in the n-th position of the sequence (the elements are numerated from one).
3
2
5
2
10
4
55
10
56
1
1,1,2,1,2,3,1,2,3,4,1,2,3,4,5…观察可得出规律1,2,3,4,5。。。第n项就是前面的和,所以用n减去sum,就是对应的数。由于数据太大,所以不能直接用for()换求和,求和公式s=(首项+末项)*项数/2;
AC代码:
#include<cstdio>typedef long long ll;int main(){ll n, sum = 0, mod;scanf("%lld", &n);for(ll i = 1; ; i++){sum = 0; sum = ((1 + i) * i) / 2;if(sum > n) {sum = ((1 + i-1) * (i-1)) / 2;mod = n - sum;if(mod == 0){printf("%lld", i-1);break;}else{printf("%lld", mod);break;}}}return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- 名字空间
- 调用手机陀螺仪
- 使用XmlDocument创建XML文档及增加删除更新节点
- 解决css边框1px
- C语言的条件编译#if, #elif, #else, #endif、#ifdef, #ifndef
- CodeForces
- 解决AS导入新项目加载时间过长
- Flask权限篇flask_principal
- centos7.3安装php
- Spring中ApplicationContext和beanfactory区别
- c++函数重载详解
- 判断手机版本
- 缓冲区溢出
- linux文件名匹配(通配符使用)