백준 2526 java : 구현

magicdrill·2025년 12월 22일

백준 문제풀이

목록 보기
673/673

백준 2526 java : 구현

import java.util.ArrayList;
import java.util.Scanner;

public class bj2526 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N, P;

        N = sc.nextInt();
        P = sc.nextInt();
        sc.close();

        int current = N;
        int next = (current * current) % P;

        ArrayList<Integer> lists = new ArrayList<>();
        lists.add(current);
        while (!lists.contains(next)) {
            System.out.println("next : " + next);
            System.out.println(lists.indexOf(next));
            lists.add(next);
            next = (next * N) % P;
        }

        System.out.println("next : " + next);
        System.out.println(lists.indexOf(next));
        System.out.println(lists.size() - lists.indexOf(next));
    }
}

0개의 댓글