信息学方法:
c
p
i
+
1
=
(
1
−
b
)
p
i
−
a
p
i
−
1
cp_{i+1}=(1-b)p_i-ap_{i-1}
cpi+1=(1−b)pi−api−1
发现
p
i
p_i
pi 都可以由
p
1
p_1
p1 表示,把系数代着走,到
p
2
∗
n
p_{2*n}
p2∗n 就可以解出
p
1
p_1
p1 再推到
p
n
p_n
pn 即可
然后发现可以矩阵快速幂优化,把
n
,
2
n
n,2n
n,2n 求出来就可以了
数学方法:
注意到
a
+
b
+
c
=
1
a+b+c=1
a+b+c=1
考虑到对称性
(
a
+
c
)
p
i
=
a
p
i
−
1
+
c
p
i
+
1
(a+c)p_i=ap_{i-1}+cp_{i+1}
(a+c)pi=api−1+cpi+1
于是有
a
(
p
i
−
p
i
−
1
)
=
c
(
p
i
+
1
−
p
i
)
a(p_i-p_{i-1})=c(p_{i+1}-p_i)
a(pi−pi−1)=c(pi+1−pi)
记
p
p
p 的差分为
d
d
d,有
d
n
∗
2
=
(
a
c
)
2
∗
n
−
1
f
1
d_{n*2}=(\frac{a}{c})^{2*n-1}f_1
dn∗2=(ca)2∗n−1f1
d
n
=
(
a
c
)
n
−
1
f
1
d_{n}=(\frac{a}{c})^{n-1}f_1
dn=(ca)n−1f1
注意到
p
n
=
∑
i
=
1
n
d
i
p_n=\sum_{i=1}^nd_i
pn=∑i=1ndi
等比数列求和,令
k
=
a
c
k=\frac{a}{c}
k=ca
p
n
=
k
n
−
1
k
−
1
p_n=\frac{k^{n}-1}{k-1}
pn=k−1kn−1
p
n
p
2
∗
n
=
k
n
−
1
k
2
n
−
1
=
1
k
n
+
1
=
c
n
a
n
+
c
n
\frac{p_n}{p_{2*n}}=\frac{k^n-1}{k^{2n}-1}=\frac{1}{k^n+1}=\frac{c^n}{a^n+c^n}
p2∗npn=k2n−1kn−1=kn+11=an+cncn
#include<bits/stdc++.h>
#define cs const
using namespace std;
int read(){
int x = 0, f = 1; char ch = 0;
while(!isdigit(ch)) { ch = getchar(); if(ch == '-') f = -1;}
while(isdigit(ch)) x = (x + (x << 2) << 1) + (ch^48), ch = getchar();
return x * f;
}
cs int Mod = 1e9 + 7;
typedef long long ll;
int add(int a, int b){ return a + b >= Mod ? a + b - Mod : a + b; }
int mul(int a, int b){ ll r = 1ll * a * b; if(r >= Mod) r %= Mod; return r; }
int ksm(int a, int b){ int ans = 1; for(;b;b>>=1,a=mul(a,a)) if(b&1) ans = mul(ans,a); return ans;}
int n, a, b, x, y, z;
cs int N = 1e7 + 5;
int f[N << 2];
int main(){
n = read(), a = read(), b = read();
x = mul(add(1,Mod-a), b);
y = add(mul(add(a,Mod-1),add(b,Mod-1)), mul(a, b));
z = mul(a, add(1,Mod-b));
int inv = ksm(z, Mod-2);
f[1] = 1;
for(int i = 2, up = n << 1; i <= up; i++){
f[i] = mul(inv, add(mul(add(1,Mod-y),f[i-1]), Mod-mul(x,f[i-2])));
} cout << mul(ksm(f[n<<1], Mod-2), f[n]); return 0;
}
用栈模拟出牌队列,
q
u
e
u
e
queue
queue模拟手牌队列即可
考场上有个地方调用了
s
t
a
[
t
o
p
+
1
]
sta[top+1]
sta[top+1] 但是清空只清空到了
t
o
p
top
top,于是就凉了
以后一定要倍加小心
#include<bits/stdc++.h>
#define cs const
using namespace std;
namespace IO{
cs int Rlen = 1 << 22 | 1;
char buf[Rlen], *p1, *p2;
char gc(){
return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, Rlen, stdin), p1 == p2) ? EOF : *p1++;
}
int read(){
int x = 0, f = 1; char ch = 0;
while(!isdigit(ch)){ ch = gc(); if(ch == '-') f = -1; }
while(isdigit(ch)) x = (x + (x << 2) << 1) + (ch - '0'), ch = gc();
return x * f;
}
} using namespace IO;
cs int N = 1e5 + 5, M = 105;
int n, m, l, s, T;
int sta[N], top; // 用栈维护序列
bool insta[N];
int a[M][M];
queue<int> q[M];
int b[N], sz; // 离散化
int ban[M]; // 出局时间
int tp[N];
void Clear(){ while(top) insta[sta[top]] = 0, top--; sz = 0; }
void Solve(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= l; j++){
a[i][j] = b[++sz] = read();
}
} b[++sz] = s; sort(b + 1, b + sz + 1);
sz = unique(b + 1, b + sz + 1) - (b + 1);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= l; j++){
a[i][j] = lower_bound(b + 1, b + sz + 1, a[i][j]) - b;
q[i].push(a[i][j]);
}
} s = lower_bound(b + 1, b + sz + 1, s) - b;
int ct = n;
for(int TIME = 1; TIME <= T; TIME++){
if(ct <= 1) break;
for(int i = 1; i <= n; i++){
if(ban[i]) continue;
int nx = q[i].front(); q[i].pop();
if(insta[nx]){
int ret = 0;
while(sta[top] ^ nx) tp[++ret] = sta[top], insta[sta[top]] = 0, top--;
tp[++ret] = sta[top], insta[sta[top]] = 0, top--;
for(int j = ret; j >= 1; j--) q[i].push(tp[j]);
q[i].push(nx);
}
else if(nx == s && top){
int ret = 0;
while(top) tp[++ret] = sta[top], insta[sta[top]] = 0, top--;
for(int j = ret; j >= 1; j--) q[i].push(tp[j]);
q[i].push(nx);
} else sta[++top] = nx, insta[nx] = true;
if(q[i].empty()) ban[i] = TIME, --ct;
}
}
for(int i = 1; i <= n; i++){
if(ban[i]) cout << - ban[i];
else cout << q[i].size();
if(i ^ n) cout << " ";
else puts("");
}
for(int i = 1; i <= n; i++){
if(ban[i]){puts(""); ban[i] = 0; continue;}
while(!q[i].empty()) cout << b[q[i].front()] << " ", q[i].pop();
puts("");
}
}
int main(){
while(1){
n = read(), m = read(), l = read(), s = read(), T = read();
if(!(~n)) break; Solve(); Clear();
} return 0;
}