ZOJ-3903-Ant【逆元】【数学】

3903-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:

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) . T 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

题目链接:ZOJ-3903

题目大意:一个立方体,已知立方体最长边长为n,一只蚂蚁从一个顶点走到对角线的另一个顶点,且走的是最短的路线,记路线长度为L。那么所有满足题意的情况的L*L之和是多少

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
#define mod 1000000007
long long square(long long n)
{
    long long a = n;
    long long b = n + 1;
    long long c = 2 * n + 1;
    if (a % 2 == 0) a /= 2;
    else b /= 2;
    if (a % 3 == 0) a /= 3;
    else if (b % 3 == 0) b /= 3;
    else c /= 3; 
    return ((a * b) % mod * c) % mod;   
}

long long cube(long long n)
{
    long long a = n;
    long long b = n + 1;
    if (a % 2 == 0) a /= 2;
    else b /= 2;
    return (a * b) % mod;
}
long long last(long long n)
{
    return cube(n) % mod * n % mod * n %mod;
}
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        long long n;
        cin >> n;
        n %= mod;
        long long ans = square(n) % mod * (n + 2) % mod + cube(n) % mod * cube(n) % mod + last(n);
        ans %= mod;
        cout << ans << endl;
    } 
    return 0;
}

你可能感兴趣的:(数学,ZOJ,3903)