#P4235. Hash?

    ID: 3147 远端评测题 1000~4500ms 188MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串哈希,HASH概率论,统计

Hash?

Description

By some means, he obtained a function: int Rand(int x), which returns an integer uniformly at random from [0,x)[0, x).

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 xx, what is the expectation of the answer ansans?

65537=216+165537 = 2^{16} + 1 is prime.

The parameter xx is fixed to 1010 in this program, but it will be provided in each input.

Input Format

The first line contains two integers x,Nx, N, denoting the parameter that generates the basebase and the number of strings.

The next NN lines each contain one string, consisting only of lowercase letters.

Output Format

Output a single number on one line: the expectation of ansans, output modulo 6553765537.

That is, if your answer is qp\frac{q}{p} (gcd(p,q)=1\gcd(p, q) = 1), then output the smallest positive integer xx such that pxq(mod65537)p x \equiv q \pmod{65537}. It can be proved that the answer ansans is always a positive rational number, and such an xx always exists.

2 2
aa
aa
32770

3 6
i
dont
know
what
to
say
58261

Hint

This problem has 33 subtask\text{subtask}s. Let MM be the maximum length among these NN strings.

For subtask 1\text{subtask} \ 1: 1N8, M10, x41 \le N \le 8, \ M \le 10, \ x \le 4, score 2020, time limit 1 s1 \ \text{s}.

For subtask 2\text{subtask} \ 2: 1N30, M500, x5001 \le N \le 30, \ M \le 500, \ x \le 500, score 5050, time limit 1 s1 \ \text{s}.

For subtask 3\text{subtask} \ 3: 1N5, M16000, x160001 \le N \le 5, \ M \le 16000, \ x \le 16000, score 3030, time limit 4.5 s4.5 \ \text{s}.

Sample #1 explanation:

With parameter x=2x = 2, the possible hash basebase values are 0,10, 1.

If the first aa and the second aa use the same basebase for hashing, then the answer is 11. If the two basebase values are different, then the answer is 22.

These two cases happen with the same probability, both 12\frac{1}{2}, so the expectation of ansans is 112+212=321 * \frac{1}{2} + 2 * \frac{1}{2} = \frac{3}{2}. The smallest positive integer xx such that 2x3 (mod65537)2 x \equiv 3 \ \pmod{65537} is 3277032770.

Sample #2 explanation:

The answer is 539\frac{53}{9}. The smallest positive integer xx such that 9x53 (mod65537)9 x \equiv 53 \ \pmod{65537} is 5826158261.

Translated by ChatGPT 5