#P13027. [GCJ 2021 #1A] Prime Time

    ID: 12816 Type: RemoteJudge 45000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>数学2021数论Google Code Jam

[GCJ 2021 #1A] Prime Time

题目描述

You are playing a new solitaire game called Prime Time. You are given a deck of cards, and each card has a prime number written on it. Multiple cards may have the same number.

Your goal is to divide the cards into two groups in such a way that the sum of the numbers in the first group is equal to the product of the numbers in the second group. Each card must belong to exactly one of the two groups, and each group must contain at least one card. The sum or product of a group that consists of a single card is simply the number on that card.

For example, in the image above, the left group has cards whose sum is 2525 and the right group has cards whose product is 2525. Therefore, this is a valid split into groups.

Your score is the sum of the numbers in the first group (which is equal to the product of the numbers in the second group), or 00 if you cannot split the cards this way at all. What is the maximum score you can achieve?

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. The first line of each test case contains a single integer M\mathbf{M}, representing the number of distinct prime numbers in your deck. Each of the next M\mathbf{M} lines contains two values: Pi\mathbf{P}_i and Ni\mathbf{N}_i, representing that you have exactly Ni\mathbf{N}_i cards with the prime Pi\mathbf{P}_i written on them.

Note that the total number of cards in your deck is the sum of all Ni\mathbf{N}_is.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the maximum score you can achieve.

4
5
2 2
3 1
5 2
7 1
11 1
1
17 2
2
2 2
3 1
1
2 7
Case #1: 25
Case #2: 17
Case #3: 0
Case #4: 8

提示

Sample Explanation

In Sample Case #1, the optimal split is: 11+2+7+3+2=5511 + 2 + 7 + 3 + 2 = 5 \cdot 5. Another split is also possible: 5+7+3+2+5=1125 + 7 + 3 + 2 + 5 = 11 \cdot 2, but it gives a lower score.

In Sample Case #2, note that cards with the same number can be placed in different groups.

Limits

  • 1T1001 \leq \mathbf{T} \leq 100.
  • 1M951 \leq \mathbf{M} \leq 95. (Note that there are exactly 95 distinct primes between 2 and 499)
  • 2Pi4992 \leq \mathbf{P}_i \leq 499, for all ii.
  • Each Pi\mathbf{P}_i is prime.
  • Pi<Pi+1\mathbf{P}_i < \mathbf{P}_{i+1}, for all ii. (The primes are given in strictly increasing order)
  • 1Ni1 \leq \mathbf{N}_i, for all ii.

Test Set 1 (7 Pts, Visible Verdict)

  • $2 \leq \mathbf{N}_1 + \mathbf{N}_2 + \cdots + \mathbf{N}_\mathbf{M} \leq 10$.

Test Set 2 (13 Pts, Visible Verdict)

  • $2 \leq \mathbf{N}_1 + \mathbf{N}_2 + \cdots + \mathbf{N}_\mathbf{M} \leq 100$.

Test Set 3 (15 Pts, Hidden Verdict)

  • $2 \leq \mathbf{N}_1 + \mathbf{N}_2 + \cdots + \mathbf{N}_\mathbf{M} \leq 10^{15}$.