洛谷题单指南-组合数学与计数-P3228 [HNOI2013] 数列

原题链接:https://www.luogu.com.cn/problem/P3228

题意解读:K天连续上涨的股票,最高不超过N元,每天涨幅最多M元,求K天的价格组合一共有多少种可能。

解题思路:

每天涨幅为1~M,不妨设di表示第i+1天和第i天的估价之差

对于一组可能的差值d1~dk-1,只要确定第一天的股价,就可以得到一组价格

而第一天的股价必然满足image,也就是确定一组差值,可以有image组可能的价格

那么,通过枚举每一种di即可得到答案:

image

快速幂即可求解。

100分代码:

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

LL n, k, m, p, ans;

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

int main()
{
    cin >> n >> k >> m >> p;
    //注意n一开始就要%p,防止后续乘法溢出
    ans = n % p * ksm(m, k - 1, p) % p - ksm(m, k - 2, p) * (k - 1) % p * (m * (m + 1) / 2 % p) % p;
    ans = (ans % p + p) % p;
    cout << ans;
    return 0;
}

 

posted @ 2025-12-17 15:01  hackerchef  阅读(3)  评论(0)    收藏  举报