思路1:暴力枚举即可,数据范围足够小。
1 #include <iostream> 2 using namespace std; 3 4 int solve( int n, int m ) 5 { 6 int ans = 0; 7 for ( int i = 1; i <= n; i++ ) 8 { 9 for ( int j = 1; j <= m; j++ ) 10 { 11 ans += ( n - i + 1 ) * ( m - j + 1 ); 12 } 13 } 14 return ans; 15 } 16 17 int main() 18 { 19 int t; 20 cin >> t; 21 while ( t-- ) 22 { 23 int n, m; 24 cin >> n >> m; 25 cout << solve( n, m ) << endl; 26 } 27 return 0; 28 }
思路2:发现规律,证明很简单,略。
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int t; 7 cin >> t; 8 while ( t-- ) 9 { 10 int n, m; 11 cin >> n >> m; 12 cout << n * ( n + 1 ) / 2 * m * ( m + 1 ) / 2 << endl; 13 } 14 return 0; 15 }