如果
Small input 8 points |
Solve A-small
|
Large input 17 points |
Solve A-large
|
Mr. Panda has recently fallen in love with a new game called Square Off, in which players compete to find as many different squares as possible on an evenly spaced rectangular grid of dots. To find a square, a player must identify four dots that form the vertices of a square. Each side of the square must have the same length, of course, but it does not matter what that length is, and the square does not necessarily need to be aligned with the axes of the grid. The player earns one point for every different square found in this way. Two squares are different if and only if their sets of four dots are different.
Mr. Panda has just been given a grid with R rows and C columns of dots. How many different squares can he find in this grid? Since the number might be very large, please output the answer modulo 109 + 7 (1000000007).
The first line of the input gives the number of test cases, T. T lines follow. Each line has two integers R and C: the number of dots in each row and column of the grid, respectively.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the number of different squares can be found in the grid.
1 ≤ T ≤ 100.
2 ≤ R ≤ 1000.
2 ≤ C ≤ 1000.
2 ≤ R ≤ 109.
2 ≤ C ≤ 109.
The pictures below illustrate the grids from the three sample cases and a valid square in the third sample case.
这题好难啊。。。我怎么这么垃圾啊。。。比赛当天large的数据根本算不出来,然后觉得google的大数据要一次通过。。。。好难啊。。
今天自己认真的写了一下。。因为数据范围是1e9,所以对复杂度要求很高的。。
写一写自己推导公式的过程吧,还是zyyyyy的代码好看,代码写的丑是我一直以来。。
题意是要求我们求出一个n*m的网格中有多少个正方形,具体看样例很快就能明白。
(一下讨论的n和m是指边长,题目中的n和m是指点,注意)
首先考虑非斜的正方形,在一个n*m(n<=m)的网格中,非斜的正方形的个数应该是:
那么斜的正方形呢,可以看出,在每个边长为k网格的非斜正方形中,斜的正方形个数应该是k-1个
比如k=4,那么如下图(其实就是从一条边枚举正方形一个角的起点罢了):
那么在n*m的网格中,斜正方形的个数如下:
将非斜的和斜的加起来并展开:
其中n(n+1)(2n+1)/6和[n(n+1)/2]^2分别是平方、立方求和公式。
这样看上去似乎对于每次询问复杂的可以降低O(1)
然而!!因为求解过程中处处都需要取模,在立方求和公式中存在着一个除法取模的问题。
关于这个问题,要利用逆元来达到以除法换乘法。在比赛中zyyyyy居然就能做出来一发过。。。实在厉害。。。
取模乘法换除法的方法可以参考这篇博客,我觉得写得很清楚,也是我最终代码实现的方法。
最后分析一下复杂度。。。应该是在那个快速幂上吧,所以是O(log mod)(如果错了还望指出)
下面代码,数论还是要好好学的啊。。。。。脑子不灵光
#include
#include
#include
#include
#include
#include
#include
#include