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:

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
Hint
(3,2,1) and (3,1,2) are regrad as the same cuboids.
来自 <http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3903>
【题意】:
已知一长方体的最大边长 N
求所有可能情况下,两对顶角沿表面的最短距离(展开成平面即可)的平方和。
【解题思路】:
若长方体边长为 N A B,则 L^2 = N^2 + (A+B)^2;
即找出所有小于N的A B组合,求出平方和
例如N=3;则有组合
(1,1)(1,2)(1,3)
(2,2)(2,3)
(3,3)
即最终答案中共有 N(N+1)/2 个 N^2;
再考虑 (A+B)^2 的和:
A取值1~N,对于每个A,B取值为x~n;
将 (A+B)^2展开为 A^2+B^2+2AB 求和
其中平方项每个出现 n+1 次,
而2AB项通过代数可得

将上述项依次求和即可,由于取模原因,除法要先求逆元,这里拓展欧几里得易求。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
#define mod 1000000007
#define IN freopen("in.txt","r",stdin);
using namespace std; LL x,y,gcd;
void ex_gcd(LL a,LL b)
{
if(!b) {x=;y=;gcd=a;}
else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}
} int main(int argc, char const *argv[])
{
//IN; LL two,six;
ex_gcd(, mod);
while(x<) {x+=mod;y-=;}
two = x;
ex_gcd(, mod);
while(x<) {x+=mod;y-=;}
six = x; int t;scanf("%d",&t);
while(t--)
{
LL n, ans = ;
scanf("%lld",&n);n%=mod; ans = (((((n*n)%mod)*((n*(n+))%mod))%mod)*two)%mod;
ans = (ans + ((((((((n+)*n)%mod)*(n+))%mod)*((*n)%mod+))%mod)*six)%mod)%mod;
ans = (ans + ((((n*n)%mod+n)%mod)*(((((n+)*n)%mod)*two)%mod))%mod)%mod;
LL tmp = (((n*(n+))%mod)*two)%mod;
ans = ((ans - ((tmp*tmp)%mod))%mod+mod)%mod; printf("%lld\n", ans%mod);
} return ;
}
ZOJ 3903 Ant(数学,推公示+乘法逆元)的更多相关文章
- ZOJ 3903 Ant ZOJ Monthly, October 2015 - A
Ant Time Limit: 1 Second Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...
- ZOJ 3903 Ant(公式推导)
这个公式推导过程是看的这位大牛的http://blog.csdn.net/bigbigship/article/details/49123643 扩展欧几里德求模的逆元方法: #include < ...
- A. On The Way to Lucky Plaza 概率 乘法逆元
A. On The Way to Lucky Plaza time limit per test 1.0 s memory limit per test 256 MB input standard i ...
- 2016中国APP分类排行榜参选入围产品公示
2016中国APP分类排行榜参选入围产品公示 由中国科学院<互联网周刊>.中国社会科学院信息化研究中心.eNet硅谷动力共同主办的2016中国APP分类排行榜发布暨颁奖晚宴即将举行.此 ...
- 一个想法(续五):IT联盟创业计划:现阶段进度公示、疑问解答及进行中的计划
前言: 首先今天是元宵节,先祝大伙元宵节快,单纯的快乐! 然后看看开展中的计划: IT联盟创业计划众筹发起:一个想法(续三):一份IT技术联盟创业计划书,开启众筹创业征程 IT联盟创业计划众筹进度:一 ...
- Luogu T7152 细胞(递推,矩阵乘法,快速幂)
Luogu T7152 细胞(递推,矩阵乘法,快速幂) Description 小 X 在上完生物课后对细胞的分裂产生了浓厚的兴趣.于是他决定做实验并 观察细胞分裂的规律. 他选取了一种特别的细胞,每 ...
- 【2017集美大学1412软工实践_助教博客】团队作业3——需求改进&系统设计 成绩公示
第三次团队作业成绩公布 题目 团队作业3: 需求改进&系统设计 团队成绩 成绩公示如下: 缩写 TD BZ GJ CJ SI WBS GS JG DB SS SJ CS DC Total 分值 ...
- 【软件工程1916|W(福州大学)_助教博客】团队第一次作业成绩公示
题目 第一次作业 评分准则: 队名(最好能够体现项目内容,要求有亮点与个性):(1分) 拟作的团队项目描述:一句话(中英文不限):(1分) 队员风采:介绍每一名队员,包括成员性格.擅长的技术.编程的兴 ...
- 工信部公示网络安全示范项目 网易云易盾“自适应DDoS攻击深度检测和防御系统”入选
本文由 网易云发布. 工信部官网 2017年年底,经专家评审和遴选,中华人民共和国工业和信息化部(以下简称“工信部”)公示了2017年电信和互联网行业网络安全试点示范项目,网易云易盾的“自适应DDo ...
随机推荐
- Interpolated Strings
https://msdn.microsoft.com/en-us/library/dn961160.aspx ; // Before C# 6.0 System.Console.WriteLine(S ...
- [58 Argo]让argo跑起来
接上一章,使用命令mvn jetty:run启动Argo,进入localhost的页面: 58在这里给了几种常见的访问和传值方法的示例,当点击到第三条<区分queryString和form参数& ...
- HDU3709 Balanced Number (数位dp)
Balanced Number Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- sqlserver错误"试图扩大物理文件时,MODIFY FILE 遇到操作系统错误 112(磁盘空间不足。)。"处理
正常还原的时候报错: Microsoft SQL-DMO (ODBC SQLState: 42000)---------------------------试图扩大物理文件时,MODIFY FILE ...
- HTTP请求头host解析
Host: 域名 Host表示请求的服务器网址: request headers中的host字段 例如有user.xiaoqiang.com,hotel.xiaoqiang.com 现在需要登录后 ...
- shell中for循环总结
关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿... 1. for((i=1;i<=10;i++));d ...
- 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)
转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...
- 编译boost (windows msvc14)
我的环境 OS: WIN10 (x64) IDE: VS2015 (VC14) http://www.boost.org/ 1. 下载 下载boost包, boost_1_62_0.7z 使用ASIO ...
- DBCP连接池原理分析及配置用法
DBCP连接池介绍 ----------------------------- 目前 DBCP 有两个版本分别是 1.3 和 1.4. DBCP 1.3 版本需要运行于 JDK 1.4-1.5 ,支持 ...
- <转>如何测试一个杯子
在软件测试的面试中, 经常会碰到类似的问题. 比如:如何测试一个杯子, 或者如何测试一只笔. 要求你设计20个以上的test case. 这类的面试题目,是考察面试者是否熟悉各种软件测试方法, 设计t ...