• codeforce465DIV2——D. Fafa and Ancient Alphabet


    概率的计算答案给出的这张图很清楚了,然后因为要求取模,a/b%M=a*b^-1%M=a*inv(b,M)%M;

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <cstdio>
     5 
     6 using namespace std;
     7 const int maxn=100000+10;
     8 const int MOD=1000000007;
     9 typedef long long LL;
    10 int n,m;
    11 int s1[maxn],s2[maxn],d[maxn];
    12 void gcd(LL a,LL b,LL &d,LL &x,LL &y){
    13     if(!b){
    14         d=a;x=1;y=0;
    15     }else{
    16         gcd(b,a%b,d,y,x);
    17         y-=x*(a/b);
    18     }
    19 }
    20 LL inv(LL a,LL n){
    21     LL d,x,y;
    22     gcd(a,n,d,x,y);
    23     return d==1?(x+n)%n:-1;
    24 }
    25 LL dp(int a){
    26     if(a==n+1)return 0;
    27     if(s1[a]==s2[a]&&s1[a]!=0&&s2[a]!=0)return dp(a+1);
    28     if(s1[a]>s2[a]&&s1[a]!=0&&s2[a]!=0)return 1;
    29     if(s1[a]<s2[a]&&s1[a]!=0&&s2[a]!=0)return 0;
    30     if(s1[a]==0&&s2[a]!=0)
    31     return (((m-s2[a])%MOD*inv(m,MOD))%MOD+dp(a+1)%MOD*inv(m,MOD))%MOD;
    32     if(s1[a]!=0&&s2[a]==0)
    33     return (((s1[a]-1)%MOD*inv(m,MOD))%MOD+(dp(a+1))%MOD*inv(m,MOD))%MOD;
    34     if(s1[a]==0&&s2[a]==0)
    35     return (((m-1)%MOD*inv(2*m,MOD))%MOD+(dp(a+1)%MOD*inv(m,MOD)))%MOD;
    36 }
    37 
    38 
    39 int main(){
    40     scanf("%d%d",&n,&m);
    41     for(int i=1;i<=n;i++)scanf("%d",&s1[i]);
    42     for(int i=1;i<=n;i++)scanf("%d",&s2[i]);
    43     LL P=dp(1);
    44     cout<<P;
    45 return 0;
    46 }
    View Code
  • 相关阅读:
    Git初级实践教程(图文)
    如何合并多个PPT
    优秀小工具集锦
    VS2015链接错误一则
    VisualStudio配色方案
    AI贪吃蛇(二)
    springMVC
    SSH三大框架的搭建整合(struts2+spring+hibernate)(转)
    生成图片验证码
    Spring JdbcTemplate详解(转)
  • 原文地址:https://www.cnblogs.com/LQLlulu/p/8786097.html
Copyright © 2020-2023  润新知