Problem Description
A UFO crash lands on Earth. The alien captain survives the crash relatively unscathed; however his trusty watch is damaged beyond repair.
The alien watch is very similar to a human watch: It is a disc 30mm in diameter and it has three hands of lengths A, B and C microns (1000 ≤ A, B, C ≤ 15000). However, the aliens measure time differently: there are N(2≤N<232)N (2 ≤ N < 2^{32})N(2≤N<232) alien seconds to an alien minute. As such, there are N markings on the edge of the disc, rather than 60.
The watch’s glass cover is broken and the hands have come loose: they can rotate freely and independently of one another. By pointing each of the hands at an arbitrary marking, their tips can be made to form an imaginary triangle (as long as they are non-collinear).
Having nothing left to do but wait for help to arrive, the alien ponders the following question: What is the number M of distinct triangles that contain the watch’s center? (Triangles where the center lies on one of the edges of the triangle count as well.)
Input
The input consists of A, B, C, and N on a single line, separated by whitespace.
Output
The output is M modulo 2642^{64}264.
Sample Input
15000 15000 15000 2
5000 10000 15000 3
15000 15000 15000 3
15000 15000 15000 4
15000 15000 15000 5
15000 15000 15000 6
Sample Output
0
6
1
4
5
14
由于模的是2^64,完全可以不用理会,用unsigned long long即可,溢出了也不用理会,能保证答案正确,一开始由于多算了很多重复的情况,WA了一发
以为是“溢出取模法”不正确(其实是代码错了),改用java写一样wa
后修改了代码顺利AC
/*
* Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
* @Author: Ng Kimbing, HNU.
* @LastModified:2019-07-28 T 14:40:37.721 +08:00
*/
import java.math.BigInteger;
import static ACMProblems.ACMIO.*;
public class Main {
private static BigInteger MOD = new BigInteger("2").pow(64);
private static BigInteger one = new BigInteger("1");
private static BigInteger two = new BigInteger("2");
private static BigInteger six = new BigInteger("6");
public static void main(String[] args) throws Exception {
while (true) {
try {
long a = nextLong();
long b = nextLong();
long c = nextLong();
long n = nextLong();
BigInteger bn = new BigInteger(Long.toString(n));
BigInteger fact = one;
if (a == b && a == c) {
fact = six;
} else if (a == b || a == c || b == c) {
fact = two;
}
if (n % 2 == 0) {
BigInteger nSecond = bn.divide(two);
BigInteger ans = bn.multiply(nSecond.subtract(one).multiply(nSecond.add(two)).add(bn.subtract(two))).divide(fact).mod(MOD);
out.println(ans);
} else {
BigInteger nSecond = bn.divide(two);
BigInteger ans = bn.multiply(nSecond).multiply(nSecond.add(one)).divide(fact).mod(MOD);
out.println(ans);
}
out.flush();
} catch (Exception e) {
out.flush();
return;
}
}
}
}