Second My Problem First 单调队列

本文介绍使用单调队列解决区间最小值问题的方法。通过维护一个单调递增的队列,快速找到指定区间内的最小值,并应用于计算特定数列乘积的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Second My Problem First 单调队列

暑假训练第一天

题目:
Give you three integers n, A and B.
Then we define S i = A i mod B and T i = Min{ S k | i-A <= k <= i, k >= 1}
Your task is to calculate the product of T i (1 <= i <= n) mod B.
输入
Each line will contain three integers n(1 <= n <= 10 7),A and B(1 <= A, B <= 2 31-1).
Process to end of file.
输出
For each case, output the answer in a single line.
样例输入
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
样例输出
2
3
4
5
6

题目大意:Si=a^i%b,Ti是i-A到i这个范围内S的最小值,求(T1T2T3…….Tn)%B的值。
求区间最小值

题解是手写单调队列,结果发现deque也能做,虽然感觉会超*,但并没有。

#include<bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull; // %llu
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = -1u>>1;
const int maxn = 1e7+10;
struct node
{
    int x,num;
};
int main()
{
    int n,a,b;
    while(cin>>n>>a>>b)
    {
        deque<node>stl;
        ll tmp=1,ans=1;
        for(int i=1; i<=n; i++)
        {
            tmp=tmp*a%b;
            while(!stl.empty()&&stl.back().x>=tmp)      //维护队尾符合单调递增
                stl.pop_back();
            stl.push_back(node{tmp,i});
            while(i-a>stl.front().num)          //维护队首大于等于i-a
                stl.pop_front();
            ans*=stl.front().x%b;           //此时队首元素为此区间最小值
        }
        cout<<ans<<"\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值