vjudge - Rectangle

E - Rectangle

Rectangle

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⋅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

题目链接:

https://vjudge.net/contest/274213#problem/E

题意描述:

给你一个m行n列的矩阵,然后问小矩阵的周长和小于等于k的有多少个。

解题思路:

刚开始我是用了两重循环,枚举每一行的矩阵周长,结果超时了,后来看了大神的博客,可以用一重循环解决,暂时不太理解,如果哪位大神看到这个博客,并且懂这个思想,期待能给我分享一下。

终于懂了,首先控制一条边j=n-j+1,然后另一条边的最长i=k/2-j;当1的时候有m种,当i的时候有m-i+1种故从1到i共有(m+m-i+1)*i/2种所以行乘列得(n-j+1)*(m+m-i+1)*i/2;种

程序代码:

#include<stdio.h>
#include<math.h>
int main()
{
	long long n,m,i,j,k,sum;
	while(scanf("%lld%lld%lld",&m,&n,&k)!=EOF)
	{
		sum=0;
		k/=2;
		for(j=1;j<=n;j++)
		{
			i=k-j;
			if(i<=0)
				break;
			if(i>m)
				i=m;
			sum+=(n-j+1)*(m+m-i+1)*i/2;
		}
		printf("%lld\n",sum);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/HeZhiYing_/article/details/84877489
今日推荐