ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

jzoj 6271. 2019.8.4【NOIP提高组A】锻造 (forging)

2019-08-14 10:01:20  阅读:239  来源: 互联网

标签:6271 2019.8 NOIP read mo ny int ll jc


Description

详见OJ

Solution

这题看题目就知道是期望\(DP\)了。
先刚了2h\(DP\)式,得到\(f[i]=f[i-1]+f[i-2]+f[i-1]*(1-p)+...\),然后不会化简,最后崩盘。
正解也是设f[i]表示生成第i级的剑的期望费用。
可以得到\(f[i] = f[i - 1] + f[i - 2] + (1 - p) * (f[i] - f[i - 2])\)
解方程即可,得到:
\[f[i] = f[i - 2] + f[i - 1] * (k/c[i - 1])\](\(k\)为题目所给的\(k\))
要用逆元(考场逆元直接用成了\(/(k!)\),而需要的是\(/k\)。。。)
\[f[i] = f[i - 2] + f[i - 1] * k * ny[c[i - 1]] * jc[c[i - 1] - 1]\]
此题需要卡常!!!

Code


#include <cstdio>
#include <algorithm>
#define N 10000010
#define ll long long
#define mo 998244353
#define fo(x, a, b) for (register int x = a; x <= b; x++)
#define fd(x, a, b) for (register int x = a; x >= b; x--)
using namespace std;
const int maxn = 10000000;
int n, A, bx, by, cx, cy, p, k;
int b[N], c[N], f[N], jc[N], ny[N];

inline int read()
{
    int x = 0; char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return x;
}

int ksm(int x, int y)
{
    int s = 1;
    while (y)
    {
        if (y & 1) s = (ll)s * x % mo;
        x = (ll)x * x % mo; y >>= 1;
    }
    return s;
}

void ycl()
{
    jc[0] = jc[1] = 1;
    fo(i, 2, maxn) jc[i] = (ll)jc[i - 1] * i % mo;
    ny[maxn] = ksm(jc[maxn], mo - 2);
    fd(i, maxn - 1, 1) ny[i] = (ll)ny[i + 1] * (i + 1) % mo;
}

int main()
{
    freopen("forging.in", "r", stdin);
    freopen("forging.out", "w", stdout);
    n = read(), A = read();
    if (n == 0) {printf("%d\n", A); return 0;} ycl();
    bx = read(), by = read(), cx = read(), cy = read(), p = read();
    b[0] = by + 1, c[0] = cy + 1;
    fo(i, 1, n - 1)
    {
        b[i] = ((ll)b[i - 1] * bx + by) % p + 1;
        c[i] = ((ll)c[i - 1] * cx + cy) % p + 1;
    }
    f[0] = A; k = std::min(c[0], b[0]);
    f[1] = (f[0] + (ll)f[0] * c[0] % mo * ny[k] % mo * jc[k - 1] % mo) % mo;
    fo(i, 2, n)
    {
        k = std::min(c[i - 1], b[i - 2]);
        f[i] = (f[i - 2] + (ll)f[i - 1] * c[i - 1] % mo * ny[k] % mo * jc[k - 1] % mo) % mo;
    }
    printf("%d\n", f[n]);
    return 0;
}

标签:6271,2019.8,NOIP,read,mo,ny,int,ll,jc
来源: https://blog.csdn.net/Larry1118/article/details/98552766

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有