結果

問題 No.1666 累乗数
ユーザー merlin
提出日時 2021-09-03 23:15:19
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,558 ms / 2,000 ms
コード長 1,904 bytes
コンパイル時間 2,777 ms
コンパイル使用メモリ 78,532 KB
実行使用メモリ 39,144 KB
最終ジャッジ日時 2024-12-15 17:42:52
合計ジャッジ時間 27,439 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int t=Integer.parseInt(bu.readLine());
        while(t-->0)
        {
            int n=Integer.parseInt(bu.readLine());
            long l=1,r=(long)1e18,mid,ans=r;
            while(l<=r)
            {
                mid=(l+r)>>1;
                if(count(mid)>=n)
                {
                    ans=mid;
                    r=mid-1;
                }
                else l=mid+1;
            }
            sb.append(ans+"\n");
        }
        System.out.print(sb);
    }

    static long c[]=new long[61];
    static int ex[]={0,0,1000000001,1000001,31623,3982,1001,373,178,101,64,44,32,25,20,16,14,12,11,9,8,8,7,7,6,6,5,5,5,5,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2};
    static long count(long n)
    {
        int i,N=60;
        for(i=2;i<=N;i++)
        {
            int l=2,r=ex[i],mid,ans=0; long v;
            while(l<=r)
            {
                mid=(l+r)>>1;
                v=power(mid,i,n);
                if(v<=n)
                {
                    ans=mid-1;
                    l=mid+1;
                }
                else r=mid-1;
            }
            c[i]=ans;
        }

        int j; long ans=1;
        for(i=N;i>1;i--)
        {
            //System.out.print(c[i]+" ");
            for(j=2*i;j<=N;j+=i) c[i]-=c[j];
            ans+=c[i];
        }
        //System.out.println(n+" "+ans);
        return ans;
    }

    static long power(long a,int b,long n)
    {
        if(a>n) return n+1;
        long res=1;
        while(b!=0)
        {
            if(res>n/a) return n+1;
            res=res*a;
            b--;
            if(res>n) return n+1;
        }
        return res;
    }
}
0