結果

問題 No.1653 Squarefree
ユーザー merlinmerlin
提出日時 2022-05-03 06:10:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,910 bytes
コンパイル時間 1,831 ms
コンパイル使用メモリ 169,248 KB
実行使用メモリ 90,076 KB
最終ジャッジ日時 2023-09-15 05:20:09
合計ジャッジ時間 67,278 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 2 ms
5,396 KB
testcase_02 AC 2 ms
5,416 KB
testcase_03 AC 2 ms
5,420 KB
testcase_04 AC 2 ms
5,412 KB
testcase_05 AC 2 ms
5,348 KB
testcase_06 AC 2 ms
5,488 KB
testcase_07 AC 2 ms
5,400 KB
testcase_08 AC 2 ms
5,408 KB
testcase_09 AC 2 ms
5,492 KB
testcase_10 AC 2 ms
5,396 KB
testcase_11 AC 2 ms
5,468 KB
testcase_12 AC 2 ms
5,352 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 AC 1,890 ms
85,252 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

//borderline code....maybe ac maybe tle
#include<bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
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