多个x的n次方模m

2019-04-13 12:37发布

根据最基本的模重复平方进行改进即可 #include using namespace std; int squreMod(long b, long n, long m) { if (0 == n) return 1; return b * squreMod(b, n - 1, m) % m; } int ssqureMod(long a, long A, long b, long B, long m) { if (0 == A && 0 == B) return 1; if(0 == A && 0 != B) return b * squreMod(b, B-1, m); if (0 == B && 0 != A) return a * squreMod(a, A-1, m); else return a * b * ssqureMod(a, A-1, b, B-1, m) % m; } int sssqureMod(long a, long A, long b, long B, long c, long C, long m) { if (0 == A && 0 == B && 0 == C) return 1; if (0 == A && 0 != B && 0 == C) return b * squreMod(b, B - 1, m); if (0 == B && 0 != A && 0 == C) return a * squreMod(a, A - 1, m); if (0 == B && 0 == A && 0 != C) return c * squreMod(c, C - 1, m); if (0 == B && 0 != A && 0 != C) return a * c * ssqureMod(a, A - 1, c, C - 1, m) % m; if (0 != B && 0 != A && 0 == C) return a * b * ssqureMod(a, A - 1, b, B - 1, m) % m; if (0 != B && 0 == A && 0 != C) return b * c * ssqureMod(b, B - 1, c, C - 1, m) % m; else return a * b * c * sssqureMod(a, A - 1, b, B - 1, c, C - 1, m) % m; } int main() { long a; long b; long c; long A; long B; long C; long m; while (cin >>a>>A>> b >>B>>c>>C>>m) { int D = sssqureMod(a, A, b, B, c, C, m); cout << D<