joj 2278 count the square

joj 2278 count the square
There is chessboard which has N * M squares size of 1 * 1 and small squares can build up to a large one. Can you tell Mr. Guo that how many squares in the chessboard?

For Example, when N = 2 and M = 3, there are 6 squares size of 1 * 1 and 2 squares size of 2 * 2 in the chessboard, so the answer is 8.

Input

There are several lines in the input, each line consist of two positive integers N and M (1 <= N <= 100, 1 <= M <= 100) except the last line which N = 0 and M = 0 implying the end of the input and you should not process it.

Output

Please output the number of the squares for each chessboard in a single line.

Sample Input

1 1
2 3
3 3
0 0

Sample Output

1
8
14

启发:人是怎么算的,就让程序怎么算,一步一步推。
#include<iostream>
using namespace std;
int main()
{
int n,m;
long sum=0;
int i;
while(cin>>n>>m,n)
{
if(n==m)
{
sum=n*(n+1)*(2*n+1)/6;
cout<<sum<<endl;
}
else
{
for(i=0;i<n&&i<m;i++)
sum+=(n-i)*(m-i);
cout<<sum<<endl;
}
sum=0;
}
return 0;
}

你可能感兴趣的:(joj 2278 count the square)