結果

問題 No.719 Coprime
ユーザー milanis48663220milanis48663220
提出日時 2021-05-13 23:46:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 250 ms / 3,000 ms
コード長 3,207 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 133,024 KB
実行使用メモリ 5,056 KB
最終ジャッジ日時 2023-10-26 03:42:02
合計ジャッジ時間 4,250 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 8 ms
4,348 KB
testcase_54 AC 28 ms
4,348 KB
testcase_55 AC 69 ms
4,348 KB
testcase_56 AC 188 ms
4,528 KB
testcase_57 AC 188 ms
4,528 KB
testcase_58 AC 246 ms
4,792 KB
testcase_59 AC 250 ms
5,056 KB
testcase_60 AC 250 ms
5,056 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In lambda function:
main.cpp:91:13: warning: control reaches end of non-void function [-Wreturn-type]
   91 |             }(x);
      |             ^

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

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; }

using namespace std;
typedef long long ll;
using P = pair<int, int>;

class Eratosthenes{
    public:
    int m;
    vector<bool> is_prime;
    vector<int> primes;
    Eratosthenes(int m_){
        m = m_;
        init();
    }
    private:
    void init(){
        is_prime.assign(m+1, true);
        is_prime[0] = false, is_prime[1] = false;
        for(int i = 2; i <= m; i++){
            if(is_prime[i]){
                primes.push_back(i);
                for(int j = 2; i*j <= m; j++) is_prime[i*j] = false;
            }
        }
    }
};

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


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    Eratosthenes erat(n);
    int m = erat.primes.size();
    vector<int> p;
    for(int x : erat.primes){
        if(x*x > n) break;
        p.push_back(x);
    }
    int k = p.size();
    
    vector<vector<int>> facs(1<<k);
    for(int i = 2; i <= n; i++){
        auto v = factorize(i);
        int s = 0;
        for(auto [x, _]: v){
            if(x*x > n) {
                s = -1;
                break;
            }
            int idx = [&](int x){
                for(int i = 0; i < k; i++){
                    if(p[i] == x) return i;
                }
            }(x);
            s |= (1<<idx);
        }
        if(s > 0) facs[s].push_back(i);
    }
    facs[0].push_back(1);
    vector<int> u;
    for(int i = 0; i < (1<<k); i++) {
        if(!facs[i].empty()) u.push_back(i);
    }
    // for(int i = 0; i < (1<<k); i++){
    //     for(int x : facs[i]) cout << x << ' ';
    //     cout << endl;
    // }
    vector<vector<int>> dp(m+1, vector<int>(1<<k));
    for(int i = 0; i < (1<<k); i++){
        for(int j : u){
            if(i&j) continue;
            if(j == 0) continue;
            chmax(dp[k][i|j], dp[k][i]+facs[j].back());
        }
    }

    for(int i = k; i < m; i++){
        int q = erat.primes[i];
        for(int j = 0 ; j < (1<<k); j++){
            for(int l : u){
                if((j&l) != 0) continue;
                for(int f : facs[l]){
                    if(f*q <= n) {
                        chmax(dp[i+1][j+l], dp[i][j]+f*q); 
                    }
                }
            }
        }
    }
    cout << dp[m][(1<<k)-1] << endl;
}
0