/** * author: USERNAME * created: yyyy-mm-dd hh:mm:ss **/ #include using namespace std; // clang-format off /* accelration */ // 高速バイナリ生成 #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") // cin cout の結びつけ解除, stdioと同期しない(入出力非同期化) // cとstdの入出力を混在させるとバグるので注意 struct Fast {Fast() {std::cin.tie(0); ios::sync_with_stdio(false);}} fast; /* alias */ // type using ull = unsigned long long; using ll = long long; using ld = long double; // pair using pii = pair; // vector using vi = vector; using vl = vector; using vll = vector; using vvi = vector; using vvl = vector; using vvll = vector; using vs = vector; using vpii = vector; // unordered set using usi = unordered_set; using usll = unordered_set; using uss = unordered_set; /* define short */ #define pb push_back #define mp make_pair #define um unordered_map #define all(obj) (obj).begin(), (obj).end() #define YESNO(bool) if(bool){cout<<"YES"<=0;i--) #define rrepd(i,n) for(ll i=n;i>=1;i--) /* debug */ // 標準エラー出力を含む提出はrejectされる場合もあるので注意 #define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl; /* func */ inline int in_int() {int x; cin >> x; return x;} inline ll in_ll() {ll x; cin >> x; return x;} inline double in_double() {{double x; cin >> x; return x;}} inline string in_str() {string x; cin >> x; return x;} inline int ctoi(char c) {return c - '0';} // vector_finder: (arg)elementを vectorの先頭から(arg)search_lengthまで先頭から検索し、boolを返す // (arg)search_length: 走査するベクトル長の上限(先頭から何要素目までを検索対象とするか、1始まりで) template inline bool vector_finder(std::vector vec, T element, unsigned int search_length) { auto itr = std::find(vec.begin(), vec.end(), element); size_t index = std::distance( vec.begin(), itr ); if (index == vec.size() || index >= search_length) {return false;} else {return true;} } template inline void print(const vector& v, string s = " ") {rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");} template inline void print(const pair& p) {cout << p.first << " " << p.second << endl;} template inline void print(const T& x) {cout << x << "\n";} template inline void print(const vector>& v) {for (auto&& p : v) print(p);} template inline void print(const map& m) {for (auto&& p : m) print(p);} // 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き template inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;} template inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;} // gcd lcm // C++17からは標準実装 // template T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);} // template inline T lcm(T a, T b) {return (a * b) / gcd(a, b);} // clang-format on long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } void recursive_comb(int *indexes, int s, int rest, std::function f) { if (rest == 0) { f(indexes); } else { if (s < 0) return; recursive_comb(indexes, s - 1, rest, f); indexes[rest - 1] = s; recursive_comb(indexes, s - 1, rest - 1, f); } } // nCkの組み合わせに対して処理を実行する void foreach_comb(int n, int k, std::function f) { int indexes[k]; recursive_comb(indexes, n - 1, k, f); } int main() { auto N = in_int(); auto P = in_int(); auto Q = in_int(); vll A(N); rep(idx, N){ A.at(idx) = in_ll(); } sort(all(A)); um mod_P_pow_5; for (const auto& a_elm : A){ auto key = modpow(5, a_elm, P); if (!mod_P_pow_5.contains(key)){ mod_P_pow_5.emplace(key, vll()); } mod_P_pow_5.at(key).push_back(a_elm); } ll ctr = 0; foreach_comb(N, 3, [&A, P, Q, &mod_P_pow_5, &ctr](int *indexes) { auto a = A.at(indexes[0]); auto b = A.at(indexes[1]); auto c = A.at(indexes[2]); auto c_mod = (modpow(10, a, P) + modpow(9, b, P) + modpow(7, c, P)) % P; auto rest_mod = (Q - c_mod) % P; if (mod_P_pow_5.contains(rest_mod)){ ctr += mod_P_pow_5.at(rest_mod).size(); ctr -= upper_bound(all(mod_P_pow_5.at(rest_mod)), c) - mod_P_pow_5.at(rest_mod).begin(); } }); print(ctr); return 0; }