143 - ZOJ Monthly, October 2015 - A(zoj3903)

Ant

Time Limit: 1 Second       Memory Limit: 32768 KB

There is an ant named Alice. Alice likes going hiking very much. Today, she wants to climb a cuboid. The length of cuboid's longest edge is n, and the other edges are all positive integers. Alice's starting point is a vertex of this cuboid, and she wants to arrive at the opposite vertex. The opposite vertex means the vertex which has no common planes or edges with the starting point. Just like the picture below:

mtz9548_ant_pic1.gif
Alice is very clever, she always walks on the shortest path. But she can only walk on the surface of the cuboid. Now, Alice only knows the length of cuboid's longest edge is  n , and doesn't know the length of other edges. Suppose the  L  is the length of shortest path of a cuboid. Alice wants to compute the sum of  L2  for every possible cuboid.

Input

The first line of input contains an integer T(T ≤ 100) . is the number of the cases. In the following T lines, there are a positive integer n(1≤n≤1014) in each line. n is the longest edge of the cuboid.

Output

For each test case, output the sum of L2 for every possible cuboid in a line. L is the length of shortest path of a cuboid. It may be very large, so you must output the answer modulo 1000000007.

Sample Input
2
3
4
Sample Output
160
440
Hint
(3,2,1) and (3,1,2) are regrad as the same cuboids.
Author:  MU, Tongzhou

Submit     Status

一个数学题。分析后就是求: ∑((x+y)^2+n^2) (1<=x<=y<=n)。
(1+1)^2+n^2
(2+1)^2+n^2 + (2+2)^2+n^2
...
(i+1)^2+n^2 + (i+2)^2+n^2 + ... + (i+i)^2+n^2
...
(n+1)^2+n^2 + (n+2)^2+n^2 + ... + (n+n)^2+n^2
直接模拟会TLE
我们可以这样
∑((x+y)^2+n^2)(1<=x<=y<=n) = ( ∑((x+y)^2+n^2)(1<=x,y<=n)+∑((x+y)^2+n^2)(1<= x=y <=n)  ) /2。

第一步:
∑((x+y)^2+n^2)(1<= x=y <=n)  = ∑((2*i)^2+n^2)(1<= i <=n) = n^3+(1^2+2^2+...n^2)*2^2
=n^3+n*(n+1)*(2*n+1)/6*4
第二步:
∑((x+y)^2+n^2)(1<=x,y<=n)
分成两部分,n^2和(x+y)^2
总共有n*n项
n^2部分等于n^4
(x+y)^2部分等于(x^2+y^2)+2xy
∑(x^2+y^2+2xy) (1<=x,y<=n)=∑(2*i^2)(1<=i<=n) + ∑(2xy)(1<=x,y<=n)
=n*(n+1)*(2*n+1)/6*(2n)+ ∑(2xy)(1<=x,y<=n)
=n*(n+1)*(2*n+1)/6*(2n)+ ∑(2x * ((1+n)*n/2) )(1<=x<=n)
=n*(n+1)*(2*n+1)/6*(2n)+ 2*((1+n)*n/2) * ((1+n)*n/2)) 
之后就简单了。用java大数模拟就可以了。
用c++的话“/6”这个地方不知道怎么处理。
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        BigInteger M = BigInteger.valueOf(1000000007);
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        while(t!=0)
        {
            t--;
            BigInteger n = cin.nextBigInteger();
            BigInteger ping = n.multiply(n);//n*n

            BigInteger tmp = n.multiply(n.add(BigInteger.valueOf(1)));//n*(n+1)
            BigInteger sum =  tmp.divide(BigInteger.valueOf(2));//n*(n+1)/2

            BigInteger temp = tmp;//n*(n+1)
            tmp = n.multiply(BigInteger.valueOf(2));//2n
            tmp = tmp.add(BigInteger.valueOf(1));//2n+1

            temp = temp.multiply(tmp);//n*(n+1)*(2n+1)
            temp = temp.divide(BigInteger.valueOf(6));//n*(n+1)*(2*n+1)/6

            tmp = sum.multiply(sum.multiply(BigInteger.valueOf(2)));//sum*sum*2
            BigInteger  sub = temp.multiply(BigInteger.valueOf(4));//n*(n+1)*(2*n+1)/6*4
            sub = sub.add(ping.multiply(n));//n*(n+1)*(2*n+1)/6*4 + ping*n

            temp = temp.multiply(n.multiply(BigInteger.valueOf(2)));//n*(n+1)*(2*n+1)/6*(2*n)

            BigInteger ans = temp.add(tmp.add(sub.add(ping.multiply(ping))));
            ans = ans.divide(BigInteger.valueOf(2));
            System.out.println(ans.mod(M));
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值