結果

問題 No.719 Coprime
ユーザー pionepione
提出日時 2020-06-11 02:49:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 3,000 ms
コード長 3,446 bytes
コンパイル時間 1,694 ms
コンパイル使用メモリ 170,344 KB
実行使用メモリ 6,560 KB
最終ジャッジ日時 2023-09-06 04:59:42
合計ジャッジ時間 5,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,384 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 1 ms
4,384 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 3 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,384 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 4 ms
4,380 KB
testcase_53 AC 9 ms
4,376 KB
testcase_54 AC 29 ms
4,444 KB
testcase_55 AC 58 ms
4,968 KB
testcase_56 AC 143 ms
5,916 KB
testcase_57 AC 143 ms
6,016 KB
testcase_58 AC 186 ms
6,560 KB
testcase_59 AC 191 ms
6,496 KB
testcase_60 AC 190 ms
6,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const ll INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

const ll maxn_eratosthenes = 100005;
bool _is_prime[maxn_eratosthenes];

vll eratosthenes(const ll N)
{
  vll P;
  for (ll i = 0; i <= N; i++) {
    _is_prime[i] = true;
  }
  _is_prime[0]=_is_prime[1]=false;
  for (ll i = 2; i <= N; i++) {
    if (_is_prime[i]) {
      for (ll j = 2 * i; j <= N; j += i) {
        _is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}

const ll maxn=1<<11; // 11 ... sqrt(1262) までの素数の個数

ll dp1[210][maxn]; // (√N以上の素数について)最大の素因数がi番目の素数までのものである数で作れる最大和、ただし√N未満の素数の使用状況はsである
ll dp2[maxn];      // (√N未満の素数について)素数の使用状況がsの時の最大和

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  ll N; cin>>N;
  
  // cout<<eratosthenes(1262).size()<<endl;
  // cout<<eratosthenes(sqrt(1262)).size()<<endl;
  
  auto p=eratosthenes(N);
  auto rp=eratosthenes(sqrt(N)); // root Nまでの素数
  
  ll n=p.size(), m=rp.size();
  
  irep(i,m,n) rep(s,1<<m){
    for(ll j=1; j*p[i]<=N; j++){
      ll now=j*p[i];
      ll msk=0;
      rep(k,m) if(now%rp[k]==0) msk|=(1<<k);
      
      if((msk&s)==0) chmax(dp1[i+1][s|msk],dp1[i][s]+now);
    }
  }
  
  // 2~Nまでの単一の数についてdp2を更新
  for(ll i=2; i<=N; i++){
    ll s=0;  // iに含まれる√Nまでの素数
    ll ok=1; // iに√N以上の素数を含まないか
    rep(j,n){
      if(i%p[j]==0){
        if(j<m) s+=(1<<j);
        else ok=0;
      }
    }
    if(ok) chmax(dp2[s], i);
  }
  
  // 複数の数の組合せについてdp2を更新
  rep(s,1<<m) for(ll msk=s; msk>=0; msk--){
    msk&=s; // sになりmskのビットを削ぎ落す
    chmax(dp2[s],dp2[msk]+dp2[s-msk]);
  }
  
  ll ans=0;
  rep(s,1<<m) chmax(ans,dp1[n][s]+dp2[(1<<m)-1-s]);
  cout<<ans<<endl;
  
}
0