HDOJ 5th Anniversary Contest 1007 Second My Problem First 单调队列

HDOJ 5th Anniversary Contest 1007 Second My Problem First 单调队列

Problem Description
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.
 


 

Input
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.


用单调队列来维护最小值即可

 1 // file name:1007.c
 2 // author:yzhw_ujs
 3 // problem: hdu contest 10.30 1007
 4 // method 单调队列+线扫 
 5
 6
 7 # include  < stdio.h >
 8 struct  node
 9 {
10   int pos,value;
11}
q[ 10000005 ];
12 int  s,e;
13 int  main()
14 {
15    int n,a,b;
16    while(scanf("%d%d%d",&n,&a,&b)!=EOF)
17    {
18      int total=a%b,t=a%b,i;
19      s=e=-1;
20      e++;
21      q[e].pos=1;
22      q[e].value=t;
23      for(i=2;i<=n;i++)
24      {
25          t=((long long)t*(a%b))%b;
26          while(e!=s&&q[e].value>=t) e--;
27          q[++e].pos=i;
28          q[e].value=t;
29          while(s!=e&&q[s+1].pos<i-a) s++;
30          total=((long long)total*(q[s+1].value%b))%b;
31      }

32      printf("%d\n",total);
33    }

34    return 0;
35}

36

你可能感兴趣的:(HDOJ 5th Anniversary Contest 1007 Second My Problem First 单调队列)