E. Rectangle
Time Limit: 1000msMemory Limit: 65536KB 64-bit integer IO format: %lld Java class name: Main
Submit Status
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
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⋅104,0≤k≤109).
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
解析:枚举矩形的长h,然后它在h的方向上就有n-h+1种放法,同时宽的最大值w即为k/2 - h,对于每个0~w的宽度wi,它在w方向上的放法有m-wi+1种,求和即为所求方案数
PS:用printf输出long long的结果,你看到的并不是你想要的,竟然是个负数。但是cout还是没问题的
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk
long long n, m, k;
while(cin>>n>>m>>k){
k /= 2;
long long ans = 0;
for(int h=1; h<=n; h++){
int w = k - h;
if(w <= 0) break;
if(w > m) w = m;
ans += (n - h + 1) * (m + m-w+1)*w/2; //对于每个h,在h方向上n-h+1种,在w方向上枚举wi求和为(m + m-w+1) * w / 2种
}
cout<<ans<<endl;
}
return 0;
}