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;
}


你可能感兴趣的:(数学)