Rectangle
frog has a piece of paper divided into nn rows and mm columns. Today, she would like to draw a rectangle whose perimeter is not greater than kk .
There are 88 (out of 99 ) ways when n=m=2,k=6n=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 33 integer n,m,kn,m,k (1≤n,m≤5?104,0≤k≤1091≤n,m≤5?104,0≤k≤109 ).
Output
For each test, write 11 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
这题我看到一个题解,感觉写的很透彻,放这存一下。
题意:给定长度,求在不大于这个长度下,有多少个矩形(矩形周长不大于给定长度)。主要是用到了矩形的对称性 以及以下这个性质 在长为n,宽为m的矩形上,长为i,宽为j的矩阵个数为(n-i+1)x(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(2m-m+1)m/2; 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; }
题解地址:http://blog.csdn.net/VictorZC8/article/details/51242491
时间: 2024-10-10 04:39:47