B - Security Guards Gym - 101954B[预处理BFS]

题意:在二维平面内有n个消防站,现在有Q次询问,每次询问给出一个坐标代表事故现场的地点,问距离该点最近的消防站的距离是多少。
距离的定义:一个点到他周围八个方向的点的距离都是1.


题解:我们先把所有消防站的点压入一个队列中去,然后进行BFS,通过BFS记录到达某个点的最短距离,最先搜到的一定是最短距离。


a c   c o d e : ac\ code: ac code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
const int maxn = 5500 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return f * x;
}

ll qp(ll base, ll n) {
    ll res = 1;
    while(n) {
        if(n & 1) res = (res * base) % mod;
        base = (base * base) % mod;
        n >>= 1;
    }
    return res;
}

ll inv[maxn], fac[maxn];

void init() {
    fac[0] = 1;
    rep(i, 1, maxn - 1) {
        fac[i] = fac[i - 1] * i % mod;
    }

    inv[maxn - 1] = qp(fac[maxn - 1], mod - 2);

    per(i, maxn - 2, 0) {
        inv[i] = inv[i + 1] * (i + 1) % mod;
        //printf("%lld %lld\n", inv[i], qp(fac[i], mod - 2));
    }

}

ll C(ll n, ll r) {
    if(n < r ||r < 0) return 0;
    return fac[n] * inv[r] % mod * inv[n - r] % mod;
}

char s[maxn];
int main() {
    init();
    int n, k;
    while(~scanf("%d%d", &n, &k)) {
        scanf("%s", s);
        if(n == 1) {
            if(k == 0) {
                if(s[0] == '1') puts("0");
                else puts("1");
            } else {
                if(s[0] == '1') puts("2");
                else puts("1");
            }
        } else {
            ll ans = 0;

            int x = k;

            rep(i, 0, n - 1) {
                if(s[i] == '0') {
                    if(x > 0)
                        ans = (ans + C(n - i - 1, x - 1)) % mod;
                } else {
                    x--;
                }
            }
            if(x == 0) ans++;

            x = k - 1;

            rep(i, 0, n - 1) {
                if(s[i] == '1') {
                    if(x >= 0)
                        ans = (ans + C(n - i - 1, x)) % mod;
                    x--;
                }
            }

            printf("%lld\n", ans % mod);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值