1. 程式人生 > >Second My Problem First HDU -單調佇列

Second My Problem First HDU -單調佇列

  • 單調佇列維護固定長度的區間的最小值即可。
  • #include<bits/stdc++.h>
    using namespace std;
    #define maxn 11111111
    #define ll long long
    int L, n,a,b;
    ll ans,num;
    deque<pair<int,ll> >minn;
    int main()
    {
        while(~scanf("%d%d%d",&n,&a,&b))
        {
            ans=num=1;
            while(!minn.empty())
                minn.pop_back();
            for(int i=1; i<=n; i++)
            {
                L=max(1,i-a);
                num=(num*a)%b;
                while(!minn.empty()&&num<minn.back().second)
                    minn.pop_back();
                minn.push_back(make_pair(i,num));
                while(!minn.empty()&&L>minn.front().first)
                    minn.pop_front();
                ans=ans*minn.front().second%b;
            }
            printf("%lld\n",ans);
        }
        return 0;
    }