牛客网-数的变换

链接:https://ac.nowcoder.com/acm/contest/52/C

题目描述

给定一个序列数列

, (ai互不相同)

3种映射关系

现在对于给定询问(x, k),求

输入描述:

多组读入数据T,
对于每组数据,
第1行一个整数n,q,n表示数列的大小,q为询问数
第2行读入n个数a1,a2,…,an,表示数列中的数
接下来q行,每行读入2个整数(x,k)如题所示

输出描述:

对于每个询问,输出并换行

示例1

输入

2
3 1
4 3 2
3 4
5 2
1 2 3 1234567 4
1234567 6
3 8

输出

4
1
2

备注

T≤10,

对于每组数据,

1≤n,q≤5×104,

1≤ai≤109,

n≤k≤105,

x为数列A中的一个元素, ai互不相同。

思路:打表到N的阶乘,先求出G(x)的值利用公式求出的值然后快速幂+逆元求出它的值。

代码:

#include <bits/stdc++.h>
using namespace std;
const long long maxn=1e6+10;
const long long mod=1e9+7;
long long a[maxn];
map<long long ,long long> mp;
long long po[maxn];
long long quli(long long a,long long b)
{
    a=a%mod;
    long long res=1;
    while(b)
    {
        if(b&1)
        {
            res = (res*a)%mod;
        }
        b/=2;
        a=(a*a)%mod;
    }
    return res;
}
void playtable()
{
    po[0]=1;
    po[1]=1;
    for(int i=2;i<=maxn;i++)
    {
        po[i]=po[i-1]*i;
        po[i]=po[i]%mod;
    }
}
int main()
{
    playtable();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        mp.clear();
        long long n,q;
        scanf("%lld%lld",&n,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            mp[a[i]]=i;
        }
        while(q--)
        {
            long long x,k;
            scanf("%lld%lld",&x,&k);
            long long p=mp[x];
		    long long num=(po[k]*quli((po[p]*po[k-p]),mod-2));
		    num=(num%mod)%n+1;
		    cout<<a[num]<<endl;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/HTallperson/article/details/83866330