結果
問題 | No.917 Make One With GCD |
ユーザー | ningenMe |
提出日時 | 2019-09-13 00:32:40 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 152 ms / 2,000 ms |
コード長 | 2,470 bytes |
コンパイル時間 | 1,958 ms |
コンパイル使用メモリ | 183,324 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-24 11:02:44 |
合計ジャッジ時間 | 3,241 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 9 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 152 ms
6,940 KB |
testcase_07 | AC | 60 ms
6,940 KB |
testcase_08 | AC | 6 ms
6,944 KB |
testcase_09 | AC | 10 ms
6,944 KB |
testcase_10 | AC | 10 ms
6,940 KB |
testcase_11 | AC | 9 ms
6,940 KB |
testcase_12 | AC | 13 ms
6,944 KB |
testcase_13 | AC | 8 ms
6,940 KB |
testcase_14 | AC | 9 ms
6,940 KB |
testcase_15 | AC | 10 ms
6,944 KB |
testcase_16 | AC | 9 ms
6,944 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 4 ms
6,944 KB |
testcase_19 | AC | 2 ms
6,940 KB |
testcase_20 | AC | 2 ms
6,940 KB |
testcase_21 | AC | 3 ms
6,940 KB |
testcase_22 | AC | 3 ms
6,944 KB |
testcase_23 | AC | 5 ms
6,944 KB |
testcase_24 | AC | 3 ms
6,944 KB |
testcase_25 | AC | 6 ms
6,944 KB |
testcase_26 | AC | 2 ms
6,944 KB |
testcase_27 | AC | 2 ms
6,944 KB |
testcase_28 | AC | 2 ms
6,944 KB |
testcase_29 | AC | 2 ms
6,944 KB |
testcase_30 | AC | 3 ms
6,944 KB |
testcase_31 | AC | 2 ms
6,940 KB |
testcase_32 | AC | 2 ms
6,944 KB |
testcase_33 | AC | 2 ms
6,944 KB |
testcase_34 | AC | 2 ms
6,940 KB |
testcase_35 | AC | 2 ms
6,940 KB |
ソースコード
#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)); } int main() { int N; cin >> N; assert(1<= N && N <= 50); vector<int> A(N); for(int i = 0; i < N; ++i) cin >> A[i]; for(int i = 0; i < N; ++i) assert(1 <= A[i] && A[i] <= 100000000); // dp[i][j] := (最後にA[i]を使ってgcdがjになるような場合の数) vector<map<int,long long>> dp(N); long long ans = 0; //dp探索 O(N) for(int i = 0; i < N; ++i) { dp[i][A[i]]++; // O( sqrt(A) ) auto d = Divisor(A[i]); // O( N*M*(log(A)+log(M)) ) M = d.size() 10^8以下の最大の約数の個数は768個 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; }