前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >三校集训Part1 QZEZ Day2 A洗牌 题解

三校集训Part1 QZEZ Day2 A洗牌 题解

作者头像
yzxoi
发布2022-09-19 12:35:10
2460
发布2022-09-19 12:35:10
举报
文章被收录于专栏:OI

三校集训Part1 QZEZ Day2 A洗牌 题解

题意

无聊的时间,小 K 喜欢和他的室友们一起打扑克(这副扑克很神奇,上面写着 1 n 的数字各一张),打扑克前当然要先洗牌啦。 宿舍洗牌的方式十分简单,先将所有牌平均分成两份,然后交叉地混合到一起,举个例子,六张牌 1 2 3 4 5 6 在混合后后会变成 1 4 2 5 3 6,但是这样的问题很明显,第一张牌和最后一张牌一定不会变化,所以他们还要将最后的 k 张牌移动到最前面,如此的过程,混合加上切牌,称为一次洗牌。 小 Y 并不信任小 K 的洗牌姿势,他决定让小 K 进行若干次洗牌后,检查其中某些牌牌面的数字,来确定小 K 是否手上抹油,他知道这样洗牌的结果是固定的,但却不知道应该是什么,你能帮帮他吗?

思路

抹油是什么意思?QWQ 转入正题,首先一看这数据范围就是O(N logN)的复杂度,设这个变换函数为f(x),然后第一次变换后就是f(x),两次是f(f(x)),三次是f(f(f(x))),以此类推\dots。 所以这个函数f(x)支持倍增。 那么就预先处理出2^n变换的f函数,对于每次询问倍增就好了。 设f[i][j]2^i次,第j位的变换。 那么:f[i][j]=f[i-1][f[i-1][j]]; 但是毒瘤出题人卡了倍增,但卡卡常也就过了。

Code

代码语言:javascript
复制
#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#pragma GCC optimize(3)
#define pc(ch) (Ftop<100000?Fout[Ftop++]=ch:(fwrite(Fout,1,100000,stdout),Fout[(Ftop=0)++]=ch))
using namespace std;
inline int read(){
    int res=0,f=1;char ch=getchar();
    while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
    return res*f;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x+'0');
    else{
        write(x/10);
        putchar(x%10+'0');
    }
}
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
// 整 数 快 速 读 入
template <typename T>
bool rn(T& _v) {
bool negative = false;
char ch;
_v = 0;
while (!isdigit(ch = nc()) && ch != EOF) { negative = ch == '-'; };
if (ch == EOF)return false;
do {
_v *= 10;
_v += ch - '0';
} while (isdigit(ch = nc()));
if (negative) _v = -_v;
return true;
}
// 整 数 快 速 输 出
template <class T>
inline void o(T p) {
static int stk[70], tp;
if (p == 0) { putchar('0'); return; }
if (p < 0) { p = -p; putchar('-'); }
while (p) stk[++tp] = p % 10, p /= 10;
while (tp) putchar(stk[tp--] + '0');
}
int n,m,k,a[500010],f[41][500011];
signed main(){
    rn(n);rn(m);rn(k);
    register int i,j,Ta,b;
    for(i=1;i<=(n>>1);i++){
        a[(i<<1)]=(n>>1)+i,a[(i<<1)-1]=i;
    }
    for(i=1;i<=n;i++){
        f[0][i]=(i<=k%n)?a[n-k+i]:a[i-(k%n)];
    }
    for(i=1;i<40;i++){
        for(j=1;j<=n;j++){
            f[i][j]=f[i-1][(f[i-1][j])];
        }
    }
    long long res=0,tot=0;
    for(i=1;i<=m;i++){
        rn(Ta);rn(b);
        tot+=Ta;
        res=tot;
        for(j=0;res;res>>=1,j++){
            if(res&1ll) b=f[j][b];
        } 
        o(b);putchar('\n');
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-07-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 三校集训Part1 QZEZ Day2 A洗牌 题解
    • 题意
      • 思路
        • Code
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档