博客
关于我
HHKB Programming Contest 2020 D - Squares(思维)
阅读量:287 次
发布时间:2019-03-01

本文共 3373 字,大约阅读时间需要 11 分钟。

Problem Statement

Given are integers N , A , and B .

We will place a white square whose side is of length NN on the coordinate plane so that the vertices are at (0,0) , (N,0) , (0,N) , and (N,N)

Then, we will place a blue square whose side is of length A and a red square whose side is of length B so that these squares are inside the white square (including its boundary).

Here, each side of these squares must be parallel to the x - or y -axis.

Additionally, each vertex of red and blue squares must be at an integer point.

Find the number of ways to place red and blue squares so that they do not strictly overlap (they may share boundary with each other), modulo 1,000,000,007

Solve this problem for T test cases in each input file.

Constraints

  • 1≤T≤105
  • 1≤N≤109
  • 1≤A≤N
  • 1≤B≤N
  • All values in input are integers.

Output

For each test case, print the number of ways to place red and blue squares so that they do not strictly overlap, modulo 1,000,000,0071,000,000,007. Use one line for each test case.


Sample Input 1 Copy

Copy

33 1 24 2 2331895368 154715807 13941326

Sample Output 1 Copy

Copy

2032409369707

For example, in the first test case, there are 99 ways to place the blue squares and 44 ways to place the red squares, ignoring overlap.

Wherever we place the red square, there are 44 ways to place the blue square so that it strictly overlaps with the red square.

Thus, there are 9×4−4×4=209×4−4×4=20 ways to place red and blue squares so that they do not strictly overlap.

Note that the squares may share boundary with each other. For example, when the bottom-left vertices of blue and red squares are (0,0)(0,0) and (0,1)(0,1), respectively, the squares do not strictly overlap.

 

题意:

在N*N的正方形区域内放置一个A*A和一个B*B的正方形,放置要求不能重叠且四个角要是整数点。

思路:
因为是在二维平面内放点,考虑起来比较复杂,我们可以把它投影到一维上。对于两个重叠的正方形投影到XY轴一定有X、Y轴的两根投影线段都会重叠,再看反面,如果有一个轴上的投影没有重叠则正方形一定是没重叠的。所以我们分下面两种情况统计答案:

(1)X轴上两线段不重合,Y轴投影随意放

因为线段都有两头,为简单起见我们只考虑两线段的左端点安放,对于A而言左端点坐标可以为[0,n-a],对于B而言左端点坐标为[0,n-b]
考虑到两线段的左右关系较为麻烦,我们不妨假设B线段一定在A线段的右边。
如果A左端点放置0,B可以放置[a,n-b],共n-b-a+1种放法
如果A左端点放置1,B可以放置[a+1,n-b],共n-b-a种放法
...
如果A左端点放置n-a-b,B可以放置在[n-b,n-b],共1种放法
我们记d=(n−a−b)d=(n−a−b),于是放法总数有(d+1)+(d)+(d−1)+...+1=(d+1)(d+2)2(d+1)+(d)+(d−1)+...+1=(d+1)(d+2)2
但是这只考虑了B在A右边的情况,B如果在A左边是相同情况,所以X轴上两线段不重合有(d+1)(d+2)(d+1)(d+2)种放置方式
Y轴因为是随意放置,利用乘法定理有(n−a+1)(n−b+1)(n−a+1)(n−b+1)中放法
故总放置方式数为(d+1)(d+2)(n−a+1)(n−b+1)(d+1)(d+2)(n−a+1)(n−b+1)

(2)X轴上两线段重合,Y轴投影不重合

第一种情况已经讨论了AB不重合的方案数为(d+1)(d+2)(d+1)(d+2),而总放法数为(n−a+1)(n−b+1)(n−a+1)(n−b+1),故重合的方案数为(n−a+1)(n−b+1)−(d+1)(d+2)(n−a+1)(n−b+1)−(d+1)(d+2)
而Y轴投影不重合的情况同X轴投影不重合相同,方案数为(d+1)(d+2)(d+1)(d+2)
故总放置方式数为(d+1)(d+2)[(n−a+1)(n−b+1)−(d+1)(d+2)](d+1)(d+2)[(n−a+1)(n−b+1)−(d+1)(d+2)]

综上,答案可用表达式(d+1)(d+2)(n−a+1)(n−b+1)+(d+1)(d+2)[(n−a+1)(n−b+1)−(d+1)(d+2)](d+1)(d+2)(n−a+1)(n−b+1)+(d+1)(d+2)[(n−a+1)(n−b+1)−(d+1)(d+2)]表示,化简结果为

ans=2(d+1)(d+2)(n−a+1)(n−b+1)−[(d+1)(d+2)]2ans=2(d+1)(d+2)(n−a+1)(n−b+1)−[(d+1)(d+2)]2

当然以上讨论都是建立在N*N的正方形区域是能放下两个正方形的,如果N<A+BN<A+B说明放不下要直接输出0。

#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll mod = 1e9 + 7;int main() {	ll t, n, a, b;	scanf("%lld", &t);	while(t--) {		scanf("%lld%lld%lld", &n, &a, &b);		if(a + b > n) {			printf("0\n");			continue;		}		ll tmp = (n - a - b) % mod;		ll ans = (tmp + 1) % mod * (tmp + 2) % mod				* (2 * (n - a + 1) % mod * (n - b + 1) % mod - (tmp + 1) % mod * (tmp + 2) % mod + mod) % mod;		printf("%lld\n", ans);	}	return 0;}

 

转载地址:http://mbio.baihongyu.com/

你可能感兴趣的文章
Python内存管理机制
查看>>
Python2中range 和xrange的区别??
查看>>
python2和3的区别
查看>>
python2和python3的pip版本升级
查看>>
python2和python3的区别
查看>>
python2备份交换机配置脚本
查看>>
Python3 - 在字符串格式化程序参数中使用变量
查看>>
Python3 A*寻路算法实现
查看>>
python3 grpc安装
查看>>
python3 mysql 封装多表_python3 mysql-多表查询
查看>>
python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
查看>>
Python关键代码总览!从Zero到Hero必看的考点
查看>>
python3 robotframework+pycharm框架搭建
查看>>
Python3 shutil模块
查看>>
Python关于strftime函数详细解析 附实战代码
查看>>
Python3 tutorial day3
查看>>
python3 week4
查看>>
Python3 创建数组
查看>>
python3 套接字异常(a byte-like object is required not str)
查看>>
python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
查看>>