必须要使用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); } }