Backpack password time limit:1000msmemory limit:65536kb64-bit integer IO format:%lldJava class Name:MainSubmitstatus pid:29141
The Backpack cipher system is a very classic public-key cryptography system, and the cryptography process is as follows:
- Select an n-length positive integer super-increment sequence a[i] to meet A[1]<a[2],a[1]+a[2]<a[3],......,a[1]+a[2]+...+a[n-1]<a[n].
- Select positive integers m>2*a[n],w and M coprime, V is the inverse of W-modulus m, i.e. (v*w)%m=1. B[i]= (W*a[i])%m.
- In the case of encryption, a binary string with a length of n x[i]. There are s= (b[1]*x[1]+b[2]*x[2]+...+b[n]*x[n]%m.
This password, tells you b[i] can be encrypted. But only b[i] is difficult to decrypt because a common 01 knapsack problem needs to be solved. Only to tell you the V and M, the decryption will become very simple, now tell you b[i], V and M and S, please help to decrypt.
Input
A positive integer T (t<100) that represents the number of data groups. The data formats for each group are as follows:
First line positive integer n (1<=n<=32) represents the length of the binary string
Second row n positive integers b[1] to b[n]
The third line is three integers, V and M, and S.
(Ensure all inputs are within 10^9, hint: the intermediate process may use a long long)
Output
Output one row for each set of data, decrypted binary string x[i]
Sample Input
233 6 14 11 441 18 20 242 33 5
Sample Output
1010110
Idea: Good question.
Known:
1.v*w%m=1
2.b[i]= (W*a[i])%m A[i] is an increment sequence
3.s= (B[1]*x[1]+b[2]*x[2]+...+b[n]*x[n]%m Beg X[i]
Solution:
From 2, 3 to be:
4.s= ((w*a[1])%m*x[1]+(w*a[2])%m*x[2]+...+(w*a[n])%m*x[n])%m
4 or so multiplied by v%m
S*v%m = v%m*((w*a[1])%m*x[1]+(w*a[2])%m*x[2]+...+(w*a[n])%m *x[n])%m
5.s*v%m =a[1]%m*x[1]+...+a[n]%m*x[n]
From 1, 2 to be:
B[i]*v%m=(W*a[i])%m*v%m
6.a[i]%m=b[i]*v%m
From 5, 6 to be:
s*v%m =b[1]*v%m*x[1]+...+b[i]*v%m*x[n]
X[i] is a 01-component encoding
Immediate as 5=1*x[1]+2*x[2]+4*x[3] (the first set of examples) A[i] is an increment sequence so we have to take precedence from the back to the forward.
if S "= b[i]*v%m x[i]=1 s-=b[i]*v%m;
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm>using namespace STD; #define LL Long Longint Main () { int t; scanf ("%d", &t); while (t--) { LL n; scanf ("%lld", &n); LL b[1005]; for (int i=0;i<n;i++) scanf ("%lld", &b[i]); LL v,m,s; scanf ("%lld%lld%lld", &v,&m,&s); for (int i=0;i<n;i++) { b[i]=b[i]*v%m; } s=s*v%m;// printf ("s:%lld\n", s);//For (int i=0;i<n;i++)// printf ("%lld", B[i]); for (int i=0;i<n;i++) { if (s>=b[n-i-1]) { s-=b[n-i-1]; b[n-i-1]=1; } else b[n-i-1]=0; } for (int i=0;i<n;i++) printf ("%lld", B[i]); printf ("\ n");} }
BNU Backpack Password (encode and decrypt)