-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathB.java
70 lines (50 loc) · 1.16 KB
/
B.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @author nellex
* Created at: 27-Aug-2016 1:42:14 AM
*/
import java.util.*;
public class CF603B {
/**
* @param args
*/
static long p, k;
static int next[] = new int[1000005];
static boolean explore[] = new boolean[1000005];
static void dfs( int x ) {
if( explore[x] )
return;
explore[x] = true;
dfs( next[x] );
}
static long MOD = (long)(1e9)+7;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
p = in.nextInt();
k = in.nextInt();
if( k == 0 ) {
long val = 1;
for( int i = 0; i < p-1; i++ )
val = ( val * p )%MOD;
System.out.println(val);
return;
}
if( k == 1 ) {
long val = 1;
for( int i = 0; i < p; i++ )
val = ( val * p )%MOD;
System.out.println(val);
return;
}
for( int i = 0; i < p; i++ )
next[ (int) ((k*i)%p) ] = i;
Arrays.fill( explore, false );
long ans = 1;
for( int i = 0; i < p; i++ ) {
if( explore[i] || next[i] == i ) continue;
dfs(i);
ans = ( ans * p )%MOD;
}
System.out.println(ans);
}
}