思路:
思考一下會發現選擇質因子越多的且越小的會使得結果更小,因此預處理前100的質因子,詢問直接查詢處理數組。需要寫大數
ac代碼:
import java.util.Scanner;
import java.math.*;
public class Main {
public static void main(String[] args) {
int N=1000;
int pri[]=new int[1005];
int vis[]=new int[1005];
node ans[]=new node[1005];
int cnt=0;
int tot=0;
for(int i=2;i<=N;i++){
if(vis[i]==0) pri[cnt++]=i;
for(int j=0;j<cnt&&pri[j]*i<=N;j++){
vis[pri[j]*i]=1;
if(i%pri[j]==0) continue;
}
}
for(int i=0;i<=1000;i++) {
ans[i]=new node();
ans[i].p = new BigInteger("0");
ans[i].q = new BigInteger("0");
ans[i].val = new BigInteger("0");
}
ans[tot].val=new BigInteger("1");
ans[tot].p=new BigInteger("1");
ans[tot++].q=new BigInteger("1");
for(int i=0;i<=100;i++){
ans[tot].val=ans[tot-1].val.multiply(BigInteger.valueOf(pri[i]));
ans[tot].p=ans[tot-1].p.multiply(BigInteger.valueOf(pri[i]+1));
ans[tot].q=ans[tot-1].q.multiply(BigInteger.valueOf(pri[i]));
BigInteger g=ans[tot].p.gcd(ans[tot].q);
ans[tot].p=ans[tot].p.divide(g);
ans[tot].q=ans[tot].q.divide(g);
tot++;
}
int t;
Scanner rd=new Scanner(System.in);
t=rd.nextInt();
while(t>0){
BigInteger n;
n=rd.nextBigInteger();
for(int i=0;i<tot;i++){
// System.out.print(ans[i].val+"\n"+n+"\n");
if(ans[i].val.compareTo(n)==1){
System.out.print(ans[i-1].q+"/"+ans[i-1].p+'\n');
// printf("%lld %lld\n",ans[tot].q,ans[tot].p);
break;
}
}
t--;
}
}
}
class node{
BigInteger p,q,val;
}