HDU5063 Operation the Sequence(逆向思想)

Operation the Sequence

题目链接:

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

解题思路:

BestCoder官方题解:

注意到查询次数不超过50次,那么可以从查询位置逆回去操作,就可以发现它在最初序列的位置,再逆回去即可求得当前查询的

值,对于一组数据复杂度约为O(50*n)。

注意到题目的这句话,Type4: Q i query current value of a[i], this operator will have at most 50.然后就知道可以直接用暴力求解了。

AC代码:

#include <iostream>
#include <cstdio>
#define MOD 1000000007
using namespace std;

typedef long long ll;
int n,m,ans;
int op[100010];

int fun1(int x){  //题目第一个函数的反函数
    int odd = n>>1;
    if(n&1)
        odd++;
    if(x <= odd)
        return 2*x-1;
    else
        return 2*(x-odd);
}

int fun2(int x){ //题目第二个函数的反函数
    return n-(x-1);
}

ll solve(ll x){
    int t = 0;
    for(int i = ans-1; i >= 0; i--){
        if(op[i] == 1)
            x = fun1(x);
        else if(op[i] == 2)
            x = fun2(x);
        else
            t++;
    }
    for(int i = 1; i <= t; i++)
        x = x*x%MOD;      //题目第三个函数
    return x;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ans = 0;
        cin>>n>>m;
        for(int i = 0; i < m; i++){
            char c;
            int d;
            cin>>c>>d;
            if(c=='O')
                op[ans++] = d;
            else
                cout<<solve((ll)d)<<endl;
        }
    }
    return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值