HDU 3706 Second My Problem First 【单调队列】

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3706
【原题】
Second My Problem First

Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1955 Accepted Submission(s): 743

Problem Description
Give you three integers n, A and B.
Then we define Si = Ai mod B and Ti = Min{ Sk | i-A <= k <= i, k >= 1}
Your task is to calculate the product of Ti (1 <= i <= n) mod B.

Input
Each line will contain three integers n(1 <= n <= 107),A and B(1 <= A, B <= 231-1).
Process to end of file.

Output
For each case, output the answer in a single line.

Sample Input
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7

Sample Output
2
3
4
5
6

Author
WhereIsHeroFrom@HDU
【中文题意】
给你三个整数n,A,B。让你求出T[i]之积,1<=i<=n,T[i]是这么求的,T[i]=min(s[i-A]~s[i]),s[i]=A^i。
反正怎么看原文怎么感觉别扭。
【思路分析】
首先s[i]是很好求的,然后我们看n的数据范围,10^7,灰常大啊,这个时候我们只能考虑线性的做法,虽然rmq区间最小值查询的时间复杂度为O(nlog(n)),但是对这个题行不通….所以我们只能用单调队列来维护区间最小值,时间复杂度为O(n)。另外一个比较烦人的地方是这个题卡内存,用long long 数组的话是爆内存的,所以只能使用int 。
【AC代码】

#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAX_N 10000005
#define LL long long

LL n,A,B;
int ss[MAX_N];
int deq[MAX_N];//双端队列 存储的是ss数组中元素的位置

void solve()
{
    int s=0,t=0;//双端队列的头部和尾部
    long long re=1;
    ss[0]=1;
    for (int i=1; i<=n; i++)
    {
        ss[i] = (int)(((long long)ss[i-1] * A) % B);
    }
    for(int i=1; i<=n; i++)
    {
        while(s<t&&deq[s]<i-A)
            s++;
        while(s<t&&ss[deq[t-1]] >= ss[i]) t--;//双端队列的尾部一直大于当前值,需要不断删除尾部
        deq[t++]=i;
        //printf("deq[s]= %lld\n",deq[s]);
        re*=(long long)ss[deq[s]];
        re%=B;
    }
    printf("%lld\n",re);
}

int main()
{
    while(~scanf("%lld %lld %lld",&n,&A,&B))
    {
        solve();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值