坐飞机——:)

瘦欲@ 提交于 2020-01-03 10:47:51

loj说我今天宜学数论。洛谷说我今天不宜泡妹子。——这就不太好啦。

【题目描述】

一架飞机有 n 个座位排成一列,有 m名乘客( m≤n )依次上飞机。

乘客会选择一个目标座位(两人可以选同一个目标座位),然后选择从前门或者后门上飞机,上飞机后,他们会走到自己的目标座位,如果目标座位已经有人坐了,他们会继续往前走,在走到第一个空位后坐下。如果走到最后还没有找到座位,这名乘客就会生气。

问有多少种登机方案能让所有乘客都不生气。两个登机方案不同当且仅当第 i位乘客的目标座位或上飞机走的门不同。

【输入输出格式】

输入格式:

The first line of input will contain two integers n,m  n,m ( 1<=m<=n<=1000000  ), the number of seats, and the number of passengers, respectively.

输出格式:

Print a single number, the number of ways, modulo 109+7  .

输入输出样例

输入样例#1:
3 3
输出样例#1:
128

说明

Here, we will denote a passenger by which seat they were assigned, and which side they came from (either "F" or "B" for front or back, respectively).

For example, one valid way is 3B, (i.e. all passengers were assigned seat 3 and came from the back entrance). Another valid way would be 2F, 1B, 3F.

One invalid way would be 2B, , since the third passenger would get to the front without finding a seat.

【嗯?】

一开始看这道题的时候想到了省集训时YHX女士的一道题,和这道题很像,那道题是用DP做的。而这道题要用数学来解决。以下是肖大佬的方法:加一个虚拟座位是n+1号,把这n+1个座位围成一个环。无论从前门还是后门进的乘客,只要坐到了这个虚拟座位上,就说明这个方案是不合理的。方案的总数为[2*(n+1)]^m,这n+1个座位没被占的机会是平等的,因此每个座位没被占的概率为(n+1-m)/n+1。将它们相乘,得到答案。

【代码】

 

 1 #include<cstdio>
 2 using namespace std;
 3 #define ll long long int
 4 ll p = 1e9+7,n,m;
 5 ll qpow(ll a,ll b)
 6 {
 7     ll q = 1;
 8     while(b)
 9     {
10         if(b&1) q *= a,q %= p;
11         a *= a,a %= p;
12         b >>= 1;
13     }
14     return q;
15 }
16 int main()
17 {
18     scanf("%d%d",&n,&m);
19     printf("%lld",qpow(2,m) * qpow(n+1,m - 1) % p * (n + 1 - m) % p);
20     return 0;    
21 }

 

end.

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!