A13355 | Bad Cryptography
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
In modern cryptography much is tied to the algorithmic complexity of solving several problems. One of such problems is a discrete logarithm problem. It is formulated as follows:
Let's fix a [finite field](https://en.wikipedia.org/wiki/Finite_field) and two it's elements $a$ and $b$ . One need to fun such $x$ that $a^x = b$ or detect there is no such x. It is most likely that modern mankind cannot solve the problem of discrete logarithm for a sufficiently large field size. For example, for a field of residues modulo prime number, primes of 1024 or 2048 bits are considered to be safe. However, calculations with such large numbers can place a significant load on servers that perform cryptographic operations. For this reason, instead of a simple module residue field, more complex fields are often used. For such field no fast algorithms that use a field structure are known, smaller fields can be used and operations can be properly optimized.
Developer Nikolai does not trust the generally accepted methods, so he wants to invent his own. Recently, he read about a very strange field — [nimbers](https://en.wikipedia.org/wiki/Nimber), and thinks it's a great fit for the purpose.
The field of nimbers is defined on a set of integers from 0 to $2^{2^k} - 1$ for some positive integer $k$ . Bitwise exclusive or ( $\oplus$ ) operation is used as addition. One of ways to define multiplication operation ( $\odot$ ) is following properties:
- $0 \odot a = a \odot 0 = 0$
- $1 \odot a = a \odot 1 = a$
- $a \odot b = b \odot a$
- $a \odot (b \odot c)= (a \odot b) \odot c$
- $a \odot (b \oplus c) = (a \odot b) \oplus (a \odot c)$
- If $a = 2^{2^n}$ for some integer $n > 0$ , and $b < a$ , then $a \odot b = a \cdot b$ .
- If $a = 2^{2^n}$ for some integer $n > 0$ , then $a \odot a = \frac{3}{2}\cdot a$ .
For example:
- $ 4 \odot 4 = 6$
- $ 8 \odot 8 = 4 \odot 2 \odot 4 \odot 2 = 4 \odot 4 \odot 2 \odot 2 = 6 \odot 3 = (4 \oplus 2) \odot 3 = (4 \odot 3) \oplus (2 \odot (2 \oplus 1)) = (4 \odot 3) \oplus (2 \odot 2) \oplus (2 \odot 1) = 12 \oplus 3 \oplus 2 = 13.$
- $32 \odot 64 = (16 \odot 2) \odot (16 \odot 4) = (16 \odot 16) \odot (2 \odot 4) = 24 \odot 8 = (16 \oplus 8) \odot 8 = (16 \odot 8) \oplus (8 \odot 8) = 128 \oplus 13 = 141$
- $5 \odot 6 = (4 \oplus 1) \odot (4 \oplus 2) = (4\odot 4) \oplus (4 \odot 2) \oplus (4 \odot 1) \oplus (1 \odot 2) = 6 \oplus 8 \oplus 4 \oplus 2 = 8$
Formally, this algorithm can be described by following pseudo-code.

It can be shown, that this operations really forms a field. Moreover, than can make sense as game theory operations, but that's not related to problem much. With the help of appropriate caching and grouping of operations, it is possible to calculate the product quickly enough, which is important to improve speed of the cryptoalgorithm. More formal definitions as well as additional properties can be clarified in the wikipedia article at [link](https://en.wikipedia.org/wiki/Nimber). The authors of the task hope that the properties listed in the statement should be enough for the solution.
Powering for such muliplication is defined in same way, formally $a^{\odot k} = \underbrace{a \odot a \odot \cdots \odot a}_{k~\texttt{times}}$ .
You need to analyze the proposed scheme strength. For pairs of numbers $a$ and $b$ you need to find such $x$ , that $a^{\odot x} = b$ , or determine that it doesn't exist.
Let's fix a [finite field](https://en.wikipedia.org/wiki/Finite_field) and two it's elements $a$ and $b$ . One need to fun such $x$ that $a^x = b$ or detect there is no such x. It is most likely that modern mankind cannot solve the problem of discrete logarithm for a sufficiently large field size. For example, for a field of residues modulo prime number, primes of 1024 or 2048 bits are considered to be safe. However, calculations with such large numbers can place a significant load on servers that perform cryptographic operations. For this reason, instead of a simple module residue field, more complex fields are often used. For such field no fast algorithms that use a field structure are known, smaller fields can be used and operations can be properly optimized.
Developer Nikolai does not trust the generally accepted methods, so he wants to invent his own. Recently, he read about a very strange field — [nimbers](https://en.wikipedia.org/wiki/Nimber), and thinks it's a great fit for the purpose.
The field of nimbers is defined on a set of integers from 0 to $2^{2^k} - 1$ for some positive integer $k$ . Bitwise exclusive or ( $\oplus$ ) operation is used as addition. One of ways to define multiplication operation ( $\odot$ ) is following properties:
- $0 \odot a = a \odot 0 = 0$
- $1 \odot a = a \odot 1 = a$
- $a \odot b = b \odot a$
- $a \odot (b \odot c)= (a \odot b) \odot c$
- $a \odot (b \oplus c) = (a \odot b) \oplus (a \odot c)$
- If $a = 2^{2^n}$ for some integer $n > 0$ , and $b < a$ , then $a \odot b = a \cdot b$ .
- If $a = 2^{2^n}$ for some integer $n > 0$ , then $a \odot a = \frac{3}{2}\cdot a$ .
For example:
- $ 4 \odot 4 = 6$
- $ 8 \odot 8 = 4 \odot 2 \odot 4 \odot 2 = 4 \odot 4 \odot 2 \odot 2 = 6 \odot 3 = (4 \oplus 2) \odot 3 = (4 \odot 3) \oplus (2 \odot (2 \oplus 1)) = (4 \odot 3) \oplus (2 \odot 2) \oplus (2 \odot 1) = 12 \oplus 3 \oplus 2 = 13.$
- $32 \odot 64 = (16 \odot 2) \odot (16 \odot 4) = (16 \odot 16) \odot (2 \odot 4) = 24 \odot 8 = (16 \oplus 8) \odot 8 = (16 \odot 8) \oplus (8 \odot 8) = 128 \oplus 13 = 141$
- $5 \odot 6 = (4 \oplus 1) \odot (4 \oplus 2) = (4\odot 4) \oplus (4 \odot 2) \oplus (4 \odot 1) \oplus (1 \odot 2) = 6 \oplus 8 \oplus 4 \oplus 2 = 8$
Formally, this algorithm can be described by following pseudo-code.

It can be shown, that this operations really forms a field. Moreover, than can make sense as game theory operations, but that's not related to problem much. With the help of appropriate caching and grouping of operations, it is possible to calculate the product quickly enough, which is important to improve speed of the cryptoalgorithm. More formal definitions as well as additional properties can be clarified in the wikipedia article at [link](https://en.wikipedia.org/wiki/Nimber). The authors of the task hope that the properties listed in the statement should be enough for the solution.
Powering for such muliplication is defined in same way, formally $a^{\odot k} = \underbrace{a \odot a \odot \cdots \odot a}_{k~\texttt{times}}$ .
You need to analyze the proposed scheme strength. For pairs of numbers $a$ and $b$ you need to find such $x$ , that $a^{\odot x} = b$ , or determine that it doesn't exist.
输入格式
In the first line of input there is single integer $t$ ( $1 \le t \le 100$ ) — number of pairs, for which you need to find the discrete logarithm.
In each of next $t$ line there is a pair of integers $a$ $b$ ( $1 \le a, b < 2^{64}$ ).
In each of next $t$ line there is a pair of integers $a$ $b$ ( $1 \le a, b < 2^{64}$ ).
输出格式
For each pair you should print one integer $x$ ( $0 \le x < 2^{64}$ ), such that $a^{\odot x} = b$ , or -1 if no such x exists. It can be shown, that if any such $x$ exists, there is one inside given bounds. If there are several good values, you can output any of them.
输入输出样例
输入 #1
7 2 2 1 1 2 3 8 10 8 2 321321321321 2 123214213213 4356903202345442785
输出 #1
1 1 2 4 -1 6148914691236517205 68943624821423112
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted