알고리즘

C++ 알고리즘 - 백준 4375 1 (모듈러 연산 원칙 관련)

마루설아 2025. 1. 19. 15:45

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

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

void CPP_INIT() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

int main(void) {
	CPP_INIT();


	int num;
	
	while (true) {
		cin >> num;

		if (cin.eof()) return 0;

		int chk = 1;
		int jari = 1;

		while (true) {
			if (chk % num == 0) {
				cout << jari << endl;
				break;
			}

			chk = chk * 10 + 1;
			chk %= num;
			jari++;
		}
	}	
}

 

 

(A mod B) mod B  ==  A mod B

 

chk %= num; 을 해준 후 chk % num을 한번 더 하여, chk 값이 long long의 값보다 커지지 않도록 방지