14行代码AC_SCU 4440 Rectangle(公式+矩阵对称性)
勵志用少的代碼做高效表達
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;}總結
以上是生活随笔為你收集整理的14行代码AC_SCU 4440 Rectangle(公式+矩阵对称性)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四种解法——求子序列的最大连续子序和(普
- 下一篇: 保证全对——2015年第六届蓝桥杯C/C