Statistics

Problem Statement for "Postcards"

Problem Statement

There are N cities in Absurdistan. They are numbered from 0 to N-1.

For simplicity, we will assume that city x has exactly (x+1) inhabitants.


Initially, no two cities were connected by roads. Then there was a Y-year period of road construction.

During this period, two cities become reachable from each other as soon as there exists a sequence of one or more roads that allows you to travel between them.


The years of the road construction period were numbered from 0 to Y-1. For each year x:

  • During July of year x exactly one new road was constructed: a bidirectional road connecting the cities A[x] and B[x].
  • Then, during December of year x each inhabitant of the country sent a Christmas postcard to each other inhabitant. All postcards that could reach their destination were delivered. All postcards that could not be delivered were burned by the post office.

Count the total number of delivered postcards during these Y years. Return that count modulo 10^9 + 7.


In order to keep the input small, the arrays A and B are generated pseudorandomly. Please use the following pseudocode to generate them:


state = seed
for i = 0 to Y-1:
    state = (state * 1103515245 + 12345) modulo 2^31
    A[i] = state modulo N
    state = (state * 1103515245 + 12345) modulo 2^31
    B[i] = state modulo N

Definition

Class:
Postcards
Method:
count
Parameters:
int, int, int
Returns:
int
Method signature:
int count(int N, int Y, int seed)
(be sure your method is public)

Notes

  • It is possible that in some years the new road will connect two cities that already were (directly or indirectly) connected. It is also possible that in some years the new road will connect a city to itself. In both cases there will be no changes to reachability between cities.
  • The reference solution does not depend on the input being pseudorandom.

Constraints

  • N will be between 1 and 10^9, inclusive.
  • Y will be between 1 and 150,000, inclusive.
  • seed will be between 0 and 2^31 - 1, inclusive.

Examples

  1. 7

    1

    47

    Returns: 112

    There is only one year. In July the people build a road that connects city 5 to itself. In December the people send postcards. As no two distinct cities are reachable, only postcards within each city will be delivered.

  2. 7

    1

    42

    Returns: 122

    This time the only road built connects cities 4 and 0. Compared to the previous example, the postcards sent from city 0 to city 4 or vice versa will also get delivered. This gives us an extra 10 postcards.

  3. 7

    2

    100

    Returns: 348

    The first road connects cities 0 and 6. Once this road is built a total of 126 postcards will be delivered. The second road then connects cities 5 and 6. During the second Christmas season the number of delivered postcards will be 222. (Don't forget about postcards between cities 0 and 5. These can now be delivered too.)

  4. 987654321

    150000

    47

    Returns: 783886815

  5. 7654321

    150000

    4747

    Returns: 511254732

  6. 654321

    150000

    436434

    Returns: 538417691

  7. 7

    20

    100

    Returns: 11942

    Here is the full list of all 20 rows in the order in which they are built: 0-6, 6-5, 3-1, 3-3, 0-4, 6-3, 5-3, 1-6, 3-1, 3-2, 3-1, 4-5, 2-5, 6-4, 0-6, 3-6, 3-4, 1-4, 0-4, 1-1 In this test case Absurdistan eventually reaches the situation where in December all postcards get delivered: each of the 28 inhabitants of Absurdistan is able to reach each of the 27 other ones.

  8. 123

    123

    123

    Returns: 410499337

    The exact total is 1,410,499,344. Remember to compute and output it modulo 10^9 + 7.

  9. 947224005

    7

    1948501517

    Returns: 865581717

  10. 976610214

    1551

    1660249968

    Returns: 926848441

  11. 952669103

    8

    1133040875

    Returns: 858582308

  12. 964632919

    188

    1542245923

    Returns: 587123589

  13. 959353897

    136

    1607036031

    Returns: 343144852

  14. 996198169

    137

    539016619

    Returns: 348131818

  15. 944819066

    13

    1647457924

    Returns: 709985664

  16. 968223645

    11

    1438208187

    Returns: 854785967

  17. 929333809

    39986

    1789542187

    Returns: 164668883

  18. 981155443

    3220

    1026423979

    Returns: 759000908

  19. 974526545

    2212

    2078532398

    Returns: 16847256

  20. 941903666

    149219

    1698564564

    Returns: 316586478

  21. 934929325

    148509

    948917258

    Returns: 497258896

  22. 981382227

    141866

    889428824

    Returns: 861026592

  23. 950984921

    147952

    1087222470

    Returns: 805177465

  24. 981003095

    149142

    640818900

    Returns: 851108045

  25. 959577419

    140248

    731414135

    Returns: 865337971

  26. 973416107

    141356

    320425005

    Returns: 60993625

  27. 935668351

    148376

    2046595582

    Returns: 682244549

  28. 958466976

    145989

    142514072

    Returns: 482264518

  29. 802174

    149971

    2026408624

    Returns: 215086031

  30. 875092130

    140364

    1043761029

    Returns: 471061315

  31. 108

    147071

    790342737

    Returns: 795274447

  32. 62

    143255

    549224465

    Returns: 909677511

  33. 10

    149332

    1477705088

    Returns: 443491998

  34. 116963

    141770

    1146177193

    Returns: 473707604

  35. 16398

    142005

    1778417307

    Returns: 843323859

  36. 6

    140339

    1134935596

    Returns: 58940974

  37. 2

    142968

    2108757054

    Returns: 857808

  38. 127

    148655

    1948158689

    Returns: 566423623

  39. 267380

    141209

    752144434

    Returns: 737361648

  40. 87317652

    148903

    1453170456

    Returns: 424426821

  41. 1146483

    143842

    263002937

    Returns: 422611363

  42. 5104

    140363

    380485713

    Returns: 935159829

  43. 567

    148976

    1321413367

    Returns: 191663764

  44. 166

    149010

    1173215853

    Returns: 616048819

  45. 608911

    149223

    2109919775

    Returns: 690161480

  46. 1130

    141877

    1775873863

    Returns: 527566979

  47. 13987

    144961

    2101334812

    Returns: 216324509

  48. 670723

    141040

    502796335

    Returns: 89879988

  49. 516075037

    9276

    1235357188

    Returns: 303995313

  50. 66451348

    80495

    1052781061

    Returns: 309814023

  51. 3404

    61

    97040700

    Returns: 225600196

  52. 313621

    25453

    102151484

    Returns: 75047007

  53. 180692

    3

    2073107849

    Returns: 538822669

  54. 4656

    11548

    392755518

    Returns: 495151980

  55. 12

    6821

    1546714289

    Returns: 20682374

  56. 163423242

    2253

    1502298833

    Returns: 698830183

  57. 291753

    363

    1244362723

    Returns: 842656838

  58. 123

    30816

    1363446327

    Returns: 985693442

  59. 148920230

    31

    2642986

    Returns: 800348459

  60. 1

    122929

    1106908031

    Returns: 0

  61. 616

    638

    729061624

    Returns: 643362583

  62. 13707

    60686

    1007203776

    Returns: 812302788

  63. 100021

    1071

    1629169908

    Returns: 16798867

  64. 258188223

    43

    334718070

    Returns: 240925081

  65. 59704

    2

    1983085133

    Returns: 127706839

  66. 57

    1

    312614420

    Returns: 64728

  67. 233075

    1

    2144595767

    Returns: 19070669

  68. 16024318

    118028

    438548027

    Returns: 926143209

  69. 7

    9900

    750893228

    Returns: 7479988

  70. 536100867

    61554

    402545275

    Returns: 374096721

  71. 6778

    11413

    1358865986

    Returns: 395334561

  72. 4707

    31

    1430484067

    Returns: 440688081

  73. 1268447

    504

    398184188

    Returns: 449197303

  74. 2169

    4762

    963098471

    Returns: 917793232

  75. 93163

    99310

    1316725677

    Returns: 676144151

  76. 80443

    693

    2097118814

    Returns: 143792021

  77. 6218571

    7979

    30401546

    Returns: 811931024

  78. 471639

    1155

    674631869

    Returns: 895319746

  79. 317660

    174

    343476289

    Returns: 320291987

  80. 21

    4

    540951194

    Returns: 16202

  81. 468600

    14

    1266477948

    Returns: 686612946

  82. 188624

    8

    866820893

    Returns: 188423247

  83. 42645783

    1210

    1371506773

    Returns: 117109846

  84. 31170965

    111693

    1827507600

    Returns: 492115135

  85. 1969481

    14960

    2107368689

    Returns: 493196956

  86. 1

    1124

    1840506704

    Returns: 0

  87. 322719420

    20448

    413576938

    Returns: 534501325

  88. 224053682

    9158

    1433250621

    Returns: 352070647

  89. 46135623

    63896

    513342065

    Returns: 956011832

  90. 783229648

    67

    2119888712

    Returns: 971879924

  91. 1314

    4

    1282909328

    Returns: 29707783

  92. 2

    432

    825599650

    Returns: 2592

  93. 187448

    3566

    573850115

    Returns: 365775322

  94. 121

    10007

    1815175106

    Returns: 529411896

  95. 123253758

    4086

    1103050310

    Returns: 504010932

  96. 3

    197

    1741737939

    Returns: 5856

  97. 2

    5658

    1792312856

    Returns: 33948

  98. 2411

    3436

    1894985334

    Returns: 681553879

  99. 152412062

    50253

    276409821

    Returns: 892857497

  100. 45

    236

    2029977897

    Returns: 196582512

  101. 46805254

    518

    490428354

    Returns: 547536935

  102. 536727

    365

    1600605750

    Returns: 285023807

  103. 46809

    1844

    164647511

    Returns: 146549771

  104. 3836615

    1729

    32134090

    Returns: 40728027

  105. 8

    561

    939197325

    Returns: 206040

  106. 67559935

    2

    1646888126

    Returns: 858854567

  107. 517563765

    75

    1190803699

    Returns: 811853941

  108. 3755704

    111131

    162392144

    Returns: 758376053

  109. 1000000000

    150000

    1

    Returns: 240530864

  110. 1000000000

    150000

    999999999

    Returns: 629964670

  111. 1000000000

    150000

    20

    Returns: 512516146

  112. 1000000000

    1

    234

    Returns: 472949513

  113. 1000000000

    150000

    100

    Returns: 303967585

  114. 1000000000

    100000

    111

    Returns: 287371035

  115. 1000000000

    150000

    12121212

    Returns: 169730998

  116. 1000000000

    150000

    12345

    Returns: 461330490


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: