#P16746. [GKS 2019 #H] Elevanagram

    ID: 19078 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP数学2019数论Google Kick Start

[GKS 2019 #H] Elevanagram

Problem Description

It is a well known fact that a number is divisible by 1111 if and only if the alternating sum of its digits is equal to 00 modulo 1111. For example, 81749588174958 is a multiple of 1111, since 81+74+95+8=228 - 1 + 7 - 4 + 9 - 5 + 8 = 22.

Given a number that consists of digits from 11-99, can you rearrange the digits to create a number that is divisible by 1111?

Since the number might be quite large, you are given integers A1,A2,...,A9A_1, A_2, ..., A_9. There are AiA_i digits i in the number, for all i.

Input Format

The first line of the input gives the number of test cases, TT. TT lines follow. Each line contains the nine integers A1,A2,...,A9A_1, A_2, ..., A_9.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 11) and y is YES if the digits can be rearranged to create a multiple of 1111, and NO otherwise.

6
0 0 2 0 0 1 0 0 0
0 0 0 0 0 0 0 0 12
0 0 0 0 2 0 1 1 0
3 1 1 1 0 0 0 0 0
3 0 0 0 0 0 3 0 2
0 0 0 0 0 0 0 1 0
Case #1: YES
Case #2: YES
Case #3: NO
Case #4: YES
Case #5: YES
Case #6: NO

Hint

  • In Sample Case #1, the digits are 336336, which can be rearranged to 363363. This is a multiple of 1111 since 36+3=03 - 6 + 3 = 0.
  • In Sample Case #2, the digits are 999999999999999999999999, which is already a multiple of 1111, since 99+99+...9=09 - 9 + 9 - 9 + ... - 9 = 0.
  • In Sample Case #3, the digits are 55785578, which cannot be rearranged to form a multiple of 1111.
  • In Sample Case #4, the digits are 111234111234, which can be rearranged to 142131142131. This is a multiple of 1111 since 14+21+31=01 - 4 + 2 - 1 + 3 - 1 = 0.
  • In Sample Case #5, the digits are 1117779911177799, which can be rearranged to 1919177719191777. This is a multiple of 1111 since 19+19+17+77=221 - 9 + 1 - 9 + 1 - 7 + 7 - 7 = -22 (which is 00 modulo 1111).
  • In Sample Case #6, the only digit is 88, which cannot be rearranged to form a multiple of 1111.

Limits

1T1001 \le T \le 100.

1A1+A2+...+A91 \le A_1 + A_2 + ... + A_9.

Test set 1 (Visible)

0Ai200 \le A_i \le 20, for all i.

Test set 2 (Hidden)

0Ai1090 \le A_i \le 10^9, for all i.