結果

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

ソースコード

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