func_code_string: #include <bits/stdc++.h>   using namespace std;   int A[10010] = {0};   int D;   int N, M, P;   int g;   int fact[20010] = {0};   int fp = 0;   int Hash[20010] = {0};   int place[10010] = {0};   int F[20010] = {0};   long long power(long long a, long long k, long long Mod) {     long long o = 1;     for (; k > 0; k >>= 1) {       if (k & 1) o = o * a % Mod;       a = a * a % Mod;     }     return o;   }   int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }   int getplace(int k) {     for (int l = 1, r = fp; l <= r;) {       int mid = (l + r) >> 1;       if (fact[mid] == k) return mid;       if (fact[mid] > k)         r = mid - 1;       else         l = mid + 1;     }     return 0;   }   int main() {     cin >> N >> M >> P;     for (int i = 1; i * i <= P - 1; i++) {       if ((P - 1) % i != 0) continue;       fact[++fp] = i;       if (i * i != (P - 1)) fact[++fp] = (P - 1) / i;     }     sort(fact + 1, fact + fp + 1);     for (int i = 1; i <= N; i++) {       scanf( %d , &A[i]);       A[i] %= P;       for (int j = 1; j <= fp; j++) {         if (power(A[i], fact[j], P) == 1) {           A[i] = (P - 1) / fact[j];           break;         }       }     }     D = P - 1;     for (int i = 1; i <= M; i++) {       int B;       scanf( %d , &B);       D = gcd(D, B);     }     P = (P - 1) / D;     fp = 0;     for (int i = 1; i * i <= P; i++) {       if (P % i != 0) continue;       fact[++fp] = i;       if (i * i != P) fact[++fp] = P / i;     }     sort(fact + 1, fact + fp + 1);     int Ans = 0;     for (int i = 1; i <= N; i++) {       A[i] = gcd(A[i], P);       int place = getplace(A[i]);       if (Hash[place] == 0) Ans += P / fact[place];       F[place] = 1;       Hash[place] = 1;     }     for (int i = fp; i >= 1; i--) {       for (int j = i + 1; j <= fp; j++) {         if (fact[j] % fact[i] == 0) F[j] += F[i];       }     }     for (int i = 1; i <= fp; i++) {       if (Hash[i] == 1) {         int Need = -(F[i] - 1);         Ans += Need * (P / fact[i]);         for (int j = i + 1; j <= fp; j++)           if (fact[j] % fact[i] == 0) {             Hash[j] = 1;             F[j] += Need;           }       }     }     cout << Ans << endl;     return 0;   }   