結果
問題 | No.917 Make One With GCD |
ユーザー |
|
提出日時 | 2019-09-09 05:34:50 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,333 bytes |
コンパイル時間 | 1,818 ms |
コンパイル使用メモリ | 177,500 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-28 00:33:26 |
合計ジャッジ時間 | 6,588 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 RE * 1 |
other | AC * 3 RE * 29 |
ソースコード
#include <bits/stdc++.h> using namespace std; template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;} template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} void print(void) {cout << endl;} template <class Head> void print(Head&& head) {cout << head;print();} template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);} //divisor O(sqrt(N)) set<long long> Divisor(long long N) { set<long long> ret; for (long long i = 1; i*i <= N; ++i) { if (N%i == 0) { ret.insert(i); ret.insert(N / i); } } return ret; } //Greatest Common Divisor long long GCD(long long a, long long b) { return ((b == 0) ? a : GCD(b, a % b)); } //GCD随時計算ver. logで落ちそう int main() { int N,MAX_A = 2000; cin >> N; vector<int> A(N); for(int i = 0; i < N; ++i) cin >> A[i]; // dp[i][j] := (最後にA[i]を使ってgcdがjになるような場合の数) vector<vector<long long>> dp(N, vector<long long>(MAX_A+1,0)); long long ans = 0; for(int i = 0; i < N; ++i) { dp[i][A[i]]++; auto d = Divisor(A[i]); for(int j = i+1; j < N; ++j) { for(auto k:d){ dp[j][GCD(k,A[j])] += dp[i][k]; } } ans += dp[i][1]; } cout << ans << endl; return 0; }