16 lines
495 B
JavaScript
16 lines
495 B
JavaScript
const [l, s] = require("fs").readFileSync(0, "utf8").toString().trim().split('\n');
|
|
let ri = 1, hash = 0;
|
|
const M = 1234567891;
|
|
for(let i=0; i<Number(l); i++) {
|
|
hash = (hash + (((s[i].charCodeAt() - 'a'.charCodeAt() + 1)%M)*ri))%M;
|
|
ri = (ri*31)%M;
|
|
}
|
|
console.log(hash);
|
|
|
|
// const M = BigInt(1234567891);
|
|
// let hash = BigInt(0), ri = BigInt(1);
|
|
// for(let c of s) {
|
|
// hash += BigInt((c.charCodeAt() - 'a'.charCodeAt() + 1)) * ri;
|
|
// ri *= BigInt(31);
|
|
// }
|
|
// console.log(hash%M);
|