P2241 statistics square (data enhanced edition), p2241 enhanced Edition

Source: Internet
Author: User

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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.