hdoj 5637 Transform

本文探讨了使用深度优先搜索(DFS)和位操作来解决计算通过异或和反转位最少需要多少步骤的问题。通过预计算得到的位集和最少操作次数表,实现快速查询。

题目

转化一下,可以理解为用n个数异或,和修改位,去凑出s异或t的值。我们可以先用dfs计算n个数互相异或能得到哪些数,并且最少需要其中的多少个数来异或(也就是需要多少次操作),然后推出范围内的数通过异或和反转位,最少需要多少步。不过更科学的方法应该是用bfs求这些最短距离(最少操作次数)。对于询问O(1)查表回答即可。

#include <iostream>  
#include <stdio.h>  
#include <cmath>  
#include <algorithm>  
#include <string>  
#include <memory.h>  
#include <vector>  
#include <queue>  
#include <stack> 

using namespace std;

#define ll long long


const ll mod = 1e9+7;

int a[20];

int tab[131072+10];

int N,m;

void dfs(int n,int k,int cur){
    if(n>N)return;
    
    if(tab[cur^a[n]]+1<tab[cur]){
        tab[cur] = tab[cur^a[n]]+1;
    }
    dfs(n+1,k,cur);
    dfs(n+1,k+1,cur^a[n+1]);
}

int main(){
    int t;
    cin>>t;
    while(t--){
        for(int i=0;i<131072;i++){
            tab[i]=1000000000;
        }
        tab[0]=0;
        cin>>N>>m;

        for(int i=1;i<=N;i++){
            cin>>a[i];
        }
        
        dfs(0,0,0);
        
        for(int i=131072-1;i>0;i--){
            int tmp=1;
            while(tmp<131072){
                if(i&tmp){
                    if(tab[i] > tab[i-tmp]+1){
                        tab[i] = tab[i-tmp]+1;
                    }
                }else{
                    if(tab[i] > tab[i+tmp]+1){
                        tab[i] = tab[i+tmp]+1;
                    }
                }
                tmp<<=1;
            }
        }
  
        for(int i=1;i<131072;i++){
            int tmp=1;
            while(tmp<131072){
                if(i&tmp){
                    if(tab[i] > tab[i-tmp]+1){
                        tab[i] = tab[i-tmp]+1;
                    }
                }else{
                    if(tab[i] > tab[i+tmp]+1){
                        tab[i] = tab[i+tmp]+1;
                    }
                }
                tmp<<=1;
            }
        }

        ll ans = 0;
        for(int i=1;i<=m;i++){
            int s,t;
            scanf("%d%d",&s,&t);
            ll cur = tab[s^t];
            cur*=i;
            ans+=cur;
            ans%=mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值