結果

問題 No.1653 Squarefree
ユーザー rianoriano
提出日時 2021-08-20 23:55:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 4,039 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 172,188 KB
実行使用メモリ 16,808 KB
最終ジャッジ日時 2024-04-22 09:28:31
合計ジャッジ時間 6,633 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
16,804 KB
testcase_01 AC 14 ms
8,228 KB
testcase_02 AC 14 ms
8,228 KB
testcase_03 AC 14 ms
8,232 KB
testcase_04 AC 14 ms
8,356 KB
testcase_05 AC 14 ms
8,288 KB
testcase_06 AC 13 ms
8,228 KB
testcase_07 AC 13 ms
8,224 KB
testcase_08 AC 13 ms
8,288 KB
testcase_09 AC 13 ms
8,232 KB
testcase_10 AC 13 ms
8,232 KB
testcase_11 AC 97 ms
16,548 KB
testcase_12 AC 102 ms
16,676 KB
testcase_13 AC 100 ms
16,680 KB
testcase_14 AC 99 ms
16,552 KB
testcase_15 AC 104 ms
16,804 KB
testcase_16 AC 106 ms
16,684 KB
testcase_17 AC 112 ms
16,592 KB
testcase_18 AC 108 ms
16,804 KB
testcase_19 AC 100 ms
16,636 KB
testcase_20 AC 103 ms
16,800 KB
testcase_21 AC 106 ms
16,756 KB
testcase_22 AC 98 ms
16,800 KB
testcase_23 AC 97 ms
16,548 KB
testcase_24 AC 111 ms
16,548 KB
testcase_25 AC 109 ms
16,752 KB
testcase_26 AC 106 ms
16,548 KB
testcase_27 AC 113 ms
16,804 KB
testcase_28 AC 109 ms
16,544 KB
testcase_29 AC 108 ms
16,652 KB
testcase_30 AC 106 ms
16,544 KB
testcase_31 AC 111 ms
16,800 KB
testcase_32 AC 112 ms
16,676 KB
testcase_33 AC 113 ms
16,716 KB
testcase_34 AC 117 ms
16,756 KB
testcase_35 AC 114 ms
16,808 KB
testcase_36 AC 14 ms
8,488 KB
testcase_37 AC 13 ms
8,232 KB
testcase_38 AC 13 ms
8,228 KB
testcase_39 AC 13 ms
8,228 KB
testcase_40 AC 13 ms
8,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Pr pair<ll,ll>
#define Tp tuple<int,int,int>
#define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr);typedef modint<mod> mint
using Graph = vector<vector<int>>;

const ll mod = 1000000007;
template<uint64_t mod>
struct modint{
    uint64_t val;
    constexpr modint(const int64_t val_=0) noexcept:val((val_%int64_t(mod)+int64_t(mod))%int64_t(mod)){}
    constexpr modint operator-() const noexcept{
        return modint(*this)=mod-val;
    }
    constexpr modint operator+(const modint rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint operator-(const modint rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint operator*(const modint rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint operator/(const modint rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint &operator+=(const modint rhs) noexcept{
        val+=rhs.val;
        val-=((val>=mod)?mod:0);
        return (*this);
    }
    constexpr modint &operator-=(const modint rhs) noexcept{
        val+=((val<rhs.val)?mod:0);
        val-=rhs.val;
        return (*this);
    }
    constexpr modint &operator*=(const modint rhs) noexcept{
        val=val*rhs.val%mod;
        return (*this);
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        uint64_t ex=mod-2;
        modint now=1;
        while(ex){
            now*=((ex&1)?rhs:1);
            rhs*=rhs,ex>>=1;
        }
        return (*this)*=now;
    }
    constexpr bool operator==(const modint rhs) noexcept{
        return val==rhs.val;
    }
    constexpr bool operator!=(const modint rhs) noexcept{
        return val!=rhs.val;
    }
    friend constexpr ostream &operator<<(ostream& os,const modint x) noexcept{
        return os<<(x.val);
    }
    friend constexpr istream &operator>>(istream& is,modint& x) noexcept{
        uint64_t t;
        is>>t,x=t;
        return is;
    }
};


//素数判定
// nまでの整数に最小の素因数minfactorを与える
vector<int> min_factor;
vector<int> sieve(int n) {
    vector<int> res(n+1);
    iota(res.begin(), res.end(), 0);
    for (int i = 2; i*i <= n; ++i) {
        if (res[i] < i) continue;
        for (int j = i*i; j <= n; j += i) {
            if (res[j] == j) res[j] = i;
        }
    }
    return res;
}

// nの素因数分解(上の"sieve"を前提とする)
// (p1,n1),(p2,n2),...
vector<pair<int,int>> factor(int n) {
    // min_factor は sieve() で得られたものとする
    vector<pair<int,int>> res; int p=-1,cnt=0;
    while (n > 1) {
        if(min_factor[n]!=p&&cnt>0){
            res.push_back(make_pair(p,cnt));
            p = min_factor[n]; cnt = 1;
        }
        else if(min_factor[n]!=p){
            p = min_factor[n]; cnt = 1;
        }
        else cnt++;
        n /= min_factor[n];
        // 割った後の値についても素因数を知っているので順次求まる
    }
    if(p!=-1) res.push_back(make_pair(p,cnt));
    return res;
}
    

int main() {
    riano_; //ll ans = 0;
    ll L,R; cin >> L >> R;
    //main関数内
    ll n = 1000001;
    min_factor = sieve(n); //nまでの素数判定
    //vector<pair<int,int>> primefac = factor(k); //kの素因数分解
    vector<ll> pr;
    rep(i,1000001){
        if(i>=2&&min_factor[i]==i){
            pr.push_back((ll)i);
        }
    }
    ll num[R-L+1]; bool sf[R-L+1];
    rep(i,R-L+1){
        num[i] = L+i; sf[i] = true;
    }
    for(ll x:pr){
        ll sx = x*x;
        ll t = (L-1)/x;
        ll s = x*t+x;
        for(ll j=s;j<=R;j+=x){
            ll k = j-L;
            if(j%sx==0) sf[k] = false;
            if(!sf[k]) continue;
            num[k] /= x;
        }
    }
    ll ans = 0;
    rep(i,R-L+1){
        if(!sf[i]) continue;
        ll ss = floor(sqrt(num[i]));
        if(num[i]!=1&&ss*ss==num[i]) continue;
        ans++;
    }
    cout << ans << endl;
}
0