Solved.ac Class 2++
public class Main {
static final int r = 31;
static final int m = 1234567891;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int sum = 0;
int size = Integer.parseInt(br.readLine());
String string = br.readLine();
for (int i = 0; i < size; i++) {
sum += (int)((string.charAt(i) - 96) * Math.pow(r, i));
}
System.out.println(sum);
}
}
부분성공
아마 mod 부분을 적용하지 않은 것 같다
public class Main {
static final int r = 31;
static final int m = 1234567891;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long sum = 0;
long pow = 1;
int size = Integer.parseInt(br.readLine());
String string = br.readLine();
for (int i = 0; i < size; i++) {
sum += ((string.charAt(i) - 96) * pow);
pow = (pow * r) % m;
}
System.out.print(sum % m);
}
}
성공