結果

問題 No.3296 81-like number
ユーザー sorachandu
提出日時 2025-10-05 13:42:21
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 5,030 bytes
コンパイル時間 2,854 ms
コンパイル使用メモリ 283,708 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-05 13:42:40
合計ジャッジ時間 3,610 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct Init { Init() { ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(13); } }init;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using minpq=priority_queue<T,vector<T>,greater<T>>;

#define rep(i, x, limit) for(int i=(x); i< (limit); ++i)
#define REP(i, x, limit) for(int i=(x); i<=(limit); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define el '\n'
#define spa ' '
#define Yes cout<<"Yes"<<el
#define No  cout<<"No" <<el
#define YES cout<<"YES"<<el
#define NO  cout<<"NO" <<el
#define END(x) cout<<(x)<<el, exit(0)
#define debug(x) cerr<<#x<<" = "<<x<<el

const int inf = 1073741823;
const ll infl = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";

template<typename T1, typename T2>
std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){
    os << "{" << p.first << "," << p.second << "}";
    return os;
}

template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){return a>b?a=b,true:false;}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){return a<b?a=b,true:false;}

// a^bを返す オーバーフローに注意
inline ll Pow(ll a,ll b){
    assert(b>=0);
    if(a==0 and b==0) return 1;
    if(a==1) return 1;
    if(a==-1) return (b&1)?-1:1;
    ll res=1;
    while(b){
        if(b&1) res*=a;
        b>>=1;
        if(b) a*=a;
    }
    return res;
}

// 配列の要素を空白区切りで出力 第二引数をtrueにすると改行区切り
template<typename T> inline void print_vec(const vector<T> &v, bool split_line=false) {
    if(v.empty()){
        cout << "This vector is empty." << el;
        return;
    }
    constexpr bool isValue = is_integral<T>::value;
    for (int i = 0; i < (int)v.size(); i++) {
        if constexpr(isValue){
            if((v[i]==inf) || (v[i]==infl)) cout << 'x' << " \n"[split_line || i+1==(int)v.size()];
            else cout << v[i] << " \n"[split_line || i+1==(int)v.size()];
        }else cout << v[i] << " \n"[split_line || i+1==(int)v.size()];
    }
}

// Pythonのenumerateみたいなやつ [index,value]を範囲for文に提供
template<typename T> inline vector<pair<int,T>> enumerate(const vector<T> &v){
    vector<pair<int,T>> res(ssize(v));
    for(int i=0;i<ssize(v);i++) res[i]={i,v[i]};
    return res;
}
inline vector<pair<int,char>> enumerate(const string &s){
    vector<pair<int,char>> res(ssize(s));
    for(int i=0;i<ssize(s);i++) res[i]={i,s[i]};
    return res;
}

// This function sorts multiple vectors based on the first vector
// and returns the indices of the sorted order.
// Note: First argument is a comparison function.
template <typename Compare, typename... Vectors>
vector<size_t> multipleSort(Compare comp = Compare(), Vectors&... vectors) {
    const size_t size = std::get<0>(std::tie(vectors...)).size();
    ((void)std::initializer_list<int>{(vectors.size() == size ? 0 : 
        throw std::invalid_argument("Vectors must have the same size"))...});

    std::vector<size_t> indices(size);
    std::iota(indices.begin(), indices.end(), 0);

    std::sort(indices.begin(), indices.end(), [&](size_t i, size_t j) {
        return comp(std::get<0>(std::tie(vectors...))[i], std::get<0>(std::tie(vectors...))[j]);
    });

    auto reorder = [&](auto& vec) {
        auto temp=vec;
        for (size_t i = 0; i < size; ++i) {
            vec[i] = temp[indices[i]];
        }
    };
    (reorder(vectors), ...);
    return indices;
}

// エラトステネスの篩, 1以上N以下の整数について素数かどうか判定する
vector<bool> Eratosthenes(ll N) {
    vector<bool> isprime(N + 1, true);
    isprime[0] = isprime[1] = false;

    for (int p = 2; p <= N; p++) {
        if (!isprime[p]) continue;
        for (int q = p * 2; q <= N; q += p) {
            isprime[q] = false;
        }
    }
    return isprime;
}

// 1以上N以下の素数を格納したvectorを返す,Eratosthenes関数と併せて使う
vector<ll> get_primes(ll N){
    vector eratosthenes = Eratosthenes(N);
    vector<ll> primes;
    for(ll i = 2; i <= N; i++){
        if(eratosthenes[i]) primes.emplace_back(i);
    }
    return primes;
}

// 二分探索による、浮動小数点型を介さないsqrt
// 制約:0 <= x <= LLONG_MAX
ll ll_sqrt(ll x){
    assert(0 <= x);
    ll ok = 0, ng = x/2+2;
    while(abs(ok-ng) > 1){
        ll mid = (ok+ng)/2;
        if(x/mid < mid) ng = mid;
        else ok = mid;
    }
    return ok;
}

int main(){
    /*//--------------------------------------------------------
    pは高々sqrt(N)
    nは高々60
    *///--------------------------------------------------------
    ll N;
    cin>>N;
    auto v=get_primes(ll_sqrt(N)+1);
    ll ans=0;
    for(auto e:v){
        __int128_t val=e;
        rep(p,2,60){
            val*=e;
            if(val>N) break;
            ans+=val;
        }
    }
    cout<<ans<<"\n";
}
0