核💗:

        看有人的做法是log求解....好吧我没有看懂

        我想法的核心就是eular定理

       a^eular(y)%y=1   当且仅当a与y互质 (好重要啊,这个条件)

      还有就是如果不互质,我们就提取公因式

    

 1 #include<cstdio>
 2 using namespace std;
 3 typedef long long LL;
 4 LL a, b, c, y;
 5 LL gcd(LL x1, LL y1) {
 6     return y1 == 0 ? x1 : gcd(y1, x1 % y1);
 7 }
 8 LL qpow(LL x1, LL x2, LL mod) {
 9     x1 = x1 % mod;
10     LL ans = 1;
11     while (x2) {
12         if (x2 & 1) ans = ans * x1 % mod;
13         x1 = x1 * x1 % mod;
14         x2 = x2 >> 1;
15     }
16     return ans;
17 }
18 LL eular(LL n) {
19     LL res = n, aa = n;
20     for (int i = 2; i * i <= aa; i++) {
21         if (aa % i == 0) {
22             res = res / i * (i - 1);
23             while (aa % i == 0) aa /= i;
24         }
25     }
26     if (aa > 1) res = res / aa * (aa - 1);
27     return res;
28 }
29 int main ()
30 {
31     //核心: 
32     //    6^t%20=4*( (6^t/4) %5 )  若t>=2   
33     //    若t小于2直接算
34     //    若a与y互质  a^eular(y)=1
35     while (scanf("%lld %lld %lld %lld", &a, &b, &c, &y) != EOF) {
36         LL p = 1;
37         LL k = 0;
38         LL t = gcd(a, y);
39         while (t != 1) {  // a不变,将y变成与a互质
40             p = p * t;
41             y = y / t;
42             k++;
43             t = gcd(a, y);
44         }
45         t = 1;
46         bool flag = 1;
47         for (int i = 1; i <= c; i++) {
48             t = t * b;
49             if (t >= k) {flag = 0; break;}
50         }
51         if (flag) {
52             LL ans = qpow(a, t, y * p);
53             printf("%lld\n", ans);
54             continue;
55         }
56         t = eular(y);
57         LL t1 = qpow(b, c, t);
58         LL t2 = qpow(p, t - 1, y);
59         LL t3 = qpow(a, t1, y);
60         LL ans = p * (t3 * t2 % y);
61         printf("%lld\n", ans);
62     }
63     return 0;
64 }