結果

問題 No.1653 Squarefree
ユーザー milanis48663220milanis48663220
提出日時 2021-08-20 22:44:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 2,256 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 123,920 KB
最終ジャッジ日時 2025-01-24 00:05:49
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

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;

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


// calc a//b 
template<typename T>
T ceil_div(T a, T b){
    return (a+b-1)/b;
}

ll sqrt_ll(ll x){
    ll ans = sqrt(x)+0.1;
    if(ans*ans > x) ans--;
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    auto erat = Eratosthenes(1000001);
    ll l, r; cin >> l >> r;
    int n = r-l+1;
    vector<bool> square_free(n, true);
    vector<ll> val(n);
    for(int i = 0; i < n; i++) val[i] = l+i;
    for(ll p : erat.primes){
        ll x = ceil_div(l, p);
        for(ll y = x*p; y <= r; y += p){
            int idx = y-l;
            // debug_value(y)
            if(val[idx]%(p*p) == 0){
                square_free[idx] = false;
            }
            while(val[idx]%p == 0) val[idx] /= p;
        }
    }
    int ans = 0;
    for(ll x = l; x <= r; x++){
        int idx = x-l;
        if(!square_free[idx]) continue;
        ll sq = sqrt_ll(val[idx]);
        // cout << val[idx] << endl;
        if(sq > 1000000 && sq*sq == val[idx]) continue;
        ans++;
    }
    ll x = 1e15;
    // cout << x-1000000 << ' ' << x << endl;
    cout << ans << endl;
}
0