HTML & JS
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <utility>
#include <memory.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define forkn(i, k, n) for (int i = k; i < (int)n; i++)
#define forit(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)
#define sz(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <string> vs;
typedef pair <int,int> pii;
typedef pair <string,int> psi;
typedef map <string,int> msi;

template <class T> void relax(T &a, T b) { a = min(a, b); }
template <class T> void mrelax(T &a, T b) { a = max(a, b); }

const ll MOD = 1000000007;
ll n, m;

ll gcdex( ll a, ll b, ll &x, ll &y )
{
    if (!b)
    {
        x = 1, y = 0;
        return a;
    }
    ll x1, y1;
    ll d = gcdex(b, a%b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return d;
}

ll mod_inverse( ll a )
{
    ll b, k;
    ll d = gcdex(a, MOD, b, k);
    return (b + MOD) % MOD;
}

ll solve( vll &a, vll &b, int i )
{
    if (i == sz(a)) return 0;
    if (a[i] && a[i] == b[i]) return solve(a, b, i+1);
    if (a[i] && b[i] && a[i] > b[i]) return 1;
    if (a[i] && b[i] && a[i] < b[i]) return 0;

    ll r = 0;
    if (!a[i] && b[i]) r = ((m-b[i]) + solve(a, b, i+1)) % MOD;
    if (a[i] && !b[i]) r = ((a[i]-1) + solve(a, b, i+1)) % MOD;
    if (!a[i] && !b[i]) r = (m * solve(a, b, i+1) + m*(m-1)/2) % MOD;

    return r;
}

int main()
{
    cin >> n >> m;
    vll a(n), b(n);
    forn(i, n) cin >> a[i];
    forn(i, n) cin >> b[i];

    ll p = solve(a, b, 0);

    cout << (p * mod_inverse(m)) % MOD << "\n";

    return 0;
}

Generated URL:

Output:

230769233
Report Bug