[AGC028B] Removing Blocks(前缀和+计数)

· · 个人记录

题意

这个题应该第一眼看过去应该会考虑算每个数的贡献,但是这个东西算起来也比较麻烦,因为我们要求的是所有情况,所以考虑算概率。对于两个位置i, j(i <= j)我们来考虑i在j消除被算进答案的概率,那么这个概率就是\displaystyle\frac{1}{j - i + 1},因为只有j是这个区间第一个消除的时候,i的权值才会被算入答案 消区间外面的块的时候是不会影响的,那么我们就得到了一个O(n^2)的做法 ,枚举两个位置计算贡献。但是这样还是过不去,我们发现有很多重复计算,我们预处理逆元的前缀和 然后就可以O(1)算一个数的贡献答案,直接分小于等于它和大于它的两种方法计算即可,这样子做的总复杂度是O(n)的。

#include<bits/stdc++.h>
#include<bits/extc++.h>

#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define go(x, i) for(register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define FOR(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define debug(x) cout << #x << " = " << x << endl
#define mem(a, b) memset(a, b, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(a))
#define inf (0x3f3f3f3f)
#define INF (1e18)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define y1 orzorz

typedef unsigned long long ull;
typedef unsigned int uint;
typedef long long ll;
typedef std::pair<ll, int> PLI;
typedef std::pair<int, int> PII;
typedef long double ldb;
typedef double db;

namespace IO {
#define getc() ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), S_ == T_) ? 0 : *S_ ++)
#define putc(x) *nowps ++ = (x)

    const uint Buffsize = 1 << 15, Output = 1 << 23;
    static char Ch_[Buffsize], *S_ = Ch_, *T_ = Ch_;
    static char Out[Output], *nowps = Out;

    inline void flush() {fwrite(Out, 1, nowps - Out, stdout); nowps = Out;}
    template<class T>inline bool chkmax(T &_, T __) {return _ < __ ? _ = __, 1 : 0;}
    template<class T>inline bool chkmin(T &_, T __) {return _ > __ ? _ = __, 1 : 0;}

    template<class T>inline void read(T &_) {
        _ = 0; static char __; T ___ = 1;
        for(__ = getc(); !isdigit(__); __ = getc()) if(__ == '-') ___ = -1;
        for(; isdigit(__); __ = getc()) _ = (_ << 3) + (_ << 1) + (__ ^ 48);
        _ *= ___;
    }

    template<class T>inline void write(T _, char __ = '\n') {
        if(!_) putc('0');
        if(_ < 0) putc('-'), _ = -_;
        static uint sta[111], tp;
        for(tp = 0; _; _ /= 10) sta[++ tp] = _ % 10;
        for(; tp; putc(sta[tp --] ^ 48)); putc(__);
    }

    inline void procStatus() {
        std::ifstream t("/proc/self/status");
        std::cerr << std::string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
    }
}

using namespace std;
using namespace IO;

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int f[N], inv[N], a[N], n, ans;

int main() {
#ifdef ylsakioi
    file("agc028");
#endif

    read(n);
    inv[1] = 1;
    For(i, 2, n) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
    For(i, 1, n) read(a[i]), f[i] = (f[i - 1] + inv[i]) % mod;
    For(i, 1, n) (ans += 1ll * a[i] * (f[i] - inv[1] + f[n - i + 1]) % mod) %= mod;
    //For(i, 1, n) For(j, i, n) (ans += 1ll * a[i] * inv[j - i + 1] % mod) %= mod;
    //For(i, 1, n) For(j, 1, i - 1) (ans += 1ll * a[i] * inv[i - j + 1] % mod) %= mod;
    For(i, 1, n) ans = 1ll * i * ans % mod;

    write(ans);

    return flush(), 0;
}