JZOJ 6812.战争

妙。

ilnil 大佬教导我们,看到这种题一半都要考虑构造个奇怪的函数,使得终态对应的函数值是唯一最值,且每次操作的期望变化量为常数。

对于最值的限制,要当 \(n\) 越小时越大,那么容易想到关于 \(a_i\) 构造指数函数。
而期望变化量为常数,注意到有 \(p\) 的概率,所以指数函数的底数可以考虑 \(\frac 1p\)。而对于分裂出来的国家,似乎不好考虑。那么强行减掉一个 \(\frac 1p\) 即可。

所以对于状态 \(S = \{a_1,a_2,\dots,a_n\}\),构造 \(g(S) = \sum\limits_{i=1}^n \left(\frac 1{p^{a_i}} - \frac 1p\right)\)
易得进行一次操作的期望增量为 \(\Delta = \frac 2p -2\)

设答案为 \(f(S)\),于是易证 \(E(f(S)) = \frac 1{\Delta} \left(g\left(\left\{\sum\limits_{i=1}^n a_i\right\}\right) - g(S)\right)\)

代码:

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
#include <cstdio>
using namespace std;
const int N = 4e7;
const int M = 1e5;
const int W = 15;
const int B = 1 << W;
const int mod = 1e9 + 7;
inline int fpow(int a,int b)
{
int ret = 1;
for(;b;b >>= 1)
(b & 1) && (ret = (long long)ret * a % mod),a = (long long)a * a % mod;
return ret;
}
int T,n,s,t,p;
int sum;
unsigned long long x,y,z,b1,b2,b;
int m,q[M + 5];
long long l[M + 5],r[M + 5];
int pw1[B + 5],pw2[B + 5];
inline int query(int x)
{
return (long long)pw2[x >> W] % mod * pw1[x & (B - 1)] % mod;
}
int ans1,ans2,ans;
int main()
{
freopen("warfare.in","r",stdin),freopen("warfare.out","w",stdout);
scanf("%d%d%d%d",&T,&n,&s,&t),p = (long long)t * fpow(s,mod - 2) % mod;
pw1[0] = pw2[0] = 1;
for(register int i = 1;i <= B;++i)
pw1[i] = (long long)pw1[i - 1] * p % mod;
for(register int i = 1;i <= B;++i)
pw2[i] = (long long)pw2[i - 1] * pw1[B] % mod;
long long a;
if(!T)
for(register int i = 1;i <= n;++i)
{
scanf("%lld",&a),a %= mod - 1;
sum = (sum + a) % (mod - 1),
ans2 = (ans2 + (query(a) - p + mod) % mod) % mod;
}
else
{
scanf("%d%llu%llu%llu%llu%llu",&m,&x,&y,&z,&b1,&b2);
for(register int i = 1;i <= m;++i)
scanf("%d%lld%lld",q + i,l + i,r + i);
for(register int i = 1,j = 1;i <= n;++i)
{
for(;q[j] < i;++j);
if(i == 1)
b = b1;
else if(i == 2)
b = b2;
else
b = x * b2 + y * b1 + z;
a = b % (r[j] - l[j] + 1) + l[j];
a %= mod - 1;
sum = (sum + a) % (mod - 1),
ans2 = (ans2 + (query(a) - p + mod) % mod) % mod;
i > 2 && (b1 = b2,b2 = b);
}
}
ans1 = (query(sum) - p + mod) % mod;
ans = (long long)(ans1 - ans2 + mod) * fpow((2LL * p + mod - 2) % mod,mod - 2) % mod;
printf("%d\n",ans);
}