PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000255. Những đoạn có tổng nguyên tố (Prime Segments)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Những đoạn có tổng nguyên tố (Prime Segments)

Prime Segments

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n. A contiguous subarray is determined by two positions l,rl,r with 1≤l≤r≤n1\le l\le r\le n.

A subarray is a prime segment if the sum of its elements is a prime number (an integer greater than 11 with exactly two positive divisors, 11 and itself).

Count all index pairs (l,r)(l,r) such that ∑i=lrai\sum_{i=l}^{r}a_i is prime. Different index pairs are distinct even if their sums are the same.

Input

The first line contains the length nn.

The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in sequence order.

Output

Print a single 64-bit integer: the number of contiguous subarrays having a prime sum. No modulo is applied.

Subtasks

  • Subtask 1 (20 points): 1≤n≤1001\le n\le 100; 1≤ai≤1061\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; ∑i=1nai≤100\sum_{i=1}^n a_i\le 100.
  • Subtask 2 (30 points): 1≤n≤100001\le n\le 10000; 1≤ai≤1061\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; ∑i=1nai≤10000\sum_{i=1}^n a_i\le 10000.
  • Subtask 3 (50 points): 1≤n≤1061\le n\le 10^6; 1≤ai≤1061\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; ∑i=1nai≤106\sum_{i=1}^n a_i\le 10^6.

Examples

Input

4
1 2 3 4

Output

5

Explanation

For the sequence [1,2,3,4][1,2,3,4], exactly five subarrays qualify:

  • (1,2)(1,2): 1+2=31+2=3.
  • (2,2)(2,2): 22.
  • (2,3)(2,3): 2+3=52+3=5.
  • (3,3)(3,3): 33.
  • (3,4)(3,4): 3+4=73+4=7.

All other subarrays have nonprime sums. Therefore the answer is 55.