HUD 3706 monotonous queue simple question
Problem DescriptionGive 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. I don't want to describe the meaning of the question. The three lines are clear in English. Today I just learned the monotonous queue. I simulated this monotonous queue 1 and kept the first element of the team as the minimum value. 2. When inserting a video, delete 3 from the element whose tail is greater than the inserted value to ensure that the first element is valid and required. Code # include
# Include
Int tou, wei, count;
Struct cc
{
_ Int64 value;
Int num;
} Mark [2, 10000005];
Void add (int x, int k)
{
If (k = 1) return;
If (tou = wei) {mark [tou]. value = x; mark [wei]. num = k; return ;}
If (mark [tou]. value> x) {mark [tou]. value = x; mark [tou]. num = k; wei = tou + 1; return ;}
For (int I = wei-1; I> = tou; I --)
{
If (mark [I]. value <= x)
{
Mark [I + 1]. value = x;
Mark [I + 1]. num = k;
Wei = I + 2;
Break;
}
}
For (int I = tou; I <wei; I ++)
{
If (mark [I]. num <count)
{
Tou ++;
}
Else break;
}
}
Int main ()
{
Int n, a, B;
_ Int64 k;
_ Int64 sum, ji;
While (scanf ("% d", & n, & a, & B )! = EOF)
{
Count =-1, ji = 1;
Mark [0]. value = a % B;
Mark [0]. num = 1;
Tou = 0, wei = 1, k = 1;
For (int I = 1; I <= n; I ++)
{
K = k * a % B;
Count = I-;
Add (k, I );
Ji * = mark [tou]. value;
If (ji> = B) ji % = B;
}
Printf ("% I64d \ n", ji );
}
}