ZOJ Problem Set - 3903 Ant
Chieh
posted @ 2015年10月30日 18:30
in NO Answer No Speak
, 267 阅读
import java.math.BigInteger; import java.util.*; public class zoj3903 { public static BigInteger pow2(long p) { BigInteger big = BigInteger.ZERO; BigInteger nn = new BigInteger(p + ""); BigInteger nn1 = nn.add(BigInteger.ONE); BigInteger tw = new BigInteger(2 + ""); BigInteger si = new BigInteger(6 + ""); big = nn.multiply(nn1).multiply(nn.multiply(tw).add(BigInteger.ONE)); return big.divide(si); } public static BigInteger pow3(long p) { BigInteger big = BigInteger.ZERO; BigInteger nn = new BigInteger(p + ""); BigInteger nn1 = nn.add(BigInteger.ONE); BigInteger tw = new BigInteger(2 + ""); big = nn.multiply(nn1).divide(tw); return big.multiply(big); } public static void main(String[] args) { // TODO Auto-generated method stub int T; Scanner sca = new Scanner(System.in); T = sca.nextInt(); long p; BigInteger MOD = new BigInteger("1000000007"); long o = 1; BigInteger tw = new BigInteger(2 + ""); for (int i = 1; i <= T; i++) { p = Long.parseLong(sca.next()); p++; BigInteger q = new BigInteger((p - 1) + ""); BigInteger b1 = pow3(p).subtract(pow3(o)); BigInteger b2 = pow2(p).subtract(pow2(o)); BigInteger ans = b1.subtract(b2); BigInteger b3 = pow2((p - 1) * 2).subtract(pow2(p)); b3 = b3.multiply(q); BigInteger b4 = pow3((p - 1) * 2).subtract(pow3(p)); BigInteger b5 = pow2((p - 1) * 2).subtract(pow2(p)); b5=b5.multiply(new BigInteger(p+"")); BigInteger ans1 = b4.subtract(b5); ans1 = b3.subtract(ans1); ans = ans.add(ans1); ans =ans.add(pow2(p - 1).multiply(tw).multiply(tw)); ans = ans.divide(tw); BigInteger st = q.multiply(new BigInteger(p + "")).divide(tw) .multiply(q).multiply(q); ans = ans.add(st); ans = ans.mod(MOD); System.out.println(ans); } } }