三校集训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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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;
}