FZU1860 :Funny Hash game I
时间限制:1000MS 内存限制:32768KByte 64位IO格式:%I64d & %I64u
描述
A classic string hashing algorithm can be simply described by the following code:
unsigned long long hash(string str) { unsigned long long ret= 5381; int i; for (i=0;i < str.size();i++) ret=(ret* 33)^str[i]; //xor operation return ret; }
You will be given a hash value,and you shall determine that whether there exists a string with this hash value.
Note that the string you searching for must satisfy the following two conditions:
1.The length of the string is not large than 8.
2.The string is composed of characters whose ASCII code are between [0,128) .
输入
Input contains multiple cases.Test cases are separated by several blank lines.
Each test case starts with a integer N(0 <= N < 2^64),which reperesents the hash value.
There are no more than 1000 test cases.
输出
For each case,if there exists some satisfied string as described above,ouput the shortest length of such string,or just output -1 if we can’t find one.
样例输入
193411340 210623374112 193415041 193434013 193422026
样例输出
3 5 3 33
题意:
告诉哈希值,求原字符的位数。
思路:
枚举每位的上下界,down为ffffff80,up位ffffffff.只涉及到后7位。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef unsigned long long ll; ll a[10],b[10]; int main() { ll down=-(1<<7); ll up=(1<<7)-1; //printf("%x %x\n",down,up); a[0]=5381,b[0]=5381; for(int i=1;i<=8;i++) { a[i]=(a[i-1]*33)&down; b[i]=(b[i-1]*33)|up; } ll n; while(scanf("%I64u",&n)!=EOF) { int ans=-1; for(int i=0;i<=8;i++) { if(n>=a[i]&&n<=b[i]) { ans=i; break; } } printf("%d\n",ans); } return 0; }