백준 26008 문제풀이

오늘내일·2023년 8월 11일
0

코딩테스트

목록 보기
1/4

오늘도 즐거운 문제풀이 시작!!(이 땐 몰랐지 내 머리를 쥐어뜯게 될 줄은)

백준 문제 26008☠️☠️


문제를 보고 처음엔 오 중복순열로 풀면 되겠다 하고 첨으로 재귀함수도 써봐야지 하고 신나게 풀었지..

import java.util.Scanner;
import java.util.stream.IntStream;
import java.util.stream.Stream;


public class Main2_false {
    //비밀번호 배열이랑 해쉬값 받을 해쉬맵 만들기
    int cnt = 0;
    public void passwordPermutation(int[] mType, int depth, int n, int a, Integer[] password, int inputHash){

        if (depth == n) {
            int hashResult = 0;
            for (int i = 0; i < password.length; i++) {
                System.out.print(password[i]+"  ");
                hashResult += password[i]*Math.pow(a, i) ;
            }
            System.out.print(" = " + hashResult + " ");
            System.out.println(hashResult % mType.length);

            return ;
        }

        for (int i = 0; i < mType.length; i++) {
            password[depth] = mType[i];
            passwordPermutation(mType, depth + 1, n, a, password, inputHash);
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] input1 = sc.nextLine().split(" ");
        int[] inputNum1 = Stream.of(input1).mapToInt(Integer::parseInt).toArray();

        Integer[] password =  new Integer[inputNum1[0]];

        int inputHash = Integer.parseInt(sc.nextLine());

        Main2_false m = new Main2_false();


        //m 종류의 문자 만들기(0, 1, 2, ..., m-1)
        int[] mType = IntStream.range(0, inputNum1[1]).toArray();

        m.passwordPermutation(mType, 0, inputNum1[0], inputNum1[2], password, inputHash);
    }
}

결과는.. StackOverFlow Error 큰 숫자가 들어가니까 안 돌아간다...😭

풀이 정리👹

어떻게 풀어야 머리를 쥐어뜯길 몇시간..🤪 포기하고 검색해도 잘 모르겠어서 대충 정리를 밑에처럼 했다

사라진 내 아까운 시간들...

다시 또 풀어보니 숫자가 크면 Math.pow()도 계산을 못 하는 거 같아서 모듈러 연산의 성질을 참고해서 풀었다.

(A * B) mod C = (A mod C * B mod C) mod C

알고리즘 파트 가면 이런 것도 많이 알아야 될려나.. 벌써 좀 후달림..💦

암튼 풀이는 요렇게 간단했었었었었다아아아

import java.util.Scanner;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] input1 = sc.nextLine().split(" ");
        int[] inputNum1 = Stream.of(input1).mapToInt(Integer::parseInt).toArray();

        int inputHash = Integer.parseInt(sc.nextLine());

        long result = 1l;

        for (int i = 0; i < inputNum1[0] -1; i++) {
            result = (result * inputNum1[1])%1000000007;
        }
        System.out.println(result);
    }
}

생각해보면..

다들 이렇게 머리 쥐어 뜯어가면서 실력 짱짱한 개발자가 되겠지..
와이프가 말했다. 개발자는 삽질이 default라고..ㅠ
다 피가 되고 살이 되것지..!👍

profile
다시 시작합니다.

0개의 댓글