Solution to a problem AtCoder abc154 F

Title effect Order \ (F (R & lt, C) = C_ {R & lt + C} ^ R & lt \) , the required output \ (\ sum_ {i = r_1 } ^ {r_2} \ sum_ {j = c_1} ^ {c_2} f (I, J) \) , where \ (r_2, r_2, c_1, c_2 \) to the given value.

Analysis so

\[g(r,c)=\sum_{i=0}^r\sum_{j=0}^c f(i,j)\]

It is entitled to ask

\[\sum_{i=r_1}^{r_2}\sum_{j=c_1}^{c_2} f(i,j)=g(r_2,c_2)-g(r_2,c_1-1)-g(r_1-1,c_2)+g(r_1-1,c_1-1)\]

If we can quickly calculate \ (g \) is to solve the problem.

We now consider how to use \ (f (r, \ cdots ) \) to obtain \ (f (r + 1, c) \) , we have

\[f(r+1,c)-f(r,c)=C_{r+1+c}^{r+1}-C_{r+c}^r=\frac{(r+c+1)!}{(r+1)!c!}-\frac{(r+c)!}{r!c!}=\frac{(r+c+1)!-(r+c)!(r+1)}{(r+1)!c!}=\frac{(r+c)![(r+c+1)-(r+1)]}{(r+1)!c!}=\frac{(r+c)!}{(r+1)!(c-1)!}=f(r+1,c-1)\]

In other words, we have a recursive

\[f(r+1,c)=f(r,c)+f(r+1,c-1)=f(r,c)+f(r,c-1)+f(r+1,c-2)=\cdots=f(r,c)+f(r,c-1)+f(r,c-2)+\cdots+f(r,0)=\sum_{i=0}^cf(r,i)\]

Then

\[g(r,c)=\sum_{i=0}^r\sum_{j=0}^c f(i,j)=\sum_{i=0}^rf(i+1,c)=\sum_{i=0}^rf(c,i+1)=f(c+1,r+1)-1\]

Then the code is very simple.

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

typedef long long ll;
const int mod = 1e+9 + 7;

ll r1, r2, c1, c2;

ll qPow(ll a, ll b)
{
    a %= mod;
    
    ll res = 1;
    while(b) {
        if(b & 1) res = res * a % mod;
        a = a * a % mod, b >>= 1;
    }
    return res;
}

ll C(ll n, ll m)
{
    if(n < m) return 0;
    
    ll res = 1;
    for(int i = 1; i <= m; ++i)
        res = res * (n - i + 1) % mod * qPow(i, mod - 2) % mod;
    return res;
}

int main()
{
    scanf("%lld%lld%lld%lld", &r1, &c1, &r2, &c2);
    
    ++r2, ++c2;
    printf("%lld", ((C(r2 + c2, c2) - C(r1 + c2, c2) - C(r2 + c1, c1) + C(r1 + c1, c1)) % mod + mod) % mod);
}

Guess you like

Origin www.cnblogs.com/whx1003/p/12302084.html