QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#505158#1647. Divisible by 3pipizhuWA 89ms57460kbJava111.4kb2024-08-04 20:52:422024-08-04 20:52:47

Judging History

你现在查看的是最新测评结果

  • [2024-08-04 20:52:47]
  • 评测
  • 测评结果:WA
  • 用时:89ms
  • 内存:57460kb
  • [2024-08-04 20:52:42]
  • 提交

answer

import java.util.Scanner;

public class DivisibleByThree {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }

        // Initialize prefix sums and dp array
        int prefixSum = 0;
        int prefixSquareSum = 0;
        int[][] dp = new int[3][3];
        dp[0][0] = 1;  // Base case: empty subarray

        long count = 0;

        for (int num : array) {
            // Update prefix sums
            prefixSum = (prefixSum + num) % 3;
            if (prefixSum < 0) {
                prefixSum += 3;
            }
            prefixSquareSum = (prefixSquareSum + num * num) % 3;
            if (prefixSquareSum < 0) {
                prefixSquareSum += 3;
            }

            // Check all (x, y) pairs in dp array
            for (int x = 0; x < 3; x++) {
                for (int y = 0; y < 3; y++) {
                    if ((prefixSum - x + 3) % 3 * (prefixSum - x + 3) % 3 == (prefixSquareSum - y + 3) % 3) {
                        count += dp[x][y];
                    }
                }
            }

            // Update dp array
            dp[prefixSum][prefixSquareSum]++;
        }

        System.out.println(count);
    }
}

详细

Test #1:

score: 100
Accepted
time: 81ms
memory: 53540kb

input:

3
5 23 2021

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 83ms
memory: 57460kb

input:

5
0 0 1 3 3

output:

15

result:

ok single line: '15'

Test #3:

score: 0
Accepted
time: 85ms
memory: 54012kb

input:

10
0 1 2 3 4 5 6 7 8 9

output:

20

result:

ok single line: '20'

Test #4:

score: 0
Accepted
time: 85ms
memory: 55244kb

input:

1
901418150

output:

1

result:

ok single line: '1'

Test #5:

score: -100
Wrong Answer
time: 89ms
memory: 53436kb

input:

2
373642622 952441803

output:

1

result:

wrong answer 1st lines differ - expected: '3', found: '1'