fork download
  1. import java.io.*;
  2. import java.util.*;
  3. class CLDSTR{
  4. public static void main(String args[]) throws Exception{
  5. Reader re = new Reader(System.in);
  6. int n = re.nextInt();
  7. int p1 = re.nextInt();
  8. int q1 = re.nextInt();
  9. int m = re.nextInt();
  10. int p2 = re.nextInt();
  11. int q2 = re.nextInt();
  12.  
  13. double out = work(n, p1, q1, m, p2, q2);
  14. System.out.printf("%.1f%n",out);
  15. }
  16.  
  17. static double work(int N, int pS, int qS, int M, int pB, int qB){
  18. int[] fS = new int[qS];
  19. int[] fB = new int[qB];
  20. int v = 1;
  21. for(int i=0; i<N; i++){
  22. v = (v*pS)%qS;
  23. fS[v]++;
  24. }
  25. v = 1;
  26. for(int i=0; i<M; i++){
  27. v = (v*pB)%qB;
  28. fB[v]++;
  29. }
  30.  
  31. int iS, iB, max, min;
  32. iS = qS-1;
  33. iB = qB-1;
  34. max = Math.max(N, M);
  35. min = Math.min(N, M);
  36. long avg = 0;
  37. while(iS>=0 && iB>=0){
  38. if(fS[iS]==0){
  39. iS--;
  40. continue;
  41. }
  42. if(fB[iB]==0){
  43. iB--;
  44. continue;
  45. }
  46. int m = Math.min(fS[iS], fB[iB]);
  47. avg += m*(iS-iB);
  48. fS[iS] -= m;
  49. fB[iB] -= m;
  50. }
  51. return (double)avg/max;
  52. }
  53. }
  54.  
  55. class Reader{
  56. br = new BufferedReader(new InputStreamReader(in));
  57. st = new StringTokenizer("");
  58. }
  59.  
  60. String next() throws Exception{
  61. while(!st.hasMoreTokens())
  62. st = new StringTokenizer(br.readLine());
  63. return st.nextToken();
  64. }
  65.  
  66. int nextInt() throws Exception{
  67. return Integer.parseInt(next());
  68. }
  69. }
  70.  
  71.  
Success #stdin #stdout 0.12s 320512KB
stdin
2
2 100
1
3 10
stdout
0.5