結果

問題 No.1653 Squarefree
ユーザー merlinmerlin
提出日時 2021-08-20 22:41:36
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,184 bytes
コンパイル時間 2,860 ms
コンパイル使用メモリ 88,688 KB
実行使用メモリ 706,696 KB
最終ジャッジ日時 2024-04-22 05:36:27
合計ジャッジ時間 9,441 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
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 #

import java.io.*;
import java.util.*;

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        long a=Long.parseLong(s[0]),b=Long.parseLong(s[1]);
        sb.append(squarefree(b)-squarefree(a-1)+"\n");
        System.out.print(sb);
    }

    static int[] mobius(int n)
    {
        boolean p[]=new boolean[n+1];
        int i,j,mob[]=new int[n+1];
        Arrays.fill(mob,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;
            }
            long sq=1l*i*i;
            for(j=1;j<=n/sq;j++)
            mob[j*i*i]=0;
        }
        return mob;
    }

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

        int imax=maxpower(n,5);
        int d=maxpower(n/imax,2);
        int m[]=mobius(d);

        long s1=0; int i;
        for(i=1;i<=d;i++) s1+=m[i]*(n/(1l*i*i));

        long mlist[]=new long[d+1];
        for(i=1;i<=d;i++) mlist[i]=mlist[i-1]+m[i];

        ArrayList<Long> mxi=new ArrayList<>();
        int sum=0;
        for(i=imax-1;i>0;i--)
        {
            long 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))*mlist[j];

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

        return s1+sum-(imax-1)*mlist[d];
    }

    static int maxpower(long 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;
            long v=1;
            for(i=0;i<p;i++) v*=m;
            if(v<=n)
            {
                ans=m;
                l=m+1;
            }
            else r=m-1;
        }
        return ans;
    }
}
0