BOJ_B2_15829
π [B2_15829] Hashing
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static final int M = 1234567891;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
// **** input start ****
int N = Integer.parseInt(br.readLine());
String str = br.readLine();
long sum = 0;
long r = 1;
for(int i=0; i<N;i++) {
sum += ((str.charAt(i)-96) * r) % M;
r = (r * 31) % M;
}
// **** input end ****
System.out.println(sum%M);
}
}
π€ λμ μκ°
μ²μμ κ·Έλ₯ Math.pow()
λ‘ νμλλ° 50μ μ΄ λμμ λ¬Έμ λ₯Ό μ λλ‘ λ΄€λ€.. γ
γ
λ¬Έμ λ₯Ό 보λ ν¬μΈνΈλ %M
μΈ κ² κ°λ€.
λͺ¨λλ¬μ°μ°μ μ΄μ©ν΄ μμ μ κ°ν μ μλ€.
(A+B)mod M = ((A mod M) + (B mod M)) mod M
κ·Έλμ λͺ¨λ μ°μ°μ mod M
μ` ν΄μ£Όκ³
rκ³Ό sum λ³μλ longμ μ¬μ©νλκ² !
μλλ©΄ 50κΉμ§ κ°λ€λ©΄ λ²μλ₯Ό νμ© λμ΄λ²λ¦¬κΈ° λλ¬Έμ long λ νμ μ΄λ€.