“今日头条”杯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]);
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值