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;
}