Educational Codeforces Round 7-A. Infinite Sequence(模拟)
2016-02-11 20:49
363 查看
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).
Sample test(s)
input
output
input
output
input
output
input
output
input
output
思路:
一开始打算用n*n+1/2来做,结果没想到竟然爆long long了,原来直接减就行了。一开始认为这个方法会超时才用公式,结果竟然不超时,看来又想错了时间了。
AC代码:
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).
Sample test(s)
input
3
output
2
input
5
output
2
input
10
output
4
input
55
output
10
input
56
output
1
思路:
一开始打算用n*n+1/2来做,结果没想到竟然爆long long了,原来直接减就行了。一开始认为这个方法会超时才用公式,结果竟然不超时,看来又想错了时间了。
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<cstdio> #include<cmath> #include<ctime> #include<cstdlib> #include<queue> #include<vector> #include<set> using namespace std; const int T=10000100; #define inf 0x3f3f3f3fL #define mod 1000000000 typedef long long ll; typedef unsigned long long ULL; int main() { #ifdef zsc freopen("input.txt","r",stdin); #endif ll n,m,i,j=0,k; while(~scanf("%I64d",&n)) { for(i=1;i<n;++i)n-=i; printf("%I64d\n",n); } return 0; }
相关文章推荐
- Transformation 能将 Windows XP/Server 2003 操作系统,完美地模拟成 Windows Vista 的软件
- 用javascript和css模拟select的脚本
- PHP模拟asp.net的StringBuilder类实现方法
- C#使用ImitateLogin模拟登录百度
- javascript用层模拟可移动的小窗口
- 自编jQuery插件实现模拟alert和confirm
- JS模拟按钮点击功能的方法
- PHP模拟asp中response类实现方法
- 模拟Spring的简单实现
- javascript 模拟点击广告
- JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
- php实现模拟post请求用法实例
- JavaScript实现MIPS乘法模拟的方法
- 模拟xcopy的函数
- JS模拟实现Select效果代码
- php模拟服务器实现autoindex效果的方法
- C# SendInput 模拟鼠标操作的实现方法
- PHP模拟登陆163邮箱发邮件及获取通讯录列表的方法
- js实现模拟银行卡账号输入显示效果
- JS模拟并美化的表单控件完整实例