“今日头条”杯2018年湖北省赛(网络赛) H. GSS and OJ Submissions【分块】


H. GSS and OJ Submissions

time limit per test 6 seconds

memory limit per test 512 megabytes

GSS is holding alarge programming contest on the website called EOJ or compileError OnlineJudge which is the largest programming contest platform in the world. Everyday, millians of codes are submitted to the platform.

One day, someonesubmitted the n-th code (1 ≤ n ≤ 4 × 108), where n is GSS's lucky number, to celebrate this day, GSS is going to send a hugeprize. In this case, he allocated every submission a number in [0, 264) (see the paragraph below), then he generate another number L in [1, n] . The user who submitted the submission with the L-th small number will receive the prize.

GSS allocatenumbers to submissions in the following way:


typedef unsigned long long ull;
void allocate(ull A, ull B, ull s0, ull s[])
{
    s[0] = s0;
   for (int i = 1; i < n; i++) {
        s[i] = s[i - 1] * A + B;
    }
}

A, B and s0 are generated elsewhere, and 0 ≤ A, B, s0 < 264. And you can assume that A, B and s0 are generated in random.

Special notice: the code above will comsume about 2 seconds or more on judger.

Now, Mingminghave collected all numbers allocated to his submissions, and he wants to knowweather he will win the prize. But he is too lazy to solve, so he asked you forhelp, please, tell him the number allocated to the submission which win theprize.

Input

Input contains 5 integers, A, B, L, n, s0.

Output

Output one line with the number —— thenumber allocated to the submission which win the prize.

Example

Input

5 7 9 11 13

Output

5761717

Note

The numbersallocated to submissions are:


13, 72, 367, 1842, 9217, 46092, 230467, 1152342, 5761717, 28808592, 144042967

it is clear that the ninth one is 5761717.

 

【题目链接】 link

【题意】

大致意思是是给出4e8个long long型的数,求第k大

【思路】

对于排序或者二分nlogn的时间复杂度显然无法承受。

于是我们考虑分块,首先根据a[i]/250去分块,那么最多有214个块,那么我们就可以先找到第k大位于哪一个块里面,然后再扫一遍,把位于这个块的那些数找出来,对这些数排序即可。


#include <cstdio>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef unsigned long long ll;
const int maxn = 100005;
const ll mod = 1e9+7;
const int INF = 1e9;
const double eps = 1e-6;

int n,m;
ll tmp[maxn];
vector<ll>vec;

int main()
{
    ll a,b,k,s;
    scanf("%llu%llu%llu%d%llu",&a,&b,&k,&n,&s);
    ll mm=s;
    ll sz=(1ULL<<50);
    tmp[s/sz]++;
    int Max=0;
    for(int i=2;i<=n;i++)
    {
        s=s*a+b;
        tmp[s/sz]++;
        Max=max(Max,(int)(s/sz));
    }
    ll pos=-1;
    for(int i=0;i<=Max;i++)
    {
        if(k>tmp[i]) k-=tmp[i];
        else
        {
            pos=i;
            break;
        }
    }
    s=mm;
    if(s/sz==pos) vec.push_back(s);
    for(int i=2;i<=n;i++)
    {
        s=s*a+b;
        if(s/sz==pos) vec.push_back(s);
    }
    sort(vec.begin(),vec.end());
    printf("%llu\n",vec[k-1]);
}



猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/80194784