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 ...
随机推荐
- poi操作oracle数据库导出excel文件
HSSFWorkbook workBook = new HSSFWorkbook();// 创建 一个excel文档对象 HSSFSheet sheet = workBook.createSheet( ...
- Java对ArrayList进行排序
数字使用直接比较大小来排序,String的话,使用compare()方法进行排序. 测试代码: 1.对字符串对象排序 @Test public void test17() throws Excepti ...
- launch genymotion simulator from command line
Command to launch genymotion headless - player --vm-name Nexus_4 if player is not already added to p ...
- ScaleGestureDetector缩放view
public class ScaleGesture implements OnScaleGestureListener { private float beforeFactor; private fl ...
- Flexigrid自定义显示数据列
近期在搞ExtJs,发现ExJs的Grid相当的强大,后来又搞Jquery时,就对原来的表格不怎么满意了,于是,花了点时间,从网上找了个Grid插件,这个插件功能是比较强大,什么行排序.筛选.分页都有 ...
- symbolicatecrash位置
symbolicatecrash是一个隐藏工具,它在我的Mac中的具体路径如下(Xcode6.1.app请换成你的Xcode名称) /Applications/Xcode6.1.app/Content ...
- RESTful API 设计最佳实践(转)
背景 目前互联网上充斥着大量的关于RESTful API(为方便,下文中“RESTful API ”简写为“API”)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API 格式如何?你的 ...
- 【C#学习笔记】打开新进程
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- poj 3160 Father Christmas flymouse
// 题目描述:从武汉大学ACM集训队退役后,flymouse 做起了志愿者,帮助集训队做一些琐碎的事情,比如打扫集训用的机房等等.当圣诞节来临时,flymouse打扮成圣诞老人给集训队员发放礼物.集 ...
- Squid故障
1.COSS will not function without large file support (off_t is 4 bytes long. Please reconsider recomp ...