Codeforces-935d:fafa and Ancient Alphabet (DP)

Source: Internet
Author: User
Tags pow time limit

D. Fafa and Ancient Alphabet time limit per test 2 seconds memory limit per test, megabytes input standard input output Standard output

Ancient Egyptians is known to has used a large set of symbols to write on the walls of the temples. Fafa and Fifa went to one of the temples and found, non-empty words S1 and S2 of equal lengths on the wall of temple WR Itten one below the other. Since This temple are very ancient, some symbols from the words were erased. The symbols in the set has equal probability for being in the position of any erased symbol.

Fifa challenged Fafa to calculate the probability that S1 are lexicographically greater than S2. Can Fafa with this task?

You know that, i. e. There were m distinct characters in Egyptians ' alphabet, in this problem these characters is denote d by integers from 1 to M in alphabet order. A word X is lexicographically greater than a word y of the same length, if the words was same up to some position, and the n the word x has a larger character, than the word y.

We can prove the probability equals to some fraction, where P and Q is coprime integers, and.  Print as the answer the value, i. e. Such a non-negative integer less than 109 + 7, such this, where means that A and B Give the same remainders when divided by M. Input

The first line contains-integers n and M (1≤n, m≤105)-the length of each of the both words and the size of the A Lphabet, respectively.

The second line contains n integers a1, a2, ..., an (0≤ai≤m)-the symbols of S1. If ai = 0, then the symbol at position I was erased.

The third line contains n integers representing S2 with the same format as S1. Output

Print the value, where P and Q are coprime and was the answer to the problem. Examples input Copy

1 2
0
1
Output
500000004
Input Copy
1 2
1
0
Output
0
Input Copy
7
0 9 0 11 1 0
13 15 12 0
Output
230769233
Note

In the first sample, the first word can is converted into (1) or (2). The second option is the one and that'll make it lexicographically larger than the second word. So, the answer to the problem would be, which is 500000004, because.

In the second example, there are no replacement for the zero in the second word that'll make the first one Lexicographica lly larger. So, the answer to the problem are, that's 0.

Idea: DP. D[i][0] Represents the [1,i]s1=s2 (dictionary order) scheme number; d[i][1] represents the number of scenarios for S1>S2. The final answer is the total number of d[n][1]/programs.

#include <bits/stdc++.h> using namespace std;
const int MAX=1E6;
const int mod=1e9+7;
typedef __int64 LL;
int A[max],b[max];
ll D[max][2];
    ll POW (ll x,ll N) {ll res=1;
        while (n) {if (n&1) Res=res*x%mod;
        X=x*x%mod;
    n/=2;
} return res;
    } int main () {ll n,m,tot=0;
    cin>>n>>m;
    for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
    for (int i=1;i<=n;i++) scanf ("%d", &b[i]);
    memset (d,0,sizeof D);
    D[0][0]=1;
        for (int i=1;i<=n;i++) {tot+= (a[i]==0);
        tot+= (b[i]==0);
                if (A[i]&&b[i]) {if (A[i]==b[i]) {d[i][0]=d[i-1][0];
            D[I][1]=D[I-1][1];
                } else if (A[i]>b[i]) {d[i][1]= (d[i-1][1]+d[i-1][0])%mod;
            d[i][0]=0;
                } else {d[i][1]=d[i-1][1];
            d[i][0]=0;
     }
        }   else if (a[i]==0&&b[i]==0) {d[i][0]=d[i-1][0]*m%mod;
        D[i][1]= ((d[i-1][1]* ((m*m)%mod)%mod+d[i-1][0]* ((m-1) *M/2)%mod)%mod)%mod;
            } else if (a[i]==0) {d[i][0]=d[i-1][0]%mod;
        D[i][1]= ((d[i-1][0]* (m-b[i))%mod+ (d[i-1][1]*m)%mod)%mod;
            } else if (b[i]==0) {d[i][0]=d[i-1][0]%mod;
        D[i][1]= ((d[i-1][0]* (a[i]-1))%mod+ (d[i-1][1]*m)%mod)%mod;
    }} cout<< (D[n][1]*pow (POW (M,tot), MOD-2))%mod<<endl;
return 0;
 }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.