zoukankan      html  css  js  c++  java
  • 坐飞机——:)

    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.

  • 相关阅读:
    redis事务详解
    redis之管道
    redis持久化
    redis之通信协议
    redis之线程IO模型
    Google、Azure、阿里云、RedHat…全球的 K8s 圈大佬聚在一起要聊啥?
    开箱即用,Knative 给您极致的容器 Serverless 体验
    2020 年 HackerEarth 调查:Go 语言成为最受欢迎的语言(内含 Go 语言图谱下载)
    Serverless 选型:深度解读 Serverless 架构及平台选择
    阿里云容器服务发布 Knative 托管服务 | 云原生生态周报 Vol. 49
  • 原文地址:https://www.cnblogs.com/peppa/p/9435826.html
Copyright © 2011-2022 走看看