题目描述

You are given three integers $ a $ , $ b $ , and $ d $ . Your task is to find any integer $ x $ which satisfies all of the following conditions, or determine that no such integers exist:

  • $ 0 \le x \lt 2^{60} $ ;
  • $ a|x $ is divisible by $ d $ ;
  • $ b|x $ is divisible by $ d $ .

输入格式

Each test contains multiple test cases. The first line of input contains one integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.

Each test case consists of one line, containing three integers $ a $ , $ b $ , and $ d $ ( $ 1 \le a,b,d \lt 2^{30} $ ).

输出格式

For each test case print one integer. If there exists an integer $ x $ which satisfies all of the conditions from the statement, print $ x $ . Otherwise, print $ -1 $ .

If there are multiple solutions, you may print any of them.

样例 #1

样例输入 #1

8
12 39 5
6 8 14
100 200 200
3 4 6
2 2 2
18 27 3
420 666 69
987654321 123456789 999999999

样例输出 #1

18
14
-1
-1
0
11
25599
184470016815529983

提示

In the first test case, $ x=18 $ is one of the possible solutions, since $ 39|18=55 $ and $ 12|18=30 $ , both of which are multiples of $ d=5 $ .

In the second test case, $ x=14 $ is one of the possible solutions, since $ 8|14=6|14=14 $ , which is a multiple of $ d=14 $ .

In the third and fourth test cases, we can show that there are no solutions.

注意,后面的 \(|\) 都是代表按位或,而不是整除。

要让 \(a|x\) 是 \(d\) 的倍数,让 \(b|x\) 是 \(b\) 的倍数。要同时让两个数满足不好写,所以尝试让 \(a|x=b|x=x\),并让 \(x\) 位 \(d\) 的倍数。

为什么敢于放到这一步呢?其实发现 \(x\) 的取值范围很大,所以极有可能达到这个条件。要满足上面这个条件,不妨让 \(x\) 的后 30 位就是 \(a|b\) 就行了

换句话说,\(x\equiv a|b\pmod{2^{30}}\)

如果把 \(x\) 表示为 \(dk\),那么就是 \(dk\equiv a|b\pmod{2^{30}}\),拿个exgcd 就好了。exgcd 的解是肯定符合要求的,同时如果 exgcd 无解,肯定原来也无解。

但是有一个更简单的方法。

原方程有解的条件是 \(\gcd(d,2^{30})\) 能整除 \(a|b\),而 \(\gcd(d,2^{30})\) 就是 \(\mathrm{lowbit(d)}\),那么可以通过不断除以 2 来判断。最后如果有解的时候,\(d\) 的最后一位已经被除成 1 了。现在假设答案 \(ans\) 满足了前 \(j-1\) 位与 \(a|b\) 相等,但是第 \(j\) 位与 \(a|b\) 不等,那么可以让 \(ans\) 加上 \(d\times 2^j\)。那么此时答案的这一位会发生变化,然后一直弄到 \(a|b\) 已经全部被模仿出来。

由于 \(a|b<2^{30}\),解合法。

#include<bits/stdc++.h>
using namespace std;
int a,b,d,t,p,cnt;
long long ret;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&a,&b,&d);
a|=b,p=1<<30,ret=cnt=0;
while(!(d&1)&&!(a&1))
a>>=1,d>>=1,p>>=1,++cnt;
if(!(d&1))
{
printf("-1\n");
continue;
}
// printf("%d %d %d\n",a,d,p);
for(int i=0;(1LL<<i)<=p&&(1LL<<i)<=a;i++)
{
if((ret>>i&1)!=(a>>i&1))
ret+=(1LL*d)<<i;
// printf("%d\n",ret);
}
printf("%lld\n",ret*(1LL<<cnt));
}
}

[CF1748D] ConstructOR的更多相关文章

  1. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  2. 分析js中的constructor 和prototype

    在javascript的使用过程中,constructor 和prototype这两个概念是相当重要的,深入的理解这两个概念对理解js的一些核心概念非常的重要. 我们在定义函数的时候,函数定义的时候函 ...

  3. js中的constructor

    定义和用法 constructor 属性返回对创建此对象的 Date 函数的引用. 语法 object.constructor constructor属性不影响任何JavaScript的内部属性.in ...

  4. Java程序设计之Constructor

    插入段代码,下次回忆吧. 先新建一个Person类,代码如下: public class Person { private String name ; private int age; public ...

  5. prototype,__proto__,constructor

    proto属性: 所有对象都有此属性.但它不是规范里定义的属性,并不是所有JavaScript运行环境都支持.它指向对象的原型,也就是你说的继承链里的原型.通过Object.getPrototypeO ...

  6. JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  7. 一张图理解prototype、proto和constructor的三角关系

    × 目录 [1]图示 [2]概念 [3]说明[4]总结 前面的话 javascript里的关系又多又乱.作用域链是一种单向的链式关系,还算简单清晰:this机制的调用关系,稍微有些复杂:而关于原型,则 ...

  8. JavaScript中typeof、toString、instanceof、constructor与in

    JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定. 这也意味着你可以使用同一个变量保存不同类型的数据. 最新的 ECMAScrip ...

  9. js中constructor和prototype

    在最开始学习js的时候,我们在讲到原型链和构造函数的时候经常会有一个例子 如果我们定义函数如下: function Foo() { /* .. */ } Foo.prototype.bar = fun ...

  10. javascript中的prototype和constructor

    构造函数 我们知道,ECMAScript5中的Object.Array.Date.RegExp.Function等引用类型都是基于构造函数的,他们本身就是ECMAScript5原生的构造函数.比如,我 ...

随机推荐

  1. Linux ALSA 核心简单分析

    Linux 内核 ALSA 框架通过向用户空间导出多个设备文件,以使用户空间程序可以与内核的音频子系统交互,可以访问音频硬件设备. Linux 内核 ALSA 音频框架初始化 Linux 内核 ALS ...

  2. Nginx map 实现时间格式转换

    哈喽大家好,我是咸鱼 最近我们需要把 Nginx 的日志接入到自研的日志采集平台上,但是这个平台只支持 JSON 格式,所以需要把 Nginx 日志格式改成 JSON 格式 例如下面这样的效果 刚开始 ...

  3. elementui vue表单提交向别的组件传参失效 路由传参格式

    目录 表单提交向别的组件传参失效 路由传参格式 表单提交向别的组件传参失效 methods: { submitForm(formName) { this.$refs[formName].validat ...

  4. JAVA实现单链表修改和删除数据节点

    JAVA实现单链表修改和删除数据节点 一.修改单链表中的一个节点 ①实现思路 因为带头节点的链表中头节点的next域不能发生改变(始终指向单链表的头节点),否则将找不到该链表.所以我们需要先找一个辅助 ...

  5. AT 下分记录

    7.30 AGC063 \(+30=1620\) B 做法假 WA 了三次,为啥总是吃了罚时才能发现问题啊 心态还是需要解决的问题.过完 B 啥都想不出来又自闭了

  6. Python基础——字符编码、文件处理

    文章目录 字符编码 一 引入 二 知识储备 2.1 三大核心硬件 2.2 文本编辑器读取文件内容的流程 2.3 python解释器执行文件的流程 2.4 总结 三.字符编码介绍 3.1 什么是字符编码 ...

  7. TOP GP 把已经编译的per反编回对应版本的4fd(画面档)

    由于GP5.1,5.2,5.3的genero对应版本画面档开发互不兼容,下面提供各版本之间互转的操作方法: xshell切换到要反编译的per档目录,执行以下命令,就会在同目录下生成对应4fd档资料 ...

  8. RocketMQ为什么要保证订阅关系一致

    这篇文章,笔者想聊聊 RocketMQ 最佳实践之一:保证订阅关系一致. 订阅关系一致指的是同一个消费者 Group ID 下所有 Consumer 实例所订阅的 Topic .Tag 必须完全一致. ...

  9. 比赛总结:Japan Registry Services (JPRS) Programming Contest 2023 (AtCoder Beginner Contest 324)

    比赛:Japan Registry Services (JPRS) Programming Contest 2023 (AtCoder Beginner Contest 324) A-same 1.常 ...

  10. 从零用VitePress搭建博客教程(2) –VitePress默认首页和头部导航、左侧导航配置

    2. 从零用VitePress搭建博客教程(2) –VitePress默认首页和头部导航.左侧导航配置 接上一节: 从零用VitePress搭建博客教程(1) – VitePress的安装和运行 四. ...