結果

問題 No.719 Coprime
ユーザー あり
提出日時 2023-02-09 22:26:18
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,471 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 91,548 KB
実行使用メモリ 139,648 KB
最終ジャッジ日時 2024-07-06 22:57:25
合計ジャッジ時間 6,414 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 43 TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <numeric>
#include <queue>
#include <cassert>
#include <cmath>
#include <bitset>
using namespace std;

vector<int> getPrimes(int n) {
  vector<bool> isPrime(n+1, true);
  for (int i = 2; i <= n; i++)
    if (isPrime[i])
      for (int j = i+i; j <= n; j += i)
        isPrime[j] = false;
  
  vector<int> primes;
  for (int i = 2; i <= n; i++)
    if (isPrime[i])
      primes.push_back(i);
  return primes;
}

vector<int> factorize(int n) {
  vector<int> res;
  for (int i = 2; i*i <= n; i++)
    if (n%i == 0) {
      while (n%i == 0)
        n /= i;
      res.push_back(i);
    }
  if (n != 1) res.push_back(n);
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<int> primes = getPrimes(n);
  vector<vector<int>> factotials(n+1);
  for (int i = 2; i <= n; i++) factotials[i] = factorize(i);
  //for (int i = 2; i <= n; i++) { cout << i << " : "; for (auto f : factotials[i]) cout << f << " "; cout << endl; }
  int m = primes.size();
  vector<int> dp(1<<m, 0);
  for (int s = 0; s < (1<<m); s++)
    for (int i = 2; i <= n; i++) {
      bool flag = true;
      int t = 0;
      for (auto f : factotials[i]) {
        int j = lower_bound(primes.begin(), primes.end(), f) - primes.begin();
        t += (1<<j);
        if (s&(1<<j)) flag = false;
      }
      if (flag) dp[s+t] = max(dp[s+t], dp[s] + i);
    }
  cout << dp[(1<<m)-1] << endl;
}
0