Second My Problem First
Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2362 Accepted Submission(s): 910
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
Source
Recommend
lcy
题目
单调队列模板题
需要注意的时时间和空间限制。
单调队列使用的是stl的deque
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e7+10;
int n;
ll A,B;
deque<pair<int,ll> >qu;
int main()
{
while(~scanf("%d%lld%lld",&n,&A,&B)) {
qu.clear();
ll res = 1,tmp = 1;
for(int i=1;i<=n;i++) {
tmp = tmp * A % B;
while(!qu.empty()&& tmp <= qu.back().second) qu.pop_back();
qu.push_back(make_pair(i,tmp));
if(i > A+1) {
while(!qu.empty() && qu.front().first < i - A) qu.pop_front();
}
res = res * qu.front().second % B;
}
printf("%lld\n",res);
}
return 0;
}