题解 | 大整数哈希

大整数哈希

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Map<Long, Long> f = new HashMap<>();
        long total = 0;

        for (int i = 1; i <= n; i++) {
            // ❌ 错误:sc.nextLong() 无法解析 > 2^63-1 的数
            // ✅ 正确:先读字符串,再 parseUnsignedLong
            String xStr = sc.next();
            String yStr = sc.next();
            long x = Long.parseUnsignedLong(xStr);
            long y = Long.parseUnsignedLong(yStr);

            long ans_i = f.getOrDefault(x, 0L);

            total += (long) i * ans_i; // 自动 mod 2^64

            f.put(x, y);
        }

        // 输出必须用无符号形式
        System.out.println(Long.toUnsignedString(total));

        sc.close();
    }
}

全部评论
坐标南京,OD岗位多多,欢迎来聊
点赞 回复 分享
发布于 08-23 14:58 贵州

相关推荐

评论
1
收藏
分享

创作者周榜

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