結果

問題 No.1653 Squarefree
ユーザー merlinmerlin
提出日時 2021-08-20 23:06:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,593 bytes
コンパイル時間 2,278 ms
コンパイル使用メモリ 197,272 KB
実行使用メモリ 167,808 KB
最終ジャッジ日時 2024-04-22 07:18:07
合計ジャッジ時間 8,695 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

const int N=17705360;
bool p[N]; int mob[N],mlist[N];
void mobius(int n)
{
    int i,j;
    for(i=0;i<=n;i++) {p[i]=false; mob[i]=1;}

    for(i=2;i<=n;i++)
    if(!p[i])
    {
        mob[i]=-1;
        for(j=2;j<=n/i;j++)
        {
            p[i*j]=true;
            mob[i*j]*=-1;
        }
        ll sq=1LL*i*i;
        for(j=1;j<=n/sq;j++)
        mob[j*i*i]=0;
    }
}

int maxpower(ll n,int p)
{
    int l=1,r=(int)1e9,m,ans,i;
    if(p==5) r=3190;
    ans=r;
    while(l<=r)
    {
        m=(l+r)>>1;
        ll v=1;
        for(i=0;i<p;i++) v*=m;
        if(v<=n)
        {
            ans=m;
            l=m+1;
        }
        else r=m-1;
    }
    return ans;
}

//https://smsxgz.github.io/post/pe/counting_square_free_numbers/
ll squarefree(ll n)
{
    if(n<3) return n;

    int imax=maxpower(n,5);
    int d=maxpower(n/imax,2);
    mobius(d);
    ll s1=0; int i;
    for(i=1;i<=d;i++) s1+=(n/(1LL*i*i))*mob[i];
    mlist[0]=0;
    for(i=1;i<=d;i++) mlist[i]=mlist[i-1]+mob[i];
    vector<int> mxi;
    int sum=0;
    for(i=imax-1;i>0;i--)
    {
        int cur=1;
        int root=maxpower(n/i,2),sqd=maxpower(root,2),j;
        for(j=1;j<=root/(sqd+1);j++) cur-=1LL*(root/j-root/(j+1))*mlist[j];

        for(j=2;j<=sqd;j++)
        if(root/j<=d) cur-=mlist[root/j];
        else cur-=mxi[imax-j*j*i-1];
        mxi.push_back(cur);
        sum+=cur;
    }
    return s1+sum-(imax-1)*mlist[d];
}

int main()
{
    ll a,b;
    cin>>a>>b;
    a--;
    cout<<(squarefree(b)- squarefree(a));
    return 0;
}
0