-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVA - 128-Software CRC.cpp
123 lines (100 loc) · 2.44 KB
/
UVA - 128-Software CRC.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ); //(will neglect the c's stdio.h property of doing inline flushing with every line of code)
//std::ios_base::sync_with_stdio(false); // (removes the interoperability that happens between C's stdio.h and iostream )
#include <iostream>
//#include<numeric>
//#include<vector>
//#include<cmath>
//#include<stack>
//#include <iomanip>
//#include <vector>
#include<bits/stdc++.h>
//#include <bitset>
////#include<iomanip>
//#include <algorithm>
//#include<queue>
////#include<deque>
////#include<numeric>
//#include<set>
//#include<unordered_set>
//#include<map>
////#include<fstream>
//#include<sstream>
////#include <thread>
////#include <chrono>
//#include<cstring>
//#include<string>
//#include <unordered_map>
////#include <thread>
////#include<limits>+++
using namespace std;
// I HAVE FUCKING OVER DO THE THINGS HERE.(AND DIDN'T THINK IT THOROGH)
//It could be solved using horner method.
const int g = 34943;
bool inline get_bit(char current_number,int bit) {
return ((int)current_number & (1<<bit));
}
int succ_pow(int pow){
long long res=2;
long long spare=1;
while(true){
if(pow==0) {
res=1;
break;
}
if(pow%2){
spare*=res;
spare%=g;
pow--;
}
else{
res = res*res;
res%=g;
pow/=2;
}
}
return ((res*spare)%g);
}
int get_bit_value(bool bit,int &max_pow){
int res=0;
if (bit) res = succ_pow(max_pow);
max_pow--;
return res;
}
void solve(string &s,int siz){
int max_pow = siz*8;
max_pow+=15;
int string_res=0;
for(int i=0;i<siz;++i){
char currentChar = s[i];
for(int bit=7;bit>=0;--bit){
string_res+=get_bit_value(get_bit(currentChar,bit),max_pow);
string_res%= g;
}
}
int remaining = g-string_res;
remaining%=g;
std::stringstream ss;
ss<< std::hex << remaining;
std::string res ( ss.str() );
string ret ="0000";
int hexaSize=res.size()-1;
int p=3;
for(int i=hexaSize ;i>=0;--i){
if (res[i]>64) res[i] -=32;
ret[p--] = res[i];
}
for(int i=0;i<2;++i) cout<<ret[i];
cout<<" ";
for(int i=2;i<4;++i) cout<<ret[i];
cout<<"\n";
return;
}
int main (){
string s;
while(true){
getline(cin,s);
int siz = s.size();
if(s == "#" && siz==1) break;
solve(s,siz);
}
}