P2241 statistics square (data enhanced edition), p2241 enhanced Edition
Background
No. 1 in the popularity Group in 1997
Description
There is an n * m checkboard, asking how many squares and rectangles are contained in the square.
Input/Output Format
Input Format:
N, m because the original data is too weak, it is stipulated that m is less than or equal to 5000, and n is less than or equal to 5000 (originally 100,100)
Output Format:
How many squares and rectangles are contained in a square
Input and Output sample
Input example #1:
2 3
Output sample #1:
8 10
Number of squares
The side length is 1 n * m.
The side length is 2 (n-1) * m-1)
Side length is 3 (n-2) * (m-2)
Therefore, the edge length is min {n, m} count (m-min {n, m} + 1) * (n-min {n, m} + 1)
Number of Rectangles and squares
Total = (1 + 2 + 3 +... + N) * (1 + 2 + 3 +... + M)
= (1 + n) * (1 + m) * n * m)/4
Number of rectangles
The above two subtraction methods can be obtained.
So the idea is to first calculate the number of squares, then use the formula to get the sum of the numbers of Rectangles and squares, and finally get the number of rectangles.
Very short code
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int main() 7 { 8 long long n,m; 9 long long ans1=1,ans2=0;10 cin>>n>>m;11 ans1=n*m;12 long long p=(((1+n)*(1+m))*(n*m))/4;13 while(n--&&m--)14 {15 ans1+=(n*m);16 }17 cout<<ans1<<" "<<p-ans1;18 }