fork(3) download
  1. import java.io.*;
  2. import java.util.Locale;
  3. import java.util.StringTokenizer;
  4.  
  5. public class TaskA_ok {
  6. private final InputReader reader;
  7. private final OutputWriter writer;
  8.  
  9. public TaskA_ok(InputReader reader, OutputWriter writer) {
  10. this.reader = reader;
  11. this.writer = writer;
  12. }
  13.  
  14. public static void main(String[] args) {
  15. InputReader reader = new InputReader(System.in);
  16. OutputWriter writer = new OutputWriter(System.out);
  17. new TaskA_ok(reader, writer).run();
  18. writer.writer.flush();
  19. }
  20.  
  21. public void run() {
  22. long p = reader.nextLong();
  23. long v1 = reader.nextLong();
  24. long t1 = reader.nextLong();
  25. long k1 = reader.nextLong();
  26. long b1 = reader.nextLong();
  27. long v2 = reader.nextLong();
  28. long t2 = reader.nextLong();
  29. long k2 = reader.nextLong();
  30. long b2 = reader.nextLong();
  31.  
  32. long f = 0;
  33. while (v1 != t1) {
  34. v1 = (v1 * k1 + b1) % p;
  35. v2 = (v2 * k2 + b2) % p;
  36. f++;
  37. if (f > p + 1) {
  38. writer.printf("-1\n");
  39. return;
  40. }
  41. }
  42. if (v2 == t2) {
  43. writer.printf("%d\n", f);
  44. return;
  45. }
  46. long per = 0;
  47. long pk2 = 1, pb2 = 0;
  48. while (per == 0 || v1 != t1) {
  49. v1 = (v1 * k1 + b1) % p;
  50. pk2 = (pk2 * k2) % p;
  51. pb2 = (pb2 * k2 + b2) % p;
  52. per++;
  53. if (per > p + 1) {
  54. writer.printf("%d\n", -1);
  55. return;
  56. }
  57. }
  58. long f2 = 0;
  59. while (v2 != t2) {
  60. f2++;
  61. v2 = (pk2 * v2 + pb2) % p;
  62. if (f2 > p + 1) {
  63. writer.printf("-1\n");
  64. return;
  65. }
  66. }
  67. writer.printf("%d\n", f + per * f2);
  68. }
  69.  
  70.  
  71. static class InputReader {
  72. public BufferedReader reader;
  73. public StringTokenizer tokenizer;
  74.  
  75. public InputReader(InputStream stream) {
  76. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  77. tokenizer = null;
  78. }
  79.  
  80. public String next() {
  81. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  82. try {
  83. tokenizer = new StringTokenizer(reader.readLine());
  84. } catch (IOException e) {
  85. throw new RuntimeException(e);
  86. }
  87. }
  88. return tokenizer.nextToken();
  89. }
  90.  
  91. public int nextInt() {
  92. return Integer.parseInt(next());
  93. }
  94.  
  95. public double nextDouble() {
  96. return Double.parseDouble(next());
  97. }
  98.  
  99. public long nextLong() {
  100. return Long.parseLong(next());
  101. }
  102. }
  103.  
  104. static class OutputWriter {
  105. public PrintWriter writer;
  106.  
  107. OutputWriter(OutputStream stream) {
  108. writer = new PrintWriter(stream);
  109. }
  110.  
  111. public void printf(String format, Object... args) {
  112. writer.print(String.format(Locale.ENGLISH, format, args));
  113. }
  114. }
  115. }
  116.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:5: error: class TaskA_ok is public, should be declared in a file named TaskA_ok.java
public class TaskA_ok {
       ^
1 error
stdout
Standard output is empty