fork download
  1.  
  2.  
  3.  
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7. class COMDIV
  8. {
  9. private static Reader in;
  10. private static PrintWriter out;
  11.  
  12. public static void main(String args[])
  13. throws IOException
  14. {
  15. in=new Reader();
  16. out=new PrintWriter(System.out,true);
  17.  
  18. int t=Integer.parseInt(br.readLine());
  19. boolean isPrime[]=new boolean[1000+4];
  20. int primes[]=new int[1000+4];//this will store the nth prime. in consecutive order
  21. int p_idx=0;
  22.  
  23. Arrays.fill(isPrime, true);//initially assume that all are prime
  24. isPrime[0]=isPrime[1]=false;
  25.  
  26. for(int i=2;i*i<=1002;i++)
  27. {
  28. if(isPrime[i])
  29. {
  30. primes[p_idx++]=i;
  31. for(int j=i;i*j<=1002;j++)
  32. isPrime[i*j]=false;
  33. }
  34. }
  35.  
  36.  
  37. while(t--!=0)
  38. {
  39. String in[]=br.readLine().split(" ");
  40. int a=Integer.parseInt(in[0]);
  41. int b=Integer.parseInt(in[1]);
  42. int g=gcd(a,b);
  43. if(g==1)
  44. {
  45. System.out.println(1);
  46. continue;
  47. }
  48. else
  49. {
  50. //all divisors of g and (common divisors of both (A&B)) are the same. its the same list
  51. //therefore the required no of divisors common to both a and b are the number of divisors of g.
  52. long prod=1;
  53. int i=0;
  54. while(primes[i]*primes[i]<=g) // && i<p_idx++
  55. {
  56. if(isPrime[primes[i]])
  57. {
  58. int c=0;
  59. while(g%primes[i]==0)
  60. {
  61.  
  62. g/=primes[i];
  63. c++;
  64. }
  65. prod=prod*(++c);
  66. }
  67. i++;
  68. }
  69. if(g>1)
  70. prod<<=1;
  71. System.out.println(prod);
  72. }
  73.  
  74. }//t
  75.  
  76. out.flush();
  77. out.close();
  78. }
  79.  
  80.  
  81. static int gcd(int a ,int b)
  82. {
  83. if(b==0)
  84. return a;
  85. else
  86. return gcd(b,a%b);
  87. }
  88. }
  89.  
  90.  
  91. /** Faster input **/
  92. class Reader
  93. {
  94. final private int BUFFER_SIZE = 1 << 16;
  95.  
  96. private DataInputStream din;
  97. private byte[] buffer;
  98. private int bufferPointer, bytesRead;
  99.  
  100. public Reader()
  101. {
  102. din = new DataInputStream(System.in);
  103. buffer = new byte[BUFFER_SIZE];
  104. bufferPointer = bytesRead = 0;
  105. }
  106.  
  107. public Reader(String file_name) throws IOException
  108. {
  109. din = new DataInputStream(new FileInputStream(file_name));
  110. buffer = new byte[BUFFER_SIZE];
  111. bufferPointer = bytesRead = 0;
  112. }
  113.  
  114. public String readLine() throws IOException
  115. {
  116. byte[] buf = new byte[64]; // line length
  117. int cnt = 0, c;
  118. while( (c=read()) != -1) {
  119. buf[cnt++] = (byte)c;
  120. if(c == '\n') break;
  121. }
  122. return new String(buf,0,cnt);
  123. }
  124.  
  125. public int nextInt() throws IOException
  126. {
  127. int ret = 0;
  128. byte c = read();
  129. while (c <= ' ') c = read();
  130. boolean neg = c == '-';
  131. if (neg) c = read();
  132. do {
  133. ret = ret * 10 + c - '0';
  134. c = read();
  135. } while (c >= '0' && c <= '9');
  136. if (neg) return -ret;
  137. return ret;
  138. }
  139.  
  140. public long nextLong() throws IOException
  141. {
  142. long ret = 0;
  143. byte c = read();
  144. while (c <= ' ') c = read();
  145. boolean neg = c == '-';
  146. if (neg) c = read();
  147. do {
  148. ret = ret * 10 + c - '0';
  149. c = read();
  150. } while (c >= '0' && c <= '9');
  151. if (neg) return -ret;
  152. return ret;
  153. }
  154.  
  155. public double nextDouble() throws IOException
  156. {
  157. double ret = 0, div = 1;
  158. byte c = read();
  159. while(c <= ' ') c = read();
  160. boolean neg = c == '-';
  161. if(neg) c = read();
  162. do {
  163. ret = ret * 10 + c - '0';
  164. c = read();
  165. } while (c >= '0' && c <= '9');
  166. if(c == '.')
  167. while((c=read()) >= '0' && c <= '9') {
  168. div *= 10;
  169. ret = ret + (c - '0') / div;
  170. }
  171. if (neg) return -ret;
  172. return ret;
  173. }
  174.  
  175. private void fillBuffer() throws IOException
  176. {
  177. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  178. if (bytesRead == -1) buffer[0] = -1;
  179. }
  180.  
  181. private byte read() throws IOException
  182. {
  183. if (bufferPointer == bytesRead) fillBuffer();
  184. return buffer[bufferPointer++];
  185. }
  186.  
  187. public void close() throws IOException
  188. {
  189. if(din == null) return;
  190. din.close();
  191. }
  192. }
  193.  
Success #stdin #stdout 0.07s 380224KB
stdin
3
100000 100000
12 24
747794 238336
stdout
36
6
2