Bzoj4872: [Shoi2017]分手是祝愿

题面

Bzoj

Sol

首先从大向小,能关就关显然是最优
然后
\(f[i]\)表示剩下最优要按i个开关的期望步数,倒推过来就是
\[ f[i]=f[i-1]*i*inv[n]+f[i+1]*(n-i)*inv[n]+1 \]
\(inv\)表示逆元
\(g[i]=f[i]-f[i-1]\)
那么上式变为
\[ \sum_{j=1}^{i}g[i]=\sum_{j=1}^{i-1}g[i] *i*inv[n]+\sum_{j=1}^{i+1}g[i]*(n-i)*inv[n]+1 \]
化简
\[ g[i]=(g[i]+g[i+1])*(n-i)*inv[n]+1\\ g[i]=((n-i)*g[i+1]+n)*inv[i]\\ \]
边界\(g[n+1]=0\)
最后就记个前缀和就好了

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);
const int Zsy(100003);

IL ll Input(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, k, step, facn = 1, inv[_], f[_], ans;
bool sta[_], nxt[_];

IL void Up(RG int &x, RG int y){
    x += y;
    if(x >= Zsy) x -= Zsy;
}

int main(RG int argc, RG char* argv[]){
    n = Input(); k = Input();
    for(RG int i = 1; i <= n; ++i) facn = 1LL * facn * i % Zsy, sta[i] = Input();
    for(RG int i = n; i; --i){
        RG bool g = sta[i];
        for(RG int j = i + i; j <= n; j += i) g ^= nxt[j];
        if(g) nxt[i] = 1, ++step;
    }
    if(step <= k) return printf("%lld\n", 1LL * facn * step % Zsy), 0;
    inv[1] = 1;
    for(RG int i = 2; i <= n; ++i) inv[i] = (-1LL * (Zsy / i) * inv[Zsy % i] % Zsy + Zsy) % Zsy;
    for(RG int i = 1; i <= k; ++i) f[i] = 1;
    for(RG int i = n; i > k; --i) f[i] = 1LL * inv[i] * (1LL * (n - i) * f[i + 1] % Zsy + n) % Zsy;
    for(RG int i = 1; i <= step; ++i) Up(ans, f[i]);
    printf("%lld\n", 1LL * facn * ans % Zsy);
    return 0;
}

转载于:https://www.cnblogs.com/cjoieryl/p/8426049.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值