luogu#P6962. [NEERC2017] Knapsack Cryptosystem

[NEERC2017] Knapsack Cryptosystem

题目描述

The Merkle-Hellman Knapsack Cryptosystem was one of the earliest public key cryptosystems invented by Ralph Merkle and Martin Hellman in 19781978 . Here is its description

Alice chooses nn positive integers a1,...,an{a_{1}, . . . , a_{n}} such that each ai>j=1i1aj,a_{i} > \sum^{i−1}_{j=1}a_{j}, a positive integer qq which is greater than the sum of all ai,a_{i}, and a positive integer rr which is coprime with qq . These n+2n + 2 integers are Alice's private key.

Then Alice calculates bi=(air)b_i = (a_{i} · r) mod qq . These nn integers are Alice's public key.

Knowing her public key, Bob can transmit a message of nn bits to Alice. To do that he calculates ss , the sum of bib_{i} with indices ii such that his message has bit 11 in i-th position. This value ss is the encrypted message.

Note that an eavesdropper Eve, who knows the encrypted message and the public key, has to solve a (presumably hard) instance of the knapsack problem to find the original message. Meanwhile, after receiving ss , Alice can calculate the original message in linear time; we leave it to you as an exercise.

In this problem you deal with the implementation of the Merkle-Hellman Knapsack Cryptosystem in which Alice chose q=264,q = 2^{64}, for obvious performance reasons, and published this information. Since everyone knows her qq , she asks Bob to send her the calculated value ss taken modulo 2642^{64} for simplicity of communication.

You are to break this implementation. Given the public key and an encrypted message, restore the original message.

输入格式

The first line contains one integer n(1n64)n (1 \le n \le 64) .

Each of the next nn lines contains one integer bi(1bi<264).b_{i} (1 \le b_{i} < 2^{64}).

The last line contains one integer ss mod qq -- the encrypted message ss taken modulo q(0sq (0 \le s mod q<264).q < 2^{64}).

The given sequence bib_{i} is a valid public key in the described implementation, and the given value ss mod qq is a valid encrypted message.

输出格式

Output exactly nn bits (00 or 11 digits) -- the original message.

题目大意

背包密码是一个简单的公钥密码系统,下面是它的具体过程:

  • Alice 选择 nn 个正整数 a1,a2,,ana_1,a_2,\cdots,a_n,满足 ai>j=1i1aja_i> \sum\limits_{j=1}^{i-1} a_j,再选择两个正整数 q,rq,r,满足 q>j=1naiq> \sum\limits_{j=1}^n a_ir,qr,q 互质。这 n+2n + 2 个数是 Alice 的私钥。再计算 bi=(air)modqb_i = (a_i\cdot r)\bmod q,这 n+2n+2 个数是 Alice 的公钥。

  • Bob 有一个 01 串 tt,他知道 Alice 的公钥,tt 加密后得到 $s=\left(\sum\limits_{i=1}^n [t_i=1]b_i\right)\bmod q$. 他把这个 01 串加密后的结果发给了 Alice。那 Alice 就可以在线性时间内解出来。

你现在截获了 ss,并知道 bib_iqq,其中 q=264q = 2^{64}。请解出这个 01 串。

n64n\le 64,保证 s,bis,b_i 合法。

5
10
20
50
140
420
440

01001

提示

Time limit: 3 s, Memory limit: 512 MB.