hdu 5063 Operation the Sequence

http://acm.hdu.edu.cn/showproblem.php?pid=5063

思路:因为3查询最多50,所以可以在查询的时候逆操作找到原来的位置,然后再求查询的值。

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cstring>

 4 #include <algorithm>

 5 #define ll long long

 6 using namespace std;

 7 const int mod=1000000007;

 8 

 9 int n,m;

10 char str[1000];

11 int a[100010];

12 

13 ll pow_mod(ll a,ll p,ll n)

14 {

15     if(p==0) return 1;

16     ll ans=pow_mod(a,p/2,n);

17     ans=ans*ans%n;

18     if(p%2==1) ans=ans*a%n;

19     return ans;

20 }

21 

22 

23 int main()

24 {

25     int t;

26     scanf("%d",&t);

27     while(t--)

28     {

29         memset(a,0,sizeof(a));

30         scanf("%d%d",&n,&m);

31         getchar();

32         char op;

33         int cnt=0;

34         int pos=0;

35         int mid=(n+1)/2;

36         int x;

37         for(int i=1; i<=m; i++)

38         {

39             scanf("%c",&op);

40             if(op=='O')

41             {

42                 scanf("%d",&x);

43                 a[i]=x;

44             }

45             else if(op=='Q')

46             {

47                 scanf("%d",&x);

48                 pos=x;

49                 int c=1;

50                 for(int j=i-1; j>=1; j--)

51                 {

52                     if(a[j]==1)

53                     {

54                         if(pos<=mid)

55                             pos=pos*2-1;

56                         else

57                             pos=(pos-mid)*2;

58                     }

59                     else if(a[j]==2)

60                     {

61                         pos=n-pos+1;

62                     }

63                     else if(a[j]==3)

64                     {

65                        c=(c+c)%(mod-1);

66                     }

67                 }

68                 printf("%d\n",pow_mod(pos,c,mod));

69             }

70             getchar();

71         }

72     }

73     return 0;

74 }
View Code

 

你可能感兴趣的:(sequence)