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 . Here is its description
Alice chooses positive integers such that each a positive integer which is greater than the sum of all and a positive integer which is coprime with . These integers are Alice's private key.
Then Alice calculates mod . These integers are Alice's public key.
Knowing her public key, Bob can transmit a message of bits to Alice. To do that he calculates , the sum of with indices such that his message has bit in i-th position. This value 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 , 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 for obvious performance reasons, and published this information. Since everyone knows her , she asks Bob to send her the calculated value taken modulo 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 .
Each of the next lines contains one integer
The last line contains one integer mod -- the encrypted message taken modulo mod
The given sequence is a valid public key in the described implementation, and the given value mod is a valid encrypted message.
输出格式
Output exactly bits ( or digits) -- the original message.
题目大意
背包密码是一个简单的公钥密码系统,下面是它的具体过程:
-
Alice 选择 个正整数 ,满足 ,再选择两个正整数 ,满足 , 互质。这 个数是 Alice 的私钥。再计算 ,这 个数是 Alice 的公钥。
-
Bob 有一个 01 串 ,他知道 Alice 的公钥, 加密后得到 $s=\left(\sum\limits_{i=1}^n [t_i=1]b_i\right)\bmod q$. 他把这个 01 串加密后的结果发给了 Alice。那 Alice 就可以在线性时间内解出来。
你现在截获了 ,并知道 和 ,其中 。请解出这个 01 串。
,保证 合法。
5
10
20
50
140
420
440
01001
提示
Time limit: 3 s, Memory limit: 512 MB.