HDU - 5637
Transform
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 918 Accepted Submission(s): 354
Problem Description
A list of n integers are given. For an integer x you can do the following operations:
+ let the binary representation of x be b31b30...b0¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯, you can flip one of the bits.
+ let y be an integer in the list, you can change x to x⊕y, where ⊕ means bitwise exclusive or operation.
There are several integer pairs (S,T). For each pair, you need to answer the minimum operations needed to change S to T.
Input
There are multiple test cases. The first line of input contains an integer T (T≤20), indicating the number of test cases. For each test case:
The first line contains two integer n and m (1≤n≤15,1≤m≤105) -- the number of integers given and the number of queries. The next line contains n integers a1,a2,...,an (1≤ai≤105), separated by a space.
In the next m lines, each contains two integers si and ti (1≤si,ti≤105), denoting a query.
Output
For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7), where zi is the answer for i-th query.
Sample Input
1
3 3
1 2 3
3 4
1 2
3 9
Sample Output
10
Hint
$3 \to 4$ (2 operations): $3 \to 7 \to 4$
$1 \to 2$ (1 operation): $1 \oplus 3 = 2$
$3 \to 9$ (2 operations): $3 \to 1 \to 9$
题意:
给出n个数的序列a,对于一个整数x,有两种操作:
1.改变x二进制中任一位
2.将x变为x^a[i]
m次查询,每次查询输入两个整数x和y,问x最少经过多少次操作可以变成y
思路:因为求s^(t[1]^t[2]^...t[j])=t。那么转化成(t[1]^t[2]^...t[j])=s^t,那么就直接求0到s^t最少步数。
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <time.h>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define PI acos(-1)
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;
const long long mod=1e9+7;
struct node
{
int x,step;
};
int dis[1000001];
int a[20];
void bfs(int n)
{
memset(dis,-1,sizeof(dis));
dis[0] = 0;
queue<node>q;
node cur,nex;
cur.x=cur.step=0;
q.push(cur);
int x=(1<<17);
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=0;i<n;i++)
{
nex.x=cur.x^a[i];
nex.step=cur.step+1;
if(nex.x<x&& dis[nex.x]==-1)
{
dis[nex.x]=nex.step;
q.push(nex);
}
}
for(int j=0;j<17;j++)
{
nex.x=cur.x^(1<<j);
nex.step = cur.step+1;
if(nex.x<x&&dis[nex.x]==-1)
{
dis[nex.x]=nex.step;
q.push(nex);
}
}
}
}
int main()
{
int T, n, m, s, t;
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
bfs(n);
ll ans=0;
for(int i=1;i<= m;i++)
{
scanf("%d%d",&s,&t);
ans=(ans+(long long)i*dis[s^t])% mod;
}
printf("%lld\n",ans);
}
return 0;
}
- Numpy 修炼之道 (8)—— 常用函数
- Numpy 修炼之道 (7)—— 形状操作
- 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)
- 12.python进程协程异步IO
- POJ3622 Gourmet Grazers(FHQ Treap)
- 洛谷P3201 [HNOI2009]梦幻布丁
- 洛谷P3374 【模板】树状数组 1(CDQ分治)
- 自然语言处理基础知识1. 分词(Word Cut)2. 词性标注(POS Tag)3.自动标注4.文本分类5.评估6.从文本提取信息7.分析句子结构《python自然语言处理》各章总结:
- 洛谷P3384 【模板】树链剖分
- 洛谷P2147 [SDOI2008]Cave 洞穴勘测
- linux基础
- 洛谷P3178 [HAOI2015]树上操作
- Numpy 修炼之道 (6)—— 复制和视图
- 事务日志已满,原因为“ACTIVE_TRANSACTION”
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 腾讯云LiteAV、IM SDK(iOS)
- 在 Docker 中生成 ProtoBuffer、gRPC 文件
- Golang MongoDB Driver 更新符合条件的数组元素的字段
- 638. 大礼包 Krains 2020-08-01 19:48:29 动态规划DFS
- 650. 只有两个键的键盘 Krains 2020-08-02 09:39:39 动态规划DFS
- 类加载子系统 Krains 2020-07-31
- 理解类装载器
- MySQL-Python:使用技巧
- 使用Pytorch和Matplotlib可视化卷积神经网络的特征
- 微软复活20年前生产力工具PowerToys,填补Wind10缺失功能,开源且免费
- 运行时数据区 Krains 2020-08-01
- Docker 之容器间通信配置
- leetcode199场周赛
- 浅谈MySQL中的锁
- mysql日志模块