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

该篇文章介绍了一种解题方法,针对给定的整数数组,通过哈希和取模操作将乘积问题规模缩小,利用三哈希判断来寻找满足Ai×Aj=Ak的整数三元组(i,j,k),在Java代码中实现了一个名为F-ProductEquality的解决方案。
摘要由CSDN通过智能技术生成

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很大,不能直接进行乘法计算

  • 考虑哈希,取模变小

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

  • 注意i,j,k可以相等

    import java.io.*;
    import java.math.BigInteger;
    import java.util.Arrays;
    import java.util.BitSet;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.PriorityQueue;
    import java.util.Queue;
    import java.util.Random;
    import java.util.StringTokenizer;
    import java.util.Vector;
    
    
    
    
    public class Main{
    	
    	public static void main(String[] args) throws IOException{
    		AReader input=new AReader();
    	    PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    
    	    int n=input.nextInt();
    	    Random rd=new Random();
    	    BigInteger[] bm=new BigInteger[3];
    	    long[] lm=new long[3];
    	    for(int i=0;i<3;++i) {
    	    	lm[i]=BigInteger.probablePrime(31, rd).longValue();//mod为素数
    	    	bm[i]=BigInteger.valueOf(lm[i]);
    	    }
    	    long[] a=new long[n+1];
    	    long[] b=new long[n+1];
    	    long[] c=new long[n+1];
    	    HashMap<Long, BigInteger> hs1=new HashMap<Long, BigInteger>();
    	    HashMap<Long, BigInteger> hs2=new HashMap<Long, BigInteger>();
    	    HashMap<Long, BigInteger> hs3=new HashMap<Long, BigInteger>();
    	    HashMap<BigInteger, Integer> Num=new HashMap<BigInteger, Integer>();//A_i映射
    	    int[] num=new int[n+1];//统计相同A_i
    	    int cnt=0;
    	    for(int i=1;i<=n;++i){
    	    	BigInteger x=input.nextBigInteger();
    	    	a[i]=x.mod(bm[0]).longValue();
    	    	b[i]=x.mod(bm[1]).longValue();
    	    	c[i]=x.mod(bm[2]).longValue();
    	    	hs1.put(a[i], x);
    	    	hs2.put(b[i], x);
    	    	hs3.put(c[i], x);
    	    	if(Num.get(x)==null) {
    	    		cnt++;
    	    		Num.put(x, cnt);
    	    		num[cnt]=1;
    	    	}else {
    	    		num[Num.get(x)]++;
    	    	}
    	    }
    	    int ans=0;
    	    for(int i=1;i<=n;++i) {
    	    	for(int j=1;j<=n;++j) {
    	    		
    	    		BigInteger k1=hs1.get(a[i]*a[j]%lm[0]);
    	    		BigInteger k2=hs2.get(b[i]*b[j]%lm[1]);
    	    		BigInteger k3=hs3.get(c[i]*c[j]%lm[2]);
    	    		if(k1==null||k2==null||k3==null)continue;
    	    		
    	    		if(k1.equals(k2)&&k2.equals(k3)) {
    	    			int t=Num.get(k1);
    	    			ans+=num[t];
    	    		}
    	    	}
    	    }
    	    out.print(ans);			
     	    out.flush();
    	    out.close();
    	}
    	static
    	class AReader{
    	    BufferedReader bf;
    	    StringTokenizer st;
    	    BufferedWriter bw;
    
    	    public AReader(){
    	        bf=new BufferedReader(new InputStreamReader(System.in));
    	        st=new StringTokenizer("");
    	        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    	    }
    	    public String nextLine() throws IOException{
    	        return bf.readLine();
    	    }
    	    public String next() throws IOException{
    	        while(!st.hasMoreTokens()){
    	            st=new StringTokenizer(bf.readLine());
    	        }
    	        return st.nextToken();
    	    }
    	    public char nextChar() throws IOException{
    	        //确定下一个token只有一个字符的时候再用
    	        return next().charAt(0);
    	    }
    	    public int nextInt() throws IOException{
    	        return Integer.parseInt(next());
    	    }
    	    public long nextLong() throws IOException{
    	        return Long.parseLong(next());
    	    }
    	    public double nextDouble() throws IOException{
    	        return Double.parseDouble(next());
    	    }
    	    public float nextFloat() throws IOException{
    	        return Float.parseFloat(next());
    	    }
    	    public byte nextByte() throws IOException{
    	        return Byte.parseByte(next());
    	    }
    	    public short nextShort() throws IOException{
    	        return Short.parseShort(next());
    	    }
    	    public BigInteger nextBigInteger() throws IOException{
    	        return new BigInteger(next());
    	    }
    	    public void println() throws IOException {
    	        bw.newLine();
    	    }
    	    public void println(int[] arr) throws IOException{
    	        for (int value : arr) {
    	            bw.write(value + " ");
    	        }
    	        println();
    	    }
    	    public void println(int l, int r, int[] arr) throws IOException{
    	        for (int i = l; i <= r; i ++) {
    	            bw.write(arr[i] + " ");
    	        }
    	        println();
    	    }
    	    public void println(int a) throws IOException{
    	        bw.write(String.valueOf(a));
    	        bw.newLine();
    	    }
    	    public void print(int a) throws IOException{
    	        bw.write(String.valueOf(a));
    	    }
    	    public void println(String a) throws IOException{
    	        bw.write(a);
    	        bw.newLine();
    	    }
    	    public void print(String a) throws IOException{
    	        bw.write(a);
    	    }
    	    public void println(long a) throws IOException{
    	        bw.write(String.valueOf(a));
    	        bw.newLine();
    	    }
    	    public void print(long a) throws IOException{
    	        bw.write(String.valueOf(a));
    	    }
    	    public void println(double a) throws IOException{
    	        bw.write(String.valueOf(a));
    	        bw.newLine();
    	    }
    	    public void print(double a) throws IOException{
    	        bw.write(String.valueOf(a));
    	    }
    	    public void print(char a) throws IOException{
    	        bw.write(String.valueOf(a));
    	    }
    	    public void println(char a) throws IOException{
    	        bw.write(String.valueOf(a));
    	        bw.newLine();
    	    }
    	}
    }
    
    

  • 29
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值