Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Scanner;
  4.  
  5. public class FactorialZeros {
  6. static Scanner sc=new Scanner(System.in);
  7. public static void main(String[] args) {
  8. int T=sc.nextInt();
  9. for(int t=0;t<T;t++){
  10. int B=sc.nextInt();
  11. long N=sc.nextLong();
  12.  
  13. HashMap<Integer,Integer> factors=new HashMap<>();
  14. int BB=B;
  15.  
  16. while(BB>1){
  17. for(int b=2;b<BB+1;b++){
  18. int pow=0;
  19. while(BB%b==0){
  20. pow++;
  21. BB/=b;
  22. }
  23. if(pow>0)factors.put(b,pow);
  24. }
  25. }
  26.  
  27. int noDistinctFactors=factors.keySet().size();
  28. int[] distFactors=new int[noDistinctFactors];
  29. int[] powers=new int[noDistinctFactors];
  30.  
  31. Iterator<Integer> it=factors.keySet().iterator();
  32. int i=0;
  33. while(it.hasNext()){
  34. distFactors[i]=it.next();
  35. powers[i]=factors.get(distFactors[i]);
  36. i++;
  37. }
  38.  
  39.  
  40. for(int d=0;d<noDistinctFactors;d++){
  41. System.out.println(distFactors[d]+" "+powers[d]);
  42. }
  43.  
  44.  
  45. long[] intendedPowers=new long[noDistinctFactors];
  46.  
  47.  
  48. for(int d=0;d<noDistinctFactors;d++){
  49. intendedPowers[d]=powers[d]*N;
  50. }
  51.  
  52. long[] answers=new long[noDistinctFactors];
  53.  
  54.  
  55.  
  56. for(int fa=0;fa<noDistinctFactors;fa++) {
  57. long max = 10l*N;
  58. long min = 1l;
  59.  
  60.  
  61. while (max >= min) {
  62. //System.out.println("DEBUG: inside the binary search");
  63. long mid = (max + min) / 2;
  64. long aa=f(mid,distFactors[fa]);
  65.  
  66. if(intendedPowers[fa]==aa){
  67. answers[fa]=mid;
  68. break;
  69. }
  70. else if(intendedPowers[fa]>aa){
  71. min=mid+1;
  72. }
  73. else{
  74. max=mid-1;
  75. }
  76. }
  77.  
  78. while(intendedPowers[fa]==f(answers[fa]-1,distFactors[fa])){
  79. answers[fa]--;
  80. }
  81.  
  82. }
  83.  
  84.  
  85. long ans=Long.MIN_VALUE;
  86. for(int aa=0;aa<noDistinctFactors;aa++){
  87. ans= Math.max(ans,answers[aa]);//System.out.println("Answer "+answers[aa]);
  88. }
  89.  
  90. if(ans>0)System.out.println(ans);
  91. else System.out.println(-1);
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. }
  102.  
  103.  
  104.  
  105. }
  106.  
  107.  
  108. public static long f(long x,long p){
  109. long ans=0;
  110.  
  111. while(x>=p){
  112. ans+=x/p;
  113. p*=p;
  114. }
  115. //System.out.println("DEBUG: "+x+" "+p+" "+ans);
  116. return ans;
  117.  
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement