#P4235. Hash?
Hash?
Description
By some means, he obtained a function: int Rand(int x), which returns an integer uniformly at random from .
He wrote the following code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int x=10,maxn=35,maxlen=16010;
ll HASH[maxn];
const ll p=65537;
char str[maxlen];
ll Hash(){
int base=Rand(x);
ll ret=0;
for(int i=1;str[i];i++)
ret=(ret*base+str[i]-'a'+1)%p;
return ret;
}
int main(){
int ans=0,n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",str+1),HASH[i]=Hash();
sort(HASH+1,HASH+n+1);
HASH[0]=-1;
for(int i=1;i<=n;i++)
ans+=(HASH[i]!=HASH[i-1]);
printf("%d\n",ans);
return 0;
}
zhoutb2333 asks: given some strings and the parameter , what is the expectation of the answer ?
is prime.
The parameter is fixed to in this program, but it will be provided in each input.
Input Format
The first line contains two integers , denoting the parameter that generates the and the number of strings.
The next lines each contain one string, consisting only of lowercase letters.
Output Format
Output a single number on one line: the expectation of , output modulo .
That is, if your answer is (), then output the smallest positive integer such that . It can be proved that the answer is always a positive rational number, and such an always exists.
2 2
aa
aa
32770
3 6
i
dont
know
what
to
say
58261
Hint
This problem has s. Let be the maximum length among these strings.
For : , score , time limit .
For : , score , time limit .
For : , score , time limit .
Sample #1 explanation:
With parameter , the possible hash values are .
If the first aa and the second aa use the same for hashing, then the answer is .
If the two values are different, then the answer is .
These two cases happen with the same probability, both , so the expectation of is . The smallest positive integer such that is .
Sample #2 explanation:
The answer is . The smallest positive integer such that is .
Translated by ChatGPT 5
京公网安备 11011102002149号