hdu 5063 Operation the Sequence (数学公式逆推)

本文介绍了一个涉及数组操作的问题,包括四种类型的操作:调整数组元素顺序、反转数组、平方数组元素及查询特定位置的元素值。文章详细解析了如何通过逆向思维解决查询问题,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Operation the Sequence

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 607    Accepted Submission(s): 227


Problem Description
You have an array consisting of n integers: a1=1,a2=2,a3=3,,an=n . Then give you m operators, you should process all the operators in order. Each operator is one of four types:
Type1: O 1 call fun1();
Type2: O 2 call fun2();
Type3: O 3 call fun3();
Type4: Q i query current value of a[i], this operator will have at most 50.
Global Variables: a[1…n],b[1…n];
fun1() {
index=1;
  for(i=1; i<=n; i +=2)
    b[index++]=a[i];
  for(i=2; i<=n; i +=2)
    b[index++]=a[i];
  for(i=1; i<=n; ++i)
    a[i]=b[i];
}
fun2() {
  L = 1;R = n;
  while(L<R) {
    Swap(a[L], a[R]);
    ++L;--R;
  }
}
fun3() {
  for(i=1; i<=n; ++i)
    a[i]=a[i]*a[i];
}
 

Input
The first line in the input file is an integer T(1T20) , indicating the number of test cases.
The first line of each test case contains two integer n(0<n100000) , m(0<m100000) .
Then m lines follow, each line represent an operator above.
 

Output
For each test case, output the query values, the values may be so large, you just output the values mod 1000000007(1e9+7).
 

Sample Input
  
1 3 5 O 1 O 2 Q 1 O 3 Q 1
 

Sample Output
  
2 4
 

Source
BestCoder Round #13
题目大意:给出四种操作,第一种是将奇数序的数提前,偶数序的数滞后,第二种是前后对称互换位置,第三种是每一位求取平方

题目分析:因为查询次数少,所以只要在每次查询,根据逆推公式得到最初没经过变化的位置,然后根据记录的次数取平方(注意,不是求n次方,别理解错)
逆推公式就是简单的数学换算,对于操作1的要分奇偶和前后位置分类讨论,具体见代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MOD 1000000007
#define MAX 200007

using namespace std;

typedef long long LL;
LL a[MAX];
int p,cnt1,cnt2;

int t,n,m;

int bf1 ( int i )
{
    if ( i > (n+1)/2 )
    {
        int temp = i*2-n-1;
        if ( n&1 ) return temp;
        else return temp+1;
    }
    else return i*2-1;
}

int bf2 ( int i )
{
    return n+1-i;
}

void f3 ( )
{
    p++;
}

LL ans[107];
char s[5];
int q[MAX],x;

int query ( int x , int cnt )
{
    for ( int i = cnt-1 ; i >= 0 ; i-- )
        if ( q[i] == 1 ) x = bf1(x);
        else x = bf2(x);
    return x;
}

int main ( )
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        
        p = cnt1 = cnt2 = 0;
        scanf ( "%d%d" , &n , &m );
        for ( int i = 1 ; i <= n ; i++ )
            a[i] = i;
        for ( int i = 1 ; i <= m ; i++ )
        {
            scanf ( "%s" , s );
            scanf ( "%d" , &x );
            if ( s[0] == 'O' )
            {
                if ( x == 3 ) f3( );
                else q[cnt1++] = x;
            }
            else 
            {
                int id = query ( x , cnt1 );
                LL tx = a[id];
                //cout << tx << endl;
                for ( int i = 0 ; i < p ; i++ )
                    tx *= tx , tx %= MOD;
                ans[cnt2++] = tx;
            }
        }
      for ( int i = 0 ; i < cnt2 ; i++ )
        printf ( "%I64d\n" , ans[i] );  
    }
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值