励志用少的代码做高效表达
Problem Describe
frog has a piece of paper divided into (n) rows and (m) columns. Today, she would like to draw a rectangle whose perimeter is not greater than (k).
There are 8 (out of 9) ways when n = m = 2, k = 6
There are (8) (out of (9)) ways when (n = m = 2, k = 6)
Find the number of ways of drawing.
Input
The input consists of multiple tests. For each test:
The first line contains 3 integer n,m,k (1≤n,m≤5⋅10e4,0≤k≤1e9).
Output
For each test, write 1 integer which denotes the number of ways of drawing.
Sample Input
2 2 6
1 1 0
50000 50000 1000000000
Sample Output
8
0
1562562500625000000
题目(提交)网址——>传送门
题意
给定长度,求在不大于这个长度下,有多少个矩形(矩形周长不大于给定长度)。
心路历程
起初看到这道题,想都没想,数…….找规律
结果找了2个小时规律
愣是没找出来
问了问同学,同学说是暴力
我*
SB的我竟然还想用公式,难怪推不出来
暴力的话,这道题就简单明了了
主要是用到了矩形的对称性
以及以下这个性质
在长为n,宽为m的矩形上,长为i,宽为j的矩阵个数为
( n − i + 1 ) ∗ ( m − j + 1 ) (n-i+1)*(m-j+1) (n−i+1)∗(m−j+1)
首先考虑n,在一个长为n的矩形中,从1~i,2~i+1,3~i+2,n-i+1~n; 分别为长为i的矩形;
同理考虑m,宽为j的矩形,1~j,2~j+1,3~j+2,m-j+1~m;
这样的话,在1~j下就有n-i+1个矩形
所以总共就是:(n-i+1)x(m-j+1)
那么这道题的答案就出来了
记num=k/2-i (num>0),k为周长 i为长 num为宽
当num<=m时,num可以取1,2,3,…,num
所以答案为:ans=(n-i+1)x(m-1+1)+(n-i+1)x(m-2+1)+…+(n-i+1)*(m-num+1);
提取(n-i+1),就是一个等差数列得:ans+=(n-i+1)x(2m-num+1)num/2;
当num>m时,num替换为m,得:ans+=(n-i+1)x(m+1)m/2;
#include<cstdio>
#define ll long long
ll n,m,k,ans,num;
int main() {
while(~scanf("%lld%lld%lld",&n,&m,&k)) {
ans=0;
for(int i=1;i<=n;i++) {
num=k/2-i;
if(num<=m&&num>0)ans+=(n-i+1)*(2*m-num+1)*num/2;
else if(num>0)ans+=(n-i+1)*(1+m)*m/2;
}
printf("%lld\n",ans);
}
return 0;}