Statistics

Problem Statement for "DonutsOnTheGrid"

Problem Statement

Petya likes donuts. He tries to find them everywhere. For example if he is given a grid where each cell contains a '0' or '.' he will construct donuts from the cells.

To make the donuts:

  1. Petya first selects a rectangle of cells of width, w, and height, h, where both are at least 3.
  2. Then he removes a rectangular hole of width w-2 and height h-2 centered in the larger rectangle.
  3. For the remaining cells (a closed rectangular ring) to be a valid donut, all of the cells must contain '0' (because donuts are shaped like zeros). Cells in the hole can contain anything since they are not part of the donut.

Here is an example with three overlapping donuts.

..........
.00000000.
.0......0.
.0.000000.
.0.0...00.
.0.0...00.
.0.000000.
.0......0.
.00000000.
..........

The grid in this problem will be pseudo-randomly generated using the following method:
You are given four ints: H (the grid height), W (the grid width), seed and threshold. Let x0=seed and for all i>=0 let xi+1 = (xi * 25173 + 13849) modulo 65536. Process the cells of the matrix in row major order (i.e., first row left to right, second row left to right, etc.). Each time you process a cell, calculate the next xi (starting with x1 for the upper left corner). If it is greater than or equal to threshold, the current cell will contain a '.', otherwise it will contain a '0'.

Return the number of distinct donuts in the figure. Two donuts are considered distinct if they either have different width or height, or if the top left hand corner is in a different location (i.e., overlapping donuts are counted).

Definition

Class:
DonutsOnTheGrid
Method:
calc
Parameters:
int, int, int, int
Returns:
long
Method signature:
long calc(int H, int W, int seed, int threshold)
(be sure your method is public)

Notes

  • The random generation of the input is only for keeping the input size small. The author's solution does not depend on any properties of the generator, and would work fast enough for any input of allowed dimensions.

Constraints

  • H will be between 1 and 350, inclusive.
  • W will be between 1 and 350, inclusive.
  • seed will be between 0 and 65535, inclusive.
  • threshold will be between 0 and 65536, inclusive.

Examples

  1. 350

    350

    1

    65536

    Returns: 3687647076

  2. 257

    210

    36894

    19420

    Returns: 5

  3. 36

    48

    1809

    31235

    Returns: 12

  4. 37

    327

    46242

    20375

    Returns: 1

  5. 344

    43

    14547

    58855

    Returns: 118610

  6. 88

    70

    9249

    26807

    Returns: 2

  7. 343

    113

    31408

    48903

    Returns: 19106

  8. 28

    298

    2230

    44929

    Returns: 1441

  9. 142

    50

    16772

    36823

    Returns: 132

  10. 145

    187

    31639

    8116

    Returns: 0

  11. 210

    117

    47189

    32284

    Returns: 167

  12. 236

    296

    18596

    11592

    Returns: 0

  13. 90

    302

    8868

    34097

    Returns: 251

  14. 111

    162

    20883

    24601

    Returns: 15

  15. 58

    228

    26591

    38876

    Returns: 507

  16. 116

    5

    13227

    3500

    Returns: 0

  17. 159

    271

    9823

    14191

    Returns: 1

  18. 185

    57

    7141

    54393

    Returns: 22040

  19. 350

    6

    9177

    24060

    Returns: 1

  20. 232

    78

    2604

    15579

    Returns: 0

  21. 58

    339

    31350

    30053

    Returns: 55

  22. 87

    128

    22200

    6058

    Returns: 0

  23. 332

    92

    23376

    36548

    Returns: 618

  24. 342

    43

    32274

    34220

    Returns: 144

  25. 170

    131

    5360

    14631

    Returns: 0

  26. 252

    14

    27833

    50994

    Returns: 2623

  27. 177

    291

    25861

    23037

    Returns: 15

  28. 257

    324

    11534

    24451

    Returns: 68

  29. 61

    159

    16367

    25481

    Returns: 9

  30. 157

    258

    29276

    32454

    Returns: 258

  31. 247

    335

    25750

    38020

    Returns: 2310

  32. 148

    297

    22764

    49608

    Returns: 25364

  33. 128

    85

    29741

    14905

    Returns: 0

  34. 289

    150

    38488

    10128

    Returns: 0

  35. 79

    335

    2858

    45733

    Returns: 5570

  36. 198

    175

    8375

    57823

    Returns: 259018

  37. 113

    39

    20603

    32353

    Returns: 19

  38. 84

    155

    31640

    20327

    Returns: 2

  39. 271

    103

    31946

    42843

    Returns: 2750

  40. 1

    317

    6663

    40637

    Returns: 0

  41. 164

    178

    7093

    44248

    Returns: 4266

  42. 117

    201

    21586

    25785

    Returns: 11

  43. 137

    329

    21739

    38377

    Returns: 1362

  44. 74

    271

    53821

    18211

    Returns: 6

  45. 337

    146

    11266

    32607

    Returns: 280

  46. 275

    161

    26701

    5489

    Returns: 0

  47. 34

    138

    27276

    32211

    Returns: 36

  48. 207

    189

    32002

    20079

    Returns: 3

  49. 77

    92

    54161

    8096

    Returns: 0

  50. 131

    277

    17232

    1609

    Returns: 0

  51. 251

    159

    31420

    12125

    Returns: 0

  52. 26

    85

    22149

    30947

    Returns: 2

  53. 144

    344

    19882

    24373

    Returns: 29

  54. 26

    57

    42540

    31153

    Returns: 3

  55. 141

    210

    7024

    9850

    Returns: 0

  56. 177

    29

    5314

    17954

    Returns: 0

  57. 34

    212

    55601

    12810

    Returns: 0

  58. 15

    322

    44752

    25529

    Returns: 4

  59. 147

    61

    26266

    21365

    Returns: 2

  60. 299

    315

    32741

    40857

    Returns: 5813

  61. 316

    2

    29740

    31558

    Returns: 0

  62. 173

    63

    29829

    10822

    Returns: 0

  63. 133

    169

    29040

    42661

    Returns: 2421

  64. 256

    346

    43261

    30973

    Returns: 334

  65. 202

    307

    33208

    14191

    Returns: 0

  66. 167

    202

    4347

    6688

    Returns: 0

  67. 179

    204

    27759

    39877

    Returns: 1623

  68. 62

    343

    45172

    42965

    Returns: 2467

  69. 33

    223

    51755

    46224

    Returns: 1520

  70. 279

    122

    7519

    28479

    Returns: 73

  71. 143

    258

    23322

    58335

    Returns: 333898

  72. 283

    40

    28914

    33605

    Returns: 105

  73. 267

    278

    46046

    4060

    Returns: 0

  74. 307

    145

    762

    39524

    Returns: 2004

  75. 341

    86

    30629

    28399

    Returns: 46

  76. 278

    77

    19466

    56497

    Returns: 93822

  77. 289

    255

    6393

    24667

    Returns: 40

  78. 111

    289

    22295

    24038

    Returns: 11

  79. 88

    271

    34773

    26050

    Returns: 14

  80. 76

    185

    20146

    43512

    Returns: 1444

  81. 142

    350

    30442

    28900

    Returns: 120

  82. 224

    208

    9140

    26890

    Returns: 39

  83. 253

    320

    35102

    41083

    Returns: 4241

  84. 209

    344

    32100

    31801

    Returns: 394

  85. 27

    260

    16065

    45582

    Returns: 1420

  86. 108

    142

    58731

    30877

    Returns: 76

  87. 146

    222

    6728

    8748

    Returns: 0

  88. 164

    201

    25496

    31906

    Returns: 111

  89. 343

    41

    21235

    3094

    Returns: 0

  90. 37

    25

    9527

    57274

    Returns: 3068

  91. 292

    265

    47961

    3378

    Returns: 0

  92. 115

    342

    7018

    28648

    Returns: 84

  93. 26

    330

    27463

    30105

    Returns: 32

  94. 211

    105

    29178

    4226

    Returns: 0

  95. 290

    108

    16735

    45368

    Returns: 5682

  96. 269

    77

    29375

    24455

    Returns: 17

  97. 95

    287

    7659

    5075

    Returns: 0

  98. 69

    118

    23578

    38344

    Returns: 210

  99. 172

    106

    39212

    19924

    Returns: 2

  100. 131

    152

    14115

    7519

    Returns: 0

  101. 127

    176

    23449

    17627

    Returns: 0

  102. 5

    5

    222

    55555

    Returns: 4

    Here is an example of the grid: 00000 00.0. .0000 00.00 00000

  103. 350

    350

    1354

    65500

    Returns: 2909504310

  104. 349

    317

    23566

    65000

    Returns: 263790994

  105. 350

    347

    11

    63500

    Returns: 23150152

  106. 337

    340

    244

    62111

    Returns: 6654659

  107. 5

    6

    121

    58000

    Returns: 3

    Here is the grid and three donuts in it: 00000. XXX... XXX... ..XXX. 0.0000 X.X... X.X... ..X.X. 0.000. X.X... X.X... ..XXX. 000.00 XXX... X.X... ...... 000.00 ...... XXX... ......

  108. 4

    5

    6

    50000

    Returns: 1

    The grid is: 0.0.0 00000 .00.0 0.000

  109. 4

    4

    1

    65536

    Returns: 9

    Here, the grid is completely filled by 0's. There are 9 possible placements of a donut: XXX. XXX. .XXX .XXX .... .... XXXX .... XXXX X.X. X.X. .X.X .X.X XXX. .XXX X..X XXXX X..X XXX. X.X. .XXX .X.X X.X. .X.X XXXX X..X X..X .... XXX. .... .XXX XXX. .XXX .... XXXX XXXX

  110. 344

    347

    1

    65535

    Returns: 3468398770

  111. 350

    350

    0

    65536

    Returns: 3687647076

  112. 350

    350

    1

    0

    Returns: 0

  113. 320

    320

    4091

    30872

    Returns: 294

  114. 350

    350

    2

    65534

    Returns: 3634680381

  115. 333

    333

    33333

    33333

    Returns: 903

  116. 100

    100

    1

    65536

    Returns: 23532201

  117. 350

    350

    1218

    31245

    Returns: 545

  118. 350

    350

    12312

    0

    Returns: 0

  119. 350

    350

    1

    65000

    Returns: 291635119

  120. 350

    350

    4325

    65536

    Returns: 3687647076

  121. 350

    350

    65535

    65536

    Returns: 3687647076

  122. 350

    350

    222

    65536

    Returns: 3687647076

  123. 349

    349

    24356

    52576

    Returns: 163585


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: