题解 | 大整数哈希

大整数哈希

https://www.nowcoder.com/practice/29f0cff8a69b4ab6a2f63fb7386defa3

必须要使用HashMap存储函数。使用BigInteger进行运算(

应该可以用// ❌ 错误:sc.nextLong() 无法解析 > 2^63-1 的数

// ✅ 正确:先读字符串,再 parseUnsignedLong

String xStr = sc.next();

String yStr = sc.next();

longx = Long.parseUnsignedLong(xStr);

longy = Long.parseUnsignedLong(yStr);

参考:https://blog.nowcoder.net/n/3fb2d41b1839440b97422e3de21b24e7

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
public class Main {
    public static final BigInteger MOD=BigInteger.valueOf(2).pow(64);
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        Map<BigInteger,BigInteger> map=new HashMap<>();
        BigInteger ans=BigInteger.valueOf(0);
        for(int i=1;i<=n;i++){
            //sc.nextLong() 无法解析 > 2^63-1 的数
            BigInteger x=in.nextBigInteger();
            BigInteger y=in.nextBigInteger();
            if(map.containsKey(x)){
                ans=ans.add(map.get(x).multiply(BigInteger.valueOf(i)).mod(MOD)).mod(MOD);
            }
            map.put(x,y);

        }
        System.out.println(ans);





    }
}

全部评论

相关推荐

11-23 15:14
中原工学院 Java
程序员花海_:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务