問題一覧 > 通常問題

No.3502 GCD Knapsack

レベル : / AI推定難易度 : 950 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 1024 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 100
作問者 : Naru820 / テスター : Nzt3 irinoirino Solalyth
お気に入りにしたユーザー ProblemId : 13218 / 自分の提出
問題文最終更新日: 2026-04-11 20:01:28
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
CPCTF 2026: PPC (順位表) の他の問題:

Problem Statement

There are $N$ items. The $i$-th item has weight $X_i$ and value $Y_i$.

Determine whether it is possible to choose $1$ or more items so that the greatest common divisor of the weights of the chosen items is at least $W$. If it is possible, find the maximum possible sum of the values of the chosen items.

Constraints

  • $1 \leq N,W \leq 2 \times 10^5$
  • $1 \le X_i \le 2\times 10^5$
  • $1 \le Y_i \le 10^9$
  • All input values are integers.

Input

The input is given from Standard Input in the following format.

$N \ W$
$X_1 \ X_2 \ldots X_N$ 
$Y_1 \ Y_2 \ldots Y_N$

Output

If it is not possible to choose items, print $0$. Otherwise, print the maximum possible sum of the values of the items.

Sample

Sample 1
Input
3 2
4 2 3
5 2 8
Output
8

The ways to choose items such that the greatest common divisor of the weights is at least $2$ are as follows.

  • Choose item $1$. The total value is $5$.
  • Choose item $2$. The total value is $2$.
  • Choose item $3$. The total value is $8$.
  • Choose items $1,2$. The total value is $7$.

Therefore, the maximum total value is $8$, so the answer is $8$.

Sample 2
Input
5 10
4 6 2 3 9
5 8 2 4 6
Output
0

It is not possible to choose items so that the greatest common divisor of the weights is at least $10$. Therefore, print $0$.

Sample 3
Input
20 26
6736 1887 1758 918 1150 1210 1975 958 763 1784 224 25 1794 15 963 1314 615 1526 7301 680
752 18717 225699 70 6427 314 61610 587 343 10924 1250 290 64100 5315 8566 84972 4135 2946 11 40640
Output
225699

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。