935D - Fafa and Ancient Alphabet - CodeForces Solution


math probabilities *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int mod = 1e9 +7;
int modinv_m;
int val;
int binexp(int x, int y)
{
    if(y==0)
        return 1;
    if(y&1)
        return (binexp((x*x)%mod, y/2) * x)%mod;
    else
        return (binexp((x*x)%mod, y/2))%mod;
}
int calculate(vector<int> &a, vector<int> &b, int n, int m, int i)
{
    if(i==n)
        return 0;
    for(;i<n;i++)
    {
        // cout<<a[i]<<" "<<b[i]<<endl; 
        if(a[i] == b[i] && a[i]!=0)
            continue;
        if(a[i] == b[i])
            return ((calculate(a,b,n,m,i+1) + (val*modinv_m)%mod)*modinv_m)%mod;
        else if(a[i] == 0)
        {
            // cout<<"Test\n";
            return ((calculate(a,b,n,m,i+1)+(m-b[i]))*modinv_m)%mod;
        }
        else if(b[i] == 0)
            return ((calculate(a,b,n,m,i+1)+(a[i]-1))*modinv_m)%mod;
        else if(a[i]>b[i])
            return 1;
        else
            return 0;
    }
    return 0;
}
void solve()
{
    int n, m;
    cin>>n>>m;
    modinv_m = binexp(m,mod-2)%mod;
    val = (m*(m-1))/2;
    vector<int> a(n);
    vector<int> b(n);
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=0;i<n;i++)
        cin>>b[i];
    int p = (calculate(a,b,n,m,0))%mod;
    cout<<p<<endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    // cin>>t;
    for(int i=1;i<=t;i++)
    {
        // cout<<"Testcase: "<<i<<endl;
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX