AOJ1250 Leaky Cryptography

Leaky Cryptography

The ACM ICPC judges are very careful about not leaking their problems, and all communications are encrypted. However, one does sometimes make mistakes, like using too weak an encryption scheme. Here is an example of that. The encryption chosen was very simple: encrypt each chunk of the input by flipping some bits according to a shared key.

一番下の桁から順番に決めていく.今までの 桁目と 桁の和が 桁と一致しているかを見る.一致していない場合に, つの桁目を反転して,答えの 桁目のビットを立てる.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <bitset>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

ll f(string s) {
  ll res = 0, t = 1;

  for(int i = s.size()-1; i >= 0; i--) {
      if('0' <= s[i] && s[i] <= '9') {
          res += (s[i] - '0') * t;
      } else {
          res += (10 + (s[i] - 'a')) * t;
      }
      t *= 16;
  }
  return res;
}

int main() {
  int n;
  cin >> n;

  rep(q, n) {
      vector<string> v(9);
      vector<ll> t(9);
      rep(i, 9) {
          cin >> v[i];
          t[i] = f(v[i]);
      }

      ll ans = 0;

      vector< vector<int> > bit(9, vector<int>(32));
      rep(i, 9) {
          rep(j, 32) {
              if(t[i] & (1LL << j)) {
                  bit[i][j] = 1;
              }
          }
      }

      ans = 0;
      ll sum = 0;
      rep(i, 32) {
          ll d = 0;
          rep(j, 8) {
              d += bit[j][i];
          }
          bitset<32> ss(sum);

          if( ((((sum >> i) & 1) + d) & 1) == bit[8][i]) {
              sum += (d << i);
          } else {
              d = 0;
              rep(j, 8) {
                  d += bit[j][i] ^ 1;
              }
              sum += (d << i);
              ans += (1LL << i);
          }
      }

      cout << hex << ans << endl;
  }

  return 0;
}
Mar 22nd, 2016