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 不熟悉导致各种问题频发。
公式:
由于有记录下公式,此题貌似很容易解决。小数据直接代入,都不用担心爆 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))
- Kickstart 2017 A. Square Counting
- Kickstart Round A 2017 Problem A. Square Counting
- Kickstart Round A 2017 Problem A. Square Counting Problem B. Patterns Overlap
- Kickstart Round A 2017 Problem A. Square Counting 公式、数论逆元、除法取模
- Kickstart Round A 2017
- Kickstart 2017 Round A
- Google Kickstart 2017 Round A 题解
- HDU 2483 Counting square
- Sicily 1918. Counting square
- [2017集训队作业自选题#134]Counting Divisors (square)
- A Square Perfect Square
- Kickstart Round A 2017 Problem.B Patterns Overlap 代补完
- Problem A. Math Encoder Google Kickstart Round B 2017
- Problem B. Patterns Overlap Google Kickstart Round A 2017
- Kickstart Practice Round 2017 Problem A. Country Leader
- Kickstart Round A 2017 Problem B. Patterns Overlap
- Kickstart Round A 2017 Problem C. Space Cubes
- Kickstart Round B 2017 Problem A. Math Encoder
- **抢答器**
- common-backend
- Vue.js实现多条件筛选、搜索、排序及分页的表格功能
- 状态压缩dp poj2411 1*2砖块
- 【Python】scikit-learn教程
- Kickstart 2017 A. Square Counting
- python开发笔记
- Qt QWidget实现开关控件SwithButton(SlipButton)
- 2017.3.6 地精部落 思考记录
- Android四大基本组件及生命周期介绍
- 体验JVM编译Java文件
- java企业网站源码,模版,有前后台,springmvcSSM,生成静态化
- 栗濡
- java后台框架源码 springmvc mybatis oracle mysql maven