zoj 3903 Ant(推公式,逆元)

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.

 

 

今天的训练题...

略水?不过这题过地竟然没有一道要写lazy标记的线段树的题过的人数多。

容易知道,最短路径一定是在最长边为单独一边的时候。

设另两条边为a,b

很容易发现(a+b)*(a+b)+n*n <=(n+b)*(n+b)+a*a  打开就看到了。

然后根据勾股定理l2= (a+b)+n2

我们设一共有t个矩阵...

容易发现,关于对角线对称...而对称的两种是算一种的...

正好是三角形数...t=(n+1)*n/2;

将l2= (a+b)+n2  打开得到 l2 = a+ b +2*a*b+n2;

n2一共有t个。

容易发现a2 和b2各有n+1个(画一个表格比较直观

a*b可以先把所有的a*b加在一起,再化简。

 

最后的结果是n*(n+1)/12*(13n2+13n+4)

然后因为要mod 1E9+7。。。又有除法。。所以要用到逆元。

(n*(n-1)/12 )%mod=(12mod-2*n*(n-1))%mod

因为没用逆元wa了一发。。。

然后因为错误地把lld写成 %d...wa了两发。。。

其实这题n有1E14...

o(n)都会跪。。

所以基本差不多是数学+公式。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <iomanip>
 5 #include <cstring>
 6 #include <string>
 7 #include <cctype>
 8 #include <map>
 9 #include <set>
10 #include <stack>
11 #include <queue>
12 
13 typedef long long LL;
14 using namespace std;
15 const LL MOD = 1E9+7;
16 LL T;
17 LL n;
18 LL ans;
19 LL qpow(LL a,LL b,LL k)
20 {
21     LL res = 1;
22     while (b > 0)
23     {
24           if (b & 1)
25               res = (res*a)%k;
26            b = b >> 1 ;
27            a = (a*a)%k;
28     }
29     return res;
30 }
31 
32 int main()
33 {
34 
35     cin>>T;
36     while (T--)
37     {
38         scanf("%lld",&n);
39         n = n % MOD;
40 
41         ans = qpow(12,MOD-2,MOD);
42        //   cout<<"ans:"<<ans<<endl;
43         ans = (ans*n)%MOD;
44       //    cout<<"ans:"<<ans<<endl;
45         ans = ans*(n+1)%MOD;
46       //    cout<<"ans:"<<ans<<endl;
47         ans = ans % MOD;
48      //     cout<<"ans:"<<ans<<endl;
49 
50  //       cout<<"ans:"<<ans<<endl;
51 
52         LL tmp = (n*n)%MOD;
53         tmp = (tmp *13)%MOD;
54         tmp = (tmp + 13*n)%MOD;
55         tmp = (tmp + 4)%MOD;
56         ans = (ans * tmp)%MOD;
57         printf("%lld\n",ans);
58     }
59 
60 
61 
62   return 0;
63 }
View Code

 

posted @ 2015-10-18 22:22  111qqz  阅读(180)  评论(0编辑  收藏  举报