結果

問題 No.1653 Squarefree
ユーザー merlinmerlin
提出日時 2021-08-21 00:49:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,575 ms / 2,000 ms
コード長 1,768 bytes
コンパイル時間 1,983 ms
コンパイル使用メモリ 166,832 KB
実行使用メモリ 90,208 KB
最終ジャッジ日時 2024-04-22 10:18:15
合計ジャッジ時間 49,996 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,512 ms
89,928 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1,542 ms
89,928 KB
testcase_14 AC 1,541 ms
90,072 KB
testcase_15 AC 1,567 ms
89,928 KB
testcase_16 AC 1,559 ms
90,056 KB
testcase_17 AC 1,552 ms
90,208 KB
testcase_18 AC 1,574 ms
90,060 KB
testcase_19 AC 1,541 ms
90,056 KB
testcase_20 AC 1,567 ms
90,104 KB
testcase_21 AC 1,557 ms
90,056 KB
testcase_22 AC 1,561 ms
90,112 KB
testcase_23 AC 1,567 ms
90,100 KB
testcase_24 AC 1,542 ms
89,796 KB
testcase_25 AC 1,575 ms
90,056 KB
testcase_26 AC 1,549 ms
90,056 KB
testcase_27 AC 1,542 ms
90,060 KB
testcase_28 AC 1,406 ms
83,536 KB
testcase_29 AC 1,558 ms
89,936 KB
testcase_30 AC 1,550 ms
89,928 KB
testcase_31 AC 1,544 ms
90,108 KB
testcase_32 AC 1,546 ms
89,928 KB
testcase_33 AC 1,537 ms
90,060 KB
testcase_34 AC 1,531 ms
90,056 KB
testcase_35 AC 1,524 ms
89,980 KB
testcase_36 AC 1,569 ms
89,940 KB
testcase_37 AC 1,574 ms
90,028 KB
testcase_38 AC 1,546 ms
89,764 KB
testcase_39 AC 1,535 ms
90,056 KB
testcase_40 AC 1,541 ms
90,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int N=17705360;
bool p[N]; int mob[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)
{
    if(n==0) return 1;
    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/
int squarefree(ll n,int imax,int d)
{
    if(d==0) return 0;
    vector<int> mxi;
    int sum=0,i;
    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-=(root/j-root/(j+1))*mob[j];

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

int main()
{
    ll a,b;
    cin>>a>>b;
    a--;
    int imax=maxpower(b,5),ima=maxpower(a,5);
    int d=maxpower(b/imax,2),d2=maxpower(a/ima,2);

    int dm=max(d,d2);
    mobius(dm);
    ll s1=0,s2=0; int i;
    mob[0]=0;
    for(i=1;i<=dm;i++)
    {
        if(i<=d) s1+=(b/(1LL*i*i))*mob[i];
        if(i<=d2) s2+=(a/(1LL*i*i))*mob[i];
        mob[i]+=mob[i-1];
    }

    s1+=squarefree(b,imax,d); s2+=squarefree(a,ima,d2);
    cout<<(s1-s2)<<"\n";
    return 0;
}
0