Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Solution {
  5. private static int cycleLength(int x) {
  6. if(x == 0) {
  7. return 0;
  8. }
  9. int NotSeenYet = 0;
  10. int[] lastPos = new int[x];
  11. int position = 1;
  12. int dividend = 1;
  13. while(true) {
  14. int remainder = dividend % x;
  15. if(remainder == 0) {
  16. return 0;
  17. }
  18. if(lastPos[remainder] != NotSeenYet) {
  19. return position - lastPos[remainder];
  20. }
  21. lastPos[remainder] = position;
  22. position++;
  23. dividend = remainder * 10;
  24. }
  25. }
  26.  
  27. public static void main(String[] args) {
  28. int MaxD = 10000;
  29. int[] cache = new int[MaxD + 1];
  30. int longD = 0;
  31. int longC = 0;
  32. for(int D = 1; D <= MaxD; D++) {
  33. int length = cycleLength(D);
  34. if(longC < length) {
  35. longC = length;
  36. longD = D;
  37. }
  38. cache[D] = longD;
  39. }
  40. System.out.println(Arrays.toString(cache));
  41. try(Scanner sc = new Scanner(System.in)) {
  42. int T = sc.nextInt();
  43. while(T-- > 0) {
  44. int N = sc.nextInt();
  45. System.out.println(cache[N - 1]);
  46. }
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement