码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5638 Transform 搜索

时间:2016-03-06 11:18:50      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

题意: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\n",ans);
    }
    return 0;
}
View Code

 

HDU 5638 Transform 搜索

标签:

原文地址:http://www.cnblogs.com/shuguangzw/p/5246696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!