HDU 5637 Transform 搜索

题意:bc round 74 div1 1002 中文题

分析(官方题解):注意到答案实际上只和st有关, bfs预处理下从0到xx的最短步数, 然后查询O(1)回答即可.

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
const int N=2e5+5;
vector<int>a;
queue<int>q;
int p[N];
int main()
{
    int T,n,m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        a.clear();
        for(int i=0;i<n;++i)
        {
            int tmp;
            scanf("%d",&tmp);
            a.push_back(tmp);
        }
        int l=log2(N);
        l++;
        for(int i=0;i<=l;++i)
           a.push_back((1<<i));
        memset(p,-1,sizeof(p));
        q.push(0);
        p[0]=0;
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=0;i<a.size();++i)
            {
                int y=(x^a[i]);
                if(y>N-5||p[y]!=-1)continue;
                p[y]=p[x]+1;
                q.push(y);
            }
        }
        LL ans=0;
        for(int i=1;i<=m;++i)
        {
            int s,t;
            scanf("%d%d",&s,&t);
            LL x=i,y=p[s^t];
            ans=(ans+x*y%mod)%mod;
        }
        printf("%I64d
",ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/shuguangzw/p/5246696.html