Codeforces Round #313 (Div. 2) Problem E - Gerald and Giant Chess

来源:互联网 发布:matlab矩阵运算 编辑:程序博客网 时间:2024/05/28 03:01
#include <iostream>#include <algorithm>#include <cstdlib>using namespace std;const int mod = 1000000007;typedef long long ll;ll fac[2000001];ll PowMod(ll a, ll b, ll mod){    ll ret = 1LL;    while(b)    {        if(b&1) ret = (ret*a)%mod;        a = (a*a)%mod;        b >>= 1;    }    return ret;}void cfac(){    fac[0] = 1;    for(ll i = 1; i <= 200000; i++) fac[i] = i * fac[i-1] % mod;}ll Lucas(ll n,ll m,ll p = mod){    ll ret=1;    if(n==0)return 1;    while(n&&m){        ll a=n%p,b=m%p;        if(a<b) return 0;        ret=(ret*fac[a]*PowMod(fac[b]*fac[a-b]%p,p-2,p))%p;        n/=p;        m/=p;    }    return ret;}ll pre[2002][2002];ll dp[2002];struct data{    ll x, y;    bool operator<(const data& b) const    {        return y == b.y ? x < b.x : y < b.y;    }} Q[2002];int main(){    ios::sync_with_stdio(0);    cfac();    int h, w, n, l;    cin>>h>>w>>n;    for(int i = 0; i < n; i++)    {        cin>>Q[i].x>>Q[i].y;    }    sort(Q, Q+n);    ll ans = 0LL;    for(int i = 0; i < n; i++)    {        dp[i] += Lucas(Q[i].x - 1 + Q[i].y - 1, Q[i].x-1);        dp[i] %= mod;        if(dp[i] < 0) dp[i] += mod;        for(int j = i + 1; j < n; j++)        {            if(Q[j].x < Q[i].x) continue;            ll temp =  Lucas(Q[j].x + Q[j].y - Q[i].y - Q[i].x,                              Q[j].x -  Q[i].x);            pre[i][j] = temp;            dp[j] -= temp * dp[i];            dp[j] %= mod;            if(dp[j] < 0) dp[j] += mod;        }        //cout<<dp[i]<<endl;    }    ll temp = Lucas(h+w-2,h-1);    for(int i = n-1; i >= 0; i--)    {        temp -= dp[i] *Lucas(h + w - Q[i].x - Q[i].y, h - Q[i].x) ;        temp %= mod;        if(temp < 0) temp += mod;        temp %= mod;        if(temp < 0) temp += mod;    }    cout<<temp<<endl;    return 0;}#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <set>#include <map>#include <vector>#include <string>#include <cstring>#include <string.h>using namespace std;const int N = 100005;const int mod = 1e9 + 7;const int inf = 1e9 + 9;typedef long long ll;int t;pair<int, int> a[N];int fact[2*N], inv_fact[2*N];int ways[N];int pow_mod(int a, int n){    int res = 1;    while(n){        if(n & 1)            res = (res*1ll*a) % mod;        n >>= 1;        a = (a*1ll*a) % mod;    }    return res;}int main(){    fact[0] = 1;    for(int i = 1; i < 2 * N; ++i)        fact[i] = (fact[i - 1]*1ll*i) % mod;    for(int i = 0; i < 2 * N; ++i){        inv_fact[i] = pow_mod(fact[i], mod - 2);    }    int n, m, k;    scanf("%d %d %d", &n, &m, &k);    for(int i = 0; i < k; ++i){        scanf("%d %d", &a[i].first, &a[i].second);        --a[i].first;        --a[i].second;    }    a[k].first = n - 1;    a[k].second = m - 1;    sort(a, a + k);    k++;    for(int i = 0; i < k; ++i){        int inv1 = inv_fact[a[i].first];        int inv2 = inv_fact[a[i].second];        int inv = (inv1*1ll*inv2) % mod;        ways[i] = fact[a[i].first + a[i].second];        ways[i] = (ways[i]*1ll*inv) % mod;    }    for(int i = 0; i < k; ++i){        for(int j = 0; j < i; ++j){            if(a[i].first >= a[j].first && a[i].second >= a[j].second){                int sz1 = a[i].first - a[j].first;                int sz2 = a[i].second - a[j].second;                int inv1 = inv_fact[sz1];                int inv2 = inv_fact[sz2];                int inv = (inv1*1ll*inv2) % mod;                int cur = fact[sz1 + sz2];                cur = (inv*1ll*cur) % mod;                ways[i] -= (ways[j]*1ll*cur) % mod;                if(ways[i] < 0) ways[i] += mod;            }        }    }    cout << ways[k - 1] << "\n";    return  0;}#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#define si(a) scanf("%d",&a)#define M 1000000007#define f first#define s secondusing namespace std;vector<pair<int,int> > all;int h,w;long long power[200010],dp[2005];long long mod_plus(long long a,long long b){    return ((a%M)+(b%M))%M;}long long mod_mul(long long a,long long b){    return ((a%M)*(b%M))%M;}long long mod_min(long long a,long long b){    return (a-b+M)%M;}long long mul_inv(long long a, long long b){long long b0 = b, t, q;long long x0 = 0, x1 = 1;if (b == 1) return 1;while (a > 1) {q = a / b;t = b, b = a % b, a = t;t = x0, x0 = x1 - q * x0, x1 = t;}if (x1 < 0) x1 += b0;return x1;}long long mod_div(long long a,long long b){    return mod_mul(a,mul_inv(b,M));}long long solve(int pos){    if(dp[pos]!=-1)        return dp[pos];    int x=(h-all[pos].f),y=(w-all[pos].s),i;    long long ans=power[x+y],divi=mod_mul(power[x],power[y]);    ans=mod_div(ans,divi);    for(i=pos+1;i<all.size();i++){        if(all[i].s>=all[pos].s){            long long temp,_x,_y;            _x=all[i].f-all[pos].f;            _y=all[i].s-all[pos].s;            divi=mod_mul(power[_x],power[_y]);            temp=mod_div(power[_x+_y],divi);            ans=mod_min(ans,mod_mul(temp,solve(i)));        }    }    return dp[pos]=ans;}int main(){    //freopen("input.txt","r",stdin);    int n,i;    si(h);    si(w);    h--;    w--;    si(n);    for(i=0;i<n;i++){        pair<int,int> temp;        si(temp.f);        si(temp.s);        temp.f--;        temp.s--;        all.push_back(temp);    }    all.push_back({0,0});    sort(all.begin(),all.end());    power[0]=1;    for(i=1;i<200010;i++)        power[i]=mod_mul(i,power[i-1]);    memset(dp,-1,sizeof(dp));    cout<<solve(0)<<endl;    return 0;} 
0 0