알고리즘

C++ 알고리즘 - 백준 15829 Hashing

마루설아 2024. 12. 28. 16:00

https://www.acmicpc.net/problem/15829

 

#include <iostream>
#define endl "\n"

using namespace std;

int main(void) {
	// C++ Init
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int input1;
	long long answer = 0;
	long long hash = 1;
	char input2;

	cin >> input1;
	for (int i = 0; i < input1; i++) {
		cin >> input2;
		answer += ((input2 - '0' - 48) * hash) % 1234567891;
		hash *= 31;
		hash %= 1234567891;
	}

	cout << answer % 1234567891;
}