Submission #2701772


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))

ll gcd(ll a, ll b) { while(a) swap(a, b%=a); return b; }

const ll MOD=1000000007LL;

ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }
ll comb(ll n, ll k) { ll v=1; for(ll i=1; i<=k; i++) v = DIV(MUL(v, n-i+1),i); return v; }
//


int H,W,A,B;

map<ii,ll> memo;

inline ll y(int dh, int dw){
    ii k = ii(dh+dw, dw);
    // fprintf(stderr, "k=(%d,%d)\n", dh+dw, dw);
    // assert(found(memo,k));
    return memo[k];
#if 0
    ll res = comb(dh+dw, dh);
#ifdef DEBUG
    fprintf(stderr, "  y(%d,%d) = %d C %d = %lld\n", dh,dw, dh+dw,dw, res);
#endif
    memo[k] = res;
    return res;
#endif
}

void prepare_comb(){
    ll last;
    for (int r=H-A+B-1,k=B; k<W; ++r,++k) {
        last = memo[ii(r,k)] = (k==B) ? comb(r,k) : DIV(MUL(last,r),k);
        // fprintf(stderr, ": %dC%d = %lld\n", r,k,last);
    }
    for (int r=A-1,k=0; k<W-B; ++r,++k) {
        last = memo[ii(r,k)] = (k==0) ? comb(r,k) : DIV(MUL(last,r),k);
        // fprintf(stderr, ": %dC%d = %lld\n", r,k,last);
    }
}

ll route(int s){
    // (0,0) - (H-A-1,s) - (H-A,s) - (H-1,W-1)
    // fprintf(stderr, "s=%d: (%d,%d)-(%d,%d)-(%d,%d)-(%d,%d)\n", s, 0,0, H-A-1,s, H-A,s, H-1,W-1);
    return MUL(y(H-A-1, s), y(A-1, W-1-s));
}

ll solve() { // ll H,ll W,ll A,ll B) {
    prepare_comb();
    // fprintf(stderr, "solve(H=%d,W=%d, A=%d,B=%d)\n", H,W,A,B);
    ll ans = 0LL;
    for (int s=B; s<W; ++s) {
        ans = ADD(ans, route(s));
    }
    return ans;
}

int main() {
    cin >> H >> W >> A >> B;
    cout << solve() << endl;
    return 0;
}

Submission Info

Submission Time
Task D - いろはちゃんとマス目 / Iroha and a Grid
User naoya_t
Language C++14 (GCC 5.4.1)
Score 400
Code Size 3027 Byte
Status
Exec Time 118 ms
Memory 12672 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask0_01.txt, subtask0_02.txt, subtask0_03.txt, subtask0_04.txt
All 400 / 400 subtask0_01.txt, subtask0_02.txt, subtask0_03.txt, subtask0_04.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_max.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_max.txt
Case Name Status Exec Time Memory
subtask0_01.txt 1 ms 256 KB
subtask0_02.txt 1 ms 256 KB
subtask0_03.txt 17 ms 256 KB
subtask0_04.txt 54 ms 5760 KB
subtask1_01.txt 1 ms 256 KB
subtask1_02.txt 1 ms 256 KB
subtask1_03.txt 1 ms 256 KB
subtask1_04.txt 1 ms 256 KB
subtask1_05.txt 1 ms 256 KB
subtask1_06.txt 1 ms 256 KB
subtask1_07.txt 1 ms 256 KB
subtask1_08.txt 1 ms 256 KB
subtask1_09.txt 1 ms 256 KB
subtask1_10.txt 1 ms 256 KB
subtask1_max.txt 1 ms 256 KB
subtask2_01.txt 4 ms 768 KB
subtask2_02.txt 3 ms 256 KB
subtask2_03.txt 3 ms 512 KB
subtask2_04.txt 5 ms 896 KB
subtask2_05.txt 4 ms 640 KB
subtask2_06.txt 109 ms 12544 KB
subtask2_07.txt 18 ms 640 KB
subtask2_08.txt 66 ms 7296 KB
subtask2_09.txt 43 ms 4480 KB
subtask2_10.txt 118 ms 12672 KB
subtask2_max.txt 9 ms 1536 KB