SRM 146 1A2A 2013.12.1
DIV 1 300
Problem Statement
Given the width and height of a rectangulargrid, return the total number of rectangles (NOT counting squares) that can befound on this grid.
For example, width = 3, height = 3 (seediagram below):
____ __
|__|__|__|
|__|__|__|
|__|__|__|
In this grid, there are 4 2x3 rectangles, 61x3 rectangles and 12 1x2 rectangles. Thus there is a total of 4 + 6 + 12 = 22rectangles. Note we don't count 1x1, 2x2 and 3x3 rectangles because they aresquares.
Definition
Class:
RectangularGrid
Method:
countRectangles
Parameters:
int, int
Returns:
long long
Method signature:
long long countRectangles(int width, intheight)
(be sure your method is public)
Notes
-
rectangles with equals sides (squares)should not be counted.
Constraints
-
width and height will be between 1 and 1000inclusive.
Examples
0)
3
3
Returns: 22
See above
1)
5
2
Returns: 31
____ __ __ __
|__|__|__|__|__|
|__|__|__|__|__|
In this grid, there is one 2x5 rectangle, 22x4 rectangles, 2 1x5 rectangles, 3 2x3 rectangles, 4 1x4 rectangles, 6 1x3rectangles and 13 1x2 rectangles. Thus there is a total of 1 + 2 + 2 + 3 + 4 +6 + 13 = 31 rectangles.
2)
10
10
Returns: 2640
3)
1
1
Returns: 0
4)
592
964
Returns: 81508708664
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
class RectangularGrid
{
public: long countRectangles(int width, intheight)
{
longans=0;
for(inti=1;i<=width;i++)
for(intj=1;j<=height;j++)
{
if(i==j) continue;
ans+= (width+1-i)*(height+1-j);
}
returnans;
}
};
DIV 2
250
Problem Statement
This task is about the scoring in the firstphase of the die-game Yahtzee, where five dice are used. The score isdetermined by the values on the upward die faces after a roll. The player getsto choose a value, and all dice that show the chosen value are consideredactive. The score is simply the sum of values on active dice.
Say, for instance, that a player ends upwith the die faces showing 2, 2, 3, 5 and 4. Choosing the value two makes thedice showing 2 active and yields a score of 2 + 2 = 4, while choosing 5 makesthe one die showing 5 active, yielding a score of 5.
Your method will take as input a vector<int> toss, where each element represents the upward face of a die, andreturn the maximum possible score with these values.
Definition
Class:
YahtzeeScore
Method:
maxPoints
Parameters:
vector <int>
Returns:
int
Method signature:
int maxPoints(vector <int> toss)
(be sure your method is public)
Constraints
-
toss will contain exactly 5 elements.
-
Each element of toss will be between 1 and6, inclusive.
Examples
0)
{ 2, 2, 3, 5, 4 }
Returns: 5
The example from the text.
1)
{ 6, 4, 1, 1, 3 }
Returns: 6
Selecting 1 as active yields 1 + 1 = 2,selecting 3 yields 3, selecting 4 yields 4 and selecting 6 yields 6, which isthe maximum number of points.
2)
{ 5, 3, 5, 3, 3 }
Returns: 10
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
228.52
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class YahtzeeScore
{
public:int maxPoints(vector <int>toss)
{
int num[10]={0};
for(vector<int>::iteratori=toss.begin();i!=toss.end();i++)
{
num[(*i)]++;
}
int temp=-1;
for(int j=1;j<=6;j++)
{
int p=num[j]*j;
if (p>temp)
{
temp=p;
}
}
return temp;
}
};