Educational Codeforces Round 7--A. Infinite Sequence

探讨一个无限整数序列的构造方式及如何找出特定位置上的数值。序列由递增的整数段构成,每段从1开始至当前段的最大值。通过数学方法定位目标数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
3
output
2
input
5
output
2
input
10
output
4
input
55
output
10
input
56
output
1

找规律的题目:

1

2 3

4 5 6 

7 8 9 10

11 12 13 14 15

。。。。

输入 一个n,求出第n个位置的数。

思路:

先求出第n个数所在的行,减去第一个数就行了,第一个数显然是个累加法的数列求法.

代码如下:

#include<bits/stdc++.h>
#define mem(x) memset(x,0,sizeof(x));
#define mem1(x) memset(x,-1,sizeof(x));
using namespace std;
const double eps = 1e-8;
const int INF = 1e8;
const int maxn = 10000 + 10;
const int maxt = 100 + 10;
typedef long long ll;
typedef unsigned long long llu;
int main()
{
    ll n,k;
    while(cin >> k){
        double det = sqrt(1 - 4*(2-2*k));
        ll n = (1+det)/2;
        cout << k-(n-1)*n/2 << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值