题目描述
Ela likes to go hiking a lot. She loves nature and exploring the various creatures it offers. One day, she saw a strange type of ant, with a cannibalistic feature. More specifically, an ant would eat any ants that it sees which is smaller than it.Curious about this feature from a new creature, Ela ain’t furious. She conducts a long, non-dubious, sentimental experiment.
She puts n n n cannibalistic ants in a line on a long wooden stick. Initially, the ants have the same weight of 1 1 1 . The distance between any two consecutive ants is the same. The distance between the first ant in the line to the left end and the last ant in the line to the right end is also the same as the distance between the ants. Each ant starts moving towards the left-end or the right-end randomly and equiprobably, at the same constant pace throughout the experiment. Two ants will crash if they are standing next to each other in the line and moving in opposite directions, and ants will change direction immediately when they reach the end of the stick. Ela can’t determine the moving direction of each ant, but she understands very well their behavior when crashes happen.
- If a crash happens between two ants of different weights, the heavier one will eat the lighter one, and gain the weight of the lighter one. After that, the heavier and will continue walking in the same direction. In other words, if the heavier one has weight x x x and walking to the right, the lighter one has weight y y y and walking to the left ( x > y x > y x>y ), then after the crash, the lighter one will diminish, and the heavier one will have weight x + y x + y x+y and continue walking to the right.
- If a crash happens between two ants with the same weight, the one walking to the left end of the stick will eat the one walking to the right, and then continue walking in the same direction. In other words, if one ant of weight x x x walking to the left, crashes with another ant of weight x x x walking to the right, the one walking to the right will disappear, and the one walking to the left will have to weight 2 x 2x 2x and continue walking to the left.
Please, check the example in the “Note” section, which will demonstrate the ants’ behavior as above.
We can prove that after a definite amount of time, there will be only one last ant standing. Initially, each ant can randomly and equiprobably move to the left or the right, which generates 2 n 2^n 2n different cases of initial movements for the whole pack. For each position in the line, calculate the probability that the ant begins in that position and survives. Output it modulo 1 0 9 + 7 10^9 + 7 109+7 .
Formally, let M = 1 0 9 + 7 M = 10^9 + 7 M=109+7 . It can be shown that the answer can be expressed as an irreducible fraction p q \frac{p}{q} qp , where p p p and q q q are integers and q ≢ 0 ( m o d M ) q \not \equiv 0 \pmod{M} q≡0(modM) . Output the integer equal to p ⋅ q − 1 m o d M p \cdot q^{-1} \bmod M p⋅q−1modM . In other words, output such an integer x x x that 0 ≤ x < M 0 \le x < M 0≤x<M and x ⋅ q ≡ p ( m o d M ) x \cdot q \equiv p \pmod{M} x⋅q≡p(modM) .
题目翻译
n n n 只蚂蚁站成一排,第 1 1 1 只蚂蚁左边和第 n n n只蚂蚁右边各有一个挡板,相邻两只蚂蚁的距离、第 1 1 1 只蚂蚁与左边挡板的距离和第 n n n 只蚂蚁与右侧挡板的距离相等。初始时每只蚂蚁重量相等,每只蚂蚁有 1 2 \frac{1}{2} 21 概率向左运动, 1 2 \frac{1}{2} 21 概率向右运动,每只蚂蚁速度相同。中途蚂蚁不可主动改变方向,如果碰到挡板则向相反方向运动,若两只蚂蚁相遇,重量大的蚂蚁会把重量小的蚂蚁吃掉,重量变为两者之和,如果重量相同,向左运动的蚂蚁会吃掉向右运动的蚂蚁。求对于所有 i ≤ 1 ≤ n i\le1\le n i≤1≤n,第 i i i 只蚂蚁成为最终的存活者的概率对 1 0 9 + 7 10^9+7 109+7 取模。
题解
求存活的期望,可以转化为求所有的可能中存活的次数。
以下简称位置为 i i i 的蚂蚁为 i i i。
此题需要两个性质:
- 除了第
n
n
n 只蚂蚁,向右走一定会被吃掉(由于最右边有挡板,所以第
n
n
n 只蚂蚁肯定会往左走)。
证明:因为重量相同的两只蚂蚁相遇时,右边那只会存活。
所以向右走的蚂蚁是无法存活的,它会被其右边第一个向左走的蚂蚁吃掉。 - 一个向左走的蚂蚁
j
j
j,它要存活必须保证没有位置大于等于
2
j
2j
2j 的向左走的蚂蚁,且没有位置在区间
(
⌊
j
2
⌋
,
j
)
(\lfloor\frac j2\rfloor,j)
(⌊2j⌋,j) 的向左走的蚂蚁。
证明:设这只蚂蚁左边第一个向左走的蚂蚁位置为 i i i,右边第一个向左走的蚂蚁位置为 k k k。显然,位置小于等于 i i i 的蚂蚁会相互决斗,最后剩下一只重量为 i i i 的蚂蚁,而蚂蚁 j j j 会吃掉位置从 i + 1 i+1 i+1 到 j − 1 j-1 j−1 的蚂蚁,最后重量为 j − i j-i j−i。若蚂蚁 j j j 想要吃掉蚂蚁 i i i,就有 j − i ≥ i j-i\ge i j−i≥i,即 i ≤ ⌊ j 2 ⌋ i\le\lfloor\frac j2\rfloor i≤⌊2j⌋。右边的情况差不多,蚂蚁 k k k 可以吃掉蚂蚁 j j j 后面的蚂蚁,重量变为 k − j k-j k−j,假设蚂蚁 j j j 能吃掉前面的蚂蚁,要使蚂蚁 j j j 存活,有 k − j < j k-j<j k−j<j,即 k < 2 j k<2j k<2j。
总的来说就是 i ≤ ⌊ j 2 ⌋ , k < 2 j i\le\lfloor\frac j2\rfloor,k<2j i≤⌊2j⌋,k<2j 是蚂蚁 j j j 存活的充要条件。
设 f i f_i fi 表示蚂蚁 i i i 存活的情况下,右边蚂蚁的排列总数。
由性质 2 2 2 得出,对于蚂蚁 i i i,它右边第一个向左走的蚂蚁一定在区间 ( i , 2 i ) (i,2i) (i,2i) 内。
容易列出转移方程
f
i
=
∑
j
=
i
+
1
2
i
−
1
f
j
f_i=\sum\limits_{j=i+1}^{2i-1}f_j
fi=j=i+1∑2i−1fj
意思是在区间内
(
i
,
2
i
)
(i,2i)
(i,2i) 的蚂蚁可以存在,
f
j
f_j
fj 可以转移到
f
i
f_i
fi。
对于蚂蚁 i i i,最终存活的概率即为 2 ⌊ i 2 ⌋ ⋅ f i 2 n − 1 \dfrac{2^{\lfloor\frac i2\rfloor}\cdot f_i}{2^{n-1}} 2n−12⌊2i⌋⋅fi,因为 ⌊ i 2 ⌋ \lfloor\frac i2\rfloor ⌊2i⌋ 及其前面的蚂蚁的方向可以任意选,而区间 ( ⌊ i 2 ⌋ , i ) (\lfloor\frac i2\rfloor,i) (⌊2i⌋,i) 内的蚂蚁只能向右走。总的方案数为 2 n − 1 2^{n-1} 2n−1,因为最后一只蚂蚁不管如何肯定会向左走。
朴素的 D P DP DP 是 O ( n 2 ) O(n^2) O(n2) 的,使用前缀和优化即可优化到 O ( n ) O(n) O(n)。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
int n;
ll f[1000002],sum[1000002],p[1000001],inv[1000001];
int main()
{
inv[0]=p[0]=1;
for(int i=1;i<=1000000;i++) p[i]=p[i-1]*2%mod,inv[i]=inv[i-1]*500000004%mod;
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
f[n]=sum[n]=1,sum[n+1]=0;
for(int i=n-1;i>=1;i--){
f[i]=((sum[i+1]-sum[min(2*i,n+1)])%mod+mod)%mod;
sum[i]=sum[i+1]+f[i];
}
for(int i=1;i<=n;i++){
printf("%lld\n",p[i/2]*f[i]%mod*inv[n-1]%mod);
}
}
}