Kickstart 2017 A. Square Counting

来源:互联网 发布:股票数据api https 编辑:程序博客网 时间:2024/05/16 11:16

Problem A. Square Counting

Problem

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).

Input

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.

Output

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.

Limits

1 ≤ T ≤ 100.

Small dataset

2 ≤ R ≤ 1000.
2 ≤ C ≤ 1000.

Large dataset

2 ≤ R ≤ 109.
2 ≤ C ≤ 109.

分析

​第一次做 Kickstart 就被狠狠地打脸:cry: 。很水的题目,But,太天真。对 Python 不熟悉导致各种问题频发。

公式:

ans=(cr)(r36r6)+(r412r212)

由于有记录下公式,此题貌似很容易解决。小数据直接代入,都不用担心爆 int 。

但是,对于大数据,一眼就可以看到即使是 long long 也无法进行表示。写大整数过于繁杂,比赛时又没有仔细去化简,故而选择了 Python (:laughing: 之后就被精度问题彻底打爆)。由于 Python 的整型可以自动支持大整数,也不用专门去化简之类的。

重点是: 用 C/C++ 都会很注意 double 对精度的影响,但是,对于 Python 来说,其弱类型就导致了当声明 1e9+7 的时候,默认会作为浮点数,若不注意强转数据类型,float 对最终的大整数取模将产生致命的错误。

代码(small)

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<fstream>using namespace std;const int mod = 1e9 + 7;long long r, c;int main(){    freopen("G://in.in","r", stdin);    freopen("G://A.out","w",stdout);    int T;    scanf("%d",&T);    for(int ica = 1;ica<=T && scanf("%I64d %I64d",&r, &c);ica++)    {        if(r > c)   swap(r, c);        long long ans = (c-r)*(r*r*r/6 - r/6) + (r*r*r*r/12-r*r/12);        printf("Case #%d: %d\n",ica, ans % mod);    }}

代码(large)

mod = 1000000007T = int(input())for ica in range(1, T+1):    r, c = map(int, input().split())    if r > c:        r, c = c, r    ans = ((2*c)*(r**3) - 2*c*r - r**4 + r**2) // 12    ans %= mod    print("Case #%d: %d" % (ica, ans%mod))
0 0
原创粉丝点击