首页 > 其他分享 >D. ConstructOR

D. ConstructOR

时间:2022-11-15 20:13:15浏览次数:62  
标签:14 lowbit 30 times ConstructOR test 18

D. ConstructOR

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 \leq x < {2}^{60}$;
  • $a|x$ is divisible by $d$;
  • $b|x$ is divisible by $d$.

Here, $|$ denotes the bitwise OR operation.

Input

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

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

Output

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.

Example

input

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

output

18
14
-1
-1
0
11
25599
184470016815529983

Note

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.

 

解题思路

  首先先确定无解的情况,对于$d$根据$\text{lowbit}$求出其二进制下末尾$0$的个数,假设为$k$。如果$a$或$b$在二进制下末尾$0$的个数小于$k$,那么一定无解。这是因为$d$含有因子$2^k$,而$a$或$b$包括按位或后的结果都不可能含有因子$2^k$,因此无解。

  下面给出一种答案$x$的构造方案,让$a|x = b|x = x$,同时$x$也是$d$的倍数,这样至少可以保证$a|x$和$b|x$能被$d$整除(下面再证明$x$一定满足$x < 2^{60}$)。

  由于$a, b < 2^{30}$,因此在第$0$到第$29$位中,如果$a$或$b$的第$i$位为$1$,那么$x$的第$i$为也应该要为$1$,这样就可以保证$a|x = b|x = x$。

  那么如何保证$x$是$d$的倍数呢?当$x$的第$i$位要变成$1$时,我们直接让$x$加上$d$的$2^{i- k}$倍,即加上$2^{i-k} \times d$。$2^{i-k} \times d$等价于把$d$左移$i-k$位,这意味着$d$的最低位的$1$从第$k$位左移到了第$i$位,而此时$x$的第$i$位为$0$,因此$x$加上$2^{i-k} \times d$后第$i$位就变成了$1$,同时这样也保证了$x$始终是$d$的倍数。

  下面来证明按照上面的构造方法得到的$x$一定是满足$x < 2^{60}$。考虑最糟糕的情况:$$\begin{align*} x &= 2^0 \times d + 2^1 \times d + \ldots + 2^{29} \times d \\ &= \frac{1 - 2^{30}}{1 - 2} \times d \\ &= \left( {2^{30} - 1} \right) \times d \\ &< 2^{30} \times 2^{30} = 2^{60} \end{align*}$$

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 int lowbit(LL x) {
 7     return x & -x;
 8 }
 9 
10 void solve() {
11     LL a, b, d;
12     scanf("%lld %lld %lld", &a, &b, &d);
13     LL ret = 0;
14     if (lowbit(a) < lowbit(d) || lowbit(b) < lowbit(d)) {   // a或b末尾0的个数小于d末尾0的个数
15         ret = -1;
16     }
17     else {
18         int k = log2(lowbit(d));    // d末尾0的个数
19         a |= b;
20         for (int i = k; i < 30; i++) {
21             // 如果a或b的第i位为1,并且此时x的第i位为0
22             if ((a >> i & 1) && (ret >> i & 1) == 0) ret += d << i - k; // 答案加上d * 2^(i-k)
23         }
24     }
25     printf("%lld\n", ret);
26 }
27 
28 int main() {
29     int t;
30     scanf("%d", &t);
31     while (t--) {
32         solve();
33     }
34     
35     return 0;
36 }

 

参考资料

  Codeforces Round #833 (Div. 2) A - D:https://zhuanlan.zhihu.com/p/582927038

  Codeforces Round #833 (Div. 2) A-D.md:https://www.cnblogs.com/BlankYang/p/16885937.html

标签:14,lowbit,30,times,ConstructOR,test,18
From: https://www.cnblogs.com/onlyblues/p/16893690.html

相关文章

  • 【题解】CF1748D ConstructOR
    前言这道题十分诈骗,比赛的时候以为是根据二进制除法原理,解同余方程,然后考试时候就没做出来QAQ。这篇题解是构造解法,至于官方题解是啥我也不知道,看这官方题解的性质十分诡......
  • CF1748D ConstructOR 题解
    可能更好的阅读体验题目传送门题目大意给定\(a,b,d\),要求找到一个\(0\lex\le2^{60}\),满足\(a|x,b|x\)都是\(d\)的倍数(\(|\)代表按位或)。\(T\le10^4\),\(0\le......
  • 构造方法(构造器 constructor)
    构造器用于对象的初始化,而不是创建对象!构造方法是负责初始化(装修),不是建房子构造器4个要点:构造器通过new关键字调用!!构造器虽然有返回值,但是不能定义返回值类型......
  • prototype和__proto__和constructor之间的关系
    深入理解原型到原型链探究prototype__proto__constructor构造函数创建对象我们知道我们可以直接定义对象或者可以用构造函数的方法创建对象functionPerson()......
  • js中的类和constructor
    问题一直搞不清constructor和super是干什么用的。前提了解js的继承原型继承原型继承是通过利用js的原型链functionPeople(name,age){this.name=name;this......
  • JS中的prototype、__proto__与constructor
    1.前言作为一名前端工程师,必须搞懂JS中的prototype、proto与constructor属性,相信很多初学者对这些属性存在许多困惑,容易把它们混淆,本文旨在帮助大家理清它们之间的关......
  • @RequiredArgsConstructor
    @RequiredArgsConstructor注解@RequiredArgsConstructor生成带有必需参数的构造函数。必需的参数是最终字段和具有约束的字段,例如@NonNull。完整的文档可在@lconstru......
  • 关于Implicit super constructor Person() is undefined for default constructor. Mu
    ImplicitsuperconstructorPerson()isundefinedfordefaultconstructor.MustdefineanexplicitconstructorJava(134217868)写继承例题的一个小错误,记一下父类:......
  • 还在使用@Autowrired注入?不妨试试@RequiredArgsConstructor
    一、前言小编最近在项目里看到有的同事大神用到了Lombok中的一个@RequiredArgsConstructor,带着好奇发现这个东西就是简化了一些@Autowired注解,想想如果一个Service还有几......
  • @RequiredArgsConstructor @Autowired
    @RequiredArgsConstructorprivatefinalCppccWyxqTypeServicecppccWyxqTypeService;必须声明的变量为final@AutowiredprivateCppccProposalServicecppccProposa......