結果

問題 No.917 Make One With GCD
ユーザー snow39snow39
提出日時 2019-10-25 23:29:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,263 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 104,384 KB
実行使用メモリ 5,652 KB
最終ジャッジ日時 2023-09-06 16:40:40
合計ジャッジ時間 2,292 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
5,424 KB
testcase_01 AC 2 ms
5,396 KB
testcase_02 AC 2 ms
5,384 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,372 KB
testcase_05 AC 2 ms
5,420 KB
testcase_06 AC 9 ms
5,536 KB
testcase_07 AC 5 ms
5,436 KB
testcase_08 AC 2 ms
5,500 KB
testcase_09 AC 6 ms
5,440 KB
testcase_10 AC 6 ms
5,428 KB
testcase_11 AC 6 ms
5,448 KB
testcase_12 AC 6 ms
5,652 KB
testcase_13 AC 5 ms
5,428 KB
testcase_14 AC 5 ms
5,452 KB
testcase_15 AC 5 ms
5,508 KB
testcase_16 AC 5 ms
5,424 KB
testcase_17 AC 2 ms
5,416 KB
testcase_18 AC 3 ms
5,428 KB
testcase_19 AC 2 ms
5,476 KB
testcase_20 AC 2 ms
5,484 KB
testcase_21 AC 3 ms
5,516 KB
testcase_22 AC 2 ms
5,380 KB
testcase_23 AC 4 ms
5,432 KB
testcase_24 AC 3 ms
5,412 KB
testcase_25 AC 4 ms
5,512 KB
testcase_26 AC 2 ms
5,440 KB
testcase_27 AC 1 ms
5,476 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,480 KB
testcase_30 AC 2 ms
5,492 KB
testcase_31 AC 2 ms
5,396 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,492 KB
testcase_34 AC 2 ms
5,500 KB
testcase_35 AC 2 ms
5,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

ll dp[2][1000010];

ll gcd(ll a,ll b){
  return b==0?a:gcd(b,a%b);
}

void add(ll &a,ll b){
  a=(a+b);
}

int main(){
  int N;
  cin>>N;
  vector<ll> A(N);
  rep(i,N) cin>>A[i];

  map<ll,int> mp;
  int cnt=1;
  vector<ll> v;
  v.push_back(0);
  for(int i=0;i<N;i++){
    for(ll j=1;j*j<=A[i];j++){
      if(A[i]%j==0){
        if(mp[j]==0){
          mp[j]=cnt++;
          v.push_back(j);
        }
        if(mp[A[i]/j]==0){
          mp[A[i]/j]=cnt++;
          v.push_back(A[i]/j);
        }
      }
    }
  }

  int t=0;
  dp[0][0]=1;
  for(int i=0;i<N;i++){
    for(int k=0;k<cnt;k++){
      if(dp[t][k]==0) continue;
      add(dp[1-t][k],dp[t][k]);
      ll val=v[k];
      if(val==0) add(dp[1-t][mp[A[i]]],dp[t][k]);
      else{
        ll z=gcd(val,A[i]);
        add(dp[1-t][mp[z]],dp[t][k]);
      }
    }

    for(int k=0;k<cnt;k++) dp[t][k]=0;
    t=1-t;
  }

  ll ans=dp[t][mp[1]];
  cout<<ans<<endl;

  return 0;
}
0