codeforces 670B Game of Robots

题目意思是由题目给出的数列按照等差数列的方式算出一个新的数列,然后给出一个位置k,问在k位的数是多少。

等差数列求和运用,从头开始,一直求和,直到大于等于k就可以判断位置了。

#include <bits/stdc++.h>

using namespace std;

typedef __int64 ll;
ll m, n;
const int MAX = 1e6 + 5;
ll arr[MAX];
void file()
{
    freopen("C:\\Users\\MrZis\\Desktop\\input.txt", "r", stdin);
    freopen("C:\\Users\\MrZis\\Desktop\\output.txt", "w", stdout);
    return ;
}

int main()
{
    //file();
    //read();
    //solve();
    //print();
    scanf("%I64d%I64d", &n, &m);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &arr[i]);
    }
    ll sum = n * (1 + n) / 2;
    int res1 = 0;
    int res2 = -1;
    //cout << m << endl;
    for (int i = 1; i <= n; i++)
    {
        //cout << i * (1 + i) / 2 << endl;
        if ((ll)i * (1 + i) / 2 > m)
        {
            res1 = i;
            break;
        }
        else if ((ll)i * (1 + i) / 2 == m)
        {
            cout << arr[i] << endl;
            return 0;
        }
    }
    //cout << res1 << endl;
    //if (res2 != -1)
     //   printf("%d\n", arr[res2]);
    //else if (res1 != 0)
    cout << arr[(ll)m - res1 * (res1 -  1) / 2] << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值