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 ...
随机推荐
- bzoj2791
每个顶点有且仅有一条出边是什么意思呢 类似一棵树,树上的边都是由儿子指向父亲的,并且这个东西带着一个环 也就是一个个有向环套有向树…… 这题还是比较简单的,把环作为根然后类似lca做即可,注意细节的p ...
- bzoj2085
首先看到k的范围就该知道这题不是倍增就是矩乘 首先肯定要求出任意一对串(a,b) a的后缀与b的前缀相同的最长长度是多少 考虑到kmp求出的失配指针是一个串最长后缀和前缀相等的长度 这里多个串我们只要 ...
- poj3683 Priest John's Busiest Day
2-SAT. 读入用了黄学长的快速读入,在此膜拜感谢. 把每对时间当作俩个点.如果有交叉代表相互矛盾. 然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解. 如果有解,跑拓扑排序就能找出一 ...
- NoSQL 数据库系统对比
虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只是时间问题:被迫使用关系数据库,但最终发现不能适应需求的情况不胜枚举. 但是NoSQL数据库之间的不同,远超过两 SQ ...
- Material Design 设计--阴影的重要性
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_cont ...
- HDU 5313 Bipartite Graph (二分图着色,dp)
题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...
- 随机森林与GBDT
前言: 决策树这种算法有着很多良好的特性,比如说训练时间复杂度较低,预测的过程比较快速,模型容易展示(容易将得到的决策树做成图片展示出来)等.但是同时,单决策树又有一些不好的地方,比如说容易over- ...
- windows 下FFMPEG的编译方法 附2012-9-19发布的FFMPEG编译好的SDK下载
经过一晚上加一上午的奋斗,终于成功编译出了最新版的FFMPEG,下面是我编译的心得,因为是最新的,应该会对大家有用,编译的FFMPEG的版本是0.11.2,2012-09-19新发布的版本 平台:WI ...
- mysql的MMM高可用方案
1 MMM高可用mysql方案 1.1 方案简介 MMM即Master-Master Replication Manager for MySQL(mysql主主复制管理器)关于mysql主主复 ...
- Oracle表操作 (未完待续)
1. Oracle 中将一个表中数据导入到另外一个表的方法 insert into scd_data_201007 select * from analog_data_201007 ; 2. 纵表转换 ...