• Japan Registry Services (JPRS) Programming Contest 2024 (AtCoder Beginner Contest 339)


    F - Product Equality

    题目大意

    • 给定N个整数A1,A2,…,AN。 找到满足以下条件的整数三元组(i,j,k)的数量:

      1≤i,j,k≤N Ai×Aj=Ak 1 ≤ N ≤ 1000 1 ≤ Ai < 10^1000

    解题思路

    • A_i很大,不能直接进行乘法计算

    • 考虑哈希,取模变小

    • 为保证结果的正确性,进行三哈希判断,都满足时为正确

    • 注意" role="presentation" style="position: relative;">i,j,k可以相等

      1. import java.io.*;
      2. import java.math.BigInteger;
      3. import java.util.Arrays;
      4. import java.util.BitSet;
      5. import java.util.HashMap;
      6. import java.util.HashSet;
      7. import java.util.Iterator;
      8. import java.util.LinkedList;
      9. import java.util.PriorityQueue;
      10. import java.util.Queue;
      11. import java.util.Random;
      12. import java.util.StringTokenizer;
      13. import java.util.Vector;
      14. public class Main{
      15. public static void main(String[] args) throws IOException{
      16. AReader input=new AReader();
      17. PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
      18. int n=input.nextInt();
      19. Random rd=new Random();
      20. BigInteger[] bm=new BigInteger[3];
      21. long[] lm=new long[3];
      22. for(int i=0;i<3;++i) {
      23. lm[i]=BigInteger.probablePrime(31, rd).longValue();//mod为素数
      24. bm[i]=BigInteger.valueOf(lm[i]);
      25. }
      26. long[] a=new long[n+1];
      27. long[] b=new long[n+1];
      28. long[] c=new long[n+1];
      29. HashMap hs1=new HashMap();
      30. HashMap hs2=new HashMap();
      31. HashMap hs3=new HashMap();
      32. HashMap Num=new HashMap();//A_i映射
      33. int[] num=new int[n+1];//统计相同A_i
      34. int cnt=0;
      35. for(int i=1;i<=n;++i){
      36. BigInteger x=input.nextBigInteger();
      37. a[i]=x.mod(bm[0]).longValue();
      38. b[i]=x.mod(bm[1]).longValue();
      39. c[i]=x.mod(bm[2]).longValue();
      40. hs1.put(a[i], x);
      41. hs2.put(b[i], x);
      42. hs3.put(c[i], x);
      43. if(Num.get(x)==null) {
      44. cnt++;
      45. Num.put(x, cnt);
      46. num[cnt]=1;
      47. }else {
      48. num[Num.get(x)]++;
      49. }
      50. }
      51. int ans=0;
      52. for(int i=1;i<=n;++i) {
      53. for(int j=1;j<=n;++j) {
      54. BigInteger k1=hs1.get(a[i]*a[j]%lm[0]);
      55. BigInteger k2=hs2.get(b[i]*b[j]%lm[1]);
      56. BigInteger k3=hs3.get(c[i]*c[j]%lm[2]);
      57. if(k1==null||k2==null||k3==null)continue;
      58. if(k1.equals(k2)&&k2.equals(k3)) {
      59. int t=Num.get(k1);
      60. ans+=num[t];
      61. }
      62. }
      63. }
      64. out.print(ans);
      65. out.flush();
      66. out.close();
      67. }
      68. static
      69. class AReader{
      70. BufferedReader bf;
      71. StringTokenizer st;
      72. BufferedWriter bw;
      73. public AReader(){
      74. bf=new BufferedReader(new InputStreamReader(System.in));
      75. st=new StringTokenizer("");
      76. bw=new BufferedWriter(new OutputStreamWriter(System.out));
      77. }
      78. public String nextLine() throws IOException{
      79. return bf.readLine();
      80. }
      81. public String next() throws IOException{
      82. while(!st.hasMoreTokens()){
      83. st=new StringTokenizer(bf.readLine());
      84. }
      85. return st.nextToken();
      86. }
      87. public char nextChar() throws IOException{
      88. //确定下一个token只有一个字符的时候再用
      89. return next().charAt(0);
      90. }
      91. public int nextInt() throws IOException{
      92. return Integer.parseInt(next());
      93. }
      94. public long nextLong() throws IOException{
      95. return Long.parseLong(next());
      96. }
      97. public double nextDouble() throws IOException{
      98. return Double.parseDouble(next());
      99. }
      100. public float nextFloat() throws IOException{
      101. return Float.parseFloat(next());
      102. }
      103. public byte nextByte() throws IOException{
      104. return Byte.parseByte(next());
      105. }
      106. public short nextShort() throws IOException{
      107. return Short.parseShort(next());
      108. }
      109. public BigInteger nextBigInteger() throws IOException{
      110. return new BigInteger(next());
      111. }
      112. public void println() throws IOException {
      113. bw.newLine();
      114. }
      115. public void println(int[] arr) throws IOException{
      116. for (int value : arr) {
      117. bw.write(value + " ");
      118. }
      119. println();
      120. }
      121. public void println(int l, int r, int[] arr) throws IOException{
      122. for (int i = l; i <= r; i ++) {
      123. bw.write(arr[i] + " ");
      124. }
      125. println();
      126. }
      127. public void println(int a) throws IOException{
      128. bw.write(String.valueOf(a));
      129. bw.newLine();
      130. }
      131. public void print(int a) throws IOException{
      132. bw.write(String.valueOf(a));
      133. }
      134. public void println(String a) throws IOException{
      135. bw.write(a);
      136. bw.newLine();
      137. }
      138. public void print(String a) throws IOException{
      139. bw.write(a);
      140. }
      141. public void println(long a) throws IOException{
      142. bw.write(String.valueOf(a));
      143. bw.newLine();
      144. }
      145. public void print(long a) throws IOException{
      146. bw.write(String.valueOf(a));
      147. }
      148. public void println(double a) throws IOException{
      149. bw.write(String.valueOf(a));
      150. bw.newLine();
      151. }
      152. public void print(double a) throws IOException{
      153. bw.write(String.valueOf(a));
      154. }
      155. public void print(char a) throws IOException{
      156. bw.write(String.valueOf(a));
      157. }
      158. public void println(char a) throws IOException{
      159. bw.write(String.valueOf(a));
      160. bw.newLine();
      161. }
      162. }
      163. }

  • 相关阅读:
    【Java编程】JavaSE基础总结(一)
    华为OD机试 - 最多颜色的车辆 - 数据结构map(Java 2022Q4 100分)
    springboot项目开发实战
    高中生可发表论文的学术期刊涵盖TCR历史期刊
    Java面向对象
    Mysql连接与存储
    《Go 语言核心 36 讲》学习笔记
    Python 基于PyCharm断点调试
    JVisualVM连接远程阿里云Linux
    小程序开发的部分功能
  • 原文地址:https://blog.csdn.net/Xing_ke/article/details/136197352