HDU3706 Second My Problem First

看似一道数论题,是道比较明显的单调队列题。

代码
#include <iostream>
#include
<string>
#include
<stdio.h>
#include
<stdlib.h>
#include
<algorithm>
#include
<string.h>
#include
<vector>
#include
<math.h>
#include
<map>
#include
<time.h>
#include
<queue>
#include
<set>
using namespace std;

typedef __int64 int64;

const int MAX = 1e7 + 5;

int n, A, B, h, t;
int S[MAX];
int q[MAX][2];

void ready()
{
S[
0] = 1;
for(int i = 1; i <= n; i++)
{
S[i]
= (int64)(S[i - 1]) * A % B;
}

//init the queue
h = 0, t = 0;
}

void insert(int key, int value)
{
while(h < t && value <= q[t - 1][1]) t--;
q[t][
0] = key;
q[t
++][1] = value;
}

int64 go()
{
int64 res
= 1;
for(int i = 1; i <= n; i++)
{
//printf("@%d %d\n", i, S[i]);
insert(i, S[i]);
if(i - q[h][0] > A) h++;
res
= res * q[h][1] % B;
}
return res;
}

int main()
{
while(scanf("%d%d%d", &n, &A, &B) != EOF)
{
ready();
printf(
"%I64d\n", go());
}
}
原文地址:https://www.cnblogs.com/litstrong/p/1866875.html