uvaoj 138 Software CRC 大数取模
2015-01-06 19:03
162 查看
uvaoj 138 Software CRC 大数取模
求循环冗余检验(CRC)的值,循环冗余检验在计算机网络中应该提到过,计算的方法差不多。不过这里固定了要在后边加上2个字节的数,加上两个字节后,就组成了一个很大的整数,这个整数要处理34943为0,我们就设计算出来的检验位为x,原数为n,那么合起来的数为m=(n<<16)+x,那么m%34943=0,也就是((n<<16)%34943+x%34943)%34943=0,则可以得x=(34943-(n<<16)%34943%34943。因为数比较大,所以要一个字节一个自己的取模,可能中间结果超过int,要使用64位整数。
代码如下:
求循环冗余检验(CRC)的值,循环冗余检验在计算机网络中应该提到过,计算的方法差不多。不过这里固定了要在后边加上2个字节的数,加上两个字节后,就组成了一个很大的整数,这个整数要处理34943为0,我们就设计算出来的检验位为x,原数为n,那么合起来的数为m=(n<<16)+x,那么m%34943=0,也就是((n<<16)%34943+x%34943)%34943=0,则可以得x=(34943-(n<<16)%34943%34943。因为数比较大,所以要一个字节一个自己的取模,可能中间结果超过int,要使用64位整数。
代码如下:
/************************************************************************* > File Name: 128.cpp > Author: gwq > Mail: gwq5210@qq.com > Created Time: 2015年01月06日 星期二 11时39分02秒 ************************************************************************/ #include <cmath> #include <ctime> #include <cctype> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <vector> #include <sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX / 10) #define clr(arr, val) memset(arr, val, sizeof(arr)) #define pb push_back #define sz(a) ((int)(a).size()) using namespace std; typedef set<int> si; typedef vector<int> vi; typedef map<int, int> mii; typedef long long ll; const double esp = 1e-5; string tohex(ll n) { string mp = "0123456789ABCDEF"; int a = (n >> 12) & ((1 << 4) - 1); int b = (n >> 8) & ((1 << 4) - 1); int c = (n >> 4) & ((1 << 4) - 1); int d = n & ((1 << 4) - 1); string ret = ""; ret += mp[a]; ret += mp[b]; ret += " "; ret += mp[c]; ret += mp[d]; return ret; } int main(int argc, char *argv[]) { ll m = 34943; string str; while (getline(cin, str)) { if (str[0] == '#') { break; } ll len = str.length(); ll res = 0; for (int i = 0; i < len; ++i) { res = ((res << 8) + str[i]) % m; } res = (res << 16) % m; res = (m - res) % m; cout << tohex(res) << endl; } return 0; }
相关文章推荐
- uvaoj 138 Street Numbers 佩尔方程
- UVaOJ138 - Street Numbers
- 3389、135、137、138、139、445等端口解释和关闭方法
- 借助IP138网站来实现本地系统的IP,身份证号,手机号码查询功能
- NetBeans Weekly News 刊号 # 138
- uvaoj 445 - Marvelous Mazes
- NYOJ 138 (简单hash)
- UvaOJ 10014 - Simple calculations
- NYOJ-138 找球号2【Hash】
- NYOJ 138
- UVaOJ 10047 - The Monocycle
- UVA 138 Street Numbers
- UVaOJ 494 - Kindergarten Counting Game
- Codeforces Round #138 (Div. 1)
- UVaOJ 537 - Artificial Intelligence?
- UVaOJ 755 - 487--3279
- UVaOJ133---The Dole Queue
- UVaOJ 537
- 128 - Software CRC
- UVaOJ10602 - Editor Nottoobad