exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
sequencelengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | baa558d0a295c1d68e26bb4e607b687a | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n, e;
static boolean[] isPrime;
static int maxx = (int)1e6;
static TreeSet<Integer>[] tset;
static int[] a;
static long res;
public static void main(String[] args) throws IOException {
isPrime = new boolean[maxx+5]; Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= maxx; i++) {
if (isPrime[i]) {
for (long j = (long)i * i; j <= maxx; j += i) {
isPrime[(int)j] = false;
}
}
}
t = in.iscan();
while (t-- > 0) {
n = in.iscan(); e = in.iscan();
tset = new TreeSet[n+1];
for (int i = 0; i <= n; i++) {
tset[i] = new TreeSet<Integer>();
}
a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.iscan();
if (a[i] != 1) {
tset[i%e].add(i);
}
if (i + e >= n) {
tset[i%e].add(i+e);
}
}
res = 0;
for (int i = 0; i < n; i++) {
if (isPrime[a[i]]) {
int ll = i, rr = tset[i%e].higher(i) - e;
res += (rr - ll + 1) / e;
if (e == 1) res--;
}
else if (a[i] == 1) {
int nxt = tset[i%e].higher(i);
if (nxt < n) {
if (isPrime[a[nxt]]) {
int ll = nxt, rr = tset[i%e].higher(nxt) - e;
res += (rr - ll + 1) / e + 1;
if (e == 1) res--;
}
}
}
}
out.println(res);
}
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | c0ea9ae7b10be48f846104a19b87428d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.lang.Exception;
import java.util.ArrayList;
public class Main{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null||!st.hasMoreElements()){
try{
st=new StringTokenizer(br.readLine());
}catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){return Integer.parseInt(next());}
long nextLong(){return Long.parseLong(next());}
double nextDouble(){return Double.parseDouble(next());}
String nextLine(){
String str="";
try{
if(st.hasMoreTokens()) str=st.nextToken("\n");
else str=br.readLine();
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
static class Pair{
int first;
boolean second;
public Pair(int first,boolean second){
this.first=first;
this.second=second;
}
}
static FastReader sc=new FastReader();
static int nMax=1000000;
static boolean[] notPrime=new boolean[nMax+1];
static void prec(){
notPrime[0]=notPrime[1]=true;
for(int i=2;i*i<=nMax;i++){
if(!notPrime[i]){
for(int j=i*i;j<=nMax;j+=i){
notPrime[j]=true;
}
}
}
}
static void solve(){
int n=sc.nextInt();
int e=sc.nextInt();
Pair[] a=new Pair[n];
for(int i=0;i<n;i++){
int x=sc.nextInt();
a[i]=new Pair(x,false);
}
long sum=0;
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0;i<n;i++){
int mem=1;
int cnt=0;
int j=i;
if(!a[j].second&&(!notPrime[a[j].first]||a[j].first==1)){
while(j<n){
a[j].second=true;
if(!notPrime[a[j].first]){
list.add(cnt);
cnt=0;
}else if(a[j].first==1){
cnt++;
}else{
list.add(cnt);
mem=0;
break;
}
j+=e;
}
if(mem==1) list.add(cnt);
}else continue;
int sz=list.size();
for(int k=0;k<sz-1;k++){
sum+=(long)list.get(k)*(list.get(k+1)+1)+list.get(k+1);
}
list.clear();
}
System.out.println(sum);
}
public static void main(String args[])throws java.lang.Exception{
prec();
int t=sc.nextInt();
while(t-->0){
solve();
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | ae7406eea9af7639f176799b80f29135 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.Queue;
import java.util.LinkedList;
import java.util.*;
import java.lang.*;
// import java.lang.reflect.Array;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=998244353;
static StringBuilder sb = new StringBuilder();
/* start */
public static void main(String [] args)
{
// int testcases = 1;
int testcases = i();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i();
int e = i();
int a[] = input(n);
long ans = 0;
for(int i=0;i<n;i++)
{
if(prime(a[i]))
{
long left = 1,right = 1;
int l = i-e,r = i+e;
while(l>=0 && a[l]==1)
{
left++;
l-=e;
}
while(r<n && a[r]==1)
{
right++;
r+=e;
}
ans+=(left*right)-1;
}
}
pl(ans);
}
/* end */
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static void p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void print(int A[]) {
for(int i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) ;
y = y >> 1;
x = (x * x);
}
return res;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
int first, second;
public Pair(int f, int s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first > p.first)
return 1;
else if (first < p.first)
return -1;
else {
if (second < p.second)
return 1;
else if (second > p.second)
return -1;
else
return 0;
}
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | ef2bcf40bd07f2c7c2c641488b4601a1 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
//Complex Market Analysis
public class ComplexMarketAnalysis {
public static void main(String[] args) {
ComplexMarketAnalysis.solution();
}
public static void solution() {
Scanner scanner = new Scanner(System.in);
int numCases = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numCases; i++) {
String[] tokens = scanner.nextLine().split(" ");
int e = Integer.parseInt(tokens[1]);
int[] arr = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int[] pre = new int[arr.length];
long count = 0;
for (int j = 0; j < arr.length; j++) {
pre[j] = arr[j] == 1 ? 1 : 0;
if (j - e >= 0 && arr[j] == 1) {
pre[j] += pre[j - e];
}
}
int[] suf = new int[arr.length];
for (int j = arr.length - 1; j >= 0; j--) {
suf[j] = arr[j] == 1 ? 1 : 0;
if (j + e < arr.length && arr[j] == 1) {
suf[j] += suf[j + e];
}
}
for (int j = 0; j < arr.length; j++) {
if (isPrime(arr[j])) {
long left = j - e >= 0 ? pre[j - e] : 0;
long right = j + e < arr.length ? suf[j + e] : 0;
count = count + left + right + (left * right);
}
}
System.out.println(count);
}
}
private static boolean isPrime(int num) {
int sqrt = (int) Math.sqrt(num);
for (int i = 2; i <= sqrt; i++) {
if (num % i == 0) {
return false;
}
}
return num > 1;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 3c505c76228d7d629bea53144b7cf2cb | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.*;
import java.util.*;
public class J {
static ArrayList<Boolean> isPrime;
static ArrayList<Integer> primes;
static ArrayList<Integer> spf;
static void preComputePrimes(int n){
isPrime = new ArrayList<>(n);
primes = new ArrayList<>();
spf = new ArrayList<>(n);
for(int i=0;i<n;i++){
isPrime.add(true);
spf.add(2);
}
isPrime.set(0, false);
isPrime.set(1, false);
for(int i=2;i<n;i++){
if(isPrime.get(i)){
primes.add(i);
spf.set(i, i);
}
for(int j=0;j<primes.size() && primes.get(j)<=spf.get(i) && primes.get(j)*i<n;j++){
isPrime.set(i*primes.get(j), false);
spf.set(i*primes.get(j), primes.get(j));
}
}
}
static long countPairs(List<Integer> list){
int l = -1;
long count = 0;
long product = 1;
for(int i=0;i<list.size();i++){
if(!isPrime.get(list.get(i)) && list.get(i) != 1){
product = 1;
l = -1;
continue;
}
if(l == -1){
l = i;
product *= list.get(i);
continue;
}
if(list.get(i) == 1){
count += (i - l);
}
else{
if(product == 1){
product *= list.get(i);
count += (i - l);
}
else{
while(product != 1){
product /= list.get(l);
l++;
}
product *= list.get(i);
count += (i - l);
}
}
}
l = 0;
for(int i=0;i<list.size();i++){
if(list.get(i) == 1){
count -= (i - l);
}
else{
l = i + 1;
}
}
return count;
}
public static void main(String[] args) throws IOException {
Soumit sc = new Soumit();
preComputePrimes(1000100);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (t-->0){
int n = sc.nextInt();
int e = sc.nextInt();
int[] arr = sc.nextIntArray(n);
long count = 0;
for(int j=0;j<e;j++){
List<Integer> list = new ArrayList<>();
for(int i=j;i<n;i+=e){
list.add(arr[i]);
}
count = count + countPairs(list);
}
sb.append(count).append("\n");
}
System.out.println(sb);
sc.close();
}
static class Soumit {
final private int BUFFER_SIZE = 1 << 18;
final private DataInputStream din;
final private byte[] buffer;
private PrintWriter pw;
private int bufferPointer, bytesRead;
StringTokenizer st;
public Soumit() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Soumit(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public void streamOutput(String file) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
}
public void println(String a) {
pw.println(a);
}
public void print(String a) {
pw.print(a);
}
public String readLine() throws IOException {
byte[] buf = new byte[3000064]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public void sort(int[] arr) {
ArrayList<Integer> arlist = new ArrayList<>();
for (int i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public void sort(long[] arr) {
ArrayList<Long> arlist = new ArrayList<>();
for (long i : arr)
arlist.add(i);
Collections.sort(arlist);
for (int i = 0; i < arr.length; i++)
arr[i] = arlist.get(i);
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
/*if (din == null)
return;*/
if (din != null) din.close();
if (pw != null) pw.close();
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 3c2fe32c330aca4ef8984b5858b6047d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | // ceil using integer division: ceil(x/y) = (x+y-1)/y
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import java.io.*;
public class practice {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int t = Reader.nextInt();
boolean[] prime = new boolean[(int)1e6 + 1];
for (int i = 0; i <= (int)1e6; i++)
prime[i] = true;
for (int p = 2; p * p <= (int)1e6; p++) {
if (prime[p]){
for (int i = p * p; i <= (int)1e6; i += p) {
prime[i] = false;
}
}
}
while(t-->0){
int n = Reader.nextInt();
int e = Reader.nextInt();
long[] arr = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Reader.nextLong();
}
long[][] dp = new long[n][2];
long ans = 0;
for(int i = n-1;i>=0;i--){
if(arr[i]==1){
if(i+e<n){
if(arr[i+e]==1){
dp[i][1] = 1 + dp[i+e][1];
dp[i][0] = dp[i+e][0];
}
else if(prime[(int) arr[i+e]]){
dp[i][1] = 1;
dp[i][0] = 1 + dp[i+e][0];
}
else{
dp[i][1] = 1;
}
}
else{
dp[i][1] = 1;
}
}
else if(prime[(int)arr[i]]){
if(i+e<n && arr[i+e]==1){
dp[i][0] = dp[i+e][1];
}
}
ans += dp[i][0];
}
// for(int i = 0;i<n;i++){
// System.out.println(dp[i][0] +" "+ dp[i][1]);
// }
System.out.println(ans);
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException{
return Long.parseLong(next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | ab07f73985f22263354cb8510e7da4aa | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1609C extends PrintWriter {
CF1609C() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1609C o = new CF1609C(); o.main(); o.flush();
}
static final int A = 1000000;
boolean[] composite = new boolean[A + 1]; {
for (int a = 2; a <= A; a++) {
if (composite[a])
continue;
for (int b = a + a; b <= A; b += a)
composite[b] = true;
}
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int e = sc.nextInt();
int[] aa = new int[n];
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
if (a == 1)
aa[i] = 1;
else if (!composite[a])
aa[i] = 2;
}
int[] pp = new int[n];
int[] qq = new int[n];
for (int r = 0; r < e; r++)
for (int i = r, j; i < n; i = j) {
j = i + e;
if (aa[i] != 1)
continue;
while (j < n && aa[j] == 1)
j += e;
int k = (j - i) / e;
if (i >= e && aa[i - e] == 2)
qq[i - e] = k;
if (j < n && aa[j] == 2)
pp[j] = k;
}
long ans = 0;
for (int i = 0; i < n; i++)
ans += (long) (pp[i] + 1) * (qq[i] + 1) - 1;
println(ans);
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 207530e6b0f002bb33bff77a75c02188 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /*
Rating: 1367
Date: 08-01-2022
Time: 17-30-13
Author: Kartik Papney
Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/
Leetcode: https://leetcode.com/kartikpapney/
Codechef: https://www.codechef.com/users/kartikpapney
*/
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C_Complex_Market_Analysis {
static HashSet<Integer> set = new HashSet<>();
public static void s() {
int n = sc.nextInt(), e = sc.nextInt();
int[] arr = sc.readArray(n);
long ans = 0;
for (int i = 0; i < arr.length; i++) {
if (set.contains(arr[i])) {
long l = 0;
long r = 0;
for (int j = i + e; j < n; j += e) {
if (arr[j] == 1)
r++;
else
break;
}
for (int j = i - e; j >= 0; j -= e) {
if (arr[j] == 1)
l++;
else
break;
}
ans += l + r + l * r;
}
}
p.writeln(ans);
}
public static void main(String[] args) {
set.addAll(Functions.generatePrimes((int) 1e6));
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
static final Integer MOD = (int) 1e9 + 7;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static int max(int[] a) {
int max = Integer.MIN_VALUE;
for (int val : a)
max = Math.max(val, max);
return max;
}
static int min(int[] a) {
int min = Integer.MAX_VALUE;
for (int val : a)
min = Math.min(val, min);
return min;
}
static long min(long[] a) {
long min = Long.MAX_VALUE;
for (long val : a)
min = Math.min(val, min);
return min;
}
static long max(long[] a) {
long max = Long.MIN_VALUE;
for (long val : a)
max = Math.max(val, max);
return max;
}
static long sum(long[] a) {
long sum = 0;
for (long val : a)
sum += val;
return sum;
}
static int sum(int[] a) {
int sum = 0;
for (int val : a)
sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr)
if (val == x)
count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++)
primes[i] = true;
for (long i = 2; i < primes.length; i++) {
if (primes[(int) i]) {
for (long j = i * i; j < primes.length; j += i) {
primes[(int) j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i])
arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void writes(int[] arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long[] arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int[] arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 670727acba2e4f35fb2998394f5f1cd3 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int primes [] = new int[1000001];
primes[0] = primes[1] = 1;
int i = 2;
while ( i <= 1000000){
if ( primes[i] == 0){
int j = i+i;
while ( j <= 1000000){
primes[j] = 1;
j+=i;
}
}
i++;
}
int t = Reader.nextInt();
while ( t > 0 ){
int n = Reader.nextInt();
int e = Reader.nextInt();
int array [] = new int[n];
i = 0;
while ( i < n){
array[i] = Reader.nextInt();
i++;
}
long dp [] = new long[n];
long ans = 0;
i = n-1;
while ( i >= 0){
boolean b = i + e < n && array[i + e] == 1;
if ( primes[array[i]] == 0){
if (b){
dp[i] = dp[i+e]+1;
ans+=dp[i];
}
}
else if (b){
dp[i] = dp[i+e]+1;
}
i--;
}
i = 0;
while ( i< n){
if ( array[i] == 1){
dp[i] = 0;
}
i++;
}
i = n-1;
while ( i >= 0){
if ( array[i] == 1){
boolean b = (i+e < n);
if ( b){
if ( primes[array[i+e]] == 0){
dp[i] = dp[i+e]+1;
ans+=dp[i];
}
else if ( array[i+e] == 1){
dp[i] = dp[i+e];
ans+=dp[i];
}
}
}
i--;
}output.write(ans+"\n");
t--;
}
output.flush();
}
public static long sub(long sum,long [] array,long k,long all){
boolean a = false;
long copySum = sum;
long allowed = all;
long first = array[0] - allowed;
copySum -= array[0];
copySum += first;
int last = array.length - 1;
for (int i = 1; i <= all && i <= array.length-1 ; i++){
if ( copySum <= k){
a = true;
break;
}
else{
copySum++;
copySum+=(i-1);
first++;
copySum-=array[last];
copySum+=first;
last--;
if ( copySum <= k){
a = true;
break;
}
}
}
if ( copySum <= k){
a = true;
}
if ( a == true){
return 1;
}
else {
return -1;
}
}
private static long newbs(long low,long high,long k,long n,long ans,long f,long sum1,long[] array){
if ( low <= high ){
long mid = low + (high-low)/2;
System.out.println("M "+mid);
long value = f-mid;
long sum = sum1;
sum-=f;
sum+=mid;
int i =(int) n-1;
while( i != -1 && sum > k ){
sum+=array[0];
sum-=array[i];
value++;
i--;
}
if ( i == -1){
value--;
}
if ( value <= ans){
low = mid+1;
return newbs(low,high,k,n,value,f,sum1,array);
}
else{
high = mid-1;
return newbs(low,high,k,n,ans,f,sum1,array);
}
}
return ans;
}
private static int bs(int low,int high,ArrayList<Integer> array,int find){
if ( low <= high ){
int mid = low + (high-low)/2;
if ( array.get(mid) > find){
high = mid -1;
return bs(low,high,array,find);
}
else if ( array.get(mid) < find){
low = mid+1;
return bs(low,high,array,find);
}
return mid;
}
return -1;
}
private static long max(long a, long b) {
return Math.max(a,b);
}
private static long min(long a,long b){
return Math.min(a,b);
}
public static long modularExponentiation(long a,long b,long mod){
if ( b == 1){
return a;
}
else{
long ans = modularExponentiation(a,b/2,mod)%mod;
if ( b%2 == 1){
return (a*((ans*ans)%mod))%mod;
}
return ((ans*ans)%mod);
}
}
public static long sum(long n){
return (n*(n+1))/2;
}
public static long abs(long a){
return a < 0 ? (-1*a) : a;
}
public static long gcd(long a,long b){
if ( a == 0){
return b;
}
else{
return gcd(b%a,a);
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static long nextLong() throws IOException{
return Long.parseLong(next());
}
}
/*
class NComparator implements Comparator<Node>{
@Override
public int compare(Node o1, Node o2) {
if ( o2.position > o1.position){
return 1;
}
else if ( o2.position < o1.position){
return -1;
}
else{
if ( o2.time < o1.time){
return 1;
}
else if ( o2.time > o2.time){
return -1;
}
else{
return 0;
}
}
}
}
*/
class DComparator implements Comparator<Long>{
@Override
public int compare(Long o1, Long o2) {
if ( o2 > o1){
return 1;
}
else if ( o2 < o1){
return -1;
}
else{
return 0;
}
}
}
class Node{
int v;
int m;
Node(int V){
v =V;
m =0;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | dbe03bc7a719560c2a277fcfa3178f00 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int m = 1000_000;
boolean prime[] = new boolean[m + 1];
for (int i = 0; i <= m; i++)
prime[i] = true;
for (int p = 2; p * p <= m; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= m; i += p)
prime[i] = false;
}
}
int inc = 0;
while (t-- > 0) {
inc++;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int arr[] = new int[n];
int onec[] = new int[n];
for (int i = 0; i < n; i++) {
int e2 = Integer.parseInt(st.nextToken());
arr[i] = e2;
if (e2 == 1)
onec[i]++;
if (i >= e) {
if (arr[i - e] == 1) {
onec[i] += onec[i - e];
}
}
}
// if(inc == 119)
// output.write(Arrays.toString(arr) + " " + e + "\n");
// output.write(Arrays.toString(onec) + "\n");
long res = 0;
for (int i = n - 1; i >= n - e; i--) {
int tempc = 0;
for (int j = i; j >= 0; j -= e) {
if (arr[j] == 1) {
tempc++;
continue;
}
else {
if (!prime[arr[j]])
{
tempc = 0;
continue;
}
// output.write("for i = " + i + " j = " + j + " temp = " + tempc + " onec[j] = " + onec[j] + " res before = " + res + " \n");
res += tempc;
res += onec[j];
if (onec[j] > 0 && tempc > 0)
res += ((long)tempc * (long)onec[j]);
tempc = 0;
// output.write("for i = " + i + " j = " + j + " temp = " + tempc + " onec[j] = " + onec[j] + " res before = " + res + " \n");
}
}
}
output.write(res + "\n");
// int k = Integer.parseInt(st.nextToken());
// char arr[] = br.readLine().toCharArray();
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.flush();
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 7073cf257d606056c7af0090526f8d8b | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static boolean isPrime[];
static void sieve(){
isPrime[1]=false;
for(int i=2;i*i<=isPrime.length;i++){
if(isPrime[i]==false)continue;
for(int j=i+i;j<isPrime.length;j+=i){
isPrime[j]=false;
}
}
}
static void solve() {
int n=i();
int e=i();
int[]arr=new int [n+1];
for(int i=1;i<=n;i++)arr[i]=i();
long ans=0;
for(int i=1;i<=n;i++){
if(!isPrime[arr[i]])continue;
long pehle=0;
for(int j=i-e;j>=0;j-=e){
if(arr[j]==1)pehle++;
else break;
}
long aft=0;
for(int j=i+e;j<=n;j+=e){
if(arr[j]==1)aft++;
else break;
}
ans+=((pehle*(aft+1))+aft);
}
sb.append(ans+"\n");
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
isPrime=new boolean[(int)(1e6+2)];
Arrays.fill(isPrime,true);
sieve();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 7b88768d474b845708586f4f95a42d3c | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* Provide prove of correctness before implementation. Implementation can cost a
* lot of time.
* Anti test that prove that it's wrong.
* <p>
* Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write
* more informative names to simulation
* <p>
* Will program ever exceed limit?
* Try all approaches with prove of correctness if task is not obvious.
* If you are given formula/rule: Try to play with it.
* Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute
* force/Symmetric data
* Number theory
* Game theory (optimal play) that consider local and global strategy.
*/
public class TemplateFast {
int n = 1_000_000;
boolean[] prime = new boolean[n + 1];
private void precalculate() {
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i <= n; ++i)
if (prime[i])
if (i * 1L * i <= n)
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
private void solveOne() {
int n = nextInt();
int e = nextInt();
int[] a = nextIntArr(n);
int[] dp = new int[n];
int[] one = new int[n];
for(int i = 0 ; i < n; i++) {
if(a[i] == 1) {
one[i] = 1;
}
}
for (int i = e; i < n; i++) {
if (a[i] == 1 && a[i - e] == 1) {
one[i] = one[i - e] + 1;
}
if (prime[a[i]] && a[i - e] == 1) {
dp[i] = one[i - e];
}
if (prime[a[i]] && prime[a[i - e]]) {
dp[i] = 0;
}
if (a[i] == 1 && prime[a[i - e]]) {
dp[i] = dp[i - e] + 1;
}
if (a[i] == 1 && a[i - e] == 1) {
dp[i] = dp[i - e];
}
}
//System.out.println(Arrays.toString(dp));
long ans = 0;
for (int i = 0; i < n; i++) {
ans += dp[i];
}
System.out.println(ans);
}
private void solve() {
// System.out.println();
precalculate();
int t = System.in.readInt();
for (int tt = 0; tt < t; tt++) {
solveOne();
}
}
class AssertionRuntimeException extends RuntimeException {
AssertionRuntimeException(Object expected,
Object actual, Object... input) {
super("expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input));
}
}
private int nextInt() {
return System.in.readInt();
}
private long nextLong() {
return System.in.readLong();
}
private String nextString() {
return System.in.readString();
}
private int[] nextIntArr(int n) {
return System.in.readIntArray(n);
}
private long[] nextLongArr(int n) {
return System.in.readLongArray(n);
}
public static void main(String[] args) {
new TemplateFast().run();
}
static class System {
private static FastInputStream in;
private static FastPrintStream out;
}
private void run() {
System.in = new FastInputStream(java.lang.System.in);
System.out = new FastPrintStream(java.lang.System.out);
solve();
System.out.flush();
}
private static class FastPrintStream {
private static final int BUF_SIZE = 8192;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastPrintStream() {
this(java.lang.System.out);
}
public FastPrintStream(OutputStream os) {
this.out = os;
}
public FastPrintStream(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastPrintStream print(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE)
innerflush();
return this;
}
public FastPrintStream print(char c) {
return print((byte) c);
}
public FastPrintStream print(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
return this;
}
public FastPrintStream print(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
});
return this;
}
// can be optimized
public FastPrintStream print0(char[] s) {
if (ptr + s.length < BUF_SIZE) {
for (char c : s) {
buf[ptr++] = (byte) c;
}
} else {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE)
innerflush();
}
}
return this;
}
// can be optimized
public FastPrintStream print0(String s) {
if (ptr + s.length() < BUF_SIZE) {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
}
} else {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
if (ptr == BUF_SIZE)
innerflush();
}
}
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000)
return 10;
if (l >= 100000000)
return 9;
if (l >= 10000000)
return 8;
if (l >= 1000000)
return 7;
if (l >= 100000)
return 6;
if (l >= 10000)
return 5;
if (l >= 1000)
return 4;
if (l >= 100)
return 3;
if (l >= 10)
return 2;
return 1;
}
public FastPrintStream print(int x) {
if (x == Integer.MIN_VALUE) {
return print((long) x);
}
if (ptr + 12 >= BUF_SIZE)
innerflush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L)
return 19;
if (l >= 100000000000000000L)
return 18;
if (l >= 10000000000000000L)
return 17;
if (l >= 1000000000000000L)
return 16;
if (l >= 100000000000000L)
return 15;
if (l >= 10000000000000L)
return 14;
if (l >= 1000000000000L)
return 13;
if (l >= 100000000000L)
return 12;
if (l >= 10000000000L)
return 11;
if (l >= 1000000000L)
return 10;
if (l >= 100000000L)
return 9;
if (l >= 10000000L)
return 8;
if (l >= 1000000L)
return 7;
if (l >= 100000L)
return 6;
if (l >= 10000L)
return 5;
if (l >= 1000L)
return 4;
if (l >= 100L)
return 3;
if (l >= 10L)
return 2;
return 1;
}
public FastPrintStream print(long x) {
if (x == Long.MIN_VALUE) {
return print("" + x);
}
if (ptr + 21 >= BUF_SIZE)
innerflush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastPrintStream print(double x, int precision) {
if (x < 0) {
print('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
print((long) x).print(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
print((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastPrintStream println(char c) {
return print(c).println();
}
public FastPrintStream println(int x) {
return print(x).println();
}
public FastPrintStream println(long x) {
return print(x).println();
}
public FastPrintStream println(String x) {
return print(x).println();
}
public FastPrintStream println(double x, int precision) {
return print(x, precision).println();
}
public FastPrintStream println() {
return print((byte) '\n');
}
private void innerflush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerflush");
}
}
public void flush() {
innerflush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
}
private static class FastInputStream {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastInputStream(InputStream stream) {
this.stream = stream;
}
public double[] readDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = readDouble();
}
return array;
}
public String[] readStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = readString();
}
return array;
}
public char[] readCharArray(int size) {
char[] array = new char[size];
for (int i = 0; i < size; i++) {
array[i] = readCharacter();
}
return array;
}
public void readIntArrays(int[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readInt();
}
}
}
public void readLongArrays(long[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readLong();
}
}
}
public void readDoubleArrays(double[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readDouble();
}
}
}
public char[][] readTable(int rowCount, int columnCount) {
char[][] table = new char[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readCharArray(columnCount);
}
return table;
}
public int[][] readIntTable(int rowCount, int columnCount) {
int[][] table = new int[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readIntArray(columnCount);
}
return table;
}
public double[][] readDoubleTable(int rowCount, int columnCount) {
double[][] table = new double[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readDoubleArray(columnCount);
}
return table;
}
public long[][] readLongTable(int rowCount, int columnCount) {
long[][] table = new long[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readLongArray(columnCount);
}
return table;
}
public String[][] readStringTable(int rowCount, int columnCount) {
String[][] table = new String[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readStringArray(columnCount);
}
return table;
}
public String readText() {
StringBuilder result = new StringBuilder();
while (true) {
int character = read();
if (character == '\r') {
continue;
}
if (character == -1) {
break;
}
result.append((char) character);
}
return result.toString();
}
public void readStringArrays(String[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readString();
}
}
}
public long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = readLong();
}
return array;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int peekNonWhitespace() {
while (isWhitespace(peek())) {
read();
}
return peek();
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return readString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | fde3b00c1d5f262c489bc72d25900f60 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.HashMap;
import java.util.Arrays;
import java.util.Scanner;
public class C519{
public static boolean isPrime(int n){
if (n==1) return false;
for(int i=2;i*i<=n;i++){
if (n%i==0) return false;
}
return true;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- >0){
int n=sc.nextInt();
int e=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++) arr[i]=sc.nextInt();
boolean arePrimes[]=new boolean[n];
for(int i=0;i<n;i++) arePrimes[i]=isPrime(arr[i]);
long ans=0;
for(int i=0;i<n;i++){
if (arePrimes[i]){
int left=0;
int index=i-e;
while(index>=0 && arr[index]==1) {
index-=e;
left++;
}
index=i+e;
int right=0;
while(index<n && arr[index]==1) {
index+=e;
right++;
}
ans+=(long)(left+1)*(right+1)-1;
}
}
System.out.println(ans);
}
sc.close();
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 0e45dcea522771dee8a1253a38b8f576 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //some updates in import stuff
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
static int mod = (int) (Math.pow(10, 9)+7);
static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 };
static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
static final double eps = 1e-10;
static Set<Integer> primeNumbers = new HashSet<>();
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
primeSieve(100_00_00);
//code below
int test = sc.nextInt();
while(test -- > 0){
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for(int i= 0; i < n; i++){
arr[i] = sc.nextInt();
}
//divide it in k k types arrays if you want
//k loops wiill be there basically for now
long answer = 0;
for(int i= 0; i < k; i++){
long last = 0;
long one = -1;
boolean prime = false;
int num = 0;
for(int j= i; j < n; j += k){
if(arr[j] == 1){
//important fix here boss
if(prime)
answer += (last+1);
if(one == -1) one = num;
}else if(!primeNumbers.contains(arr[j])){
// out.println(2);
last = 0;
one = -1;
prime = false;
}else{
// out.println(3);
if(one == -1){
last = 0;
}else{
last = num-one;
one = -1;
answer += last;
}
prime = true;
}
num++;
// out.println(answer);
}
}
out.println(answer);
}
out.close();
}
//Updation Required
//Fenwick Tree (customisable)
//Segment Tree (customisable)
//-----CURRENTLY PRESENT-------//
//Graph
//DSU
//powerMODe
//power
//Segment Tree (work on this one)
//Prime Sieve
//Count Divisors
//Next Permutation
//Get NCR
//isVowel
//Sort (int)
//Sort (long)
//Binomial Coefficient
//Pair
//Triplet
//lcm (int & long)
//gcd (int & long)
//gcd (for binomial coefficient)
//swap (int & char)
//reverse
//Fast input and output
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//GRAPH (basic structure)
public static class Graph{
public int V;
public ArrayList<ArrayList<Integer>> edges;
//2 -> [0,1,2] (current)
Graph(int V){
this.V = V;
edges = new ArrayList<>(V+1);
for(int i= 0; i <= V; i++){
edges.add(new ArrayList<>());
}
}
public void addEdge(int from , int to){
edges.get(from).add(to);
}
}
//DSU (path and rank optimised)
public static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
Arrays.fill(rank, 1);
Arrays.fill(parent,-1);
this.n = n;
}
public int find(int curr){
if(parent[curr] == -1)
return curr;
//path compression optimisation
return parent[curr] = find(parent[curr]);
}
public void union(int a, int b){
int s1 = find(a);
int s2 = find(b);
if(s1 != s2){
if(rank[s1] < rank[s2]){
parent[s1] = s2;
rank[s2] += rank[s1];
}else{
parent[s2] = s1;
rank[s1] += rank[s2];
}
}
}
}
//with mod
public static long powerMOD(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
x %= mod;
res %= mod;
res = (res * x)%mod;
}
// y must be even now
y = y >> 1; // y = y/2
x%= mod;
x = (x * x)%mod; // Change x to x^2
}
return res%mod;
}
//without mod
public static long power(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
res = (res * x);
}
// y must be even now
y = y >> 1; // y = y/
x = (x * x);
}
return res;
}
public static class segmentTree{
public long[] arr;
public long[] tree;
public long[] lazy;
segmentTree(long[] array){
int n = array.length;
arr = new long[n];
for(int i= 0; i < n; i++) arr[i] = array[i];
tree = new long[4*n + 1];
lazy = new long[4*n + 1];
}
public void build(int[]arr, int s, int e, int[] tree, int index){
if(s == e){
tree[index] = arr[s];
return;
}
//otherwise divide in two parts and fill both sides simply
int mid = (s+e)/2;
build(arr, s, mid, tree, 2*index);
build(arr, mid+1, e, tree, 2*index+1);
//who will build the current position dude
tree[index] = Math.min(tree[2*index], tree[2*index+1]);
}
public int query(int sr, int er, int sc, int ec, int index, int[] tree){
if(lazy[index] != 0){
tree[index] += lazy[index];
if(sc != ec){
lazy[2*index+1] += lazy[index];
lazy[2*index] += lazy[index];
}
lazy[index] = 0;
}
//no overlap
if(sr > ec || sc > er) return Integer.MAX_VALUE;
//found the index baby
if(sr <= sc && ec <= er) return tree[index];
//finding the index on both sides hehehehhe
int mid = (sc + ec)/2;
int left = query(sr, er, sc, mid, 2*index, tree);
int right = query(sr, er, mid+1, ec, 2*index + 1, tree);
return Integer.min(left, right);
}
//now we will do point update implementation
//it should be simple then we expected for sure
public void update(int index, int indexr, int increment, int[] tree, int s, int e){
if(lazy[index] != 0){
tree[index] += lazy[index];
if(s != e){
lazy[2*index+1] = lazy[index];
lazy[2*index] = lazy[index];
}
lazy[index] = 0;
}
//no overlap
if(indexr < s || indexr > e) return;
//found the required index
if(s == e){
tree[index] += increment;
return;
}
//search for the index on both sides
int mid = (s+e)/2;
update(2*index, indexr, increment, tree, s, mid);
update(2*index+1, indexr, increment, tree, mid+1, e);
//now update the current range simply
tree[index] = Math.min(tree[2*index+1], tree[2*index]);
}
public void rangeUpdate(int[] tree , int index, int s, int e, int sr, int er, int increment){
//if not at all in the same range
if(e < sr || er < s) return;
//complete then also move forward
if(s == e){
tree[index] += increment;
return;
}
//otherwise move in both subparts
int mid = (s+e)/2;
rangeUpdate(tree, 2*index, s, mid, sr, er, increment);
rangeUpdate(tree, 2*index + 1, mid+1, e, sr, er, increment);
//update current range too na
//i always forget this step for some reasons hehehe, idiot
tree[index] = Math.min(tree[2*index], tree[2*index + 1]);
}
public void rangeUpdateLazy(int[] tree, int index, int s, int e, int sr, int er, int increment){
//update lazy values
//resolve lazy value before going down
if(lazy[index] != 0){
tree[index] += lazy[index];
if(s != e){
lazy[2*index+1] += lazy[index];
lazy[2*index] += lazy[index];
}
lazy[index] = 0;
}
//no overlap case
if(sr > e || s > er) return;
//complete overlap
if(sr <= s && er >= e){
tree[index] += increment;
if(s != e){
lazy[2*index+1] += increment;
lazy[2*index] += increment;
}
return;
}
//otherwise go on both left and right side and do your shit
int mid = (s + e)/2;
rangeUpdateLazy(tree, 2*index, s, mid, sr, er, increment);
rangeUpdateLazy(tree, 2*index + 1, mid+1, e, sr, er, increment);
tree[index] = Math.min(tree[2*index+1], tree[2*index]);
return;
}
}
//prime sieve
public static void primeSieve(int n){
BitSet bitset = new BitSet(n+1);
for(long i = 0; i < n ; i++){
if (i == 0 || i == 1) {
bitset.set((int) i);
continue;
}
if(bitset.get((int) i)) continue;
primeNumbers.add((int)i);
for(long j = i; j <= n ; j+= i)
bitset.set((int)j);
}
}
//number of divisors
// public static int countDivisors(long number){
// if(number == 1) return 1;
// List<Integer> primeFactors = new ArrayList<>();
// int index = 0;
// long curr = primeNumbers.get(index);
// while(curr * curr <= number){
// while(number % curr == 0){
// number = number/curr;
// primeFactors.add((int) curr);
// }
// index++;
// curr = primeNumbers.get(index);
// }
// if(number != 1) primeFactors.add((int) number);
// int current = primeFactors.get(0);
// int totalDivisors = 1;
// int currentCount = 2;
// for (int i = 1; i < primeFactors.size(); i++) {
// if (primeFactors.get(i) == current) {
// currentCount++;
// } else {
// totalDivisors *= currentCount;
// currentCount = 2;
// current = primeFactors.get(i);
// }
// }
// totalDivisors *= currentCount;
// return totalDivisors;
// }
// //now adding next permutation function to java hehe
// public static boolean next_permutation(int[] p) {
// for (int a = p.length - 2; a >= 0; --a)
// if (p[a] < p[a + 1])
// for (int b = p.length - 1;; --b)
// if (p[b] > p[a]) {
// int t = p[a];
// p[a] = p[b];
// p[b] = t;
// for (++a, b = p.length - 1; a < b; ++a, --b) {
// t = p[a];
// p[a] = p[b];
// p[b] = t;
// }
// return true;
// }
// return false;
// }
//finding the value of NCR in O(RlogN) time and O(1) space
public static long getNcR(int n, int r)
{
long p = 1, k = 1;
if (n - r < r) r = n - r;
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else {
p = 1;
}
return p;
}
//is vowel function
public static boolean isVowel(char c)
{
return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U');
}
//to sort the array with better method
public static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//sort long
public static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//for calculating binomialCoeff
public static int binomialCoeff(int n, int k)
{
int C[] = new int[k + 1];
// nC0 is 1
C[0] = 1;
for (int i = 1; i <= n; i++) {
// Compute next row of pascal
// triangle using the previous row
for (int j = Math.min(i, k); j > 0; j--)
C[j] = C[j] + C[j - 1];
}
return C[k];
}
//Pair with int int
public static class Pair{
public int a;
public int b;
Pair(int a , int b){
this.a = a;
this.b = b;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
//Triplet with int int int
public static class Triplet{
public int a;
public int b;
public int c;
Triplet(int a , int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
//Shortcut function
public static long lcm(long a , long b){
return a * (b/gcd(a,b));
}
//let's make one for calculating lcm basically
public static int lcm(int a , int b){
return (a * b)/gcd(a,b);
}
//int version for gcd
public static int gcd(int a, int b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//long version for gcd
public static long gcd(long a, long b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//for ncr calculator(ignore this code)
public static long __gcd(long n1, long n2)
{
long gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
// Checks if i is factor of both integers
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
//swapping two elements in an array
public static void swap(int[] arr, int left , int right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//for char array
public static void swap(char[] arr, int left , int right){
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//reversing an array
public static void reverse(int[] arr){
int left = 0;
int right = arr.length-1;
while(left <= right){
swap(arr, left,right);
left++;
right--;
}
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 5fdd59b6a5b2816ccddb90b146e6d6da | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static int MOD = 1000000007;
// After writing solution, quick scan for:
// array out of bounds
// special cases e.g. n=1?
// npe, particularly in maps
//
// Big numbers arithmetic bugs:
// int overflow
// sorting, or taking max, after MOD
void solve() throws IOException {
int T = ri();
boolean[] isPrime = sieve(1000000);
for (int Ti = 0; Ti < T; Ti++) {
int[] ne = ril(2);
int n = ne[0];
int e = ne[1];
int[] a = ril(n);
// a[i] * a[i+e] * a[i+2*e] * ... * a[i+k*e]
// Obviously, the sequence should be all 1s with a single non-1.
// e is the skip factor, we can think as e separate lists.
long ans = 0;
for (int i = 0; i < e; i++) {
// Lets aggregate as 1s, X, 1s, X, 1s, X, ..., here 1s can be 0.
List<Integer> list = new ArrayList<>();
int countOnes = 0;
for (int curr = i; curr < n; curr += e) {
if (a[curr] == 1) countOnes++;
else {
list.add(countOnes);
countOnes = 0;
list.add(a[curr]);
}
}
list.add(countOnes);
for (int j = 1; j < list.size(); j += 2) {
if (!isPrime[list.get(j)]) continue;
ans += (long) (list.get(j-1)+1) * (list.get(j+1)+1);
}
}
for (int ai : a) if (isPrime[ai]) ans--;
pw.println(ans);
}
}
// IMPORTANT
// DID YOU CHECK THE COMMON MISTAKES ABOVE?
// Computes and returns an array prime, where prime[i] is true if i is prime
// and false otherwise (for i <= n). Implemented using linear sieve. O(n).
public static boolean[] sieve(int n) {
List<Integer> primes = new ArrayList<>();
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i <= n; i++) {
if (prime[i]) primes.add(i);
for (int j = 0; j < primes.size() && i * primes.get(j) <= n; j++) {
prime[i * primes.get(j)] = false;
if (i % primes.get(j) == 0) break;
}
}
return prime;
}
// Template code below
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
Main m = new Main();
m.solve();
m.close();
}
void close() throws IOException {
pw.flush();
pw.close();
br.close();
}
int ri() throws IOException {
return Integer.parseInt(br.readLine().trim());
}
long rl() throws IOException {
return Long.parseLong(br.readLine().trim());
}
int[] ril(int n) throws IOException {
int[] nums = new int[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
long[] rll(int n) throws IOException {
long[] nums = new long[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
long x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
int[] rkil() throws IOException {
int sign = 1;
int c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
return ril(x);
}
long[] rkll() throws IOException {
int sign = 1;
int c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
return rll(x);
}
char[] rs() throws IOException {
return br.readLine().toCharArray();
}
void sort(int[] A) {
Random r = new Random();
for (int i = A.length-1; i > 0; i--) {
int j = r.nextInt(i+1);
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
Arrays.sort(A);
}
void printDouble(double d) {
pw.printf("%.16f", d);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | db27908dfd381a8bab265910845da130 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static final int M = 1000000007;
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[] readArrayInt(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static class Pair<F, S> {
public F first;
public S second;
Pair(F first, S second) {
this.first = first;
this.second = second;
}
public F getFirst() {
return this.first;
}
public S getSecond() {
return this.second;
}
}
static long __gcd(long a, long b) {
if (b == 0)
return a;
return __gcd(b, a % b);
}
/* Iterative Function to calculate (x^y) in O(log y) */
static long power(long a, long b) {
if (b == 0)
return 1;
if (b % 2 == 1)
return (a * power((a * a) % M, b / 2)) % M;
return power((a * a) % M, b / 2);
}
public static void main(String[] args) throws IOException {
FastScanner fs = new FastScanner();
int T = fs.nextInt();
// int T = 1;
StringBuffer res = new StringBuffer();
boolean[] isPrime = new boolean[1000001];
seive(isPrime);
for (int t = 1; t <= T; t++) {
int n = fs.nextInt(), e = fs.nextInt();
int[] a = fs.readArrayInt(n);
long ans = 0;
for (int i = 0; i < n; i++) {
long l = 0, r = 0;
if (isPrime[a[i]]) {
for (int j = i + e; j < n; j += e) {
if (a[j] == 1)
r++;
else
break;
}
for (int j = i - e; j >= 0; j -= e) {
if (a[j] == 1)
l++;
else
break;
}
}
ans += (l + r + l * r);
}
res.append(ans).append("\n");
}
System.out.println(res);
}
private static void seive(boolean[] isPrime) {
isPrime[0] = isPrime[1] = false;
for (int i = 2; i < isPrime.length; i++) {
isPrime[i] = true;
}
for (int i = 2; i * i < isPrime.length; i++) {
if (isPrime[i]) {
for (int j = i * i; j < isPrime.length; j += i) {
isPrime[j] = false;
}
}
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 4ae01ae983fd321ba9021387fb42a930 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean pass[] = new boolean[1000006];
int arr[] = new int[200005];
pass[1] = true;
for(int i = 2;i<=1000000;i++) {
if(pass[i])continue;
for(int j = 2;(long)i*j<=1000000;j++) {
pass[i*j] = true;
}
}
int t = in.nextInt();
while(t-->0) {
int n,d;
n = in.nextInt();
d = in.nextInt();
for(int i = 1;i<=n;i++) {
arr[i] = in.nextInt();
}
long res = 0;
for(int i = 1;i<=d;i++) {
int p = i;
while(p<=n) {
while(p<=n&&pass[arr[p]]) {
p+=d;
}
int l = p - d;
int r = p + d;
long a = 0;
long b = 0;
if(p>n)break;
while(l>=1&&arr[l]==1) {
l-=d;
a++;
}
while(r<=n&&arr[r]==1) {
r+=d;
b++;
}
res += (a+1)*(b+1) - 1;
p += d;
}
}
System.out.println(res);
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 11 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | e6aaa1fe63ab66959381003fd3365d74 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
int first, second,third;
public Tuple(int first, int second, int third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long modPow(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return modPow(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Integer> l = new ArrayList<>();
for (int p = 2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i = p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p = 2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(long a[], long x)
{
int l=-1,r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(long a[], long x)
{
int l=-1, r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt(), k = sc.nextInt();
int[] a = sc.readArray(n);
long res = 0;
for(int i = 0;i<n;i++)
{
if(isPrime(a[i]))
{
int j = i + k, c = 1;
while(j < n && a[j] == 1)
{
c+=1;
j+=k;
}
j = i - k;
res += c;
while(j >= 0 && a[j] == 1)
{
res += c;
j -= k;
}
res-=1;
}
}
fout.println(res);
}
fout.close();
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | f5470a8540b4bba2f415eaac38f3bbcc | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
int first, second,third;
public Tuple(int first, int second, int third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long modPow(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return modPow(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Integer> l = new ArrayList<>();
for (int p = 2; p*p<=n; p++)
{
if (prime[p] == true)
{
for(int i = p*p; i<=n; i += p)
{
prime[i] = false;
}
}
}
for (int p = 2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(long a[], long x)
{
int l=-1,r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(long a[], long x)
{
int l=-1, r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt(), k = sc.nextInt();
int[] a = sc.readArray(n);
long res = 0;
int[] dp = new int[n];
for(int i = 0;i<n;i++)
{
if(isPrime(a[i]))
{
int j = i + k, c = 1;
while(j < n && a[j] == 1)
{
c+=1;
j+=k;
}
j = i - k;
res += c;
while(j >= 0 && a[j] == 1)
{
res += c;
j -= k;
}
res--;
}
}
fout.println(res);
}
fout.close();
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | debd1b4192dc257eb97fd92863d320d7 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C_Complex_Marke_Analysis {
static Scanner in=new Scanner();
static PrintWriter out=new PrintWriter( new OutputStreamWriter(System.out) );
static int testCases,n,k,N=10000007;
static int a[];
static boolean primes[]=new boolean[10000007];
static StringBuilder finalAns=new StringBuilder();
static void solve(){
boolean visited[]=new boolean[n+1];
long ans=0;
ArrayList<Integer> ones=new ArrayList<>();
for(int i=1;i<=n;i++){
// ArrayList<Integer> ones=new ArrayList<>();
int one=0;
if(!visited[i] ){
// visited[i]=true;
ones=new ArrayList<>();
//one=0;
for(int j=i;j<=n;j+=k){
if((a[j]!=1 && !primes[a[j]] ) || visited[j] ){
break;
}
visited[j]=true;
if(a[j]==1 ){
one++;
}else{
ones.add(one);
one=0;
}
}
if(ones.size()==0 ){
continue;
}
ones.add(one);
int y=ones.size();
long arr[]=new long[y];
int index=0;
while(!ones.isEmpty() ){
arr[index++]=ones.popFront();
}
for(int j=0;j<y;j++){
ans+=arr[j];
if(j>0 && j<y-1){
ans+=arr[j];
}
if(j<y-1){
ans+=arr[j]*arr[j+1];
}
}
}
}
finalAns.append(ans).append("\n");
// out.println(ans);
//out.flush();
}
public static void main(String[] amit) throws IOException {
testCases=in.nextInt();
findPrimes();
for(int t=0;t<testCases;t++){
n=in.nextInt();
k=in.nextInt();
a=new int[n+1];
for(int i=1;i<=n;i++){
a[i]=in.nextInt();
}
solve();
}
out.print(finalAns.toString());
out.flush();
in.close();
}
static void findPrimes(){
boolean used[]=new boolean[N+1];
for(int i=2;i<N;i++){
if(!used[i] ){
primes[i]=true;
for(int j=i;j<N;j+=i){
used[j]=true;
}
}
}
}
static String sum(String a,String b){
StringBuilder ans=new StringBuilder();
int n1=a.length(),m=b.length();
if(n1>m ){
String t=a;
a=b;
b=t;
}
n1=a.length();
m=b.length();
StringBuilder str1=new StringBuilder(a).reverse();
StringBuilder str2=new StringBuilder(b).reverse();
char first[]=str1.toString().toCharArray();
char second[]=str2.toString().toCharArray();
int carry=0;
for(int i=0;i<n1;i++){
int sum=(first[i]-'0')+(second[i]-'0')+carry;
ans.append(sum%10 );
carry=sum/10;
}
for(int i=n1;i<m;i++){
int sum=(second[i]-'0')+carry;
ans.append(sum%10 );
sum/=10;
}
if(carry>0){
ans.append(carry);
}
ans.reverse();
return ans.toString();
}
static String mul(String num1, String num2)
{
int len1 = num1.length();
int len2 = num2.length();
if (len1 == 0 || len2 == 0)
return "0";
int result[] = new int[len1 + len2];
int i_n1 = 0;
int i_n2 = 0;
for (int i = len1 - 1; i >= 0; i--)
{
int carry = 0;
int n1 = num1.charAt(i) - '0';
i_n2 = 0;
for (int j = len2 - 1; j >= 0; j--)
{
int n2 = num2.charAt(j) - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0)
result[i_n1 + i_n2] += carry;
i_n1++;
}
int i = result.length - 1;
while (i >= 0 && result[i] == 0)
i--;
if (i == -1)
return "0";
String s = "";
while (i >= 0)
s += (result[i--]);
return s;
}
static int multiply(int x, int res[], int res_size) {
int carry = 0;
for (int i = 0; i < res_size; i++) {
int prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod / 10;
}
while (carry > 0) {
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
static long power(int x, int n) {
int MAX = 100000;
if(n == 0 ){
return 1;
}
int res[] = new int[MAX];
int res_size = 0;
int temp = x;
while (temp != 0) {
res[res_size++] = temp % 10;
temp = temp / 10;
}
for (int i = 2; i <= n; i++)
res_size = multiply(x, res, res_size);
StringBuilder sb=new StringBuilder();
for (int i = res_size - 1; i >= 0; i--)
sb.append(res[i]);
return Long.parseLong(sb.toString());
}
static class Node<T>{
T data;
Node<T> next;
public Node() {
this.next=null;
}
public Node(T data) {
this.data = data;
this.next=null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString(){
return this.getData().toString()+" ";
}
}
static class ArrayList<T>{
Node<T> head,tail;
int len;
public ArrayList() {
this.head=null;
this.tail=null;
this.len=0;
}
int size(){
return len;
}
boolean isEmpty(){
return len==0;
}
int indexOf(T data){
if( isEmpty() ){
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp=head;
int index=0,i=0;
while(temp!=null){
if( temp.getData()==data ){
index=i;
}
i++;
temp=temp.getNext();
}
return index;
}
void add(T data){
Node<T> newNode=new Node<>(data);
if( isEmpty() ){
head=newNode;
tail=newNode;
len++;
}else{
tail.setNext(newNode);
tail=newNode;
len++;
}
}
void see(){
if( isEmpty() ){
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp=head;
while( temp!=null ){
out.print(temp.getData().toString()+" ");
out.flush();
temp=temp.getNext();
}
out.println();
out.flush();
}
void inserFirst(T data){
Node<T> newNode=new Node<>(data);
Node<T> temp=head;
if( isEmpty() ){
head=newNode;
tail=newNode;
len++;
}else{
newNode.setNext(temp);
head=newNode;
len++;
}
}
T get(int index){
if( isEmpty() || index>=len ){
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp=head;
if(index==0){
return temp.getData();
}
int i=0;
T data=null;
while(temp!=null){
if( i==index ){
data=temp.getData();
}
i++;
temp=temp.getNext();
}
return data;
}
void addAt(T data,int index){
if( index>=len ){
throw new ArrayIndexOutOfBoundsException();
}
Node<T> newNode=new Node<>(data);
int i=0;
Node<T> temp=head;
while( temp.next!=null ){
if(i==index){
newNode.setNext(temp.next);
temp.next=newNode;
}
i++;
temp=temp.getNext();
}
// temp.setNext(temp);
len++;
}
T popFront(){
if( isEmpty() ){
throw new ArrayIndexOutOfBoundsException();
}
T data=head.getData();
if( head==tail ){
head=null;
tail=null;
}else{
head=head.getNext();
}
len--;
return data;
}
void removeAt(int index){
if( index>=len ){
throw new ArrayIndexOutOfBoundsException();
}
if(index==0){
this.popFront();
return;
}
Node<T> temp=head;
int i=0;
Node<T> n=new Node<>();
while(temp!=null){
if(i==index){
n.next=temp.next;
temp.next=n;
break;
}
i++;
n=temp;
temp=temp.getNext();
}
tail=n;
--len;
}
void clearAll(){
this.head=null;
this.tail=null;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void merge(long a[],int left,int right,int mid){
int n1=mid-left+1,n2=right-mid;
long L[]=new long[n1];
long R[]=new long[n2];
for(int i=0;i<n1;i++){
L[i]=a[left+i];
}
for(int i=0;i<n2;i++){
R[i]=a[mid+1+i];
}
int i=0,j=0,k1=left;
while(i<n1 && j<n2){
if( L[i]<=R[j] ){
a[k1]=L[i];
i++;
}else{
a[k1]=R[j];
j++;
}
k1++;
}
while(i<n1){
a[k1]=L[i];
i++;
k1++;
}
while(j<n2){
a[k1]=R[j];
j++;
k1++;
}
}
static void sort(long a[],int left,int right){
if(left>=right){
return;
}
int mid=(left+right)/2;
sort(a,left,mid);
sort(a,mid+1,right);
merge(a,left,right,mid);
}
static class Node1<T>{
T data;
Node1 next;
public Node1(T data) {
this.data = data;
this.next=null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node1 getNext() {
return next;
}
public void setNext(Node1 next) {
this.next = next;
}
}
static class Stack<T>{
int len;
Node1<T> node;
public Stack() {
len=0;
node=null;
}
boolean isEmpty(){
return len==0;
}
int size(){
return len;
}
void push(T data){
Node1<T> temp=new Node1<>(data);
temp.setNext(this.node);
node=temp;
len++;
}
T top(){
if( isEmpty() ){
throw new ArrayIndexOutOfBoundsException();
}
return this.node.getData();
}
T pop(){
if( isEmpty() ){
throw new ArrayIndexOutOfBoundsException();
}
T data=this.node.getData();
this.node=this.node.getNext();
len--;
return data;
}
}
static class Scanner{
BufferedReader in;
StringTokenizer st;
public Scanner() {
in=new BufferedReader( new InputStreamReader(System.in) );
}
String next() throws IOException{
while(st==null || !st.hasMoreElements()){
st=new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException{
return Integer.parseInt(next());
}
long nextLong() throws IOException{
return Long.parseLong(next());
}
String nextLine() throws IOException{
return in.readLine();
}
char nextChar() throws IOException{
return next().charAt(0);
}
double nextDouble() throws IOException{
return Double.parseDouble(next());
}
float nextFloat() throws IOException{
return Float.parseFloat(next());
}
boolean nextBoolean() throws IOException{
return Boolean.parseBoolean(next());
}
void close() throws IOException{
in.close();
}
}
}
/*
1
7 3
10 2 1 3 1 19 3
*/
/*
6
7 3
10 2 1 3 1 19 3
3 2
1 13 1
9 3
2 4 2 1 1 1 1 4 2
3 1
1 1 1
4 1
1 2 1 1
2 2
1 2
*/
/*
1
9 3
2 4 2 1 1 1 1 4 2
*/ | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 3834a738d8c2bd6a90c7375cd954d0a9 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.*;
import java.util.*;
public class solution {
static boolean prime[] = new boolean[1000001];
public static void main(String[] args) {
FScanner sc = new FScanner();
Arrays.fill(prime, true);
sieve();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int e=sc.nextInt();
int arr[]=new int[n];
long l[]=new long[n];
long r[]=new long[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
long ans=0;
for(int i=0;i<n;i++)
{
if(arr[i]==1)
{
l[i]=1;
}
if(i-e>=0 && arr[i-e]==1)
{l[i]+=l[i-e];}
}
for(int i=n-1;i>=0;i--)
{
if(arr[i]==1)
{
r[i]=1;
}
if(i+e<n && arr[i+e]==1)
{
r[i]+=r[i+e];
}
}
for(int i=0;i<n;i++)
{
if(prime[arr[i]])
{ long l_count=0,r_count=0;
if(i-e>=0 && arr[i-e]==1)
l_count=l[i-e];
if(i+e<n && arr[i+e]==1)
r_count=r[i+e];
ans=ans+ (l_count+1)*(r_count+1)-1;
}
}
System.out.println(ans);
}
}
public static void sieve()
{ prime[0]=prime[1]=false;
int n=1000000;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
}
class pair implements Comparable<pair>{
int f;int ind;
public pair(int f,int ind)
{
this.f=f;
this.ind=ind;
}
public int compareTo(pair p)
{return this.f-p.f;}
}
class FScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sb = new StringTokenizer("");
String next(){
while(!sb.hasMoreTokens()){
try{
sb = new StringTokenizer(br.readLine());
} catch(IOException e){ }
}
return sb.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
float nextFloat(){
return Float.parseFloat(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 16598fbecd854ecbc8c94ee9ea8c89ee | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] hi) {
FastIO io = new FastIO();
int t = io.nextInt();
while (t --> 0) {
int n = io.nextInt(), e = io.nextInt();
int[] array = new int[n];
boolean[] prime = new boolean[n];
for (int i=0; i<n; i++) {
array[i] = io.nextInt();
}
//System.out.println(Arrays.toString(prime));
long answer = 0;
for (int i=0; i<e; i++) {
long left = 0, right = 0;
boolean side = true;
for (int j=i; j<n; j+=e) {
if (side) {
if (array[j] == 1) {
left++;
}
else if (isPrime(array[j])) {
side = false;
}
else if (!isPrime(array[j])) {
left = 0;
right = 0;
}
}
else {
if (array[j] == 1) {
right++;
}
else if (isPrime(array[j])) {
answer += (left + right + (left*right));
left = right;
right = 0;
}
else if (!isPrime(array[j])) {
answer += (left + right + (left*right));
left = 0;
right = 0;
side = true;
}
}
}
if (!side) {
answer += (left + right + (left*right));
}
}
io.println(answer);
}
io.close();
}
static long[] dp = new long[(int)Math.pow(10, 7)];
static boolean isPrime(int n) {
if (n == 1) {
return false;
}
if (dp[n] != 0) {
if (dp[n] == 1) return true;
else return false;
}
for (int i=2; i<=Math.sqrt(n); i++) {
if (n%i == 0) {
dp[n] = 2;
return false;
}
}
dp[n] = 1;
return true;
}
static class FastIO extends PrintWriter {
BufferedReader br;
StringTokenizer st;
public FastIO() {
super(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
catch (Exception e) {
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public void close() {
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
super.close();
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 8eebda916826d11cc2e5d9a5426dc4fe | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] hi) {
FastIO io = new FastIO();
int t = io.nextInt();
while (t --> 0) {
int n = io.nextInt(), e = io.nextInt();
int[] array = new int[n];
boolean[] prime = new boolean[n];
for (int i=0; i<n; i++) {
array[i] = io.nextInt();
prime[i] = isPrime(array[i]);
}
//System.out.println(Arrays.toString(prime));
long answer = 0;
for (int i=0; i<e; i++) {
long left = 0, right = 0;
boolean side = true;
for (int j=i; j<n; j+=e) {
if (side) {
if (array[j] == 1) {
left++;
}
else if (prime[j]) {
side = false;
}
else if (!prime[j]) {
left = 0;
right = 0;
}
}
else {
if (array[j] == 1) {
right++;
}
else if (prime[j]) {
answer += (left + right + (left*right));
left = right;
right = 0;
}
else if (!prime[j]) {
answer += (left + right + (left*right));
left = 0;
right = 0;
side = true;
}
}
}
if (!side) {
answer += (left + right + (left*right));
}
}
io.println(answer);
}
io.close();
}
static boolean isPrime(int n) {
if (n == 1) {
return false;
}
for (int i=2; i<=Math.sqrt(n); i++) {
if (n%i == 0) {
return false;
}
}
return true;
}
static class FastIO extends PrintWriter {
BufferedReader br;
StringTokenizer st;
public FastIO() {
super(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
catch (Exception e) {
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public void close() {
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
super.close();
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | d0d5989fef01f35cc82158dcb5aad566 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /*==========================================================================
* AUTHOR: RonWonWon
* CREATED: 01.12.2021 01:54:42
/*==========================================================================*/
import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException {
/*
in.ini();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
*/
long startTime = System.nanoTime();
int t = in.nextInt(), T = 1;
prime = new int[1_000_002];
prime[1] = 1;
for(int i=2;i*i<=1_000_001;i++){
if(prime[i]==0) for(int p=i*i;p<=1_000_001;p+=i) prime[p] = 1;
}
while(t-->0) {
int n = in.nextInt(), e = in.nextInt();
a = in.readArrayL(n);
long cnt = 0;
long pre[] = new long[n];
for(int i=0;i<e;i++) pre[i] = a[i]-1;
for(int i=e;i<n;i++){
pre[i] = pre[i-e] + a[i]-1;
}
for(int i=0;i<n;i++){
if(prime[(int)a[i]]!=0) continue;
long x = bs1(pre,i,e);
long y = bs2(pre,i,e);
if(y==0) cnt += x;
else if(x==0) cnt += y;
else cnt += (x+1)*(y+1) - 1;
//out.println(i+" "+x+" "+y);
}
out.println(cnt);
//out.println("Case #"+T+": "+ans); T++;
}
/*
startTime = System.nanoTime() - startTime;
System.err.println("Time: "+(startTime/1000000));
*/
out.flush();
}
static int bs2(long pre[], int i, int e){
int n = pre.length, low = 1, mid, high = 1_000_000;
while(low<=high){
mid = (low+high)>>1;
if(i-(long)e*mid<0) high = mid-1;
else{
long p = pre[i] - pre[i-e*mid] + a[i-e*mid]-1;
if(p+1==a[i]) low = mid+1;
else high = mid-1;
}
}
return high;
}
static int bs1(long pre[], int i, int e){
int n = pre.length, low = 1, mid, high = 1_000_000;
while(low<=high){
mid = (low+high)>>1;
if(i+(long)e*mid>=n) high = mid-1;
else{
long p = pre[i+e*mid] - pre[i] + a[i]-1;
if(p+1==a[i]) low = mid+1;
else high = mid-1;
}
}
return high;
}
static int prime[];
static long a[];
static FastScanner in = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static int oo = Integer.MAX_VALUE;
static long ooo = Long.MAX_VALUE;
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
void ini() throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() {
while(!st.hasMoreTokens())
try { st = new StringTokenizer(br.readLine()); }
catch(IOException e) {}
return st.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] readArrayL(int n) {
long a[] = new long[n];
for(int i=0;i<n;i++) a[i] = nextLong();
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] readArrayD(int n) {
double a[] = new double[n];
for(int i=0;i<n;i++) a[i] = nextDouble();
return a;
}
}
static final Random random = new Random();
static void ruffleSort(int[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n), temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
static void ruffleSortL(long[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n);
long temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 6479b67c466739ffff26d9eabccfe0ed | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.lang.*;
import static java.lang.Math.*;
// Sachin_2961 submission //
public class Codeforces {
static boolean[] mark;
static void solve() {
int n = fs.nInt(), e = fs.nInt();
int[]ar = new int[n];
for(int i=0;i<n;i++)
ar[i] = fs.nInt();
long ans = 0;
for(int i=0;i<n;i++){
if( mark[ar[i]] ){
int k = i + e;
long cnt1 = 0;
while (k < n && ar[k] == 1){
k+=e;
cnt1++;
}
k = i - e;
long cnt2 = 0;
while ( k >=0 && ar[k] == 1){
k -= e;
cnt2++;
}
ans += (cnt1+1)*(cnt2+1) -1;
// out.println(ar[i]+" "+k);
}
}
out.println(ans);
}
static boolean[] findPrime(int bound) {
mark = new boolean[bound + 1];
mark[0] = false;
mark[1] = false;
for (int i = 2; i <= bound; i++)
mark[i] = true;
for (int i = 2; i * i <= bound; i++) {
if (mark[i]) {
for (int j = i * i; j <= bound; j += i)
mark[j] = false;
}
}
return mark;
}
static class Pair{
int f,s;
Pair(int f,int s){
this.f = f;
this.s = s;
}
}
static boolean multipleTestCase = true;
static FastScanner fs;
static PrintWriter out;
public static void main(String[]args){
try{
out = new PrintWriter(System.out);
fs = new FastScanner();
int tc = multipleTestCase?fs.nInt():1;
findPrime((int)1e6+10);
while (tc-->0)solve();
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String n() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String Line()
{
String str = "";
try
{
str = br.readLine();
}catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int nInt() {return Integer.parseInt(n()); }
long nLong() {return Long.parseLong(n());}
double nDouble(){return Double.parseDouble(n());}
int[]aI(int n){
int[]ar = new int[n];
for(int i=0;i<n;i++)
ar[i] = nInt();
return ar;
}
}
public static void sort(int[] arr){
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static void sort(long[] arr){
ArrayList<Long> ls = new ArrayList<>();
for(long x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | e01a4570a6510381a3a47f4a0fac01e3 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class C1609{
static FastScanner fs = null;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
int N = (int)1e6+4;
boolean prime[] = new boolean[N];
for(int i=2;i<=Math.sqrt(N);i++){
for(int j = i*i;j<N;j+=i){
prime[j] = true;
}
}
prime[1] = true;
while(t-->0){
int n = fs.nextInt();
int e = fs.nextInt();
int a[] = new int[n+1];
int s = 0;
for(int i=1;i<=n;i++){
int num = fs.nextInt();
if(!prime[num])
s+=1;
a[i] = num;
}
long ans = (long)0;
if(e==1){
int pre[] = new int[n+2];
int suf[] = new int[n+2];
pre[0] = 0;
suf[n+1] = 0;
for(int i=1;i<=n;i++){
if(a[i]==1){
pre[i]=pre[i-1]+1;
}
else{
pre[i] = 0;
}
}
for(int i=n;i>=1;i--){
if(a[i]==1){
suf[i]=suf[i+1]+1;
}
else{
suf[i] = 0;
}
}
for(int i=1;i<=n;i++){
if(!prime[a[i]]){
long su = (long)(pre[i-1]);
long s2 = (long)suf[i+1]+(long)1;
ans+=((su*s2));
ans+=((s2-(long)1));
}
}
// if(s!=0)
// ans+=pre[n];
}
else{
for(int i=1;i<=n;i++){
int on = 0;
int p = 0;
for(int k = 0;k<=(n/e);k++){
int id = i+k*e;
if(id>n)
break;
if(a[id]==1){
on+=1;
}
else if(!prime[a[id]]){
p+=1;
}
else{
break;
}
if(p>2){
break;
}
if(p==1){
if(on>=1)
ans+=(long)1;
}
}
}
}
out.println(ans);
}
out.close();
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 1d516c34a858751bb72b74704f306021 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /*
Challenge 1: Newbie to CM in 1year (Dec 2021 - Nov 2022) 5* Codechef
Challenge 2: CM to IM in 1 year (Dec 2022 - Nov 2023) 6* Codechef
Challenge 3: IM to GM in 1 year (Dec 2023 - Nov 2024) 7* Codechef
Goal: Become better in CP!
Key: Consistency and Discipline
Desire: SDE @ Google USA
Motto: Do what i Love <=> Love what i do
*/
import java.util.*;
import java.io.*;
import java.math.*;
public class Coder {
static StringBuffer str=new StringBuffer();
static int e, n;
static int a[];
static int sz=(int)1e6+1;
static boolean np[]=new boolean[sz];
static void cal(){
np[0]=np[1]=true;
for(int i=2;i*i<sz;i++){
if(!np[i]){
for(int j=i*i;j<sz;j+=i){
np[j]=true;
}
}
}
}
static void solve(){
// left[i]=min. valid index
// right[i]=max. valid index
int left[]=new int[n];
int right[]=new int[n];
for(int i=0;i<e;i++){
int j=i;
int mn=n;
while(j<n){
if(!np[a[j]]){
left[j]=mn;
mn=n;
}else if(a[j]==1){
mn=Math.min(j, mn);
}else{
mn=n;
}
j+=e;
}
}
for(int i=n-1;i>n-1-e;i--){
int j=i;
int mx=0;
while(j>=0){
if(!np[a[j]]){
right[j]=mx;
mx=0;
}else if(a[j]==1){
mx=Math.max(j, mx);
}else{
mx=0;
}
j-=e;
}
}
long ans=0;
for(int i=0;i<n;i++){
if(!np[a[i]]){
int mn=left[i];
int mx=right[i];
if(mn!=n && mx!=0) ans+=((mx-i)/e + 1) * 1l * ((i-mn)/e + 1)-1;
else if(mx!=0){
ans+=(mx-i)/e;
}else if(mn!=n){
ans+=(i-mn)/e;
}
}
}
str.append(ans).append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
cal();
int t = Integer.parseInt(bf.readLine().trim());
while (t-- > 0) {
String s[]=bf.readLine().trim().split("\\s+");
n=Integer.parseInt(s[0]);
e=Integer.parseInt(s[1]);
a=new int[n];
s=bf.readLine().trim().split("\\s+");
for(int i=0;i<n;i++) a[i]=Integer.parseInt(s[i]);
solve();
}
pw.println(str);
pw.flush();
// System.outin.print(str);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | a262e59e1ba945eb14d9bc432c918f82 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
/*
Challenge 1: Newbie to CM in 1year (Dec 2021 - Nov 2022) 🔥 5* Codechef
Challenge 2: CM to IM in 1 year (Dec 2022 - Nov 2023) 🔥🔥 6* Codechef
Challenge 3: IM to GM in 1 year (Dec 2023 - Nov 2024) 🔥🔥🔥 7* Codechef
Goal: Become better in CP!
Key: Consistency!
*/
public class Coder {
static StringBuffer str=new StringBuffer();
static int n, e;
static int a[];
static boolean prime[]=new boolean[(int)1e6+1];
static void cal(){
prime[0]=prime[1]=true;
for(int i=2;i*i<=(int)1e6;i++){
if(!prime[i]){
for(int j=i*i;j<=(int)1e6;j+=i){
prime[j]=true;
}
}
}
}
static long solve2(List<Integer> l){
long prev=0;
long ans=0;
for(int i=0;i<l.size();i++){
if(prime[l.get(i)]){
if(l.get(i)==1) prev++;
else prev=0;
continue;
}
int j=i+1;
int next=0;
while(j<l.size() && l.get(j)==1){
next++;
j++;
}
i=j-1;
ans+=(prev+1) * (next+1) - 1;
prev=next;
}
return ans;
}
static void solve(){
long ans=0;
for(int i=0;i<e;i++){
List<Integer> l=new ArrayList<>();
for(int j=i;j<=n;j+=e) l.add(a[j]);
ans+=solve2(l);
}
str.append(ans).append("\n");
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader bf;
PrintWriter pw;
boolean lenv=false;
if(lenv){
bf = new BufferedReader(
new FileReader("input.txt"));
pw=new PrintWriter(new
BufferedWriter(new FileWriter("output.txt")));
}else{
bf = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new OutputStreamWriter(System.out));
}
cal();
int t = Integer.parseInt(bf.readLine().trim());
while (t-- > 0) {
String s[]=bf.readLine().trim().split("\\s+");
n=Integer.parseInt(s[0]);
e=Integer.parseInt(s[1]);
a=new int[n+1];
s=bf.readLine().trim().split("\\s+");
for(int i=1;i<=n;i++){
a[i]=Integer.parseInt(s[i-1]);
}
solve();
}
pw.print(str);
pw.flush();
// System.out.print(str);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 6fac94c56d28dddb1cfd75a4f75b4b29 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
public class c731 {
public static void main(String[] args) throws IOException {
//try {
FastReader sc = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
int n = 1000000;
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++){
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
HashSet<Integer> check = new HashSet<Integer>();
for (int i = 2; i <= n; i++){
if (prime[i] == true)
// System.out.print(i + " ");
check.add(i);
}
int t = sc.nextInt();
for(int o = 0 ; o<t;o++) {
int m = sc.nextInt();
int e = sc.nextInt();
int[] arr = new int[m];
for(int i = 0 ; i<m;i++) {
arr[i] = sc.nextInt();
}
long ans = 0;
for(int i = 0 ; i<m;i++) {
if(!check.contains(arr[i])) {
continue;
}
int c1 = 0;
int k = i + e;
while(k<m) {
if(arr[k]!=1) {
break;
}
k+=e;
c1++;
}
int c2 = 0;
int l = i - e;
while(l>=0) {
if(arr[l]!=1) {
break;
}
l-=e;
c2++;
}
ans += (long)c1 + (long)c2 + (long)c1*c2;
}
//System.out.println(ans);
//for(int i = 0 ; i<m;i++) {
// if(!check.contains(arr[i]) && arr[i]!=1) {
// continue;
// }
// if(check.contains(arr[i])) {
// int k = i + e;
// while(k<m){
// if(arr[k] !=1) {
// break;
// }
// ans++;
// k+=e;
//
// }
// }else {
// int k = i +e;
// int f = 0;
// if(k<m && check.contains(arr[k])) {
// ans++;
// f = 1;
// }
// if(f == 1) {
// k += e;
// while(k<m) {
// if(arr[k]!=1) {
// break;
// }
// ans++;
// k+=e;
// }
// }
//
// }
//
//
//}
System.out.println(ans);
}
//}catch(Exception e ) {
// return;
//}
}
public static long smallestDivisor(long n){
if (n % 2 == 0)
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
public static boolean check(String s , ArrayList<Integer> al) {
String st = "";
int a = 0;
for(int x : al) {
st += s.substring(a,x);
a = x+1;
}
st += s.substring(a,s.length());
int i = 0;
int j = st.length()-1;
// System.out.println(st + " " + al);
while(i<=j) {
if(st.charAt(i)!=st.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | a5e6ee58a6f36770da89d9d4e85f7252 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt(), e = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = sc.nextInt();
long res = 0;
for(int i = 0; i < e; i++) {
boolean primeBeforeOnes = false;
long onesBeforePrime = 0;
long numOnes = 0;
for(int j = i; j < n; j += e) {
if(a[j] == 1)
numOnes++;
else {
if(primeBeforeOnes) {
res += (numOnes + 1) * (onesBeforePrime + 1) - 1;
}
if(isPrime(a[j])) {
onesBeforePrime = numOnes;
primeBeforeOnes = true;
}
else
primeBeforeOnes = false;
numOnes = 0;
}
}
if(primeBeforeOnes) {
res += (numOnes + 1) * (onesBeforePrime + 1) - 1;
}
}
System.out.println(res);
}
}
static boolean isPrime(int n) {
if (n<= 1) {
return false;
}
if(n == 2)
return true;
if(n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i+=2) {
if (n % i == 0) {
return false;
}
}
return true;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 915bf0fc008918b5b8dd1c314921ce0d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package practice.codeforces.deltix;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
// https://codeforces.com/contest/1609/problem/C
public class C {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int e = scanner.nextInt();
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = scanner.nextInt();
}
System.out.println(count(a, e));
}
scanner.close();
}
private static Set<Integer> primes = new HashSet<>();
static {
primes.add(2);
}
private static int maxPrime = 2;
private static boolean isPrime(int i, boolean fromOutside) {
if (i < 2) {
return false;
}
if (i <= maxPrime) {
return primes.contains(i);
}
for (int p : primes) {
if (i % p == 0) {
return false;
}
}
if (!fromOutside) {
primes.add(i);
maxPrime = i;
return true;
}
for (int p = maxPrime; p < Math.sqrt(i) + 1; p++) {
if (isPrime(p, false)) {
if (i % p == 0) {
return false;
}
}
}
return true;
}
private static int countOld(int[] a, int e) {
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == 1) {
int numOfOnesPrev = 1;
int numOfOnesPost = 0;
boolean foundPrime = false;
for (int j = i + e; j < a.length; j += e) {
if (a[j] == 1) {
if (!foundPrime) {
a[j] = -1; // to skip it on further iterations
numOfOnesPrev++;
} else {
numOfOnesPost++;
}
} else if (isPrime(a[j], true) && !foundPrime) {
foundPrime = true;
a[j] = -1;
} else {
break;
}
}
if (foundPrime) {
count += (numOfOnesPrev + 1) * (numOfOnesPost + 1) - 1;
}
} else if (isPrime(a[i], true)) {
for (int j = i + e; j < a.length; j += e) {
if (a[j] == 1) {
count++;
} else {
break;
}
}
}
}
return count;
}
private static long count(int[] a, int e) {
long count = 0;
for (int i = 0; i < a.length && i < e; i++) {
long numOfOnesPrev = 0;
long numOfOnesPost = 0;
boolean foundPrime = false;
for (int j = i; j < a.length; j += e) {
if (a[j] == 1) {
if (!foundPrime) {
numOfOnesPrev++;
} else {
numOfOnesPost++;
}
} else if (isPrime(a[j], true)) {
if (foundPrime) {
count += (numOfOnesPrev + 1) * (numOfOnesPost + 1) - 1;
numOfOnesPrev = numOfOnesPost;
numOfOnesPost = 0;
} else {
foundPrime = true;
}
} else {
if (foundPrime) {
count += (numOfOnesPrev + 1) * (numOfOnesPost + 1) - 1;
}
numOfOnesPrev = 0;
numOfOnesPost = 0;
foundPrime = false;
}
}
if (foundPrime) {
count += (numOfOnesPrev + 1) * (numOfOnesPost + 1) - 1;
}
}
return count;
}
private static void check(int expected, int e, int... a) {
long c = count(a, e);
if (count(a, e) == expected) {
System.out.println("ok");
} else {
System.out.println(c + " != " + expected);
}
}
// public static void main(String[] args) {
// check(2, 3, 10, 2, 1, 3, 1, 19, 3);
// check(4, 3, 2, 4, 2, 1, 1, 1, 1, 4, 2);
// check(8, 1, 1, 1, 3, 1, 1);
// check(2, 1, 3, 1, 1);
// check(2, 1, 1, 1, 3);
// check(0, 1, 2, 3);
// check(1, 1, 4, 1, 3);
// check(0, 1, 4, 1, 6, 3);
// check(1, 1, 4, 4, 2, 1, 4);
// check(0, 1, 4, 3, 5, 4);
// check(2, 1, 4, 3, 1, 5, 4);
// check(2, 1, 1, 2, 3, 5, 1);
// check(8, 1, 1, 2, 1, 1, 3, 5, 1);
// check(0, 1, 1, 1, 1, 1, 1);
// check(0, 1, 4);
// check(0, 1, 4, 6, 8, 3, 6, 9, 10);
//
// }
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | e9404eb0038dfd165bb4a324fb7be93c | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int t;
//t = in.nextInt();
t = 1;
while (t > 0) {
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
int[] isPrime = new int[1000005];
public void call(InputReader in, PrintWriter out) {
init();
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
int n, e;
n = in.nextInt();
e = in.nextInt();
int[] arr = new int[n];
ArrayList<Integer>[] arrayLists = new ArrayList[e];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i < e; i++) {
arrayLists[i] = new ArrayList<>();
for (int j = i; j < n; j += e) {
if(arr[j]!=1)
arrayLists[i].add(j);
}
}
int k, l, r, mid;
long c = 0;
for (int i = 0; i < n; i++) {
k = ((n-1) - i)/e;
if(k==0)
break;
if(arr[i]==1){
l = -1;
r = arrayLists[i%e].size();
while(l+1 < r){
mid = (l + r)/2;
if(arrayLists[i%e].get(mid) < i){
l = mid;
}
else{
r = mid;
}
}
if(r!=arrayLists[i%e].size()){
if(arrayLists[i%e].size()==r+1){
if(prime(arr[arrayLists[i%e].get(r)])) {
c += (n-1 - (arrayLists[i % e].get(r))) / e;
c++;
}
}
else{
if(prime(arr[arrayLists[i%e].get(r)])) {
c+= (arrayLists[i%e].get(r + 1) -(arrayLists[i%e].get(r))) / e;
}
}
}
}
else{
if(prime(arr[i])){
l = -1;
r = arrayLists[i%e].size();
while(l+1 < r){
mid = (l + r)/2;
if(arrayLists[i%e].get(mid)<=i){
l = mid;
}
else{
r = mid;
}
}
if(r==arrayLists[i%e].size()){
c += (n-1 - i) / e;
}
else{
c+= (arrayLists[i%e].get(r) - i) / e -1;
}
}
}
}
out.println(c);
t--;
}
out.close();
}
public boolean prime(long a){
return isPrime[(int) a] == 0;
}
public void init(){
isPrime[1] = 1;
for (int i = 2; i*i <= 1000002; i++) {
if(isPrime[i]==0){
for (int j = i*i; j < 1000002; j+=i) {
isPrime[j] = 1;
}
}
}
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a,b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return o.a - this.a;
}
}
static class arrayListClass {
ArrayList<Integer> arrayList2 ;
public arrayListClass(ArrayList<Integer> arrayList) {
this.arrayList2 = arrayList;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 39fd7fcc8a17f9c714af5c90d6807376 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
/**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
public static class Task {
private boolean[] notPrime;
private void init() {
notPrime = new boolean[1000002];
for(int i=2;i<=1000001;i++) {
for(int j=2;j*i<=1000001;j++) {
notPrime[i*j] = true;
}
}
}
private boolean isPrime(Integer num) {
return !notPrime[num];
}
public void solve(int cnt, InputReader in, PrintWriter out) {
cnt = in.nextInt();
init();
for (int c = 0; c < cnt; c++) {
int n = in.nextInt();
int e = in.nextInt();
int[] a = new int[n + 1];
int[] l = new int[n+1]; //记录以e为间隔,到当前数为止的>1的数的数量
int[] r = new int[n+1]; //记录以e为间隔,最近一个大于1的数的索引
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
//记录当前点往左连续1的个数,不包含当前点
for(int i=1;i<=n;i++) {
if(a[i]==1) {
l[i] = i-e>=1?l[i-e]+1:1;
} else {
l[i] = 0;
}
}
for(int i=n;i>=1;i--) {
if(a[i]==1) {
r[i] = i+e<=n?r[i+e]+1:1;
} else {
r[i] = 0;
}
}
long ret = 0;
for(int i=1;i<=n;i++) {
if(a[i]>1 && isPrime(a[i])) {
long l1 = i-e>=1?l[i-e]:0;
long r1 = i+e<=n?r[i+e]:0;
ret+=l1*r1+l1+r1;
}
}
out.println(ret);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 6bfc112ad402008a7e5d745f88d84521 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package Div2.C;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ComplexMarketAnalysis {
private static int LIM = 1000000;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
List<Integer> primes = getPrimes();
Set<Integer> primesSet = new HashSet<>(primes);
int t = Integer.parseInt(br.readLine());
while (t > 0) {
String[] ne = br.readLine().split(" ");
int n = Integer.parseInt(ne[0]);
int e = Integer.parseInt(ne[1]);
String[] str = br.readLine().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(str[i]);
long ans = 0;
for (int i = 0; i <= e - 1; i++) {
int j = i;
boolean primeEncountered = false;
int onesBeforePrime = 0;
int onesAfterPrime = 0;
while (j < n) {
if(a[j]>1){
if(!primesSet.contains(a[j])){
if (primeEncountered && onesAfterPrime + onesBeforePrime > 0) {
ans += (long)(onesBeforePrime + 1) * (onesAfterPrime + 1) - 1;
}
onesBeforePrime = 0;
onesAfterPrime = 0;
primeEncountered = false;
}else{
if(!primeEncountered){
primeEncountered = true;
}else{
if(onesAfterPrime + onesBeforePrime > 0) {
ans += (long)(onesBeforePrime + 1) * (onesAfterPrime + 1) - 1;
onesBeforePrime = onesAfterPrime;
onesAfterPrime = 0;
}
}
}
}else{
if(primeEncountered){
onesAfterPrime+=1;
}else
onesBeforePrime+=1;
}
j=j+e;
}
if(primeEncountered && onesAfterPrime + onesBeforePrime > 0) {
ans += (long)(onesBeforePrime + 1) * (onesAfterPrime + 1) - 1;
}
}
System.out.println(ans);
t--;
}
}
private static List<Integer> getPrimes() {
List<Integer> primes = new ArrayList<>();
boolean[] sieve = new boolean[LIM + 1];
for (int i = 2; i * i <= LIM; i++) {
if (!sieve[i]) {
for (long j = i * i; j <= LIM; j += i) {
sieve[(int) j] = true;
}
}
}
for (int i = 2; i <= LIM; i++) {
if (!sieve[i])
primes.add(i);
}
return primes;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 912ca413ed5934ed43ec2facb4916cd5 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package com.company;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.util.*;
public class Main {
public static class Dsu{
int[] parent;
Set<Integer>[] rest;
Set<Integer>[] restrictions;
Dsu(int n, int[][] restrictions)
{
parent = new int[n];
rest = new Set[n];
for(int i =0; i < n; i ++)
parent[i]=i;
for(int i =0; i < n; i++)
{
rest[i] = new HashSet<>();
rest[i].add(i);
}
this.restrictions = new Set[n];
for(int i =0; i < n; i++)
{
this.restrictions[i] = new HashSet<>();
}
for(int[] restriction : restrictions)
{
this.restrictions[restriction[0]].add(restriction[1]);
this.restrictions[restriction[1]].add(restriction[0]);
}
}
int find(int son)
{
if(parent[son]==son) return son;
return find(parent[son]);
}
boolean union(int p, int q)
{
int pp = find(p);
int pq = find(q);
if(pp==pq) return true;
if(valid(rest[pp],restrictions[pq]))
{
rest[pp].addAll(rest[pq]);
parent[pq] = pp;
return true;
}
return false;
}
boolean valid(Set<Integer> s1, Set<Integer> s2)
{
for(Integer i : s1)
{
if(s2.contains(i)) return false;
//System.out.println(i);
}
return true;
}
}
public static boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {
boolean[] ans = new boolean[requests.length];
Dsu dsu = new Dsu(n,restrictions);
int i =0;
for(int[] req : requests)
{
ans[i++] = dsu.union(req[0],req[1]);
}
return ans;
}
public static void main(String[] args) {
// write your code here
boolean prime[] = new boolean[1000000 + 1];
for (int i = 0; i <= 1000000; i++)
prime[i] = true;
for (int p = 2; p * p <= 1000000; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= 1000000; i += p)
prime[i] = false;
}
}
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while (t-->0)
{
int n = scanner.nextInt();
int e = scanner.nextInt();
int[] arr = new int[n];
for(int i =0; i < n ; i++)
arr[i] = scanner.nextInt();
long sol = 0;
for(int i =0 ; i < n; i++)
{
if(arr[i]>1&&prime[arr[i]])
{
int j = i+e;
int temp=0;
while(j<n&&arr[j]==1)
{
sol++;
j+=e;
temp++;
}
j = i-e;
while(j>=0&&arr[j]==1)
{
sol+=temp+1;
j-=e;
}
}
}
System.out.println(sol);
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 003d24d27dc8e80264140ca35371effc | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class C{
static int[] p;
public static void main(String[] args) throws IOException{
// br = new BufferedReader(new FileReader(".in"));
// out = new PrintWriter(new FileWriter(".out"));
// new Thread(null, new (), "fisa balls", 1<<28).start();
int t =readInt();
p= new int[(int)1e6+10];
p[0]=p[1]=0;
for (int i = 2; i <p.length;i++) {
if (p[i] == 0) {
for (int j =i; j < p.length; j+=i) {
int temp = j;
while(temp%i==0) {
p[j]++;
temp/=i;
}
}
}
}
while(t-->0) {
int n = readInt(), e =readInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i]=readInt();
long ans = 0;
for (int i = 0 ; i < e; i++) {
List<Integer> shit = new ArrayList<Integer>();
for (int j = i; j < n; j+=e) {
shit.add(a[j]);
}
int[] pre = new int[shit.size()+1];
for (int j = 0; j < shit.size(); j++) {
pre[j+1] = p[shit.get(j)];
pre[j+1]+=pre[j];
}
//for(int x: pre) System.out.print(x + " |");
//System.out.println();
for (int j = 1; j < pre.length; j++) {
if (pre[j]-pre[j-1] >= 2) continue;
if (pre[j]-pre[j-1] ==0) {
// Find largest shitter greater than this and the zero
int l = j, r = pre.length;
while(l+1<r) {
int mid = (l+r)/2;
if (pre[mid] - pre[j-1] <= 1) l = mid;
else r = mid;
}
int last = l;
l = j;
r = pre.length;
while(l+1<r) {
int mid = (l+r)/2;
if (pre[mid] - pre[j-1]<=0) l = mid;
else r= mid;
}
ans += last-l;
//System.out.println(last - l + " shi");
}
else {
// In the one case, you want as many zeroes as possible
int l = j, r = pre.length;
while (l+1<r) {
int mid = (l+r)/2;
if (pre[mid] == pre[j]) l = mid;
else r = mid;
}
ans += l-j;
}
}
//System.out.println("Ansy" + ans);
}
out.println(ans);
}
out.close();
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static StringTokenizer st = new StringTokenizer("");
static String read() throws IOException{
while (!st.hasMoreElements()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public static int readInt() throws IOException{return Integer.parseInt(read());}
public static long readLong() throws IOException{return Long.parseLong(read());}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 82340b80bcb2adc2dd285d5a43d2b548 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /* || श्री राम समर्थ ||
|| जय जय रघुवीर समर्थ ||
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.*;
import static java.util.Arrays.sort;
public class CodeforcesTemp {
static Reader scan = new Reader();
static FastPrinter out = new FastPrinter();
static int max = 1000001;
static int primes[] = new int[max];
public static void main(String[] args) throws IOException {
int tt = scan.nextInt();
// int tt = 1;
primeinit();
for (int tc = 1; tc <= tt; tc++) {
long n= scan.nextLong();
long e= scan.nextLong();
long[] arr= scan.nextLongArray(n);
long[] dp=new long[(int) n];
for (int i = 0; i < n; i++) {
if(arr[i]==1){
dp[i]=Math.max(1,dp[i]);
if((i+e)<n && arr[(int) (i+e)]==1){
dp[(int) (i+e)]=dp[i]+1;
}
}
}
long[] dp2=new long[(int) n];
for (int i = (int) (n-1); i >=0; i--) {
if(arr[i]==1){
dp2[i]=Math.max(1,dp2[i]);
if((i-e)>=0 && arr[(int) (i-e)]==1){
dp2[(int) (i-e)]=dp2[i]+1;
}
}
}
long ans=0;
for (int i = 0; i < n; i++) {
if(arr[i]==1)continue;
if(primes[(int) arr[i]]==0){
long res=1;
if((i-e)>=0){
res+=dp[(int) (i-e)];
}
if((i+e)<n && arr[(int) (i+e)]==1){
long res1=(res-1)*(dp2[(int) (i+e)]+1);
res1+=dp2[(int) (i+e)];
ans+=res1;
}else{
ans+=(res-1);
}
}
}
out.println(ans);
out.flush();
}
out.close();
}
static void primeinit() {
primes[1] = 1;
for(int i = 2;i<max;i++)
{
if(primes[i]==0) {
for(int j = 2;j*i<max;j++)
primes[i*j] = 1;
}
}
}
static class Reader {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public Reader(InputStream in) {
this.in = in;
}
public Reader() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
}
}
throw new ArithmeticException(
String.format(" overflows long."));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(long length) {
long[] array = new long[(int) length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(PrintStream stream) {
super(stream);
}
public FastPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printMatrix(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
this.printArray(arr[i]);
}
}
public void printCharMatrix(char[][] arr, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
this.print(arr[i][j] + " ");
}
this.println();
}
}
}
static Random __r = new Random();
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] arr, int i, int j) {
while (i < j) {
char temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(int[] arr, int i, int j) {
while (i < j) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(long[] arr, int i, int j) {
while (i < j) {
long temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
private static int getlowest(int l) {
if (l >= 1000000000) return 1000000000;
if (l >= 100000000) return 100000000;
if (l >= 10000000) return 10000000;
if (l >= 1000000) return 1000000;
if (l >= 100000) return 100000;
if (l >= 10000) return 10000;
if (l >= 1000) return 1000;
if (l >= 100) return 100;
if (l >= 10) return 10;
return 1;
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 51328d4ab48667860778ab2e50343d43 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /* || श्री राम समर्थ ||
|| जय जय रघुवीर समर्थ ||
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.*;
import static java.util.Arrays.sort;
public class CodeforcesTemp {
static Reader scan = new Reader();
static FastPrinter out = new FastPrinter();
public static void main(String[] args) throws IOException {
int tt = scan.nextInt();
// int tt = 1;
HashSet<Long> isprime = getprimes();
for (int tc = 1; tc <= tt; tc++) {
long n= scan.nextLong();
long e= scan.nextLong();
long[] arr= scan.nextLongArray(n);
long[] dp=new long[(int) n];
for (int i = 0; i < n; i++) {
if(arr[i]==1){
dp[i]=Math.max(1,dp[i]);
if((i+e)<n && arr[(int) (i+e)]==1){
dp[(int) (i+e)]=dp[i]+1;
}
}
}
long[] dp2=new long[(int) n];
for (int i = (int) (n-1); i >=0; i--) {
if(arr[i]==1){
dp2[i]=Math.max(1,dp2[i]);
if((i-e)>=0 && arr[(int) (i-e)]==1){
dp2[(int) (i-e)]=dp2[i]+1;
}
}
}
long ans=0;
for (int i = 0; i < n; i++) {
if(arr[i]==1)continue;
if(isprime.contains(arr[i])){
long res=1;
if((i-e)>=0){
res+=dp[(int) (i-e)];
}
if((i+e)<n && arr[(int) (i+e)]==1){
long res1=(res-1)*(dp2[(int) (i+e)]+1);
res1+=dp2[(int) (i+e)];
ans+=res1;
}else{
ans+=(res-1);
}
}
}
out.println(ans);
out.flush();
}
out.close();
}
public static HashSet<Long> getPrimesInIntervalInclusive(int lower, int higher) {
HashSet<Long> primes = new HashSet<>();
boolean[] sieve = new boolean[higher + 1];
for (int i = 2; i < sieve.length; i++) {
if (!sieve[i]) {
for (int j = 2 * i; j < sieve.length; j += i) {
sieve[j] = true;
}
if (i >= lower) {
primes.add((long) i);
}
}
}
return primes;
}
private static HashSet<Long> getprimes() {
HashSet<Long> primes = getPrimesInIntervalInclusive(1, 1000_000);
return primes;
}
static class Reader {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public Reader(InputStream in) {
this.in = in;
}
public Reader() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
}
}
throw new ArithmeticException(
String.format(" overflows long."));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(long length) {
long[] array = new long[(int) length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(PrintStream stream) {
super(stream);
}
public FastPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printMatrix(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
this.printArray(arr[i]);
}
}
public void printCharMatrix(char[][] arr, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
this.print(arr[i][j] + " ");
}
this.println();
}
}
}
static Random __r = new Random();
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] arr, int i, int j) {
while (i < j) {
char temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(int[] arr, int i, int j) {
while (i < j) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(long[] arr, int i, int j) {
while (i < j) {
long temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
private static int getlowest(int l) {
if (l >= 1000000000) return 1000000000;
if (l >= 100000000) return 100000000;
if (l >= 10000000) return 10000000;
if (l >= 1000000) return 1000000;
if (l >= 100000) return 100000;
if (l >= 10000) return 10000;
if (l >= 1000) return 1000;
if (l >= 100) return 100;
if (l >= 10) return 10;
return 1;
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 39608c046365446f08a0368c29b72892 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static Scanner sc = new Scanner(new BufferedInputStream(System.in));
static int sc(){return sc.nextInt();}
static long scl(){return sc.nextLong();}
static double scd(){return sc.nextDouble();};
static String scs(){return sc.next();};
static char scf(){return scs().charAt(0);}
static char[] carr(){return sc.next().toCharArray();};
static char[] chars(){
char[] s = carr();
char[] c = new char[s.length+1];
System.arraycopy(s,0,c,1,s.length);
return c;
}
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static void println (Object o) {out.println(o);}
static void println() {out.println();}
static void print(Object o) {out.print(o);}
static int[] dx = new int[]{-1,0,1,0},dy = new int[]{0,-1,0,1};
static final int N = (int)4e6+10,M = 2010,INF = 0x3f3f3f3f,P = 131,MOD = (int)1e9+7;
static int n;
// static int[] h = naew int[N],e = new int[N],ne = new int[N],w = new int[N];
// static int idx;
static int[] a = new int[N];
static boolean[] st = new boolean[N];
static int[] primes = new int[N];
static int idx;
static Set<Integer> set = new HashSet();
static void getPrimes(int x){
for(int i = 2;i<=x;i++){
if(!st[i]) primes[idx++] = i;
for(int j = 0;primes[j]<=x/i;j++){
st[primes[j]*i] = true;
if(i%primes[j] == 0) break;
}
}
}
public static void main(String[] args) throws Exception{
int t = sc();
getPrimes(N/2);
for(int i = 0;i<idx;i++) set.add(primes[i]);
while(t-->0){
long res = 0;
int n = sc(),k = sc();
for(int i = 1;i<=n;i++) a[i] = sc();
for(int i = 1;i<=k;i++){
boolean f = false;
boolean fi = false;
long len = 0;
long nlen = 0;
for(int j = i;j<=n;j+=k){
if(a[j] == 1) {
if(f){
res+=len+1;
nlen++;
}
else len++;
fi = true;
}else if(set.contains(a[j])){
if(f) {
len = nlen;
nlen = 0;
}
res+=len;
f = true;
}else {
len = 0;
nlen = 0;
f = false;
fi = false;
}
}
}
println(res);
}
out.close();
}
static class D{
int x,y; D(int x,int y){this.x = x;this.y = y;}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 4e57192704d4c892e64d966e60f897ab | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import static java.lang.Math.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class cf1 {
static FastReader x = new FastReader();
static OutputStream outputStream = System.out;
static PrintWriter out = new PrintWriter(outputStream);
/*---------------------------------------CODE STARTS HERE-------------------------*/
public static void main(String[] args) {
int t = x.nextInt();
boolean s[] = esieve(1000001);
StringBuilder str = new StringBuilder();
while (t > 0) {
int n =x.nextInt();
int e = x.nextInt();
s[1]=false;
HashSet<Integer> prime = new HashSet<>();
HashSet<Integer> one = new HashSet<>();
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = x.nextInt();
if(s[a[i]]) {
// System.out.println("prime "+a[i]);
prime.add(i);
}else if(a[i]==1) {
one.add(i);
}
}
long ans =0;
for(int curr:prime) {
int temp =curr;
long tans =0,tans1=0;
while(temp<n&&one.contains(temp+e)) {
tans++;
// System.out.println(temp+" i");
temp = temp+e;
}
ans+=tans;
temp = curr;
while(temp>0&&one.contains(temp-e)) {
tans1++;
// System.out.println(temp+" d");
temp=temp-e;
}
if(tans1>0)
ans+=((tans*tans1)+tans1);
}
System.out.println(ans);
str.append("\n");
t--;
}
out.println(str);
out.flush();
}
/*--------------------------------------------FAST I/O--------------------------------*/
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
char nextchar() {
char ch = ' ';
try {
ch = (char) br.read();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}
/*--------------------------------------------BOILER PLATE---------------------------*/
static int[] readarr(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = x.nextInt();
}
return arr;
}
static int[] sortint(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static long[] sortlong(long a[]) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < al.size(); i++) {
a[i] = al.get(i);
}
return a;
}
static int pow(int x, int y) {
int result = 1;
while (y > 0) {
if (y % 2 == 0) {
x = x * x;
y = y / 2;
} else {
result = result * x;
y = y - 1;
}
}
return result;
}
static int[] revsort(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al, Comparator.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static int[] gcd(int a, int b, int ar[]) {
if (b == 0) {
ar[0] = a;
ar[1] = 1;
ar[2] = 0;
return ar;
}
ar = gcd(b, a % b, ar);
int t = ar[1];
ar[1] = ar[2];
ar[2] = t - (a / b) * ar[2];
return ar;
}
static boolean[] esieve(int n) {
boolean p[] = new boolean[n + 1];
Arrays.fill(p, true);
for (int i = 2; i * i <= n; i++) {
if (p[i] == true) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
static ArrayList<Integer> primes(int n) {
boolean p[] = new boolean[n + 1];
ArrayList<Integer> al = new ArrayList<>();
Arrays.fill(p, true);
int i = 0;
for (i = 2; i * i <= n; i++) {
if (p[i] == true) {
al.add(i);
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
for (i = i; i <= n; i++) {
if (p[i] == true) {
al.add(i);
}
}
return al;
}
static int etf(int n) {
int res = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res /= i;
res *= (i - 1);
while (n % i == 0) {
n /= i;
}
}
}
if (n > 1) {
res /= n;
res *= (n - 1);
}
return res;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 03de96cd0610382e5d37d038b0f4be5d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
boolean[] isPrime = sieve((int)1e6);
int T = in.nextInt();
for(int ttt = 1; ttt <= T; ttt++) {
int n = in.nextInt();
int e = in.nextInt();
int[] a = in.readInt(n);
long ans = 0;
for(int i = 0; i < n; i++) {
if(isPrime[a[i]]) {
long prev = 0, nxt = 0;
for(int j = i-e; j >= 0; j-=e) {
if(a[j]!=1) break;
prev++;
}
for(int j = i+e; j < n; j+=e) {
if(a[j]!=1) break;
nxt++;
}
ans += (prev+1)*(nxt+1)-1;
}
}
out.println(ans);
}
out.close();
}
private static boolean[] sieve(int n) {
boolean[] isPrime = new boolean[n+1];
for(int i = 2; i <= n; i++)
isPrime[i] = true;
for(int i = 2; i*i <= n; i++) {
for(int j = i*i; j <= n; j+=i)
isPrime[j] = false;
}
return isPrime;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
String next() {
while (st == null || !st.hasMoreElements()) {
try { st = new StringTokenizer(br.readLine()); }
catch(IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String nextLine() {
String str = "";
try { str = br.readLine(); }
catch(IOException e) { e.printStackTrace(); }
return str;
}
int[] readInt(int size) {
int[] arr = new int[size];
for(int i = 0; i < size; i++)
arr[i] = Integer.parseInt(next());
return arr;
}
long[] readLong(int size) {
long[] arr = new long[size];
for(int i = 0; i < size; i++)
arr[i] = Long.parseLong(next());
return arr;
}
int[][] read2dArray(int rows, int cols) {
int[][] arr = new int[rows][cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
arr[i][j] = Integer.parseInt(next());
}
return arr;
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | aa648d649ab6c03386bf8ad479b2f62e | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.Math.sqrt;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc;
// static FastWriter out;
public static void main(String hi[]){
initializeIO();
sc=new FastReader();
// FastWriter out=new FastWriter();
int t=sc.nextInt();
boolean[] seave=sieveOfEratosthenes((int)(1e6));
while(t--!=0){
int n=sc.nextInt();
int k=sc.nextInt();
int[] arr=rintArray(n);
long[] pre=new long[n];
long[] suff=new long[n];
// long max=maxOfArray(arr);
boolean[] vis=new boolean[n];
long ans=0;
for(int i=0;i<n;i++){
if(seave[arr[i]]&&!vis[i]){
long l=0;
long r=0;
for(int j=i+k;j<n;j=j+k){
if(arr[j]==1){
r++;
vis[j]=true;
}else{
break;
}
}
for(int j=i-k;j>=0;j=j-k){
if(arr[j]==1){
l++;
vis[j]=true;
}else{
break;
}
}
ans+=((l+1)*(r+1))-1;
// out.println(i+" "+arr[i]+" "+l+" "+r+" "+ans);
}
}
out.println(ans);
}
// System.out.println(String.format("%.10f", max));
}
private static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static boolean[] sieveOfEratosthenes(long n)
{
boolean prime[] = new boolean[(int)n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true){
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static long maxProfit(List<Long> prices) {
long sofar=0;
long max_v=0;
for(int i=0;i<prices.size();i++){
sofar+=prices.get(i);
if (sofar<0) {
sofar=0;
}
max_v=Math.max(max_v,sofar);
}
return max_v;
}
static boolean isMemberAC(int a, int d, int x)
{
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static long sum(int[] arr){
long sum=0;
for(int x:arr){
sum+=x;
}
return sum;
}
private static long[] readLongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}
return arr;
}
private static long evenSumFibo(long n){
long l1=0,l2=2;
long sum=0;
while (l2<n) {
long l3=(4*l2)+l1;
sum+=l2;
if(l3>n)break;
l1=l2;
l2=l3;
}
return sum;
}
private static void initializeIO(){
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr){
int max=Integer.MIN_VALUE;
for(int x:arr){
max=Math.max(max,x);
}
return max;
}
private static long maxOfArray(long[] arr){
long max=Long.MIN_VALUE;
for(long x:arr){
max=Math.max(max,x);
}
return max;
}
private static int[][] getIntervals(int n,int m){
int[][] arr=new int[n][m];
for(int j=0;j<n;j++){
for(int i=0;i<m;i++){
arr[j][m]=sc.nextInt();
}
}
return arr;
}
private static int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int[] rintArray(int n){
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n){
double[] arr=new double[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextDouble();
}
return arr;
}
private static long[] rlongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}
return arr;
}
private static String[] rstringArray(int n){
String[] arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLine();
}
return arr;
}
private static void print(int[] arr){
out.println(Arrays.toString(arr));
}
private static void print(long[] arr){
out.println(Arrays.toString(arr));
}
private static void print(String[] arr){
out.println(Arrays.toString(arr));
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | cf087456409889c826d9877e3afe2954 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.lang.reflect.Array;
import java.util.*;
import java.io.*;
//import static com.sun.tools.javac.jvm.ByteCodes.swap;
public class fastTemp {
static FastScanner fs = null;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
sieveOfEratosthenes(2_000_001);
while(t-->0) {
int n = fs.nextInt();
int e = fs.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] = fs.nextInt();
}
long ans = 0;
for(int i=0;i<n;i++){
if(prime[a[i]] && a[i]!=1){
long l=0;
long r=0;
for(int j=i+e;j<n;j+=e){
if(a[j]==1){
r++;
}else{
break;
}
}
for(int j=i-e;j>=0;j-=e){
if(a[j]==1){
l++;
}else{
break;
}
}
ans += r+l+(r*l);
}
}
System.out.println(ans);
}
out.close();
}
public static void union(int a,int b,int rank[],int [] p){
int x = get(a,rank,p);
int y = get(b,rank,p);
if(rank[x]==rank[y]){
rank[x]++;
}
if(rank[x]>rank[y]){
p[y] = x;
}else{
p[x] = y;
}
}
public static int get(int a,int [] rank,int []p){
return p[a] = (p[a]==a)? a : get(p[a],rank,p);
}
public static long[] sort(long arr[]) {
List<Long> list = new ArrayList<>();
for(long i:arr)
list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) {
arr[i] = list.get(i);
}
return arr;
}
static class p {
int id;
int id1;
p(int id,int id1){
this.id=id;
this.id1=id1;
}
}
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(long n, long r, long p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long[] fac = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p;
}
//public static int dijkstra(int src , int dist[] ){
//
//PriorityQueue<Pair> q = new PriorityQueue<>();
//q.add(new Pair(1,0));
//
//while(q.size()>0){
//
// Pair rem = q.remove();
// for(Pair x:graph[rem.y]){
// if(dist[x.y]>dist[rem.y]+x.wt){
// dist[x.y] = dist[rem.y] + x.wt;
// q.add(new Pair(x.y,dist[x.y]));
// }
// }
//
//}
//
//return dist[dist.length-1];
//
//}
// T --> O(n) && S--> O(d)
public static long lower(long arr[], long key) {
int l = 0, r = arr.length-1;
long ans = -1;
while(l<=r) {
int mid = l +(r-l)/2;
if(arr[mid]<=key) {
ans = arr[mid];
l = mid+1;
}
else {
r = mid-1;
}
}
return ans;
}
public static long upper(long arr[], long key) {
int l = 0, r = arr.length-1;
long ans = -1;
while(l<=r) {
int mid = l +(r-l)/2;
if(arr[mid] >= key) {
ans = arr[mid];
r = mid-1;
}
else {
l = mid+1;
}
}
return ans;
}
public static class String1 implements Comparable<String1>{
String str;
int id;
String1(String str , int id){
this.str = str;
this.id = id;
}
public int compareTo(String1 o){
int i=0;
while(i<str.length() && str.charAt(i)==o.str.charAt(i)){
i++;
}
if(i<str.length()){
if(i%2==1){
return o.str.compareTo(str); // descending order
}else{
return str.compareTo(o.str); // ascending order
}
}
return str.compareTo(o.str);
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
// ------------------------------------------swap----------------------------------------------------------------------
static void swap(int arr[],int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
//-------------------------------------------seiveOfEratosthenes----------------------------------------------------
static boolean prime[];
static void sieveOfEratosthenes(int n)
{
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
prime= new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
// for (int i = 2; i <= n; i++)
// {
// if (prime[i] == true)
// System.out.print(i + " ");
// }
}
//------------------------------------------- power------------------------------------------------------------------
public static long power(int a , int b) {
if (b == 0) {
return 1;
} else if (b == 1) {
return a;
} else {
long R = power(a, b / 2);
if (b % 2 != 0) {
return (((power(a, b / 2))) * a * ((power(a, b / 2))));
} else {
return ((power(a, b / 2))) * ((power(a, b / 2)));
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
//---------------------------------------EXTENDED EUCLID ALGO--------------------------------------------------------
// public static class Pair{
// int x;
// int y;
// public Pair(int x,int y){
// this.x = x;
// this.y = y ;
// }
// }
// public static Pair Euclid(int a,int b){
//
// if(b==0){
// return new Pair(1,0); // answer of x and y
// }
//
// Pair dash = Euclid(b,a%b);
//
// return new Pair(dash.y , dash.x - (a/b)*dash.y);
//
//
// }
//--------------------------------GCD------------------GCD-----------GCD--------------------------------------------
public static long gcd(long a,long b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
public static void BFS(ArrayList<Integer>[] graph) {
}
// This is an extension of method 2. Instead of moving one by one, divide the array in different sets
//where number of sets is equal to GCD of n and d and move the elements within sets.
//If GCD is 1 as is for the above example array (n = 7 and d =2), then elements will be moved within one set only, we just start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.
//Here is an example for n =12 and d = 3. GCD is 3 and
// void leftRotate(int arr[], int d, int n)
// {
// /* To handle if d >= n */
// d = d % n;
// int i, j, k, temp;
// int g_c_d = gcd(d, n);
// for (i = 0; i < g_c_d; i++) {
// /* move i-th values of blocks */
// temp = arr[i];
// j = i;
// while (true) {
// k = j + d;
// if (k >= n)
// k = k - n;
// if (k == i)
// break;
// arr[j] = arr[k];
// j = k;
// }
// arr[j] = temp;
// }
// }
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | aa73830a71c56b97859bb548dee4cbe9 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static long startTime = System.currentTimeMillis();
// for global initializations and methods starts here
static int N = (int) (2e6) + 5;
//static int counter = 0;
static boolean[] primes = new boolean[N + 1];
static List<Integer> prime = new ArrayList<>();
static void seive() {
for (int i = 0; i <= N; i++) primes[i] = true;
primes[0] = primes[1] = false;
for (int p = 2; p * p <= N; p++) {
if (primes[p]) {
for (int i = p * p; i <= N; i += p) {
primes[i] = false;
}
}
}
for (int i = 2; i <= N; i++) if (primes[i]) prime.add(i);
}
// global initialisations and methods end here
static void run() {
boolean tc = true;
AdityaFastIO r = new AdityaFastIO();
//FastReader r = new FastReader();
try (OutputStream out = new BufferedOutputStream(System.out)) {
//long startTime = System.currentTimeMillis();
int testcases = tc ? r.ni() : 1;
int tcCounter = 1;
// Hold Here Sparky------------------->>>
// Solution Starts Here
seive();
start:
while (testcases-- > 0) {
int n = r.ni();
int e = r.ni();
int[] arr = new int[n + 1];
for (int i = 1; i <= n; i++) arr[i] = r.ni();
long res = 0;
int pr = 1;
while (pr <= e) {
List<Integer> ans = new ArrayList<>();
for (int i = pr; i <= n; i += e) {
if (primes[arr[i]]) ans.add(2);
else if (arr[i] == 1) ans.add(1);
else ans.add(0);
}
int count = 0;
List<Pair> al = new ArrayList<>();
for (int ele : ans) {
if (ele == 1) count++;
else {
if (count > 0) al.add(new Pair(count, 1));
if (ele == 0) al.add(new Pair(0, 0));
else al.add(new Pair(0, 2));
count = 0;
}
}
if (count > 0) al.add(new Pair(count, 1));
for (int i = 0; i < al.size(); i++) {
if (al.get(i).second == 1) {
if (i + 1 < al.size() && al.get(i + 1).second == 2) res += al.get(i).first;
if (i + 1 < al.size() && al.get(i + 1).second == 2 && i + 2 < al.size() && al.get(i + 2).second == 1)
res += (long) al.get(i).first * al.get(i + 2).first;
}
if (al.get(i).second == 2 && i + 1 < al.size() && al.get(i + 1).second == 1)
res += al.get(i + 1).first;
}
pr++;
}
out.write((res + " ").getBytes());
out.write(("\n").getBytes());
}
// Solution Ends Here
} catch (IOException e) {
e.printStackTrace();
}
}
static class AdityaFastIO {
final private int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public BufferedReader br;
public StringTokenizer st;
public AdityaFastIO() {
br = new BufferedReader(new InputStreamReader(System.in));
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public AdityaFastIO(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String word() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String line() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public String readLine() throws IOException {
byte[] buf = new byte[100000001]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int ni() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public long nl() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) return -ret;
return ret;
}
public double nd() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') c = read();
boolean neg = (c == '-');
if (neg) c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10);
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null) return;
din.close();
}
}
public static void main(String[] args) throws Exception {
run();
}
static long mod = 998244353;
static long modInv(long base, long e) {
long result = 1;
base %= mod;
while (e > 0) {
if ((e & 1) > 0) result = result * base % mod;
base = base * base % mod;
e >>= 1;
}
return result;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String word() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String line() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int ni() {
return Integer.parseInt(word());
}
long nl() {
return Long.parseLong(word());
}
double nd() {
return Double.parseDouble(word());
}
}
static int MOD = (int) (1e9 + 7);
static long powerLL(long x, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 1) result = result * x % MOD;
n = n / 2;
x = x * x % MOD;
}
return result;
}
static long powerStrings(int i1, int i2) {
String sa = String.valueOf(i1);
String sb = String.valueOf(i2);
long a = 0, b = 0;
for (int i = 0; i < sa.length(); i++) a = (a * 10 + (sa.charAt(i) - '0')) % MOD;
for (int i = 0; i < sb.length(); i++) b = (b * 10 + (sb.charAt(i) - '0')) % (MOD - 1);
return powerLL(a, b);
}
static long gcd(long a, long b) {
if (a == 0) return b;
else return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static long lower_bound(int[] arr, int x) {
int l = -1, r = arr.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (arr[m] >= x) r = m;
else l = m;
}
return r;
}
static int upper_bound(int[] arr, int x) {
int l = -1, r = arr.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (arr[m] <= x) l = m;
else r = m;
}
return l + 1;
}
static void addEdge(ArrayList<ArrayList<Integer>> graph, int edge1, int edge2) {
graph.get(edge1).add(edge2);
graph.get(edge2).add(edge1);
}
public static class Pair implements Comparable<Pair> {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(Pair o) {
// TODO Auto-generated method stub
if (this.first != o.first)
return (int) (this.first - o.first);
else return (int) (this.second - o.second);
}
}
public static class PairC<X, Y> implements Comparable<PairC> {
X first;
Y second;
public PairC(X first, Y second) {
this.first = first;
this.second = second;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(PairC o) {
// TODO Auto-generated method stub
return o.compareTo((PairC) first);
}
}
static boolean isCollectionsSorted(List<Long> list) {
if (list.size() == 0 || list.size() == 1) return true;
for (int i = 1; i < list.size(); i++) if (list.get(i) <= list.get(i - 1)) return false;
return true;
}
static boolean isCollectionsSortedReverseOrder(List<Long> list) {
if (list.size() == 0 || list.size() == 1) return true;
for (int i = 1; i < list.size(); i++) if (list.get(i) >= list.get(i - 1)) return false;
return true;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 699046c5f3da168edc60a1de6a63d62c | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class Division {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = nextInt();
boolean chisla[] = new boolean[1000010];
TreeSet<Integer> set = new TreeSet<>();
for (int j = 2; j < chisla.length; j++) {
if (!chisla[j]) {
set.add(j);
int copy = j + j;
for (int k = copy; k < chisla.length; k += j) {
chisla[k] = true;
}
}
}
for (int i = 0; i < t; i++) {
int a = nextInt();
int e = nextInt();
int mas[] = new int[a];
for (int j = 0; j < a; j++) {
mas[j] = nextInt();
}
long ans = 0;
for (int j = 0; j < mas.length; j++) {
if (set.contains(mas[j])) {
int c = 1;
for (int k = j + e; k < mas.length; k += e) {
if (mas[k] == 1) {
ans++;
c++;
}else break;
}
long cc = c;
for (int k = j - e; k>=0; k -= e) {
if (mas[k] == 1) {
ans += cc;
}else break;
}
}
}
System.out.println(ans);
}
}
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
static StringTokenizer in = new StringTokenizer("");
public static boolean hasNext() throws IOException {
if (in.hasMoreTokens()) return true;
String s;
while ((s = br.readLine()) != null) {
in = new StringTokenizer(s);
if (in.hasMoreTokens()) return true;
}
return false;
}
public static String nextToken() throws IOException {
while (!in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 429c14bb51496f7da59ea54749ae934a | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
static BufferedReader br;
static StringTokenizer st;
static PrintWriter pw;
static String nextToken() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
static int nextInt() {
return Integer.parseInt(nextToken());
}
static long nextLong() {
return Long.parseLong(nextToken());
}
static double nextDouble() {
return Double.parseDouble(nextToken());
}
static String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
throw new IllegalArgumentException();
}
}
static char nextChar() {
try {
return (char) br.read();
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
int t = 1;
t = nextInt();
while (t-- > 0) {
solve();
}
pw.close();
}
private static void solve() {
int n = nextInt();
int e = nextInt();
long ans = 0L;
int[][] arr = new int[e][3];
for (int i = 0; i < e; i++) {
arr[i][0] = -1;
}
for (int i = 0; i < n; i++) {
int t = nextInt();
if (i < e) {
if (t == 1) {
arr[i][2]++;
}
if (t > 1) {
if (isPrime(t)) {
arr[i][0] = t;
}
}
continue;
}
int id = i % e;
if (t == 1) {
if (arr[id][0] != -1) {
ans++;
ans += arr[id][1];
}
arr[id][2]++;
}
if (t > 1) {
if (isPrime(t)) {
ans += arr[id][2];
arr[id][1] = arr[id][2];
arr[id][2] = 0;
arr[id][0] = t;
} else {
arr[id][1] = 0;
arr[id][2] = 0;
arr[id][0] = -1;
}
}
}
pw.println(ans);
pw.flush();
}
private static boolean isPrime(int t) {
if (t == 2) return true;
for (int i = 2; i < Math.sqrt(t) + 1; i++) {
if (t % i == 0) return false;
}
return true;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | bc8ccb44ecb7e61ff6244a88a8c075bb | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /**
* check out my youtube channel sh0rkyboy
* https://tinyurl.com/zdxe2y4z
* I do screencasts, solutions, and other fun stuff in the future
*/
import java.util.*;
import java.io.*;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.Math.max;
public class EdA {
static long[] mods = {1000000007, 998244353, 1000000009};
static long mod = mods[0];
public static MyScanner sc;
public static PrintWriter out;
public static void main(String[] largewang) throws Exception{
// TODO Auto-generated method stub
sc = new MyScanner();
out = new PrintWriter(System.out);
int[] prime = prime(1000000);
int t = sc.nextInt();
while (t-->0) {
int n = sc.nextInt();
int e = sc.nextInt();
long ans = 0;
int[] arr = readArrayInt(n);
for(int j = 0;j<e;j++){
int prevOne = 0;
int currOne = 0;
int prevNumber = 0;
int currNumber = 0;
boolean isNoneOne = false;
for(int k = j;k<n;k+=e) {
if (arr[k] != 1){
isNoneOne = true;
if (prime[currNumber] == 1) {
ans += (long)((long)prevOne*(long)currOne + (long)prevOne + (long)currOne);
}
prevNumber = currNumber;
prevOne = currOne;
currNumber = arr[k];
currOne = 0;
} else {
currOne++;
}
}
if (isNoneOne && prime[currNumber] == 1)
ans += (long)((long)prevOne*(long)currOne + (long)prevOne + (long)currOne);
}
out.println(ans);
}
out.close();
}
public static int[] prime(int n){
int[] isPrime = new int[n+1];
Arrays.fill(isPrime, 1);
for(int i = 2;i<=n;i++){
if (isPrime[i] == 1){
for(long j = (long)i*(long)i;j<=n;j+=i){
isPrime[(int)j] = 0;
}
}
}
isPrime[0] = 0;
isPrime[1] = 0;
return isPrime;
}
public static void sort(int[] array){
ArrayList<Integer> copy = new ArrayList<>();
for (int i : array)
copy.add(i);
Collections.sort(copy);
for(int i = 0;i<array.length;i++)
array[i] = copy.get(i);
}
static String[] readArrayString(int n){
String[] array = new String[n];
for(int j =0 ;j<n;j++)
array[j] = sc.next();
return array;
}
static int[] readArrayInt(int n){
int[] array = new int[n];
for(int j = 0;j<n;j++)
array[j] = sc.nextInt();
return array;
}
static int[] readArrayInt1(int n){
int[] array = new int[n+1];
for(int j = 1;j<=n;j++){
array[j] = sc.nextInt();
}
return array;
}
static long[] readArrayLong(int n){
long[] array = new long[n];
for(int j =0 ;j<n;j++)
array[j] = sc.nextLong();
return array;
}
static double[] readArrayDouble(int n){
double[] array = new double[n];
for(int j =0 ;j<n;j++)
array[j] = sc.nextDouble();
return array;
}
static int minIndex(int[] array){
int minValue = Integer.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static int minIndex(long[] array){
long minValue = Long.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static int minIndex(double[] array){
double minValue = Double.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static long power(long x, long y){
if (y == 0)
return 1;
if (y%2 == 1)
return (x*power(x, y-1))%mod;
return power((x*x)%mod, y/2)%mod;
}
static void verdict(boolean a){
out.println(a ? "YES" : "NO");
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | eada94fdfe56f749bfc9281a9891ff67 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /*
Author:-crazy_coder-
*/
import java.io.*;
import java.util.*;
public class cp{
static long mod=1000000007;
static int ans=0;
static int[] par=new int[100005];
static int[] rank=new int[100005];
static BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
static PrintWriter pw=new PrintWriter(System.out);
public static void main(String[] args)throws Exception{
int T=Integer.parseInt(br.readLine());
// int t=1;
// comp();
boolean isPrime[]=sieveOfEratosthenes(1000006);
while(T-->0){
solve(isPrime);
}
pw.flush();
}
public static void solve(boolean[] isPrime)throws Exception{
String[] str=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int e=Integer.parseInt(str[1]);
int[] arr=new int[n];
str=br.readLine().split(" ");
for(int i=0;i<n;i++)arr[i]=Integer.parseInt(str[i]);
long ans=0l;
for(int i=0;i<n;i++){
long l=0l,r=0l;
int temp=e;
if(isPrime[arr[i]]){
while(i-temp>=0&&arr[i-temp]==1){l++;temp+=e;}
temp=e;
while(i+temp<n&&arr[i+temp]==1){r++;temp+=e;}
}
ans+=(l+r+l*r);
}
pw.println(ans);
}
public static boolean[] sieveOfEratosthenes(int n){
boolean isPrime[]=new boolean[n+1];
Arrays.fill(isPrime,true);
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i*i<n;i++){
for(int j=i*i;j<=n;j+=i){
isPrime[j]=false;
}
}
return isPrime;
}
public static long NCR(int n,int r){
if(r==0||r==n)return 1;
return (fac[n]*invfact[r]%mod)*invfact[n-r]%mod;
}
static long[] fac=new long[100];
static long[] invfact=new long[100];
public static void comp(){
fac[0]=1;
invfact[0]=1;
for(int i=1;i<100;i++){
fac[i]=(fac[i-1]*i)%mod;
invfact[i]=modInverse(fac[i]);
}
}
public static long modInverse(long n){
return power(n,mod-2);
}
public static long power(long x,long y){
long res=1;
x=x%mod;
while(y>0){
if((y&1)==1)res=(res*x)%mod;
y=y>>1;
x=(x*x)%mod;
}
return res;
}
//*************************************DSU************************************ */
public static void initialize(int n){
for(int i=1;i<=n;i++){
par[i]=i;
rank[i]=1;
}
}
public static void union(int x,int y){
int lx=find(x);
int ly=find(y);
if(lx!=ly){
if(rank[lx]>rank[ly]){
par[ly]=lx;
}else if(rank[lx]<rank[ly]){
par[lx]=ly;
}else{
par[lx]=ly;
rank[ly]++;
}
}
}
public static int find(int x){
if(par[x]==x){
return x;
}
int temp=find(par[x]);
par[x]=temp;
return temp;
}
//************************************DSU END********************************** */
public static boolean isPresent(ArrayList<Long> zero,long val){
for(long x:zero){
if(x==val)return true;
}
return false;
}
public static class Pair implements Comparable<Pair>{
int val;
int k;
Pair(int val,int k){
this.val=val;
this.k=k;
}
public int compareTo(Pair o){
return this.val-o.val;
}
}
public static int countDigit(int x){
return (int)Math.log10(x)+1;
}
//****************************function to find all factor*************************************************
public static ArrayList<Long> findAllFactors(long num){
ArrayList<Long> factors = new ArrayList<Long>();
for(long i = 1; i <= num/i; ++i) {
if(num % i == 0) {
//if i is a factor, num/i is also a factor
factors.add(i);
factors.add(num/i);
}
}
//sort the factors
Collections.sort(factors);
return factors;
}
//*************************** function to find GCD of two number*******************************************
public static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 8d54083621db38024e20787a3a3c9c45 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class Analysis {
static BufferedReader bf;
static PrintWriter out;
static Scanner sc;
static StringTokenizer st;
public static void main (String[] args)throws IOException {
bf = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new Scanner(System.in);
int t = Integer.parseInt(bf.readLine());
boolean primes[] = new boolean[1000001];
primes[0] = true;
primes[1] = true;
for(int i = 2;i*i<primes.length;i++){
for(int j = i*2;j<primes.length;j+=i){
if(primes[j])continue;
primes[j] = true;
}
}
while(t-->0){
solve(primes);
}
}
public static void solve(boolean[]primes)throws IOException{
int n = nextInt();
int e = nextInt();
long[]arr = new long[n];
for(int i =0;i<n;i++){
arr[i] = nextLong();
}
long count =0;
for(int i=0;i<n;i++){
if(!primes[(int)arr[i]]){
long leftCount = 0;
long rightCount = 0;
for(int j = i-e;j>=0;j-=e){
if(arr[j] != 1)break;
leftCount++;
}
for(int j = i+e;j<n;j+=e){
if(arr[j] != 1)break;
rightCount++;
}
count += (leftCount * (1+rightCount));
count += rightCount;
}
}
println(count);
}
// code for input
public static void print(String s ){
System.out.print(s);
}
public static void print(int num ){
System.out.print(num);
}
public static void print(long num ){
System.out.print(num);
}
public static void println(String s){
System.out.println(s);
}
public static void println(int num){
System.out.println(num);
}
public static void println(long num){
System.out.println(num);
}
public static void println(){
System.out.println();
}
public static int Int(String s){
return Integer.parseInt(s);
}
public static long Long(String s){
return Long.parseLong(s);
}
public static String[] nextStringArray()throws IOException{
return bf.readLine().split(" ");
}
public static String nextString()throws IOException{
return bf.readLine();
}
public static long[] nextLongArray(int n)throws IOException{
String[]str = bf.readLine().split(" ");
long[]arr = new long[n];
for(int i =0;i<n;i++){
arr[i] = Long.parseLong(str[i]);
}
return arr;
}
public static int[][] newIntMatrix(int r,int c)throws IOException{
int[][]arr = new int[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Integer.parseInt(str[j]);
}
}
return arr;
}
public static long[][] newLongMatrix(int r,int c)throws IOException{
long[][]arr = new long[r][c];
for(int i =0;i<r;i++){
String[]str = bf.readLine().split(" ");
for(int j =0;j<c;j++){
arr[i][j] = Long.parseLong(str[j]);
}
}
return arr;
}
static class pair{
int one;
int two;
pair(int one,int two){
this.one = one ;
this.two =two;
}
}
public static long gcd(long a,long b){
if(b == 0)return a;
return gcd(b,a%b);
}
public static long lcm(long a,long b){
return (a*b)/(gcd(a,b));
}
public static boolean isPalindrome(String s){
int i = 0;
int j = s.length()-1;
while(i<=j){
if(s.charAt(i) != s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
// these functions are to calculate the number of smaller elements after self
public static void sort(int[]arr,int l,int r){
if(l < r){
int mid = (l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
smallerNumberAfterSelf(arr, l, mid, r);
}
}
public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){
int n1 = mid - l +1;
int n2 = r - mid;
int []a = new int[n1];
int[]b = new int[n2];
for(int i = 0;i<n1;i++){
a[i] = arr[l+i];
}
for(int i =0;i<n2;i++){
b[i] = arr[mid+i+1];
}
int i = 0;
int j =0;
int k = l;
while(i<n1 && j < n2){
if(a[i] < b[j]){
arr[k++] = a[i++];
}
else{
arr[k++] = b[j++];
}
}
while(i<n1){
arr[k++] = a[i++];
}
while(j<n2){
arr[k++] = b[j++];
}
}
public static String next(){
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(bf.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
static int nextInt() {
return Integer.parseInt(next());
}
static long nextLong() {
return Long.parseLong(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
static String nextLine(){
String str = "";
try {
str = bf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// use some math tricks it might help
// sometimes just try to think in straightforward plan in A and B problems don't always complecate the questions with thinking too much differently
// always use long number to do 10^9+7 modulo
// if a problem is related to binary string it could also be related to parenthesis
// *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work******
// try sorting
// try to think in opposite direction of question it might work in your way
// if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general
// if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work.
// in range query sums try to do binary search it could work
// analyse the time complexity of program thoroughly
// anylyse the test cases properly
// if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required
// try to do the opposite operation of what is given in the problem
//think about the base cases properly
//If a question is related to numbers try prime factorisation or something related to number theory
// keep in mind unique strings
//you can calculate the number of inversion in O(n log n)
// in a matrix you could sometimes think about row and cols indenpendentaly. | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 8fcb5a8d67cc01901def3d3e521b388d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
// A -> CodeForcesProblemSet
public final class A {
static FastReader fr = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static final int gigamod = 1000000007;
static int t = 1;
static double epsilon = 0.0000001;
public static void main(String[] args) {
boolean[] isPrime = primeGenerator(1000001);
t = fr.nextInt();
OUTER:
for (int tc = 0; tc < t; tc++) {
int n = fr.nextInt(), e = fr.nextInt();
int[] arr = fr.nextIntArray(n);
// Observations:
// 1. The whole product can be a prime number only when there are
// all ones except an element which will be a prime.
ArrayList<Integer>[] idxModLists = (ArrayList<Integer>[]) new ArrayList[e];
for (int i = 0; i < e; i++)
idxModLists[i] = new ArrayList<>();
for (int i = 0; i < n; i++)
idxModLists[i % e].add(arr[i]);
/*for (int i = 0; i < e; i++) {
for (int elem : idxModLists[i])
out.print(elem + " ");
out.println();
}*/
// Now, we will process each list individually.
long numPairs = 0;
for (ArrayList<Integer> list : idxModLists) {
int s = list.size();
for (int i = 0; i < s; i++) {
int elem = list.get(i);
if (elem != 1 && !isPrime[elem]) continue;
int j = i;
int primeIdx = -1;
if (isPrime[elem]) primeIdx = i;
// We need a chain of ones, then a prime and then ones.
while (primeIdx == -1 && j < s - 1 && (list.get(j + 1) == 1 || isPrime[list.get(j + 1)])) {
j++;
if (isPrime[list.get(j)])
primeIdx = j;
}
// If we didn't find the prime at all,
if (primeIdx == -1) {
i = j;
continue;
}
// Now we will only consider ones.
while (j < s - 1 && list.get(j + 1) == 1)
j++;
// 'k' has to be at least one.
if (i == primeIdx && j == i)
continue;
// The starting positions will be [i, primeIdx - 1].
// The ending positions will be [primeIdx, j].
numPairs += (long) (primeIdx - i) * (j + 1 - primeIdx);
// If j != primeIdx, we can start from primeIdx as well.
if (j > primeIdx)
numPairs += (j - primeIdx);
i = primeIdx;
}
// out.println(numPairs);
}
out.println(numPairs);
}
out.close();
}
// Manual implementation of RB-BST for C++ PBDS functionality.
// Modification required according to requirements.
public static final class RedBlackCountSet {
// Colors for Nodes:
private static final boolean RED = true;
private static final boolean BLACK = false;
// Pointer to the root:
private Node root;
// Public constructor:
public RedBlackCountSet() {
root = null;
}
// Node class for storing key value pairs:
private class Node {
long key;
long value;
Node left, right;
boolean color;
long size; // Size of the subtree rooted at that node.
public Node(long key, long value, boolean color) {
this.key = key;
this.value = value;
this.left = this.right = null;
this.color = color;
this.size = value;
}
}
/***** Invariant Maintenance functions: *****/
// When a temporary 4 node is formed:
private Node flipColors(Node node) {
node.left.color = node.right.color = BLACK;
node.color = RED;
return node;
}
// When there's a right leaning red link and it is not a 3 node:
private Node rotateLeft(Node node) {
Node rn = node.right;
node.right = rn.left;
rn.left = node;
rn.color = node.color;
node.color = RED;
return rn;
}
// When there are 2 red links in a row:
private Node rotateRight(Node node) {
Node ln = node.left;
node.left = ln.right;
ln.right = node;
ln.color = node.color;
node.color = RED;
return ln;
}
/***** Invariant Maintenance functions end *****/
// Public functions:
public void put(long key) {
root = put(root, key);
root.color = BLACK; // One of the invariants.
}
private Node put(Node node, long key) {
// Standard insertion procedure:
if (node == null)
return new Node(key, 1, RED);
// Recursing:
int cmp = Long.compare(key, node.key);
if (cmp < 0)
node.left = put(node.left, key);
else if (cmp > 0)
node.right = put(node.right, key);
else
node.value++;
// Invariant maintenance:
if (node.left != null && node.right != null) {
if (node.right.color = RED && node.left.color == BLACK)
node = rotateLeft(node);
if (node.left.color == RED && node.left.left.color == RED)
node = rotateRight(node);
if (node.left.color == RED && node.right.color == RED)
node = flipColors(node);
}
// Property maintenance:
node.size = node.value + size(node.left) + size(node.right);
return node;
}
private long size(Node node) {
if (node == null)
return 0;
else
return node.size;
}
public long rank(long key) {
return rank(root, key);
}
// Modify according to requirements.
private long rank(Node node, long key) {
if (node == null) return 0;
int cmp = Long.compare(key, node.key);
if (cmp < 0)
return rank(node.left, key);
else if (cmp > 0)
return node.value + size(node.left) + rank(node.right, key);
else
return size(node.left);
}
}
static long modDiv(long a, long b) {
return mod(a * power(b, gigamod - 2));
}
static class SegTree {
// Setting up the parameters.
int n;
long[] sumTree;
long[] ogArr;
long[] maxTree;
long[] minTree;
public static final int sumMode = 0;
public static final int maxMode = 1;
public static final int minMode = 2;
// Constructor
public SegTree(long[] arr, int mode) {
if (mode == sumMode) {
Arrays.fill(sumTree = new long[4 * (n = ((ogArr = arr.clone()).length))], -1);
buildSum(0, n - 1, 0);
} else if (mode == maxMode) {
Arrays.fill(maxTree = new long[4 * (n = ((ogArr = arr.clone()).length))], -1);
buildMax(0, n - 1, 0);
} else if (mode == minMode) {
Arrays.fill(minTree = new long[4 * (n = ((ogArr = arr.clone()).length))], Long.MAX_VALUE - 1);
buildMin(0, n - 1, 0);
}
}
/**********Code for sum***********/
/*********************************/
// Build the segment tree node-by-node
private long buildSum(int l, int r, int segIdx) {
return sumTree[segIdx] = (l == r) ? ogArr[l] :
(buildSum(l, l + (r - l) / 2, 2 * segIdx + 1)
+ buildSum((l + (r - l) / 2) + 1, r, 2 * segIdx + 2));
}
// Change a single element
public void changeForSum(int idx, long to) {
changeForSum(idx, to, 0, n - 1, 0);
}
private long changeForSum(int idx, long to, int l, int r, int segIdx) {
return sumTree[segIdx] =
((l == r) ? (ogArr[idx] = to) :
((0 * ((idx <= (l + (r - l) / 2))
? changeForSum(idx, to, l, l + (r - l) / 2, 2 * segIdx + 1)
: changeForSum(idx, to, (l + (r - l) / 2) + 1, r, 2 * segIdx + 2)))
+ sumTree[2 * segIdx + 1] + sumTree[2 * segIdx + 2]));
}
// Return the sum arr[l, r]
public long segSum(int l, int r) {
if (l > r) return 0;
return segSum(l, r, 0, n - 1, 0);
}
private long segSum(int segL, int segR, int l, int r, int segIdx) {
if (segL == l && segR == r)
return sumTree[segIdx];
if (segR <= l + (r - l) / 2)
return segSum(segL, segR, l, l + (r - l) / 2, 2 * segIdx + 1);
if (segL >= l + (r - l) / 2 + 1)
return segSum(segL, segR, l + (r - l) / 2 + 1, r, 2 * segIdx + 2);
return segSum(segL, l + (r - l) / 2, l, l + (r - l) / 2, 2 * segIdx + 1)
+ segSum(l + (r - l) / 2 + 1, segR, l + (r - l) / 2 + 1, r, 2 * segIdx + 2);
}
/********End of code for sum********/
/***********************************/
/*******Start of code for max*******/
/***********************************/
// Build the max tree node-by-node
private long buildMax(int l, int r, int segIdx) {
return maxTree[segIdx] = (l == r) ? ogArr[l] :
Math.max(buildMax(l, (l + (r - l) / 2), 2 * segIdx + 1),
buildMax(l + (r - l) / 2 + 1, r, 2 * segIdx + 2));
}
// Change a single element
public void changeForMax(int idx, long to) {
changeForMax(0, n - 1, idx, to, 0);
}
private long changeForMax(int l, int r, int idx, long to, int segIdx) {
return maxTree[segIdx] = (l == r && l == idx) ? (ogArr[idx] = to) :
Math.max(changeForMax(l, l + (r - l) / 2, idx, to, 2 * segIdx + 1),
changeForMax(l + (r - l) / 2 + 1, r, idx, to, 2 * segIdx + 2));
}
public long segMax(int segL, int segR) {
if (segL > segR) return 0;
return segMax(0, n - 1, segL, segR, 0);
}
private long segMax(int l, int r, int segL, int segR, int segIdx) {
int midL = l + (r - l) / 2;
if (segL == segR && segL == l)
return ogArr[segL];
if (segR <= midL)
return segMax(l, midL, segL, segR, 2 * segIdx + 1);
if (segL >= midL + 1)
return segMax(midL + 1, r, segL, segR, 2 * segIdx + 2);
return Math.max(segMax(l, midL, segL, midL, 2 * segIdx + 1),
segMax(midL + 1, r, midL + 1, segR, 2 * segIdx + 2));
}
/*******End of code for max*********/
/***********************************/
/*******Start of code for min*******/
/***********************************/
private long buildMin(int l, int r, int segIdx) {
return minTree[segIdx] = (l == r) ? ogArr[l] :
Math.min(buildMin(l, (l + (r - l) / 2), 2 * segIdx + 1),
buildMin(l + (r - l) / 2 + 1, r, 2 * segIdx + 2));
}
// Change a single element
public void changeForMin(int idx, long to) {
changeForMin(0, n - 1, idx, to, 0);
}
private long changeForMin(int l, int r, int idx, long to, int segIdx) {
return minTree[segIdx] = (l == r && l == idx) ? (ogArr[idx] = to) :
Math.min(changeForMin(l, l + (r - l) / 2, idx, to, 2 * segIdx + 1),
changeForMin(l + (r - l) / 2 + 1, r, idx, to, 2 * segIdx + 2));
}
public long segMin(int segL, int segR) {
if (segL > segR) return 0;
return segMin(0, n - 1, segL, segR, 0);
}
private long segMin(int l, int r, int segL, int segR, int segIdx) {
int midL = l + (r - l) / 2;
if (segL == segR && segL == l)
return ogArr[segL];
if (segR <= midL)
return segMin(l, midL, segL, segR, 2 * segIdx + 1);
if (segL >= midL + 1)
return segMin(midL + 1, r, segL, segR, 2 * segIdx + 2);
return Math.min(segMin(l, midL, segL, midL, 2 * segIdx + 1),
segMin(midL + 1, r, midL + 1, segR, 2 * segIdx + 2));
}
/*******End of code for min*********/
/***********************************/
public String toString() {
return A.toString(minTree);
}
}
static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) {
if (m > limit) return;
if (m <= limit && n <= limit)
coprimes.add(new Point(m, n));
if (coprimes.size() > numCoprimes) return;
coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes);
coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes);
coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes);
}
// Returns the length of the longest prefix that is also
// a suffix (no overlap).
static int longestPrefixSuffix(String s) {
int n = s.length();
int[] arr = new int[n];
arr[0] = 0;
int len = 0;
for (int i = 1; i < n;) {
if (s.charAt(len) == s.charAt(i)) {
len++;
arr[i++] = len;
} else {
if (len != 0) {
len = arr[len - 1];
} else {
arr[i] = 0;
i++;
}
}
}
return arr[n - 1];
}
static long hash(long i) {
return (i * 2654435761L % gigamod);
}
static long hash2(long key) {
long h = Long.hashCode(key);
h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4);
return h & (gigamod-1);
}
static long power(long x, long y)
{
// int p = 998244353;
int p = gigamod;
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long power2(long x, long y)
{
// int p = 998244353;
// int p = gigamod;
long res = 1; // Initialize result
// x = x /*% p*/; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) /*% p*/;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) /*% p*/;
}
return res;
}
// Maps elements in a 2D matrix serially to elements in
// a 1D array.
static int mapTo1D(int row, int col, int n, int m) {
return row * m + col;
}
// Inverse of what the one above does.
static int[] mapTo2D(int idx, int n, int m) {
int[] rnc = new int[2];
rnc[0] = idx / m;
rnc[1] = idx % m;
return rnc;
}
// Checks if s has subsequence t.
static boolean hasSubsequence(String s, String t) {
char[] schars = s.toCharArray();
char[] tchars = t.toCharArray();
int slen = schars.length, tlen = tchars.length;
int tctr = 0;
if (slen < tlen) return false;
for (int i = 0; i < slen || i < tlen; i++) {
if (tctr == tlen) break;
if (schars[i] == tchars[tctr]) {
tctr++;
}
}
if (tctr == tlen) return true;
return false;
}
// Returns the binary string of length at least bits.
static String toBinaryString(long num, int bits) {
StringBuilder sb = new StringBuilder(Long.toBinaryString(num));
sb.reverse();
for (int i = sb.length(); i < bits; i++)
sb.append('0');
return sb.reverse().toString();
}
@SuppressWarnings("serial")
static class CountMap<T> extends TreeMap<T, Integer>{
CountMap() {
}
CountMap(T[] arr) {
this.putCM(arr);
}
public Integer putCM(T key) {
if (super.containsKey(key)) {
return super.put(key, super.get(key) + 1);
} else {
return super.put(key, 1);
}
}
public Integer removeCM(T key) {
Integer count = super.get(key);
if (count == null) return -1;
if (count == 1)
return super.remove(key);
else
return super.put(key, super.get(key) - 1);
}
public Integer getCM(T key) {
Integer count = super.get(key);
if (count == null)
return 0;
return count;
}
public void putCM(T[] arr) {
for (T l : arr)
this.putCM(l);
}
}
static class Point implements Comparable<Point> {
long x;
long y;
int id;
Point() {
x = y = id = 0;
}
Point(Point p) {
this.x = p.x;
this.y = p.y;
this.id = p.id;
}
/*Point(double a, double b) {
x = a;
y = b;
}*/
Point(long a, long b, int id) {
this.x = a;
this.y = b;
this.id = id;
}
Point(long a, long b) {
this.x = a;
this.y = b;
}
@Override
public int compareTo(Point o) {
if (this.x > o.x)
return 1;
if (this.x < o.x)
return -1;
if (this.y > o.y)
return 1;
if (this.y < o.y)
return -1;
return 0;
}
public boolean equals(Point that) {
return this.compareTo(that) == 0;
}
}
static class PointComparator implements Comparator<Point> {
@Override
public int compare(Point o1, Point o2) {
// Comparision will be done on the basis of the start
// of the segment and then on the length of the segment.
if (o1.id > o2.id)
return -1;
if (o1.id < o2.id)
return 1;
return 0;
}
}
static double distToOrigin(Point p1) {
return Math.sqrt(Math.pow(p1.y, 2) + Math.pow(p1.x, 2));
}
static double distance(Point p1, Point p2) {
double y2 = p2.y, y1 = p1.y;
double x2 = p2.x, x1 = p1.x;
double distance = Math.sqrt(Math.pow(y2-y1, 2) + Math.pow(x2-x1, 2));
return distance;
}
// Returns the largest power of k that fits into n.
static int largestFittingPower(long n, long k) {
int lo = 0, hi = logk(Long.MAX_VALUE, 3);
int largestPower = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
long val = (long) Math.pow(k, mid);
if (val <= n) {
largestPower = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return largestPower;
}
// Returns map of factor and its power in the number.
static HashMap<Long, Integer> primeFactorization(long num) {
HashMap<Long, Integer> map = new HashMap<>();
while (num % 2 == 0) {
num /= 2;
Integer pwrCnt = map.get(2L);
map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1);
}
for (long i = 3; i * i <= num; i += 2) {
while (num % i == 0) {
num /= i;
Integer pwrCnt = map.get(i);
map.put(i, pwrCnt != null ? pwrCnt + 1 : 1);
}
}
// If the number is prime, we have to add it to the
// map.
if (num != 1)
map.put(num, 1);
return map;
}
// Returns map of factor and its power in the number.
static HashMap<Integer, Integer> primeFactorization(int num) {
HashMap<Integer, Integer> map = new HashMap<>();
while (num % 2 == 0) {
num /= 2;
Integer pwrCnt = map.get(2);
map.put(2, pwrCnt != null ? pwrCnt + 1 : 1);
}
for (int i = 3; i * i <= num; i += 2) {
while (num % i == 0) {
num /= i;
Integer pwrCnt = map.get(i);
map.put(i, pwrCnt != null ? pwrCnt + 1 : 1);
}
}
// If the number is prime, we have to add it to the
// map.
if (num != 1)
map.put(num, 1);
return map;
}
static HashSet<Long> divisors(long num) {
HashSet<Long> divisors = new HashSet<Long>();
divisors.add(1L);
divisors.add(num);
for (long i = 2; i * i <= num; i++) {
if (num % i == 0) {
divisors.add(num/i);
divisors.add(i);
}
}
return divisors;
}
// Returns the index of the first element
// larger than or equal to val.
static int bsearch(int[] arr, int val, int lo, int hi) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
idx = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
return idx;
}
static int bsearch(long[] arr, long val, int lo, int hi) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
idx = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
return idx;
}
// Returns the index of the last element
// smaller than or equal to val.
static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] > val) {
hi = mid - 1;
} else {
idx = mid;
lo = mid + 1;
}
}
return idx;
}
static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] > val) {
hi = mid - 1;
} else {
idx = mid;
lo = mid + 1;
}
}
return idx;
}
static long factorial(long n) {
if (n <= 1)
return 1;
long factorial = 1;
for (int i = 1; i <= n; i++)
factorial = mod(factorial * i);
return factorial;
}
static long factorialInDivision(long a, long b) {
if (a == b)
return 1;
if (b < a) {
long temp = a;
a = b;
b = temp;
}
long factorial = 1;
for (long i = a + 1; i <= b; i++)
factorial = mod(factorial * i);
return factorial;
}
static BigInteger factorialInDivision(BigInteger a, BigInteger b) {
if (a.equals(b))
return BigInteger.ONE;
return a.multiply(factorialInDivision(a.subtract(BigInteger.ONE), b));
}
static long nCr(long n, long r) {
long p = gigamod;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long fac[] = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)n] * modInverse(fac[(int)r], p) % p
* modInverse(fac[(int)n - (int)r], p) % p) % p;
}
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
static long power(long x, long y, long p) {
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long nPr(long n, long r) {
return factorialInDivision(n, n - r);
}
static int log2(long n) {
return (int)(Math.log(n) / Math.log(2));
}
static double log2(long n, boolean doubleMode) {
return (Math.log(n) / Math.log(2));
}
static int logk(long n, long k) {
return (int)(Math.log(n) / Math.log(k));
}
// Sieve of Eratosthenes:
static boolean[] primeGenerator(int upto) {
boolean[] isPrime = new boolean[upto + 1];
Arrays.fill(isPrime, true);
isPrime[1] = isPrime[0] = false;
for (long i = 2; i * i < upto + 1; i++)
if (isPrime[(int) i])
// Mark all the multiples greater than or equal
// to the square of i to be false.
for (long j = i; j * i < upto + 1; j++)
isPrime[(int) j * (int) i] = false;
return isPrime;
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static int gcd(int[] arr) {
int n = arr.length;
int gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = gcd(gcd, arr[i]);
}
return gcd;
}
static long gcd(long[] arr) {
int n = arr.length;
long gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = gcd(gcd, arr[i]);
}
return gcd;
}
static long lcm(int[] arr) {
long lcm = arr[0];
int n = arr.length;
for (int i = 1; i < n; i++) {
lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
}
return lcm;
}
static long lcm(long[] arr) {
long lcm = arr[0];
int n = arr.length;
for (int i = 1; i < n; i++) {
lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
}
return lcm;
}
static long lcm(int a, int b) {
return (a * b)/gcd(a, b);
}
static long lcm(long a, long b) {
return (a * b)/gcd(a, b);
}
static boolean less(int a, int b) {
return a < b ? true : false;
}
static boolean isSorted(int[] a) {
for (int i = 1; i < a.length; i++) {
if (less(a[i], a[i - 1]))
return false;
}
return true;
}
static boolean isSorted(long[] a) {
for (int i = 1; i < a.length; i++) {
if (a[i] < a[i - 1])
return false;
}
return true;
}
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(long[] a, int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(double[] a, int i, int j) {
double temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void sort(int[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(char[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(long[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(double[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void reverseSort(int[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(char[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(long[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(double[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void shuffleArray(long[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(int[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(double[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
double tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
private static void shuffleArray(char[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
char tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static String toString(int[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(boolean[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(long[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(char[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + "");
return sb.toString();
}
static String toString(int[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(long[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(double[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(char[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static char toChar(int i) {
return (char) (i + 48);
}
static long mod(long a, long m) {
return (a%m + m) % m;
}
static long mod(long num) {
return (num % gigamod + gigamod) % gigamod;
}
// Uses weighted quick-union with path compression.
static class UnionFind {
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of sites in tree rooted at i
// Note: not necessarily correct if i is not a root node
private int count; // number of components
public UnionFind(int n) {
count = n;
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
// Number of connected components.
public int count() {
return count;
}
// Find the root of p.
public int find(int p) {
int root = p;
while (root != parent[root])
root = parent[root];
while (p != root) {
int newp = parent[p];
parent[p] = root;
p = newp;
}
return root;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int numConnectedTo(int node) {
return size[find(node)];
}
// Weighted union.
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make smaller root point to larger one
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
count--;
}
public static int[] connectedComponents(UnionFind uf) {
// We can do this in nlogn.
int n = uf.size.length;
int[] compoColors = new int[n];
for (int i = 0; i < n; i++)
compoColors[i] = uf.find(i);
HashMap<Integer, Integer> oldToNew = new HashMap<>();
int newCtr = 0;
for (int i = 0; i < n; i++) {
int thisOldColor = compoColors[i];
Integer thisNewColor = oldToNew.get(thisOldColor);
if (thisNewColor == null)
thisNewColor = newCtr++;
oldToNew.put(thisOldColor, thisNewColor);
compoColors[i] = thisNewColor;
}
return compoColors;
}
}
static class UGraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
public UGraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
adj[to].add(from);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int degree(int v) {
return adj[v].size();
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void dfsMark(int current, boolean[] marked, UGraph g) {
if (marked[current]) return;
marked[current] = true;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, marked, g);
}
public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) {
if (marked[current]) return;
marked[current] = true;
if (from != -1)
distTo[current] = distTo[from] + 1;
HashSet<Integer> adj = g.adj(current);
int alreadyMarkedCtr = 0;
for (int adjc : adj) {
if (marked[adjc]) alreadyMarkedCtr++;
dfsMark(adjc, current, distTo, marked, g, endPoints);
}
if (alreadyMarkedCtr == adj.size())
endPoints.add(current);
}
public static void bfsOrder(int current, UGraph g) {
}
public static void dfsMark(int current, int[] colorIds, int color, UGraph g) {
if (colorIds[current] != -1) return;
colorIds[current] = color;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, colorIds, color, g);
}
public static int[] connectedComponents(UGraph g) {
int n = g.V();
int[] componentId = new int[n];
Arrays.fill(componentId, -1);
int colorCtr = 0;
for (int i = 0; i < n; i++) {
if (componentId[i] != -1) continue;
dfsMark(i, componentId, colorCtr, g);
colorCtr++;
}
return componentId;
}
public static boolean hasCycle(UGraph ug) {
int n = ug.V();
boolean[] marked = new boolean[n];
boolean[] hasCycleFirst = new boolean[1];
for (int i = 0; i < n; i++) {
if (marked[i]) continue;
hcDfsMark(i, ug, marked, hasCycleFirst, -1);
}
return hasCycleFirst[0];
}
// Helper for hasCycle.
private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) {
if (marked[current]) return;
if (hasCycleFirst[0]) return;
marked[current] = true;
HashSet<Integer> adjc = ug.adj(current);
for (int adj : adjc) {
if (marked[adj] && adj != parent && parent != -1) {
hasCycleFirst[0] = true;
return;
}
hcDfsMark(adj, ug, marked, hasCycleFirst, current);
}
}
}
static class Digraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
public Digraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public Digraph reversed() {
Digraph dg = new Digraph(V());
for (int i = 0; i < V(); i++)
for (int adjVert : adj(i)) dg.addEdge(adjVert, i);
return dg;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void dfsMark(int source, boolean[] marked, Digraph g) {
if (marked[source]) return;
marked[source] = true;
Iterable<Integer> adj = g.adj(source);
for (int adjc : adj)
dfsMark(adjc, marked, g);
}
public static void bfsOrder(int source, Digraph g) {
}
public static int[] KosarajuSharirSCC(Digraph dg) {
int[] id = new int[dg.V()];
Digraph reversed = dg.reversed();
// Gotta perform topological sort on this one to get the stack.
Stack<Integer> revStack = Digraph.topologicalSort(reversed);
// Initializing id and idCtr.
id = new int[dg.V()];
int idCtr = -1;
// Creating a 'marked' array.
boolean[] marked = new boolean[dg.V()];
while (!revStack.isEmpty()) {
int vertex = revStack.pop();
if (!marked[vertex])
sccDFS(dg, vertex, marked, ++idCtr, id);
}
return id;
}
private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) {
marked[source] = true;
id[source] = idCtr;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id);
}
public static Stack<Integer> topologicalSort(Digraph dg) {
// dg has to be a directed acyclic graph.
// We'll have to run dfs on the digraph and push the deepest nodes on stack first.
// We'll need a Stack<Integer> and a int[] marked.
Stack<Integer> topologicalStack = new Stack<Integer>();
boolean[] marked = new boolean[dg.V()];
// Calling dfs
for (int i = 0; i < dg.V(); i++)
if (!marked[i])
runDfs(dg, topologicalStack, marked, i);
return topologicalStack;
}
static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) {
marked[source] = true;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex])
runDfs(dg, topologicalStack, marked, adjVertex);
topologicalStack.add(source);
}
public static boolean hasCycle(Digraph dg) {
int n = dg.V();
boolean[] marked = new boolean[n];
boolean[] hasCycleFirst = new boolean[1];
for (int i = 0; i < n; i++) {
if (marked[i]) continue;
hcDfsMark(i, dg, marked, hasCycleFirst, -1);
}
return hasCycleFirst[0];
}
// Helper for hasCycle.
private static void hcDfsMark(int current, Digraph dg, boolean[] marked, boolean[] hasCycleFirst, int parent) {
if (marked[current]) return;
if (hasCycleFirst[0]) return;
marked[current] = true;
HashSet<Integer> adjc = dg.adj(current);
for (int adj : adjc) {
if (marked[adj] && adj != parent && parent != -1) {
hasCycleFirst[0] = true;
return;
}
hcDfsMark(adj, dg, marked, hasCycleFirst, current);
}
}
}
static class Edge {
int from;
int to;
long weight;
public Edge(int from, int to, long weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
}
static class WeightedUGraph {
// Adjacency list.
private HashSet<Edge>[] adj;
private static final String NEWLINE = "\n";
private int E;
public WeightedUGraph(int V) {
adj = (HashSet<Edge>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Edge>();
}
public void addEdge(int from, int to, int weight) {
if (adj[from].contains(new Edge(from, to, weight))) return;
E++;
adj[from].add(new Edge(from, to, weight));
adj[to].add(new Edge(to, from, weight));
}
public HashSet<Edge> adj(int from) {
return adj[from];
}
public int degree(int v) {
return adj[v].size();
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (Edge w : adj[v]) {
s.append(w.toString() + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void dfsMark(int current, boolean[] marked, UGraph g) {
if (marked[current]) return;
marked[current] = true;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, marked, g);
}
public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) {
if (marked[current]) return;
marked[current] = true;
if (from != -1)
distTo[current] = distTo[from] + 1;
HashSet<Integer> adj = g.adj(current);
int alreadyMarkedCtr = 0;
for (int adjc : adj) {
if (marked[adjc]) alreadyMarkedCtr++;
dfsMark(adjc, current, distTo, marked, g, endPoints);
}
if (alreadyMarkedCtr == adj.size())
endPoints.add(current);
}
public static void bfsOrder(int current, UGraph g) {
}
public static void dfsMark(int current, int[] colorIds, int color, UGraph g) {
if (colorIds[current] != -1) return;
colorIds[current] = color;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, colorIds, color, g);
}
public static int[] connectedComponents(UGraph g) {
int n = g.V();
int[] componentId = new int[n];
Arrays.fill(componentId, -1);
int colorCtr = 0;
for (int i = 0; i < n; i++) {
if (componentId[i] != -1) continue;
dfsMark(i, componentId, colorCtr, g);
colorCtr++;
}
return componentId;
}
public static boolean hasCycle(UGraph ug) {
int n = ug.V();
boolean[] marked = new boolean[n];
boolean[] hasCycleFirst = new boolean[1];
for (int i = 0; i < n; i++) {
if (marked[i]) continue;
hcDfsMark(i, ug, marked, hasCycleFirst, -1);
}
return hasCycleFirst[0];
}
// Helper for hasCycle.
private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) {
if (marked[current]) return;
if (hasCycleFirst[0]) return;
marked[current] = true;
HashSet<Integer> adjc = ug.adj(current);
for (int adj : adjc) {
if (marked[adj] && adj != parent && parent != -1) {
hasCycleFirst[0] = true;
return;
}
hcDfsMark(adj, ug, marked, hasCycleFirst, current);
}
}
}
static class FastReader {
private BufferedReader bfr;
private StringTokenizer st;
public FastReader() {
bfr = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
if (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(bfr.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().toCharArray()[0];
}
String nextString() {
return next();
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
double[] nextDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextDouble();
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
int[][] nextIntGrid(int n, int m) {
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
char[] line = fr.next().toCharArray();
for (int j = 0; j < m; j++)
grid[i][j] = line[j] - 48;
}
return grid;
}
}
}
// NOTES:
// ASCII VALUE OF 'A': 65
// ASCII VALUE OF 'a': 97
// Range of long: 9 * 10^18
// ASCII VALUE OF '0': 48
// Primes upto 'n' can be given by (n / (logn)). | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 6794a0c3c65cb18b55f3dd47350acb5d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class ComplexMarketAnalysis {
public static void main(String[] args) throws IOException {
FastReader fr = new FastReader();
PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
int t = fr.nextInt();
boolean[] comp = new boolean[1000005];
// sieve
for (int i = 2; i < comp.length; i++) {
if (comp[i])
continue;
for (int j = i + i; j < comp.length; j += i) {
comp[j] = true;
}
}
while (t-- > 0) {
int n = fr.nextInt();
int e = fr.nextInt();
int[] arr = new int[n + 1];
for (int i = 1; i <= n; i++) {
int a = fr.nextInt();
arr[i] = a;
}
long total = 0;
boolean[] chained = new boolean[n + 1];
ArrayList<Integer> ones = new ArrayList<>();
int currentOnes = 0;
for (int i = 1; i <= n; i++) {
// reset structures
ones.clear();
currentOnes = 0;
// pr.println("Start: " + i);
// loop through sequence
for (int j = i; j <= n; j += e) {
if ((arr[j] != 1 && comp[arr[j]]) || chained[j]) {
break;
}
chained[j] = true;
if (arr[j] == 1) {
// pr.println("is one: " + j);
currentOnes++;
} else {
// pr.println("end of group: " + j + " : cur : " + currentOnes);
ones.add(currentOnes);
currentOnes = 0;
}
}
if (ones.size() == 0) continue;
ones.add(currentOnes);
for (int j = 0; j < ones.size(); j++) {
total += ones.get(j);
if (j > 0 && j < ones.size() - 1) {
total += ones.get(j);
}
if (j < ones.size() - 1) {
total += (long)(ones.get(j)) * (long)(ones.get(j + 1));
}
}
}
pr.println(total);
}
pr.close();
}
static class Pair {
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int toInt(String s) {
return Integer.parseInt(s);
}
// MERGE SORT IMPLEMENTATION
void sort(int[] arr, int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
// call merge
merge(arr, l, m, r);
}
}
void merge(int[] arr, int l, int m, int r) {
// find sizes
int len1 = m - l + 1;
int len2 = r - m;
int[] L = new int[len1];
int[] R = new int[len2];
// push to copies
for (int i = 0; i < L.length; i++)
L[i] = arr[l + i];
for (int i = 0; i < R.length; i++) {
R[i] = arr[m + 1 + i];
}
// fill in new array
int i = 0, j = 0;
int k = l;
while (i < len1 && j < len2) {
if (L[i] < R[i]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[i];
j++;
}
k++;
}
// add remaining elements
while (i < len1) {
arr[k] = L[i];
i++;
k++;
}
while (j < len2) {
arr[k] = R[j];
j++;
k++;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | efbf5ccecfa6a2630c52b2f56e6834b5 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class new1{
public static void main(String[] args) throws IOException{
//long l1 = System.currentTimeMillis();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int[] seive = new int[1000001];
for(int i = 2; i < seive.length; i++) {
for(int j = i; j < seive.length; j = j + i) {
if(seive[j] == 0) seive[j] = i;
}
}
// System.out.println(seive[9]);
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
int e = s.nextInt();
int[] arr =new int[n + 1];
for(int i = 1; i <= n; i++) arr[i] = s.nextInt();
long count = 0;
if(e == 1) {
TreeSet<Integer> ts = new TreeSet<Integer>();
for(int i = 1; i <= n; i++) {
if(arr[i] != 1) ts.add(i);
}
int i = 1;
// System.out.println(ts.toString());
while(i < n) {
if(!ts.isEmpty() && arr[ts.first()] == seive[arr[ts.first()]]) {
int fi = ts.first(); int se = n + 1;
if(ts.last() != fi) se = ts.higher(fi);
count = count + se - fi;
if(i == fi) count--;
}
//System.out.println(ts.toString() + " " + count + " " + i);
if(ts.contains(i)) ts.remove(i);
i++;
}
}
else {
for(int i = 1; i < n; i++) {
long prod = arr[i];
for(int j = i + e; j <= n; j = j + e) {
if(prod > 1 && arr[j] > 1) break;
prod = prod * arr[j];
if(seive[(int) prod] == prod) count = count + 1;
//System.out.println(count + " " + i + " " + j);
}
}
}
System.out.println(count);
}
// output.flush();
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | fa1d8a130abe538f5c29cbfddbee2a7f | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
for (int t = IO.nextInt(), i = 0; i < t; i++)
new SolC().solve();
IO.close();
}
}
class SolC {
int n, e;
int[] a;
int[] cntL, cntR;
SolC() {
n = IO.nextInt();
e = IO.nextInt();
a = new int[n];
for (int i = 0; i < n; i++)
a[i] = IO.nextInt();
}
void solve() {
computeOnes();
long ans = 0;
for (int i = 0; i < n; i++) {
if (isPrime(a[i])) {
ans += (long) (cntL[i] + 1) * (cntR[i] + 1) - 1;
}
}
IO.writer.println(ans);
}
void computeOnes() {
cntL = new int[n];
cntR = new int[n];
for (int i = 0; i < n; i++) {
if (a[i] == 1 && i + e < n)
cntL[i+e] = 1;
if (i-e >= 0 && a[i-e] == 1)
cntL[i] += cntL[i-e];
}
for (int i = n-1; i >= 0; i--) {
if (a[i] == 1 && i - e >= 0)
cntR[i-e] = 1;
if (i+e < n && a[i+e] == 1)
cntR[i] += cntR[i+e];
}
//IO.writer.println(Arrays.toString(cntL));
//IO.writer.println(Arrays.toString(cntR));
}
boolean isPrime(int v) {
if (v == 1)
return false;
if (v > 2 && v % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(v); i += 2)
if (v % i == 0)
return false;
return true;
}
}
class IO {
static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static final PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
private static StringTokenizer tokens;
static String readLine() {
try {
return reader.readLine();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static void initializeTokens() {
while (null == tokens || !tokens.hasMoreTokens())
tokens = new StringTokenizer(readLine());
}
static String next() {
initializeTokens();
return tokens.nextToken();
}
static String next(String delim) {
initializeTokens();
return tokens.nextToken(delim);
}
static int nextInt() {
return Integer.parseInt(next());
}
static long nextLong() {
return Long.parseLong(next());
}
static void close() {
try {
reader.close();
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | e71ffa9174ec1beb28cde610b3edff50 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /**
* @author vivek
* programming is thinking, not typing
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C {
// static boolean isPrime(long n) {
//
// if (n <= 1)
// return false;
//
// else if (n == 2)
// return true;
//
// else if (n % 2 == 0)
// return false;
//
// for (long i = 3; i <= Math.sqrt(n); i += 2) {
// if (n % i == 0)
// return false;
// }
// return true;
// }
private static void solveTC(int __) {
/* For Google */
// ans.append("Case #").append(__).append(": ");
//code start
int n = scn.nextInt();
int e = scn.nextInt();
long[] arr = scn.nextLongArray(n);
int[] dpa = new int[n];
int[] dpb = new int[n];
for (int i = 0, j = n - 1; i < n; i++, j--) {
if (i - e >= 0) {
dpa[i] = (arr[i - e] == 1 ? dpa[i - e] + 1 : 0);
}
if (j + e < n) {
dpb[j] = (arr[j + e] == 1 ? dpb[j + e] + 1 : 0);
}
}
long ans = 0;
for (int i = 0; i < n; i++) {
if (isPrime[(int) arr[i]]) {
ans += calc(dpa[i], dpb[i]);
}
}
print(ans);
//code end
print("\n");
}
private static long calc(long left, long right) {
return left + right + left * right;
}
public static void main(String[] args) {
scn = new Scanner();
ans = new StringBuilder();
int t = scn.nextInt();
// int t = 1;
int limit= 1_000_001 ;
sieve(limit);
/*
try {
System.setOut(new PrintStream(new File("file_i_o\\output.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
for (int i = 1; i <= t; i++) {
solveTC(i);
}
System.out.print(ans);
}
//Stuff for prime start
/**
* sorting algos
*/
private static void sort(int[] arr) {
ArrayList<Integer> li = new ArrayList<>(arr.length);
for (int ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
private static void sort(long[] arr) {
ArrayList<Long> li = new ArrayList<>(arr.length);
for (long ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
private static void sort(float[] arr) {
ArrayList<Float> li = new ArrayList<>(arr.length);
for (float ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
private static void sort(double[] arr) {
ArrayList<Double> li = new ArrayList<>(arr.length);
for (double ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
/**
* List containing prime numbers <br>
* <b>i<sup>th</sup></b> position contains <b>i<sup>th</sup></b> prime number <br>
* 0th index is <b>null</b>
*/
private static ArrayList<Integer> listOfPrimes;
/**
* query <b>i<sup>th</sup></b> element to get if its prime of not
*/
private static boolean[] isPrime;
/**
* Performs Sieve of Erathosnesis and initialise isPrime array and listOfPrimes list
*
* @param limit the number till which sieve is to be performed
*/
private static void sieve(int limit) {
listOfPrimes = new ArrayList<>();
listOfPrimes.add(null);
boolean[] array = new boolean[limit + 1];
Arrays.fill(array, true);
array[0] = false;
array[1] = false;
for (int i = 2; i <= limit; i++) {
if (array[i]) {
for (long j = (long) i * i; j <= limit; j += i) {
array[(int) j] = false;
}
}
}
isPrime = array;
for (int i = 0; i <= limit; i++) {
if (array[i]) {
listOfPrimes.add(i);
}
}
}
//stuff for prime end
/**
* Calculates the Least Common Multiple of two numbers
*
* @param a First number
* @param b Second Number
* @return Least Common Multiple of <b>a</b> and <b>b</b>
*/
private static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
/**
* Calculates the Greatest Common Divisor of two numbers
*
* @param a First number
* @param b Second Number
* @return Greatest Common Divisor of <b>a</b> and <b>b</b>
*/
private static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static void print(Object obj) {
ans.append(obj.toString());
}
static Scanner scn;
static StringBuilder ans;
//Fast Scanner
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new
InputStreamReader(System.in));
/*
try {
br = new BufferedReader(new
InputStreamReader(new FileInputStream(new File("file_i_o\\input.txt"))));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
Integer[] nextIntegerArray(int n) {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = nextLong();
}
return array;
}
String[] nextStringArray() {
return nextLine().split(" ");
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++) {
array[i] = next();
}
return array;
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 3e5b417ec4dd18a99edb7f17eddc6a62 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class s1 {
public static FastScanner scan;
public static PrintWriter out;
public static void main(String[] args) throws Exception {
scan=new FastScanner(System.in);
out=new PrintWriter(System.out);
// int T=1;
int T=scan.nextInt();
while(T-->0) {
int n=scan.nextInt(),e=scan.nextInt();
int[] a=new int[n];
HashSet<Integer> prime=new HashSet<>();
HashMap<Integer,Integer> before=new HashMap<>();
HashMap<Integer,Integer> last=new HashMap<>();
long ans=0;
for(int i=0;i<n;i++) {
a[i]=scan.nextInt();
int mod=i%e;
if(a[i]==1) {
last.put(mod,last.getOrDefault(mod,0)+1);
if(prime.contains(mod)) ans++;
if(before.containsKey(mod)) ans+=before.get(mod);
} else {
int cur=a[i];
boolean can=false;
for(int j=2;j*j<=cur&&!can;j++) if(cur%j==0) can=true;
if(!can) {
prime.add(mod);
if(last.containsKey(mod)) {
ans+=last.get(mod);
before.put(mod,last.get(mod));
}
last.put(mod,0);
} else {
if(prime.contains(mod)) prime.remove(mod);
before.put(mod,0);
last.put(mod,0);
}
}
}
out.println(ans);
} out.close();
}
}
class Pair {
int a,b;
Pair(int a,int b) {
this.a=a;
this.b=b;
}
}
class FastScanner {
private InputStream stream;
private byte[] buf=new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) { this.stream=stream; }
int read() {
if(numChars==-1) throw new InputMismatchException();
if(curChar>=numChars) {
curChar=0;
try { numChars=stream.read(buf); }
catch(IOException e) { throw new InputMismatchException(); }
if(numChars<=0) return -1;
} return buf[curChar++];
}
boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; }
boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; }
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() { return Double.parseDouble(next()); }
String next() {
int c=read();
while(isSpaceChar(c)) c=read();
StringBuilder res=new StringBuilder();
do {
res.appendCodePoint(c);
c=read();
} while(!isSpaceChar(c));
return res.toString();
}
String nextLine() {
int c=read();
while(isEndline(c)) c=read();
StringBuilder res=new StringBuilder();
do {
res.appendCodePoint(c);
c=read();
} while(!isEndline(c));
return res.toString();
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 2b052a34cc55430f7ebd8c04317035c8 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
public class code1 {
static boolean[] seive=new boolean[(int)1e8];
static void seiveofprime(int n){
Arrays.fill(seive,true);
seive[0]=false;
seive[1]= false;
for (int i = 2; i <=n; i++) {
if(!seive[i]) continue;
for (int j = i*2;j<=n; j+=i) {
seive[j]=false;
}
}
}
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
seiveofprime((int)1e7);
int t= sc.nextInt();
while(t-->0){
solve();
}
}
static void solve() {
int n = sc.nextInt();
int e = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long ans=0;
for (int i = 0; i < n; i++) {
if(seive[a[i]]){
int j=i-e;
long left=0;
while(j>=0 && a[j]==1){
left++;
j-=e;
}
long right=0;
j=i+e;
while(j<n && a[j]==1){
right++;
j+=e;
}
ans+=Math.max(((left+1)*(right+1))-1,Math.max(left,right));
}
}
System.out.println(ans);
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 7234a1abf2bf0a0705fc94b70016353d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static class Pair implements Comparable < Pair > {
int d;
int i;
Pair(int d, int i) {
this.d = d;
this.i = i;
}
public int compareTo(Pair o) {
return this.d - o.d;
}
}
public static class SegmentTree {
long[] st;
long[] lazy;
int n;
SegmentTree(long[] arr, int n) {
this.n = n;
st = new long[4 * n];
lazy = new long[4 * n];
construct(arr, 0, n - 1, 0);
}
public long construct(long[] arr, int si, int ei, int node) {
if (si == ei) {
st[node] = arr[si];
return arr[si];
}
int mid = (si + ei) / 2;
long left = construct(arr, si, mid, 2 * node + 1);
long right = construct(arr, mid + 1, ei, 2 * node + 2);
st[node] = left + right;
return st[node];
}
public long get(int l, int r) {
return get(0, n - 1, l, r, 0);
}
public long get(int si, int ei, int l, int r, int node) {
if (r < si || l > ei)
return 0;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei)
return st[node];
int mid = (si + ei) / 2;
return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2);
}
public void update(int index, int value) {
update(0, n - 1, index, 0, value);
}
public void update(int si, int ei, int index, int node, int val) {
if (si == ei) {
st[node] = val;
return;
}
int mid = (si + ei) / 2;
if (index <= mid) {
update(si, mid, index, 2 * node + 1, val);
} else {
update(mid + 1, ei, index, 2 * node + 2, val);
}
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
public void rangeUpdate(int l, int r, int val) {
rangeUpdate(0, n - 1, l, r, 0, val);
}
public void rangeUpdate(int si, int ei, int l, int r, int node, int val) {
if (r < si || l > ei)
return;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei) {
st[node] += val * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (si + ei) / 2;
rangeUpdate(si, mid, l, r, 2 * node + 1, val);
rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val);
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setIn(new FileInputStream(new File("input.txt")));
System.setOut(new PrintStream(new File("output.txt")));
} catch (Exception e) {
}
}
Reader sc = new Reader();
int tc = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (tc-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int left[] = new int[n];
int right[] = new int[n];
for (int i = 0; i < n; i++) {
if (arr[i] == 1) {
if (i - k >= 0)
left[i] = left[i - k] + 1;
else
left[i] = 1;
}
}
for (int i = n - 1; i >= 0; i--) {
if (arr[i] == 1) {
if (i + k < n)
right[i] = right[i + k] + 1;
else
right[i] = 1;
}
}
long ans = 0;
for (int i = 0; i < n; i++) {
if (isPrime(arr[i]) == false)
continue;
long l = 0;
if (i - k >= 0) {
ans += left[i - k];
l = left[i - k];
}
long r = 0;
if (i + k < n) {
ans += right[i + k];
r = right[i + k];
}
ans += (l * r);
}
sb.append(ans);
sb.append("\n");
}
System.out.println(sb);
}
static boolean isPrime(int n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
public static int Xor(int n) {
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
public static long pow(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) > 0)
res = (res * a) % mod;
b = b >> 1;
a = ((a % mod) * (a % mod)) % mod;
}
return (res % mod + mod) % mod;
}
public static boolean isEqual(ArrayList<Integer> a, ArrayList<Integer> b, int n) {
HashSet<Integer> hs = new HashSet<>();
if (a.size() < (n / 2) || b.size() < (n / 2))
return false;
int s1 = 0;
for (int ele : a)
hs.add(ele);
for (int ele : b)
hs.add(ele);
if (hs.size() == n)
return true;
return false;
}
public static double digit(long num) {
return Math.floor(Math.log10(num) + 1);
}
public static int count(ArrayList<Integer> al, int num) {
if (al.get(al.size() - 1) == num)
return 0;
int k = al.get(al.size() - 1);
ArrayList<Integer> temp = new ArrayList<>();
for (int i = 0; i < al.size(); i++) {
if (al.get(i) > k)
temp.add(al.get(i));
}
return 1 + count(temp, num);
}
public static String Util(String s) {
for (int i = s.length() - 1; i >= 1; i--) {
int l = s.charAt(i - 1) - '0';
int r = s.charAt(i) - '0';
if (l + r >= 10) {
return s.substring(0, i - 1) + (l + r) + s.substring(i + 1);
}
}
int l = s.charAt(0) - '0';
int r = s.charAt(1) - '0';
return (l + r) + s.substring(2);
}
public static boolean isPos(int idx, long[] arr, long[] diff) {
if (idx == 0) {
for (int i = 0; i <= Math.min(arr[0], arr[1]); i++) {
diff[idx] = i;
arr[0] -= i;
arr[1] -= i;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[0] += i;
arr[1] += i;
}
} else if (idx == 1) {
if (arr[2] - arr[1] >= 0) {
long k = arr[1];
diff[idx] = k;
arr[1] = 0;
arr[2] -= k;
if (isPos(idx + 1, arr, diff)) {
return true;
}
arr[1] = k;
arr[2] += k;
} else
return false;
} else {
if (arr[2] == arr[0] && arr[1] == 0) {
diff[2] = arr[2];
return true;
} else {
return false;
}
}
return false;
}
public static boolean isPal(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
static int upperBound(ArrayList<Long> arr, long key) {
int mid, N = arr.size();
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr.get(mid)) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
return low;
}
static int lowerBound(ArrayList<Long> array, long key) {
// Initialize starting index and
// ending index
int low = 0, high = array.size();
int mid;
// Till high does not crosses low
while (low < high) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is less than or equal
// to array[mid], then find in
// left subarray
if (key <= array.get(mid)) {
high = mid;
}
// If key is greater than array[mid],
// then find in right subarray
else {
low = mid + 1;
}
}
// If key is greater than last element which is
// array[n-1] then lower bound
// does not exists in the array
if (low < array.size() && array.get(low) < key) {
low++;
}
// Returning the lower_bound index
return low;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | d304a73d9cbe6b6958489eef653b9dd1 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package labels.implementation.p_01_cf_1609_C;
import java.util.Arrays;
import java.util.Scanner;
/**
* https://codeforces.com/problemset/problem/1609/C
* C. Complex Market Analysis
* */
public class Main {
static int MAX = (int) (1e6 + 5);
static boolean[] isPrime = new boolean[MAX];
static void init() {
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for(int i = 2; i * i < MAX; i++) {
if(isPrime[i]) {
for(int j = i * i; j < MAX; j += i) {
isPrime[j] = false;
}
}
}
}
public static void main(String[] args) {
init();
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
solve(sc);
}
}
static void solve(Scanner sc) {
int n = sc.nextInt(), e = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
long ans = 0;
for(int i = 0; i < n; i++) {
if(isPrime[a[i]]) {
long bef = 0, aft = 0;
for(int j = i - e; j >= 0; j -= e) {
if(a[j] != 1) {
break;
}
bef++;
}
for(int j = i + e; j < n; j += e) {
if(a[j] != 1) {
break;
}
aft ++;
}
ans += aft + bef * (aft + 1);
}
}
System.out.println(ans);
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 72843e3f5a75103bec03afd33551cd35 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import static java.lang.Math.*;
import java.io.*;
public class S {
public static int surv = 0;
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
int primes[] = new int[1000001];
for(int i = 2; i*i < primes.length; i++){
if(primes[i] == 0){
for(int j = i*i; j < primes.length; j+= i){
primes[j] = 1;
}
}
}
while(test > 0){
test--;
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
int a[] = new int[n];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(st.nextToken());
}
solve(a, n, e, primes, sb);
}
System.out.print(sb.toString());
}
final static int MOD = 1000000007;
public static void solve(int a[], int n, int e, int primes[], StringBuilder sb){
long ans = 0;
for(int i = 0; i < n; i++){
int curr = a[i];
if(primes[curr] == 0 && curr > 1){
long l = 0;
long r = 0;
for(int j = i - e; j >= 0; j-= e){
if(a[j] != 1)break;
l++;
}
for(int j = i + e; j < n; j+= e){
if(a[j] != 1)break;
r++;
}
ans += (l+r + l*r);
}
}
sb.append(ans + "\n");
}
public static long powerr(long base, long exp){
if(exp < 2)return base;
if(exp % 2 == 0){
long ans = powerr(base, exp/2) % MOD;
ans *= ans;
ans %= MOD;
return ans;
}else{
return (((powerr(base, exp-1)) % MOD) * base) % MOD;
}
}
public static long power(long a, long b){
if(b == 0)return 1l;
long ans = power(a, b/2);
ans *= ans;
ans %= MOD;
if(b % 2 != 0){
ans *= a;
}
return ans % MOD;
}
public static int logLong(long a){
int ans = 0;
long b = 1;
while(b < a){
b*=2;
ans++;
}
return ans;
}
public static void rec(int a[], int l, int r, int d, int d_map[]){
if(l > r)return;
if(l == r){
d_map[a[l]] = d;
return;
}
int max = 0;
int max_ind = -1;
for(int i = l; i <= r; i++){
if(a[i] > max){
max_ind = i;
max = a[i];
}
}
d_map[a[max_ind]] = d;
rec(a, l, max_ind - 1, d+1, d_map);
rec(a, max_ind + 1, r, d+1, d_map);
}
public static void sort(int a[]){
List<Integer> l = new ArrayList<Integer>();
for(int val : a){
l.add(val);
}
Collections.sort(l);
int k = 0;
for(int val : l){
a[k++] = val;
}
}
public static void sortLong(long a[]){
List<Long> l = new ArrayList<Long>();
for(long val : a){
l.add(val);
}
Collections.sort(l);
int k = 0;
for(long val : l){
a[k++] = val;
}
}
public static boolean isPal(String s){
int l = 0;
int r = s.length() - 1;
while(l <= r){
if(s.charAt(l) != s.charAt(r)){
return false;
}
l++;
r--;
}
return true;
}
public static int gcd(int a, int b){
if(b > a){
int temp = a;
a = b;
b = temp;
}
if(b == 0)return a;
return gcd(b, a%b);
}
/* public static long gcd(long a, long b){
if(b > a){
long temp = a;
a = b;
b = temp;
}
if(b == 0)return a;
return gcd(b, a%b);
}*/
// public static long lcm(long a, long b){
// return a * b/gcd(a, b);
// }
/*public static class DJSet{
public int a[];
public DJSet(int n){
this.a = new int[n];
Arrays.fill(a, -1);
}
public int find(int val){
if(a[val] >= 0){
a[val] = find(a[val]);
return a[val];
}else{
return val;
}
}
public boolean union(int val1, int val2){
int p1 = find(val1);
int p2 = find(val2);
if(p1 == p2){
return false;
}
int size1 = Math.abs(a[p1]);
int size2 = Math.abs(a[p2]);
if(size1 >= size2){
a[p2] = p1;
a[p1] = (size1 + size2) * -1;
}else{
a[p1] = p2;
a[p2] = (size1 + size2) * -1;
}
return true;
}*/
/*
public static class DSU{
public int a[];
public DSU(int size){
this.a = new int[size];
Arrays.fill(a, -1);
}
public int find(int u){
if(a[u] < 0)return u;
a[u] = find(a[u]);
return a[u];
}
public boolean union(int u, int v){
int p1 = find(u);
int p2 = find(v);
if(p1 == p2)return false;
int size1 = a[p1] * -1;
int size2 = a[p2] * -1;
if(size1 >= size2){
a[p2] = p1;
a[p1] = (size1 + size2) * -1;
}else{
a[p1] = p2;
a[p2] = (size1 + size2) * -1;
}
return true;
}
}*/
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 7503225de1685107476e6c3aa8ec7f13 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /*
* Everything is Hard
* Before Easy
* Jai Mata Dii
*/
import java.util.*;
import java.io.*;
public class Main {
static class FastReader{ BufferedReader br;StringTokenizer st;public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException e){e.printStackTrace();}}return st.nextToken();}int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; }}
static long mod = (long)(1e9+7);
// static long mod = 998244353;
// static Scanner sc = new Scanner(System.in);
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static int ans;
public static void main (String[] args) {
int ttt = 1;
ttt = sc.nextInt();
sieve();
HashSet<Integer> p = new HashSet<>();
for(int i=2;i<=1000005;i++) {
if(!prime[i])
// System.out.println(i);
p.add(i);
}
z :for(int tc=1;tc<=ttt;tc++){
int n = sc.nextInt();
int e = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
long dp[] = new long[n];
for(int i=n-1;i>=0;i--) {
if(a[i] == 1) {
dp[i] = 1;
if(i + e < n) {
dp[i] += dp[i+e];
}
}
}
long ans = 0;
long left[] = new long[n];
for(int i=0;i<n;i++) {
if(a[i] == 1) {
left[i] = 1;
if(i - e >= 0) {
left[i] += left[i-e];
}
}
}
for(int i=0;i<n;i++) {
long cur = 0;
if(p.contains(a[i]) && i-e >= 0) {
// System.out.println(i);
cur = cur + left[i-e];
}
if(p.contains(a[i]) && i+e<n) {
if(dp[i+e]!=0) {
cur = (cur)+(dp[i+e]);
}
}
if(p.contains(a[i]) && i-e>=0 && i+e<n) {
cur = (cur)+(dp[i+e]*left[i-e]);
}
ans = ans + cur;
}
out.write(ans+"\n");
}
out.close();
}
static boolean prime[] = new boolean[1000009];
static void sieve() {
prime[0] = true;
prime[1] = true;
int max = 1000005;
for(int i=2;i*i<=max;i++) {
if(!prime[i]) {
for(int j=i*i;j<=max;j+=i) {
prime[j] = true;
// fac[j] = i;
}
}
}
}
static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;}
static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b); }
private static void sort(int[] a) {List<Integer> k = new ArrayList<>();for(int val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);}
private static void ini(List<Integer>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}}
private static void init(List<int[]>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}}
private static void sort(long[] a) {List<Long> k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 3b495d8aabe07a7161b4b960bcde27ae | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static FastScanner sc = new FastScanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
static long mod = (long) 1e9 + 7;
static Sieve s = new Sieve(1000005);
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
for(int i = 0; i < n; i++) solve();
pw.flush();
}
public static void solve() {
int n = sc.nextInt();
int K = sc.nextInt();
ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
for(int i = 0; i < K; i++) arr.add(new ArrayList<>());
for(int i = 0; i < n; i++){
int v = sc.nextInt();
arr.get(i % K).add(v);
}
long ans = 0;
boolean is1 = true;
for(int i = 0; i < K; i++){
ArrayList<Integer> a = arr.get(i);
int N = a.size();
int right = 0;
for(int left = 0; left < N; left++){
while (right < N && (a.get(right) == 1 || s.isPrime(a.get(right)))) {
if(a.get(right) != 1){
if(is1){
is1 = false;
}else{
break;
}
}
++right;
}
ans += Math.max(0,right-left-1);
if (right == left) ++right; // right が left に重なったら right も動かす
else if(a.get(left) != 1) is1 = true;
}
right = 0;
for(int left = 0; left < N; left++){
while (right < N && a.get(right) == 1) {
++right;
}
ans -= Math.max(0,right-left-1);
if (right == left) ++right; // right が left に重なったら right も動かす
}
//pw.println(ans);
}
pw.println(ans);
}
static class GeekInteger {
public static void save_sort(int[] array) {
shuffle(array);
Arrays.sort(array);
}
public static int[] shuffle(int[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
int randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
public static void save_sort(long[] array) {
shuffle(array);
Arrays.sort(array);
}
public static long[] shuffle(long[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
long randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
}
}
class Sieve{
static int n;
static int[] f;
static ArrayList<Integer> prime;
public Sieve(int n){
long ln = n;
prime = new ArrayList<Integer>();
f = new int[n+1];
f[0] = f[1] = -1;
for(int i = 2; i <= n; i++){
if(f[i] != 0){
continue;
}
f[i] = i;
prime.add(i);
long li = (long)i;
for(long j = li*li; j <= ln; j += li){
if(f[(int)j] == 0){
f[(int)j] = i;
}
}
}
}
public static boolean isPrime(int x){
return f[x] == x;
}
public static ArrayList<Integer> factorList(int x){
ArrayList<Integer> res = new ArrayList<Integer>();
while(x != 1){
res.add(f[x]);
x /= f[x];
}
return res;
}
public static HashMap<Integer,Integer> factor(int x){
ArrayList<Integer> fl = factorList(x);
HashMap<Integer,Integer> res = new HashMap<Integer,Integer>();
if(fl.size()==0){
return new HashMap<Integer,Integer>();
}
int prev = fl.get(0);
int cnt = 0;
for(int p : fl){
if(prev == p){
cnt++;
}else{
res.put(prev,cnt);
prev = p;
cnt = 1;
}
}
res.put(prev,cnt);
return res;
}
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public FastScanner(FileReader in) {
reader = new BufferedReader(in);
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String[] nextArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 60048f8a5a109e525779f1e63613670e | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
/**
*
* @author Lenovo
*/
public class Problem {
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
int[] n=new int[t]; int[] e=new int[t];
int[][] g=new int[t][];
for(int i=0;i<t;i++)
{
n[i]=in.nextInt(); e[i]=in.nextInt();
g[i]=new int[n[i]];
for(int j=0;j<n[i];j++)
{g[i][j]=in.nextInt();}
}
ArrayList<Integer> m=new ArrayList<Integer>();
m.add(2);
m.add(3);
m.add(5);
m.add(7);
int count=0;
m:while(count<3){
boolean prime=true;
int x=m.get(m.size()-1);
int q=m.size();
for(int i=x+1;i<x*x;i++)
{prime=true;
for(int j=0;j<q;j++)
{if (i%m.get(j)==0) {prime=false;break;}}
if (prime) m.add(i);
if (i>1000000) break m;
}
count++;
}
HashSet<Integer>h =new HashSet<Integer>();
for(int i=0;i<m.size();i++) h.add(m.get(i));
long[] count1array=new long[t];
for(int i=0;i<t;i++)
{
long count1=0;
boolean[] one=new boolean[n[i]];
ArrayList<Integer> prime=new ArrayList<Integer>();
for(int j=0;j<n[i];j++)
{if (g[i][j]==1) one[j]=true;}
for(int j=0;j<n[i];j++)
{if (one[j]) continue;
if (h.contains(g[i][j])) prime.add(j);
}
for(int j=0;j<prime.size();j++)
{int k=0;
int l=0;
while((prime.get(j)+e[i]*(k+1)<n[i])&&one[prime.get(j)+e[i]*(k+1)]) k++;
while ((prime.get(j)-e[i]*(l+1)>=0)&&one[prime.get(j)-e[i]*(l+1)]) l++;
count1+=k;
count1+=l;
count1+=(long)k*(long)l;
}
count1array[i]=count1;
}
for(int i=0;i<t;i++)
System.out.println(count1array[i]);
}} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | bc4add70a29e20bf8a91a9e50e959ea9 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void calcPrimes(int n, boolean prime[]){
for(int i=0;i<=n;i++){
prime[i] = true;
}
prime[0] = prime[1] = false;
for(int i=2;i<=n;i++){
if(prime[i]){
for(int j=i*2;j<=n;j+=i){
prime[j] = false;
}
}
}
}
public static void main(String[] args) {
int maxNumber = 1000000;
boolean prime[] = new boolean[maxNumber+1];
calcPrimes(maxNumber, prime);
int t, n, e;
Scanner scan = new Scanner(System.in);
t = scan.nextInt();
while(t-- > 0){
n = scan.nextInt();
e = scan.nextInt();
int arr[] = new int[n];
int lo[] = new int[n];
int ro[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = scan.nextInt();
lo[i] = ro[i] = 0;
}
for(int i=0;i<n;i++){
if(arr[i] == 1){
lo[i] = 1;
if(i - e >= 0){
lo[i] += lo[i-e];
}
}
}
for(int i=n-1;i>=0;i--){
if(arr[i] == 1){
ro[i] = 1;
if(i+e < n){
ro[i] += ro[i+e];
}
}
}
long ans = 0;
for(int i=0;i<n;i++){
if(prime[arr[i]]){
int fromLeft = i-e >= 0? lo[i-e]: 0;
int fromRight = i+e < n? ro[i+e]: 0;
if(fromLeft > 0 && fromRight > 0){
ans += (long)fromLeft * (fromRight+1);
ans += fromRight;
}else if(fromLeft > 0){
ans += fromLeft;
}else if(fromRight > 0){
ans += fromRight;
}
}
}
System.out.println(ans);
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 973d941665c7e815695940d7260dff2b | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
public class Contest_yandexA{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean[] isPrime = new boolean[1000001];
for (int i = 2; i <= 1000000; ++i)
isPrime[i] = true;
for (int i = 2; i <= 1000; ++i) {
for (int j = i*i; j <= 1000000; j+=i)
isPrime[j] = false;
}
int t = input.nextInt();
for (int z = 0; z < t; ++z) {
int n = input.nextInt();
int e = input.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = input.nextInt();
long ans = 0;
for (int i = 0; i < n; ++i) {
if (isPrime[a[i]]) {
long bef = 0;
long aft = 0;
for (int j = i-e; j >= 0; j-=e) {
if (a[j]!=1)
break;
++bef;
}
for (int j = i+e; j < n; j+=e) {
if (a[j]!=1)
break;
++aft;
}
ans += (bef+1)*(aft+1)-1;
}
}
System.out.println(ans);
}
}
public static int gcd(int a,int b){
if(b == 0){
return a;
}
return gcd(b,a%b);
}
public static int lcm(int a,int b){
return (a / gcd(a, b)) * b;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | f1f1e7b7ef19ba4c274eb7e3ecd19722 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //Complex Market Analysis
import java.io.*;
import java.util.*;
public class C1609{
public static void main(String[] args)throws IOException{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int N = 1000005;
boolean[] isprime = new boolean[N];
Arrays.fill(isprime,true);
for(int k = 2; k < N; k++){
if(isprime[k]){
for(int j = 2*k; j < N; j += k){
isprime[j] = false;
}
}
}
isprime[0] = false;
isprime[1] = false;
int t = Integer.parseInt(f.readLine());
for(int q = 1; q <= t; q++){
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
int[] array = new int[n];
for(int k = 0; k < n; k++){
array[k] = Integer.parseInt(st.nextToken());
}
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left,-1);
Arrays.fill(right,-1);
for(int k = 0; k < e; k++){
int curleft = -1;
for(int j = k; j < n; j += e){
left[j] = curleft;
if(array[j] == 1){
if(curleft == -1){
curleft = j;
}
} else {
curleft = -1;
}
}
int curright = -1;
for(int j = n-k-1; j >= 0; j -= e){
right[j] = curright;
if(array[j] == 1){
if(curright == -1){
curright = j;
}
} else {
curright = -1;
}
}
}
long answer = 0L;
for(int k = 0; k < n; k++){
if(!isprime[array[k]]) continue;
long onleft = left[k] == -1 ? 1L : ((long)((k-left[k])/e)+1);
long onright = right[k] == -1 ? 1L : ((long)((right[k]-k)/e)+1);
answer += onleft*onright-1;
}
out.println(answer);
}
out.close();
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | f44e8e6103b677f63b4bb6bc8e01c5ec | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Omar {
static PrintWriter pw = new PrintWriter(System.out);
static Scanner sc=new Scanner(System.in);
static int[] lp;
static void sieveLinear(int N) {
ArrayList<Integer> primes = new ArrayList<Integer>();
lp = new int[N + 1]; // lp[i] = least prime divisor of i
for (int i = 2; i <= N; ++i) {
if (lp[i] == 0) {
primes.add(i);
lp[i] = i;
}
int curLP = lp[i];
for (int p : primes)// all primes smaller than or equal my lowest prime divisor
if (p > curLP || p * 1l * i > N)
break;
else
lp[p * i] = p;
}
}
public static void main(String[] args) throws IOException {
sieveLinear(1000000);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt(),e=sc.nextInt();
int []arr=sc.nextIntArray(n);
int []ones=new int[n],ones2=new int[n];
for(int i=0;i<n;i++) {
if(arr[i]==1)
ones[i]++;
if(arr[i]==1&& i-e>=0)ones[i]+=ones[i-e];
}
for(int i=n-1;i>=0;i--) {
if(arr[i]==1)
ones2[i]++;
if(i+e<n && arr[i]==1)ones2[i]+=ones2[i+e];
}
long ans=0;
for(int i=0;i<n;i++) {
if(lp[arr[i]]==arr[i]) {
int onesBe=0,onesAF=0;
if(i-e>=0)onesBe=ones[i-e];
if(i+e<n)onesAF=ones2[i+e];
ans+=1l*onesBe*(1+onesAF);
ans+=onesAF;
}
}
pw.println(ans);
}
pw.flush();
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static class PairPoint {
pair p1;
pair p2;
public PairPoint(pair p1, pair p2) {
this.p1 = p1;
this.p2 = p2;
}
public String toString() {
return p1.toString() + p2.toString();
}
public boolean equals(PairPoint pp) {
if (p1.equals(pp.p1) && p2.equals(pp.p2))
return true;
return false;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws IOException {
br = new BufferedReader(new FileReader(file));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | f5bbe8c6dd116445ab34463b4bd91768 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package codeforce.div2.deltix.Autumn2021;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1609/problem/C" target="_top">https://codeforces.com/contest/1609/problem/C</a>
* @since 28/11/21 8:39 PM
*/
public class C {
static FastScanner sc = new FastScanner(System.in);
static boolean[] isPrime;
public static void main(String[] args) {
prePrime();
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
int n = sc.nextInt();
int e = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
List<Integer> primeIdx = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (isPrime[arr[i]])
primeIdx.add(i);
}
long cnt = 0;
for (int pIDx : primeIdx) {
long mul = 1;
long right = 1;
while (pIDx + mul * e < n && arr[pIDx + (int) mul * e] == 1) {
right++;
mul++;
}
long left = 1;
mul = 1;
while (pIDx - mul * e >= 0 && arr[pIDx - (int) mul * e] == 1) {
left++;
mul++;
}
cnt += left * right - 1;
}
System.out.println(cnt);
}
}
}
private static void prePrime() {
isPrime = new boolean[1000000 + 1];
Arrays.fill(isPrime, true);
isPrime[1] = false;
for (int i = 2; i < isPrime.length; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j < isPrime.length; j += i)
isPrime[j] = false;
}
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 3508e82bc3d3f9ee4c45f1f1978a72ac | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package codeforce.div2.deltix.Autumn2021;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import static java.util.stream.Collectors.joining;
/**
* @author pribic (Priyank Doshi)
* @see <a href="https://codeforces.com/contest/1609/problem/C" target="_top">https://codeforces.com/contest/1609/problem/C</a>
* @since 28/11/21 8:39 PM
*/
public class C {
static FastScanner sc = new FastScanner(System.in);
static boolean[] isPrime;
public static void main(String[] args) {
prePrime();
try (PrintWriter out = new PrintWriter(System.out)) {
int T = sc.nextInt();
for (int tt = 1; tt <= T; tt++) {
int n = sc.nextInt();
int e = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
List<Integer> primeIdx = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (isPrime[arr[i]])
primeIdx.add(i);
}
long cnt = 0;
for (int pIDx : primeIdx) {
long mul = 1;
long right = 0;
while (pIDx + mul * e < n && arr[pIDx + (int) mul * e] == 1) {
right++;
mul++;
}
long left = 0;
mul = 1;
while (pIDx - mul * e >= 0 && arr[pIDx - (int) mul * e] == 1) {
left++;
mul++;
}
cnt += right;
cnt += left;
cnt += left * right;
}
System.out.println(cnt);
}
}
}
private static void prePrime() {
isPrime = new boolean[1000000 + 1];
Arrays.fill(isPrime, true);
isPrime[1] = false;
for (int i = 2; i < isPrime.length; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j < isPrime.length; j += i)
isPrime[j] = false;
}
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f), 32768);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 48baf4e8da622f16a1077876430a408f | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
public static boolean isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i <= Math. sqrt(num); i++)
{
if (num % i == 0)
return false;
}
return true;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int t=Integer.parseInt(br.readLine());
while(t-->0)
{
String str[]=br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int e = Integer.parseInt(str[1]);
String str2[]=br.readLine().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(str2[i]);
}
long count =0;
for(int i=0;i<n;i++)
{
if(isPrime(a[i]))
{
int p=0;
for(int k=i-e;k>=0&&a[k]==1;k=k-e)
{
count++;
p++;
}
for(int k=i+e;k<n&&a[k]==1;k=k+e)
count=count+p+1;
}
}
System.out.println(count+"\n");
}
bw.close();
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 5b7b757c51509b9f42fbcaab30f0420e | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static boolean[] prime ;
public static void main(String[] args) {
FastReader sc = new FastReader();
prime(1000000);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int e = sc.nextInt();
int m = (n-1)/e + 2 ;
int[][]arr = new int[e][m];
for (int j = 1 ; j < n+1; j++) {
int eind = j%e ;
int mind = (j-1)/e ;
arr[eind][mind] = sc.nextInt();
}
long ans = 0 ;
for (int j = 0 ; j < e ; j++) {
int l1 = 0 ;
int r1 = 0 ;
int state = -1 ;
for (int k = 0 ; k < m; k++) {
if (state == -1) {
if (arr[j][k] == 1) {
l1++;
} else if (prime[arr[j][k]]) {
ans +=l1 ;
state = 0 ;
} else {
l1 = 0 ;
}
} else if (state == 0 ) {
if (arr[j][k] == 1) {
r1++ ;
} else if (prime[arr[j][k]]) {
ans+=r1;
ans+= (long) l1 *r1;
ans+= r1 ;
l1 = r1 ;
r1 = 0 ;
} else {
ans+=r1;
ans+= (long) l1 *r1;
l1 = 0 ;
r1 = 0 ;
state = -1 ;
}
}
}
}
System.out.println(ans);
}
}
static void prime(int x) {
//sieve algorithm. nlog(log(n)).
prime = new boolean[(x + 1)];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i * i <= x; i++)
if (prime[i])
for (int j = i * i; j <= x; j += i)
prime[j] = false;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 535da97db3f7188ff490ab1a051242c2 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int z=0;z<t;z++){
int n=sc.nextInt();
int e=sc.nextInt();
int arr[]=new int[n];
HashSet<Integer> set=new HashSet<>();
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
if(arr[i]==1) continue;
int j=2;
int flag=0;
while(j*j<=arr[i]){
if(arr[i]%j==0){
flag=1;
break;
}
j++;
}
if(flag==0){
set.add(arr[i]);
}
}
int dp[][]=new int[n][2];
for(int i=0;i<n;i++){
if(arr[i]==1){
dp[i][0]=1;
if(i-e>=0){
dp[i][0]+=dp[i-e][0];
}
}
}
for(int i=n-1;i>=0;i--){
if(arr[i]==1){
dp[i][1]=1;
if(i+e<n){
dp[i][1]+=dp[i+e][1];
}
}
}
long count=0;
for(int i=0;i<n;i++){
long size1=0,size2=0;
if(set.contains(arr[i])){
if(i-e>=0 && arr[i-e]==1){
size1+=dp[i-e][0];
}
if(i+e<n && arr[i+e]==1){
size2+=dp[i+e][1];
}
}
long size=size1+size2;
if(size>=1) count+=(size+(1L*size1*size2));
}
System.out.println(count);
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 349b1f62ecb2deea647310528f56c6ac | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class c {
public static void main(String[] args) {
FastScanner scan=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
boolean[] prime=new boolean[1000001];
Arrays.fill(prime,true);
prime[1]=false;
for(int i=2;i<prime.length;i++) {
if(!prime[i]) continue;
for(int j=i+i;j<prime.length;j+=i) {
prime[j]=false;
}
}
int t=scan.nextInt();
for(int tt=0;tt<t;tt++) {
int n=scan.nextInt(), step=scan.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++) a[i]=scan.nextInt();
// System.out.println("go");
long res=0L;
long[] onesleft=new long[n];
long[] onesright=new long[n];
for(int start=0;start<step;start++) {
int ones=0;
for(int i=start;i<n;i+=step) {
onesleft[i]=ones;
if(a[i]==1) ones++;
else ones=0;
}
}
for(int e=0;e<step;e++) {
int start=n-e-1;
int ones=0;
for(int i=start;i>=0;i-=step) {
onesright[i]=ones;
if(a[i]==1) ones++;
else ones=0;
}
}
// System.out.println(Arrays.toString(onesleft));
// System.out.println(Arrays.toString(onesright));
for(int start=0;start<step;start++) {
for(int i=start;i<n;i+=step) {
if(prime[a[i]]) {
res+=(onesleft[i]+1)*(onesright[i]+1)-1;
}
}
}
out.println(res);
}
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e){e.printStackTrace();}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {st = new StringTokenizer(br.readLine());}
catch (Exception e) {e.printStackTrace();}
return st.nextToken();
}
public int nextInt() {return Integer.parseInt(next());}
public long nextLong() {return Long.parseLong(next());}
public double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {
String line = "";
if(st.hasMoreTokens()) line = st.nextToken();
else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
while(st.hasMoreTokens()) line += " "+st.nextToken();
return line;
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | c4739299f0e91514ee3ecb3b1b2623c7 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.*;
import java.util.*;
public final class Main {
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)};
static int mod = (int) (1e9 + 7);
static int mod2 = 998244353;
public static void main(String[] args) {
int tt = i();
while (tt-- > 0) {
solve();
}
out.flush();
}
public static void solve() {
int n = i();
int e = i();
int[] a = input(n);
long ans = 0;
for (int i = 0; i < e; i++) {
List<Integer> list = new ArrayList<>();
int j = i;
while (j < n) {
list.add(a[j]);
j += e;
}
int[] ro = new int[list.size()];
int[] lo = new int[list.size()];
for (int k = 0; k < list.size(); k++) {
lo[k] = (list.get(k) == 1 ? (1 + ((k - 1 >= 0) ? lo[k - 1] : 0)) : 0);
}
for (int k = list.size() - 1; k >= 0; k--) {
ro[k] = (list.get(k) == 1 ? (1 + ((k + 1 < list.size()) ? ro[k + 1] : 0)) : 0);
}
for (int k = 0; k < list.size(); k++) {
if (isPrime(list.get(k))) {
int left = ((k - 1 >= 0) ? lo[k - 1] : 0);
int right = ((k + 1 < list.size()) ? ro[k + 1] : 0);
ans += (long) (left) * right + left + right;
}
}
}
out.println(ans);
}
static long[] pre(int[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] pre(long[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static void print(char A[]) {
for (char c : A) {
out.print(c);
}
out.println();
}
static void print(boolean A[]) {
for (boolean c : A) {
out.print(c + " ");
}
out.println();
}
static void print(int A[]) {
for (int c : A) {
out.print(c + " ");
}
out.println();
}
static void print(long A[]) {
for (long i : A) {
out.print(i + " ");
}
out.println();
}
static void print(List<Integer> A) {
for (int a : A) {
out.print(a + " ");
}
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static String c() {
return in.next();
}
static int[][] inputWithIdx(int N) {
int A[][] = new int[N][2];
for (int i = 0; i < N; i++) {
A[i] = new int[]{i, in.nextInt()};
}
return A;
}
static int[] input(int N) {
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[] = new long[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextLong();
}
return A;
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long LCM(int a, int b) {
return (long) a / GCD(a, b) * b;
}
static long LCM(long a, long b) {
return a / GCD(a, b) * b;
}
static void shuffleAndSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int[] temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr, comparator);
}
static void shuffleAndSort(long[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
long temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static boolean isPerfectSquare(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static void swap(int A[], int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
static void swap(char A[], int a, int b) {
char t = A[a];
A[a] = A[b];
A[b] = t;
}
static long pow(long a, long b, int mod) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow = (pow * x) % mod;
}
x = (x * x) % mod;
b /= 2;
}
return pow;
}
static long pow(long a, long b) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow *= x;
}
x = x * x;
b /= 2;
}
return pow;
}
static long modInverse(long x, int mod) {
return pow(x, mod - 2, mod);
}
static boolean isPrime(long N) {
if (N <= 1) {
return false;
}
if (N <= 3) {
return true;
}
if (N % 2 == 0 || N % 3 == 0) {
return false;
}
for (int i = 5; i * i <= N; i = i + 6) {
if (N % i == 0 || N % (i + 2) == 0) {
return false;
}
}
return true;
}
public static String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
public static void reverse(int[] arr) {
for (int i = 0; i < arr.length / 2; i++) {
int tmp = arr[i];
arr[arr.length - 1 - i] = tmp;
arr[i] = arr[arr.length - 1 - i];
}
}
public static String repeat(char ch, int repeat) {
if (repeat <= 0) {
return "";
}
final char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {
buf[i] = ch;
}
return new String(buf);
}
public static int[] manacher(String s) {
char[] chars = s.toCharArray();
int n = s.length();
int[] d1 = new int[n];
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
return d1;
}
public static int[] kmp(String s) {
int n = s.length();
int[] res = new int[n];
for (int i = 1; i < n; ++i) {
int j = res[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = res[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
++j;
}
res[i] = j;
}
return res;
}
}
class Pair {
int i;
int j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return i == pair.i && j == pair.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class Node {
int val;
public Node(int val) {
this.val = val;
}
}
class ST {
int n;
Node[] st;
ST(int n) {
this.n = n;
st = new Node[4 * Integer.highestOneBit(n)];
}
void build(Node[] nodes) {
build(0, 0, n - 1, nodes);
}
private void build(int id, int l, int r, Node[] nodes) {
if (l == r) {
st[id] = nodes[l];
return;
}
int mid = (l + r) >> 1;
build((id << 1) + 1, l, mid, nodes);
build((id << 1) + 2, mid + 1, r, nodes);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
void update(int i, Node node) {
update(0, 0, n - 1, i, node);
}
private void update(int id, int l, int r, int i, Node node) {
if (i < l || r < i) {
return;
}
if (l == r) {
st[id] = node;
return;
}
int mid = (l + r) >> 1;
update((id << 1) + 1, l, mid, i, node);
update((id << 1) + 2, mid + 1, r, i, node);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
Node get(int x, int y) {
return get(0, 0, n - 1, x, y);
}
private Node get(int id, int l, int r, int x, int y) {
if (x > r || y < l) {
return new Node(0);
}
if (x <= l && r <= y) {
return st[id];
}
int mid = (l + r) >> 1;
return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y));
}
Node comb(Node a, Node b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return new Node(a.val | b.val);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 7766bdc4f6bcf11b88792aa72a4f19ae | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class C {
public static void main(String[] args) throws IOException {
/**/
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
/*/
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/c.in"))));
/**/
boolean[] isPrime = new boolean[1000001];
for (int i = 2; i <= 1000000; ++i)
isPrime[i] = true;
for (int i = 2; i <= 1000; ++i) {
for (int j = i*i; j <= 1000000; j+=i)
isPrime[j] = false;
}
int t = sc.nextInt();
for (int z = 0; z < t; ++z) {
int n = sc.nextInt();
int e = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = sc.nextInt();
long ans = 0;
for (int i = 0; i < n; ++i) {
if (isPrime[a[i]]) {
long bef = 0;
long aft = 0;
for (int j = i-e; j >= 0; j-=e) {
if (a[j]!=1)
break;
++bef;
}
for (int j = i+e; j < n; j+=e) {
if (a[j]!=1)
break;
++aft;
}
ans += (bef+1)*(aft+1)-1;
}
}
System.out.println(ans);
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | c250f93d26cae3d32990217583780198 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
/*
1
7 3
10 2 1 3 1 19 3
*/
public class C{
static FastReader sc=null;
static boolean prime[];
static int nax=(int)1e6+5;
public static void main(String[] args) {
sc=new FastReader();
sieve();
int t=sc.nextInt();
for(int tt=0;tt<t;tt++) {
int n=sc.nextInt(),e=sc.nextInt();
int a[]=sc.readArray(n);
int left[]=new int[n],right[]=new int[n];
Arrays.fill(left, 1);
Arrays.fill(right, 1);
for(int i=0;i<n;i++) {
if(i-e>=0 && a[i-e]==1) {
left[i]+=left[i-e];
}
}
for(int i=n-1;i>=0;i--) {
if(i+e<n && a[i+e]==1) {
right[i]+=right[i+e];
}
}
long ans=0;
for(int i=0;i<n;i++) {
if(prime[a[i]]) {
ans+=(left[i]*1L*right[i])-1;
}
}
System.out.println(ans);
}
}
static void sieve() {
prime=new boolean[nax];
Arrays.fill(prime, true);
prime[0]=prime[1]=false;
for(int i=2;i<nax;i++) {
if(prime[i])for(int j=2*i;j<nax;j+=i)prime[j]=false;
}
}
static int[] ruffleSort(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
return a;
}
static void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
StringTokenizer st=new StringTokenizer("");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String next() {
while(!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 224056a1359f2bdf2c7dfbf08c81a532 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package kg.my_algorithms.Codeforces;
/*
If you can't Calculate, then Stimulate
*/
/*
1) Physical Strength
2) Mental Strength
3) Emotional Strength
*/
import java.util.*;
import java.io.*;
public class Solution {
static boolean[] isPrime = new boolean[1_000_001];
private static final FastReader fr = new FastReader();
public static void main(String[] args) throws IOException {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
Arrays.fill(isPrime,true);
for(int i=2;i<isPrime.length;i++){
if(isPrime[i]){
for(int j=2*i;j<isPrime.length;j+=i) isPrime[j] = false;
}
}
// System.out.println("Is 2 a prime number= " + isPrime[2]);
int testCases = fr.nextInt();
for(int test=1;test<=testCases;test++){
int n = fr.nextInt();
int e = fr.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++) arr[i] = fr.nextInt();
long res = 0L;
for(int i=0;i<e;i++){
List<Integer> list = new ArrayList<>();
for(int j=i;j<n;j=j+e) list.add(arr[j]);
res += getNumberOfPairs(list);
}
sb.append(res).append("\n");
}
output.write(sb.toString());
output.flush();
}
private static long getNumberOfPairs(List<Integer> list){
int[] numberOfOnesLeft = new int[list.size()];
List<Integer> primeIndex = new ArrayList<>();
for(int i=0;i<list.size();i++){
if(i==0){
if(list.get(0)==1) numberOfOnesLeft[0] = 1;
else if(isPrime[list.get(0)]) primeIndex.add(i);
continue;
}
int value = list.get(i);
if(list.get(i-1)==1) numberOfOnesLeft[i] = numberOfOnesLeft[i-1];
if(value == 1) numberOfOnesLeft[i]++;
else if(isPrime[value]) primeIndex.add(i);
}
int[] numberOfOnesRight = new int[list.size()];
for(int i=list.size()-1;i>=0;i--){
if(i==list.size()-1){
if(list.get(i)==1) numberOfOnesRight[i] = 1;
continue;
}
int value = list.get(i);
if(list.get(i+1)==1) numberOfOnesRight[i] = numberOfOnesRight[i+1];
if(value==1) numberOfOnesRight[i]++;
}
long res = 0;
for(int index: primeIndex){
int leftOnes = numberOfOnesLeft[index];
int rightOnes = numberOfOnesRight[index];
// if(list.get(0)==2){
// System.out.println("lo= " + leftOnes+" ro= "+ rightOnes);
// }
res += ((leftOnes+1L)*(rightOnes+1L)-1L);
}
// if(list.get(0)==2){
// System.out.println("noor= " + Arrays.toString(numberOfOnesRight));
// }
// System.out.println("list= " + list +" res= " + res);
return res;
}
}
/*
1
7 3
10 2 1 3 1 19 3
*/
//Fast Input
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 23e661df9b3ea4f2cedfe198aaa53f92 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class ComplexMarketAnalysis {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t!=0){
solve(br,pr);
t--;
}
pr.flush();
pr.close();
}
public static void solve(BufferedReader br,PrintWriter pr) throws IOException{
String[] temp=br.readLine().split(" ");
int n=Integer.parseInt(temp[0]);
int e=Integer.parseInt(temp[1]);
temp=br.readLine().split(" ");
int[] nums=new int[n];
int[] flag=new int[n];
for(int i=0;i<n;i++){
nums[i]=Integer.parseInt(temp[i]);
if(nums[i]==1){
flag[i]=1;
}
else if(isPrime(nums[i])){
flag[i]=2;
}
}
long res=0;
for(int i=0;i<n;i++){
if(flag[i]==2){
int left=0;
for(int j=i-e;j>=0;j-=e){
if(flag[j]==1){
left++;
}
else{
break;
}
}
int right=0;
for(int j=i+e;j<n;j+=e){
if(flag[j]==1){
right++;
}
else{
break;
}
}
res+=(long)(left+1)*(long)(right+1)-1;
}
}
pr.println(res);
}
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | aa8f4f166ebe960ad9bbbbc31d590149 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.Scanner;
public class C1609 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
int E = in.nextInt();
int[] A = new int[N];
for (int n=0; n<N; n++) {
A[n] = in.nextInt();
}
long answer = 0;
for (int n=0; n<N; n++) {
if (isPrime(A[n])) {
long onesBefore = 0;
int i = n-E;
while (i >= 0 && A[i] == 1) {
onesBefore++;
i -= E;
}
long onesAfter = 0;
i = n+E;
while (i < N && A[i] == 1) {
onesAfter++;
i += E;
}
answer += (onesBefore+1)*(onesAfter+1)-1;
}
}
System.out.println(answer);
}
}
private static boolean isPrime(int number) {
if (number == 1) return false;
for (int d=2; d*d<=number; d++) {
if (number%d == 0) {
return false;
}
}
return true;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 73c0ade33fc90b6b23e56db52f518281 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
public class Contest_yandexA{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean[] isPrime = new boolean[1000001];
for (int i = 2; i <= 1000000; ++i)
isPrime[i] = true;
for (int i = 2; i <= 1000; ++i) {
for (int j = i*i; j <= 1000000; j+=i)
isPrime[j] = false;
}
int t = input.nextInt();
for (int z = 0; z < t; ++z) {
int n = input.nextInt();
int e = input.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = input.nextInt();
long ans = 0;
for (int i = 0; i < n; ++i) {
if (isPrime[a[i]]) {
long bef = 0;
long aft = 0;
for (int j = i-e; j >= 0; j-=e) {
if (a[j]!=1)
break;
++bef;
}
for (int j = i+e; j < n; j+=e) {
if (a[j]!=1)
break;
++aft;
}
ans += (bef+1)*(aft+1)-1;
}
}
System.out.println(ans);
}
}
public static int gcd(int a,int b){
if(b == 0){
return a;
}
return gcd(b,a%b);
}
public static int lcm(int a,int b){
return (a / gcd(a, b)) * b;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 88c504e511202083405f3e76c3c4ee95 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
public class Codeforces1609C{
static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
public static void main (String args[]) throws NumberFormatException, IOException {
InputReader in = new InputReader(System.in);
PrintWriter pr = new PrintWriter(System.out);
int numTc = in.nextInt();
for(int tc = 0; tc <numTc; tc++) {
int n = in.nextInt();
int e = in.nextInt();
int data []= in.nextIntArray(n);
boolean vis[] = new boolean [n];
long dp [] = new long [n];
long total = 0;
for(int start = 0; start<n; start++)
{
if(vis[start])
continue;
int itr = start;
int num1 = 0;
while(itr<n)
{
vis[itr] = true;
if(data[itr] == 1)
{
num1++;
}
else
{
if(isPrime(data[itr]))
{
total+= num1;
dp[itr] = num1;
}
num1 = 0;
}
itr+=e;
}
num1 = 0;
itr-=e;
while(itr>=0)
{
if(data[itr] == 1)
{
num1++;
}
else
{
if(isPrime(data[itr]))
{
total+= num1;
total+= num1*dp[itr];
}
num1 = 0;
}
itr-=e;
}
}
pr.println(total);
}
pr.flush();
}
static class InputReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 749a7da7df3f5f95663bc3fc63ddb106 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Practice1 {
// static long[] sort(long[] arr) {
// int n=arr.length;
// ArrayList<Long> al=new ArrayList<>();
// for(int i=0;i<n;i++) {
// al.add(arr[i]);
// }
// Collections.sort(al);
// for(int i=0;i<n;i++) {
// arr[i]=al.get(i);
// }
// return arr;
// }
//
// static long nCr(int n, int r)
// {
// // int x=1000000007;
// long dp[][]=new long[2][r+1];
//
// for(int i=0;i<=n;i++){
// for(int j=0;j<=i&&j<=r;j++){
// if(i==0||j==0||i==j){
// dp[i%2][j]=1;
// }else {
//
// // dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1])%x;
// dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1]);
// }
//
// }
// }
//
// return dp[n%2][r];
//
// }
//
public static class UnionFind {
private final int[] p;
public UnionFind(int n) {
p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
public int find(int x) {
return x == p[x] ? x : (p[x] = find(p[x]));
}
public void union(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
p[x] = y;
}
}
}
public static boolean ispalin(String str) {
int n=str.length();
for(int i=0;i<n/2;i++) {
if(str.charAt(i)!=str.charAt(n-i-1)) {
return false;
}
}
return true;
}
static long power(long N,long R)
{
long x=1000000007;
if(R==0) return 1;
if(R==1) return N;
long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p
temp=(temp*temp)%x;
if(R%2==0){
return temp%x;
}else{
return (N*temp)%x;
}
}
public static String binary(int n) {
StringBuffer ans=new StringBuffer();
int a=4;
while(a-->0) {
int temp=(n&1);
if(temp!=0) {
ans.append('1');
}else {
ans.append('0');
}
n =n>>1;
}
ans=ans.reverse();
return ans.toString();
}
public static int find(String[][] arr,boolean[][] vis,int dir,int i,int j) {
if(i<0||i>=arr.length||j<0||j>=arr[0].length) return 0;
if(vis[i][j]==true) return 0;
if(dir==1&&arr[i][j].charAt(0)=='1') return 0;
if(dir==2&&arr[i][j].charAt(1)=='1') return 0;
if(dir==3&&arr[i][j].charAt(2)=='1') return 0;
if(dir==4&&arr[i][j].charAt(3)=='1') return 0;
vis[i][j]=true;
int a=find(arr,vis,1,i+1,j);
int b=find(arr,vis,2,i-1,j);
int c=find(arr,vis,3,i,j+1);
int d=find(arr,vis,4,i,j-1);
return 1+a+b+c+d;
}
// static ArrayList<Integer> allDivisors(int n) {
// ArrayList<Integer> al=new ArrayList<>();
// int i=2;
// while(i*i<=n) {
// if(n%i==0) al.add(i);
// if(n%i==0&&i*i!=n) al.add(n/i);
// i++;
// }
// return al;
// }
// static int[] sort(int[] arr) {
// int n=arr.length;
// ArrayList<Integer> al=new ArrayList<>();
// for(int i=0;i<n;i++) {
// al.add(arr[i]);
// }
// Collections.sort(al);
// for(int i=0;i<n;i++) {
// arr[i]=al.get(i);
// }
// return arr;
// }
//
/** Code for Dijkstra's algorithm **/
public static class ListNode {
int vertex, weight;
ListNode(int v, int w) {
vertex = v;
weight = w;
}
int getVertex() { return vertex; }
int getWeight() { return weight; }
}
public static int[] dijkstra(
int V, ArrayList<ArrayList<ListNode> > graph,
int source) {
int[] distance = new int[V];
for (int i = 0; i < V; i++)
distance[i] = Integer.MAX_VALUE;
distance[0] = 0;
PriorityQueue<ListNode> pq = new PriorityQueue<>(
(v1, v2) -> v1.getWeight() - v2.getWeight());
pq.add(new ListNode(source, 0));
while (pq.size() > 0) {
ListNode current = pq.poll();
for (ListNode n :
graph.get(current.getVertex())) {
if (distance[current.getVertex()]
+ n.getWeight()
< distance[n.getVertex()]) {
distance[n.getVertex()]
= n.getWeight()
+ distance[current.getVertex()];
pq.add(new ListNode(
n.getVertex(),
distance[n.getVertex()]));
}
}
}
// If you want to calculate distance from source to
// a particular target, you can return
// distance[target]
return distance;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static class Pair{
int l;
int r;
char c;
Pair(int l,int r,char c){
this.l=l;
this.r=r;
this.c=c;
}
}
public static void fill(int[][] arr,boolean[][] vis, int i,int j,int k,int l) {
int n=arr.length;
int m=arr[0].length;
if(i<0||j<0||i>=n||j>=m||vis[i][j]==true) return;
vis[i][j]=true;
int max=0;
if(i+1<n) {
max=Math.max(arr[i+1][j],max);
}
if(i-1>=0) {
max=Math.max(arr[i-1][j],max);
}
if(j+1<m) {
max=Math.max(arr[i][j+1],max);
}
if(j-1>=0) {
max=Math.max(arr[i][j-1],max);
}
arr[i][j]=Math.min(l-max,k);
fill(arr,vis,i+1,j,k,l);
fill(arr,vis,i-1,j,k,l);
fill(arr,vis,i,j+1,k,l);
fill(arr,vis,i,j-1,k,l);
}
static long nCr(int n, int r)
{
// int x=1000000007;
long dp[][]=new long[2][r+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=i&&j<=r;j++){
if(i==0||j==0||i==j){
dp[i%2][j]=1;
}else {
// dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1])%x;
dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1]);
}
}
}
return dp[n%2][r];
}
static int[] sort(int[] arr) {
int n=arr.length;
ArrayList<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
static boolean isPrime(int n)
{
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
public static void main (String[] args) {
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// out.print();
//out.println();
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int e=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
int[] dpleft=new int[n];
int[] dpright=new int[n];
for(int i=n-1;i>=0;i--) {
if(arr[i]==1) {
if(i+e>=n) {
dpright[i]=1;
}else {
dpright[i]=dpright[i+e]+1;
}
}
}
for(int i=0;i<n;i++) {
if(arr[i]==1) {
if(i-e<0) {
dpleft[i]=1;
}else {
dpleft[i]=dpleft[i-e]+1;
}
}
}
long count=0;
for(int i=0;i<n;i++) {
if(isPrime(arr[i])==true) {
long a=0,b=0;
if(i-e>=0) a=dpleft[i-e];
if(i+e<n) b=dpright[i+e];
if(a!=0&&b!=0) {
count +=a*b+a+b;
}else {
count +=a+b;
}
}
}
out.println(count);
}
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 75fa24af3046e48ac3d49c142177dc49 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import static java.lang.Math.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class cf1 {
static FastReader x = new FastReader();
static OutputStream outputStream = System.out;
static PrintWriter out = new PrintWriter(outputStream);
/*---------------------------------------CODE STARTS HERE-------------------------*/
public static void main(String[] args) {
int t = x.nextInt();
boolean s[] = esieve(1000001);
StringBuilder str = new StringBuilder();
while (t > 0) {
int n =x.nextInt();
int e = x.nextInt();
s[1]=false;
HashSet<Integer> prime = new HashSet<>();
HashSet<Integer> one = new HashSet<>();
int a[] = new int[n];
for(int i=0;i<n;i++) {
a[i] = x.nextInt();
if(s[a[i]]) {
// System.out.println("prime "+a[i]);
prime.add(i);
}else if(a[i]==1) {
one.add(i);
}
}
long ans =0;
for(int curr:prime) {
int temp =curr;
long tans =0,tans1=0;
while(temp<n&&one.contains(temp+e)) {
tans++;
// System.out.println(temp+" i");
temp = temp+e;
}
ans+=tans;
temp = curr;
while(temp>0&&one.contains(temp-e)) {
tans1++;
// System.out.println(temp+" d");
temp=temp-e;
}
if(tans1>0)
ans+=((tans*tans1)+tans1);
}
System.out.println(ans);
str.append("\n");
t--;
}
out.println(str);
out.flush();
}
/*--------------------------------------------FAST I/O--------------------------------*/
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
char nextchar() {
char ch = ' ';
try {
ch = (char) br.read();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}
/*--------------------------------------------BOILER PLATE---------------------------*/
static int[] readarr(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = x.nextInt();
}
return arr;
}
static int[] sortint(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static long[] sortlong(long a[]) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < al.size(); i++) {
a[i] = al.get(i);
}
return a;
}
static int pow(int x, int y) {
int result = 1;
while (y > 0) {
if (y % 2 == 0) {
x = x * x;
y = y / 2;
} else {
result = result * x;
y = y - 1;
}
}
return result;
}
static int[] revsort(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al, Comparator.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static int[] gcd(int a, int b, int ar[]) {
if (b == 0) {
ar[0] = a;
ar[1] = 1;
ar[2] = 0;
return ar;
}
ar = gcd(b, a % b, ar);
int t = ar[1];
ar[1] = ar[2];
ar[2] = t - (a / b) * ar[2];
return ar;
}
static boolean[] esieve(int n) {
boolean p[] = new boolean[n + 1];
Arrays.fill(p, true);
for (int i = 2; i * i <= n; i++) {
if (p[i] == true) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
static ArrayList<Integer> primes(int n) {
boolean p[] = new boolean[n + 1];
ArrayList<Integer> al = new ArrayList<>();
Arrays.fill(p, true);
int i = 0;
for (i = 2; i * i <= n; i++) {
if (p[i] == true) {
al.add(i);
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
for (i = i; i <= n; i++) {
if (p[i] == true) {
al.add(i);
}
}
return al;
}
static int etf(int n) {
int res = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
res /= i;
res *= (i - 1);
while (n % i == 0) {
n /= i;
}
}
}
if (n > 1) {
res /= n;
res *= (n - 1);
}
return res;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 1e26899c313e4ef5bf41f8578d01504d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class MarketAnalysis{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static void solve() {
int n = sc.nextInt();
int e = sc.nextInt();
int arr[] = sc.readIntArray(n);
int left[] = new int[n];
int right[] = new int[n];
for(int i = 0;i<n;i++){
if(arr[i]==1){
left[i] = 1+((i-e>=0)?left[i-e]:0);
}
}
for(int i = n-1;i>=0;i--){
if(arr[i]==1){
right[i] = 1+((i+e<n)?right[i+e]:0);
}
}
long ans = 0;
for(int i = 0;i<n;i++){
if(isPrime(arr[i])){
long val1 = 1+((i-e>=0)?left[i-e]:0);
long val2 = 1+((i+e<n)?right[i+e]:0);
ans += ((val1*val2)-1);
}
}
out.println(ans);
}
static boolean isPrime(int n){
if(n<=1) return false;
if(n==2 || n==3) return true;
if(n%2==0 || n%3==0){
return false;
}
for(int i = 5;i*i<=n;i+= 6){
if(n%i==0 || n%(i+2)==0)
return false;
}
return true;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-- >0){
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | cddae30da58e10f2b1dae48da89425a2 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
Set<Integer> primes = getPrimes(100);
int tc = io.nextInt();
for (int i = 0; i < tc; i++) {
solve();
}
io.close();
}
static Set<Integer> primes = getPrimes(1_000_005);
private static void solve() throws Exception {
int n = io.nextInt();
int e = io.nextInt();
int[] data = io.nextInts(n);
List<List<Integer>> groups = new ArrayList<>();
for (int i = 0; i < e; i++) {
groups.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
groups.get(i % e).add(data[i]);
}
long output = 0;
for (int a = 0; a < groups.size(); a++) {
List<Integer> grup = groups.get(a);
boolean prevPrime = false;
int prevOne = 0;
int currOne = 0;
for (int i = 0; i < grup.size(); i++) {
if (grup.get(i) == 1) {
currOne++;
if (prevPrime) {
output += prevOne + 1;
}
} else if (primes.contains(grup.get(i))) {
prevPrime = true;
prevOne = currOne;
currOne = 0;
output += prevOne;
} else {
prevPrime = false;
prevOne = 0;
currOne = 0;
}
}
}
io.println(output);
}
private static Set<Integer> getPrimes(int limit) {
boolean[] isPrime = new boolean[limit];
Arrays.fill(isPrime, true);
Set<Integer> primes = new HashSet<>();
for (int i = 2; i < limit; i++) {
if (isPrime[i]) {
for (int j = i + i; j < limit; j += i) {
isPrime[j] = false;
}
primes.add(i);
}
}
return primes;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>(a.length);
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
//-----------PrintWriter for faster output---------------------------------
public static FastIO io = new FastIO();
//-----------MyScanner class for faster input----------
static class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public String nextLine() {
int c;
do {
c = nextByte();
} while (c < '\n');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > '\n');
return res.toString();
}
public int nextInt() {
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() {
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
int[] nextInts(int n) {
int[] data = new int[n];
for (int i = 0; i < n; i++) {
data[i] = io.nextInt();
}
return data;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
//--------------------------------------------------------
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | e7a6d8fd05e95e2702f875f9426d0714 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef {
static long fans[] = new long[200001];
static long inv[] = new long[200001];
static long mod = 1000000007;
static void init() {
fans[0] = 1;
inv[0] = 1;
fans[1] = 1;
inv[1] = 1;
for (int i = 2; i < 200001; i++) {
fans[i] = ((long) i * fans[i - 1]) % mod;
inv[i] = power(fans[i], mod - 2);
}
}
static long ncr(int n, int r) {
return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod;
}
static int max = 1000001;
static boolean primes[] = new boolean[max];
static void primeinit() {
primes[1] = true;
for(int i = 2;i<max;i++)
{
if(!primes[i]) {
for(int j = 2;j*i<max;j++)
primes[i*j] = true;
}
}
}
public static void main(String[] args) throws java.lang.Exception {
FastReader in = new FastReader(System.in);
StringBuilder sb = new StringBuilder();
primeinit();
int t = 1;
t = in.nextInt();
while (t > 0) {
--t;
int n = in.nextInt();
int e = in.nextInt();
int arr[] = new int[n];
for(int i = 0;i<arr.length;i++)
arr[i] = in.nextInt();
int left[] = new int[n];
int right[] = new int[n];
for(int i = 0,j = n-1;i<n;i++,j--)
{
if(arr[i] == 1)
left[i] = 1 + (i-e>=0?left[i-e]:0);
if(arr[j] == 1)
right[j] = 1 + (j+e<n?right[j+e]:0);
}
long ans = 0;
for(int i = 0;i<n;i++)
{
if(!primes[arr[i]])
{
long val1 = 1 + (i-e>=0?left[i-e]:0);
long val2 = 1 + (i+e<n?right[i+e]:0);
ans+= ((val1*val2)-1);
}
}
sb.append(ans+"\n");
}
System.out.print(sb);
}
static long power(long x, long y) {
long res = 1; // Initialize result
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = ((res % mod) * (x % mod)) % mod;
// y must be even now
y = y >> 1; // y = y/2
x = ((x % mod) * (x % mod)) % mod; // Change x to x^2
}
return res;
}
static long[] generateArray(FastReader in, int n) throws IOException {
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = in.nextLong();
return arr;
}
static long[][] generatematrix(FastReader in, int n, int m) throws IOException {
long arr[][] = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = in.nextLong();
}
}
return arr;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
}
class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
String nextLine() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
return (char) c;
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | dac6d43c7cb0fb425459aeff217cc689 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
public final class A
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<Integer> g[];
static long mod=(long) 998244353,INF=Long.MAX_VALUE;
static boolean set[];
static int par[],col[],D[];
static long A[];
public static void main(String args[])throws IOException
{
/*
* star,rope,TPST
* BS,LST,MS,MQ
*/
int prime[]=new int[(int)(1e6)+5];
for(int i=2; i<=1e6; i++)
{
if(prime[i]==0)
{
long x=i;
if(x*x<=1e6)
{
for(int j=i*i; j<=1e6; j+=i)
{
prime[j]=1;
}
}
}
}
prime[1]=1;
int T=i();
while(T-->0)
{
int N=i(),e=i();
int A[]=input(N);
long tot=0;
long post[]=new long[N+1];
long pre[]=new long[N+1];
for(int i=N-1; i>=0; i--)
{
if(A[i]==1)
{
if(i+e<N )post[i]=post[i+e];
post[i]++;
}
}
for(int i=0; i<N; i++)
{
if(A[i]==1)
{
if(i>=e)pre[i]=pre[i-e];
pre[i]++;
}
}
//print(pre);
// print(post);
for(int i=0; i<N; i++)
{
if(prime[A[i]]==0)
{
long l=0,r=0;
//if(i+e>=N && i-e<0)continue;
if( i+e<N)
{
r=post[i+e];
}
if(i-e>=0)
{
l=pre[i-e];
}
tot+=l;
tot+=r;
tot+=(l*r);
}
}
ans.append(tot+"\n");
}
out.println(ans);
out.close();
}
static int compute(char X[],int i)
{
int N=X.length;
int c=0;
for(int j=i-2; j<=i+2; j++)
{
if(j>=0 && j+2<N)
{
if(X[j]=='a'&& X[j+1]=='b' && X[j+2]=='c')c++;
}
}
return c;
}
static boolean f(long X[],long V[],double m)
{
double l=0,r=1e18;
for(int i=0; i<X.length; i++)
{
double d=((double)(V[i]))*m;
double x=(double)X[i];
l=Math.max(l, x-d);
r=Math.min(r, x+d);
// System.out.println(l+" "+r);
}
return l<=r;
}
static int query(long a,TreeNode root)
{
long p=1L<<30;
TreeNode temp=root;
while(p!=0)
{
System.out.println(a+" "+p);
if((a&p)!=0)
{
temp=temp.right;
}
else temp=temp.left;
p>>=1;
}
return temp.index;
}
static void delete(long a,TreeNode root)
{
long p=1L<<30;
TreeNode temp=root;
while(p!=0)
{
if((a&p)!=0)
{
temp.right.cnt--;
temp=temp.right;
}
else
{
temp.left.cnt--;
temp=temp.left;
}
p>>=1;
}
}
static void insert(long a,TreeNode root,int i)
{
long p=1L<<30;
TreeNode temp=root;
while(p!=0)
{
if((a&p)!=0)
{
temp.right.cnt++;
temp=temp.right;
}
else
{
temp.left.cnt++;
temp=temp.left;
}
p>>=1;
}
temp.index=i;
}
static TreeNode create(int p)
{
TreeNode root=new TreeNode(0);
if(p!=0)
{
root.left=create(p-1);
root.right=create(p-1);
}
return root;
}
static boolean f(long A[],long m,int N)
{
long B[]=new long[N];
for(int i=0; i<N; i++)
{
B[i]=A[i];
}
for(int i=N-1; i>=0; i--)
{
if(B[i]<m)return false;
if(i>=2)
{
long extra=Math.min(B[i]-m, A[i]);
long x=extra/3L;
B[i-2]+=2L*x;
B[i-1]+=x;
}
}
return true;
}
static int f(int l,int r,long A[],long x)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]>=x)l=m;
else r=m;
}
return r;
}
static boolean f(long m,long H,long A[],int N)
{
long s=m;
for(int i=0; i<N-1;i++)
{
s+=Math.min(m, A[i+1]-A[i]);
}
return s>=H;
}
static boolean f(int i,int j,long last,boolean win[])
{
if(i>j)return false;
if(A[i]<=last && A[j]<=last)return false;
long a=A[i],b=A[j];
//System.out.println(a+" "+b);
if(a==b)
{
return win[i] | win[j];
}
else if(a>b)
{
boolean f=false;
if(b>last)f=!f(i,j-1,b,win);
return win[i] | f;
}
else
{
boolean f=false;
if(a>last)f=!f(i+1,j,a,win);
return win[j] | f;
}
}
static long ask(long l,long r)
{
System.out.println("? "+l+" "+r);
return l();
}
static long f(long N,long M)
{
long s=0;
if(N%3==0)
{
N/=3;
s=N*M;
}
else
{
long b=N%3;
N/=3;
N++;
s=N*M;
N--;
long a=N*M;
if(M%3==0)
{
M/=3;
a+=(b*M);
}
else
{
M/=3;
M++;
a+=(b*M);
}
s=Math.min(s, a);
}
return s;
}
static int ask(StringBuilder sb,int a)
{
System.out.println(sb+""+a);
return i();
}
static void swap(char X[],int i,int j)
{
char x=X[i];
X[i]=X[j];
X[j]=x;
}
static int min(int a,int b,int c)
{
return Math.min(Math.min(a, b), c);
}
static long and(int i,int j)
{
System.out.println("and "+i+" "+j);
return l();
}
static long or(int i,int j)
{
System.out.println("or "+i+" "+j);
return l();
}
static int len=0,number=0;
static void f(char X[],int i,int num,int l)
{
if(i==X.length)
{
if(num==0)return;
//update our num
if(isPrime(num))return;
if(l<len)
{
len=l;
number=num;
}
return;
}
int a=X[i]-'0';
f(X,i+1,num*10+a,l+1);
f(X,i+1,num,l);
}
static boolean is_Sorted(int A[])
{
int N=A.length;
for(int i=1; i<=N; i++)if(A[i-1]!=i)return false;
return true;
}
static boolean f(StringBuilder sb,String Y,String order)
{
StringBuilder res=new StringBuilder(sb.toString());
HashSet<Character> set=new HashSet<>();
for(char ch:order.toCharArray())
{
set.add(ch);
for(int i=0; i<sb.length(); i++)
{
char x=sb.charAt(i);
if(set.contains(x))continue;
res.append(x);
}
}
String str=res.toString();
return str.equals(Y);
}
static boolean all_Zero(int f[])
{
for(int a:f)if(a!=0)return false;
return true;
}
static long form(int a,int l)
{
long x=0;
while(l-->0)
{
x*=10;
x+=a;
}
return x;
}
static int count(String X)
{
HashSet<Integer> set=new HashSet<>();
for(char x:X.toCharArray())set.add(x-'0');
return set.size();
}
static int f(long K)
{
long l=0,r=K;
while(r-l>1)
{
long m=(l+r)/2;
if(m*m<K)l=m;
else r=m;
}
return (int)l;
}
// static void build(int v,int tl,int tr,long A[])
// {
// if(tl==tr)
// {
// seg[v]=A[tl];
// }
// else
// {
// int tm=(tl+tr)/2;
// build(v*2,tl,tm,A);
// build(v*2+1,tm+1,tr,A);
// seg[v]=Math.min(seg[v*2], seg[v*2+1]);
// }
// }
static int [] sub(int A[],int B[])
{
int N=A.length;
int f[]=new int[N];
for(int i=N-1; i>=0; i--)
{
if(B[i]<A[i])
{
B[i]+=26;
B[i-1]-=1;
}
f[i]=B[i]-A[i];
}
for(int i=0; i<N; i++)
{
if(f[i]%2!=0)f[i+1]+=26;
f[i]/=2;
}
return f;
}
static int[] f(int N)
{
char X[]=in.next().toCharArray();
int A[]=new int[N];
for(int i=0; i<N; i++)A[i]=X[i]-'a';
return A;
}
static int max(int a ,int b,int c,int d)
{
a=Math.max(a, b);
c=Math.max(c,d);
return Math.max(a, c);
}
static int min(int a ,int b,int c,int d)
{
a=Math.min(a, b);
c=Math.min(c,d);
return Math.min(a, c);
}
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static long mul(long a, long b)
{
return ( a %mod * 1L * b%mod )%mod;
}
static void swap(int A[],int a,int b)
{
int t=A[a];
A[a]=A[b];
A[b]=t;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
par[a]+=par[b];
par[b]=a;
}
}
static boolean isSorted(int A[])
{
for(int i=1; i<A.length; i++)
{
if(A[i]<A[i-1])return false;
}
return true;
}
static boolean isDivisible(StringBuilder X,int i,long num)
{
long r=0;
for(; i<X.length(); i++)
{
r=r*10+(X.charAt(i)-'0');
r=r%num;
}
return r==0;
}
static int lower_Bound(int A[],int low,int high, int x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static String f(String A)
{
String X="";
for(int i=A.length()-1; i>=0; i--)
{
int c=A.charAt(i)-'0';
X+=(c+1)%2;
}
return X;
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static int sD(long n)
{
if (n % 2 == 0 )
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0 )
return i;
}
return (int)n;
}
static void setGraph(int N)
{
par=new int[N+1];
D=new int[N+1];
g=new ArrayList[N+1];
set=new boolean[N+1];
col=new int[N+1];
for(int i=0; i<=N; i++)
{
col[i]=-1;
g[i]=new ArrayList<>();
D[i]=Integer.MAX_VALUE;
}
}
static long pow(long a,long b)
{
//long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static int kadane(int A[])
{
int lsum=A[0],gsum=A[0];
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(boolean A[][])
{
for(boolean a[]:A)print(a);
}
static void print(long A[][])
{
for(long a[]:A)print(a);
}
static void print(int A[][])
{
for(int a[]:A)print(a);
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
class pair implements Comparable<pair>
{
long a,l,r;
boolean left;
int index;
pair(long a,long l,int i)
{
this.a=a;
index=i;
this.l=l;
}
public int compareTo(pair x)
{
if(this.a==x.a)
{
if(this.l>x.l)return -1;
else if(x.l>this.l)return 1;
return 0;
}
if(this.a>x.a)return 1;
return -1;
}
}
class TreeNode
{
int cnt,index;
TreeNode left,right;
TreeNode(int c)
{
cnt=c;
index=-1;
}
TreeNode(int c,int index)
{
cnt=c;
this.index=index;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | a275226b36834500d8c0487d01b9bf31 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | /*
stream Butter!
eggyHide eggyVengeance
I need U
xiao rerun when
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class x1609C
{
static final int MAX = 1000000;
public static void main(String hi[]) throws Exception
{
int[] prime = new int[MAX+1];
for(int d=2; d <= MAX; d++)
if(prime[d] == 0)
for(int v=d; v <= MAX; v+=d)
prime[v] = d;
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
int[] arr = readArr(N, infile, st);
long res = 0L;
for(int a=0; a < E; a++)
{
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int i=a; i < N; i+=E)
{
if(arr[i] == 1)
ls.add(1);
else if(prime[arr[i]] == arr[i])
ls.add(2);
else
ls.add(1000);
}
//print(ls);
int left = -1;
for(int i=0; i < ls.size(); i++)
{
if(ls.get(i) == 1000)
{
res += solve(left, i, ls);
left = i;
}
if(ls.get(i) == 2)
res--;
}
res += solve(left, ls.size(), ls);
}
sb.append(res+"\n");
}
System.out.print(sb);
}
public static long solve(int L, int R, ArrayList<Integer> ls)
{
//count # of subarrays that have exactly 1 prime number
long res = 0L;
ArrayList<Integer> blocks = new ArrayList<Integer>();
int curr = 0;
for(int a=L+1; a < R; a++)
{
if(ls.get(a) == 1)
curr++;
else
{
blocks.add(curr);
curr = 0;
}
}
blocks.add(curr);
//print(blocks); System.out.println(res);
for(int i=1; i < blocks.size(); i++)
res += (long)(blocks.get(i-1)+1)*(blocks.get(i)+1);
//System.out.println(res+"\n");
return res;
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
{
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for(int i=0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
public static void print(ArrayList<Integer> arr)
{
//for debugging only
for(int x: arr)
System.out.print(x+" ");
System.out.println();
}
}
/*
1
7 3
10 2 1 3 1 19 3
(10, 3, 3)
(2, 1)
(1, 19)
*/ | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 110665be6c3bf5a134369d4bc4c448ec | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.util.*;
public class Main {
private static final int ma = (int) 4e5 + 10;
static int a[] = new int[ma];
static int vis[] = new int[ma];
static int one[] = new int[ma];
static boolean prime[] = new boolean[1000005];
public static void main(String[] args) throws Exception {
initReader();
int t = nextInt();
int n, e;
for (int i = 2; i <= 1000000; i++) {
if (!prime[i]) {
for (int j = i * 2; j <= 1000000; j += i) {
prime[j] = true;
}
}
}
while (t-- != 0) {
n = nextInt();
e = nextInt();
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
vis[i] = one[i] = 0;
}
for (int i=n;i<=n+e;i++)
vis[i]=one[i]=0;
long ans = 0;
for (int i = n; i >= 1; i--) {
if (a[i] == 1) {
vis[i] = vis[i + e];
one[i] = one[i + e] + 1;
ans += vis[i];
} else if (!prime[a[i]]) {
ans += one[i + e];
vis[i] = one[i + e] + 1;
one[i] = 0;
}
}
pw.println(ans);
}
pw.close();
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(0);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 29a8db0d4d0c7de681e0db16f57814cb | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
*
* @author pttrung
*/
public class C_Deltix_2021 {
public static long MOD = 1000000007;
static long[][] dp;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int T = in.nextInt();
boolean[] p = new boolean[1000001];
for (int i = 2; i < p.length; i++) {
if (p[i]) {
continue;
}
for (int j = i + i; j < p.length; j += i) {
p[j] = true;
}
}
p[0] = true;
p[1] = true;
for (int z = 0; z < T; z++) {
//System.out.println("START " + z);
int n = in.nextInt();
int e = in.nextInt();
int[] data = new int[n];
int[] cur = new int[e];
for (int i = 0; i < e; i++) {
cur[i] = i;
}
int[] dp = new int[n];
long result = 0;
for (int i = 0; i < n; i++) {
data[i] = in.nextInt();
if (!p[data[i]]) {
dp[i] = Integer.min(i, cur[i % e]);
}
if (data[i] != 1) {
cur[i % e] = i + e;
}
}
Arrays.fill(cur, n - 1);
for (int i = n - 1; i >= 0; i--) {
if (!p[data[i]]) {
int last = Integer.max(cur[i % e], i);
long a = (last - i) / e;
long b = (i - dp[i]) / e;
result += a;
result += (a + 1) * b;
}
if (data[i] != 1) {
cur[i % e] = i - e;
}
}
out.println(result);
}
out.close();
}
static long abs(long a) {
return a < 0 ? -a : a;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point implements Comparable<Point> {
int x;
long y;
public Point(int start, long end) {
this.x = start;
this.y = end;
}
@Override
public int compareTo(Point o) {
return Integer.compare(x, o.x);
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val;
} else {
return val * (val * a);
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | aaf397412f6dccfdaa0107c134707d77 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
/**
* @author Naitik
*
*/
public class Main
{
static FastReader sc=new FastReader();
static long dp[][][];
//static int v[][];
// static int mod=998244353;;
static int mod=1000000007;
static int max;
static int bit[];
// static int seg[];
//static long fact[];
// static long A[];
// static TreeMap<Integer,Integer> map;
//static StringBuffer sb=new StringBuffer("");
static HashMap<Integer,Integer> map;
static PrintWriter out=new PrintWriter(System.out);
static TreeSet<Long> set=new TreeSet<Long>();
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
boolean prime[] = new boolean[2000000+1];
for(int i=0;i<=2000000;i++)
prime[i] = true;
for(int p = 2; p*p <=2e6; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= 2e6; i += p)
prime[i] = false;
}
}
outer :while (ttt-- > 0)
{
int n=i();
int e=i();
int A[]=input(n);
ArrayList<Integer> l=new ArrayList<Integer>();
ArrayList<Integer> a=new ArrayList<Integer>();
ArrayList<Integer> b=new ArrayList<Integer>();
long ans=0;
for(int i=1;i<=e;i++) {
l.clear();
a.clear();
b.clear();
for(int j=i;j<=n;j+=e) {
l.add(A[j-1]);
}
// System.out.println(l);
int c=0;
for(int j=0;j<l.size();j++) {
int y=l.get(j);
if(y!=1) {
if(prime[y])
a.add(c);
c=0;
}
else {
c++;
}
}
c=0;
for(int j=l.size()-1;j>=0;j--) {
int y=l.get(j);
if(y!=1) {
if(prime[y])
b.add(c);
c=0;
}
else {
c++;
}
}
Collections.reverse(b);
//System.out.println(a.size()+" "+b.size());
for(int j=0;j<a.size();j++) {
long t=b.get(j);
long t1=a.get(j);
ans+=t1*(t+1);
ans+=t;
}
}
System.out.println(ans);
}
//System.out.println(sb.toString());
out.close();
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1 //CHECK FOR M=0
//CHECK FOR N=1
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return -1;
else if(this.x<o.x)
return 1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
// public int hashCode()
// {
// final int temp = 14;
// int ans = 1;
// ans =x*31+y*13;
// return ans;
// }
// @Override
// public boolean equals(Object o)
// {
// if (this == o) {
// return true;
// }
// if (o == null) {
// return false;
// }
// if (this.getClass() != o.getClass()) {
// return false;
// }
// Pair other = (Pair)o;
// if (this.x != other.x || this.y!=other.y) {
// return false;
// }
// return true;
// }
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
//static int find(int A[],int a) {
// if(A[a]<0)
// return a;
// return A[a]=find(A, A[a]);
//}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return find(A, A[a]);
}
//FENWICK TREE
static void update(int i, int x){
for(; i < bit.length; i += (i&-i))
bit[i] += x;
}
static int sum(int i){
int ans = 0;
for(; i > 0; i -= (i&-i))
ans += bit[i];
return ans;
}
//END
static void add(int v) {
if(!map.containsKey(v)) {
map.put(v, 1);
}
else {
map.put(v, map.get(v)+1);
}
}
static void remove(int v) {
if(map.containsKey(v)) {
map.put(v, map.get(v)-1);
if(map.get(v)==0)
map.remove(v);
}
}
public static int upper(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(int A[],int k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void print(int A[]) {
for(int i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Integer> hash(int A[]){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static HashMap<Long,Integer> hash(long A[]){
HashMap<Long,Integer> map=new HashMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 63aa2978e8a5bdb00412fa68ed79f49a | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
//--------------------------INPUT READER---------------------------------//
static class fs {
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public fs() { this(System.in); }
public fs(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long nn) {
int n = (int) nn;
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] nal(long nn) {
int n = (int) nn;
long[] l = new long[n];
for(int i = 0; i < n; i++) l[i] = nl();
return l;
}
}
//-----------------------------------------------------------------------//
//---------------------------PRINTER-------------------------------------//
static class Printer {
static PrintWriter w;
public Printer() {this(System.out);}
public Printer(OutputStream os) {
w = new PrintWriter(os);
}
public void p(int i) {w.println(i);}
public void p(long l) {w.println(l);}
public void p(double d) {w.println(d);}
public void p(String s) { w.println(s);}
public void pr(int i) {w.print(i);}
public void pr(long l) {w.print(l);}
public void pr(double d) {w.print(d);}
public void pr(String s) { w.print(s);}
public void pl() {w.println();}
public void close() {w.close();}
}
//-----------------------------------------------------------------------//
//--------------------------VARIABLES------------------------------------//
static fs sc = new fs();
static OutputStream outputStream = System.out;
static Printer w = new Printer(outputStream);
static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
static long ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
static long mod = 1000000007;
//-----------------------------------------------------------------------//
//--------------------------ADMIN_MODE-----------------------------------//
private static void ADMIN_MODE() throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
w = new Printer(new FileOutputStream("output.txt"));
sc = new fs(new FileInputStream("input.txt"));
}
}
//-----------------------------------------------------------------------//
static int[] primes;
static boolean[] isComposite;
//----------------------------START--------------------------------------//
public static void main(String[] args)
throws IOException {
ADMIN_MODE();
primes = sieve(1000009);
int t = sc.ni();while(t-->0)
solve();
w.close();
}
static void solve() throws IOException {
int n = sc.ni(), e = sc.ni();
int[] arr = sc.na(n);
int[] pre = new int[n], suff = new int[n];
for(int i = 0; i < n; i++) {
if(arr[i]==1) {
pre[i] = (i-e>=0?pre[i-e]:0)+1;
}
}
for(int i = n-1; i > 0; i--) {
if(arr[i]==1) {
suff[i] = (i+e<n?suff[i+e]:0)+1;
}
}
long sum = 0;
for(int i = 0; i < n; i++) {
/*if(arr[i]==1) {
long r = i+e<n?suff[i+e]:0;
sum+=r;
}*/
if(!isComposite[arr[i]] && arr[i] != 1) {
int l = i-e>=0?pre[i-e]:0;
int r = i+e<n?suff[i+e]:0;
sum+=l; sum+=r;
sum+=((long)l*r);
}
}
w.p(sum);
}
public static int[] sieve(int limit) {
if (limit <= 2) return new int[0];
//Upper bound of the number of prime numbers up to our limit -> https://en.wikipedia.org/wiki/Prime-counting_function#Inequalities
final int numPrimes = (int) (1.25506 * limit / Math.log((double) limit));
int[] primes = new int[numPrimes];
int index = 0;
boolean[] isComposite = new boolean[limit];
final int sqrtLimit = (int) Math.sqrt(limit);
for (int i = 2; i <= sqrtLimit; i++) {
if (!isComposite[i]) {
primes[index++] = i;
for (int j = i * i; j < limit; j += i) isComposite[j] = true;
}
}
for (int i = sqrtLimit + 1; i < limit; i++) if (!isComposite[i]) primes[index++] = i;
A.isComposite = isComposite;
return Arrays.copyOf(primes, index);
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 0498e50da2369f79d5aa654a593daa6c | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.System.*;
import static java.util.Arrays.sort;
import static java.util.Collections.replaceAll;
import static java.util.Collections.reverseOrder;
public class C {
public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null;
// public static final boolean LOCAL = System.getProperty("LOCAL")!=null;
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_RESET = "\u001B[0m";
static final int M = (int) 1e9 + 7;
static final long MM = (long) M * M;
static final int MAX = Integer.MAX_VALUE;
static final int MIN = Integer.MIN_VALUE;
static final int SIZE = (int) 1e9 * 2;
static ArrayList<Integer> primes = new ArrayList<>();
//static Scanner sc = new Scanner(System.in);
static FastReader sc = new FastReader();
static long startTime;
static long endTime;
static boolean[] prime;
static int[] visited;
static ArrayList<ArrayList<Integer>> graph;
static ArrayList<Integer> dfs;
static boolean isCyclic = false;
public static int swap(int... args) {
return args[0];
}
public static void main(String[] args) throws IOException {
do {
startTime = currentTimeMillis();
//setOut(new PrintStream("output"));
//setIn(new FileInputStream("input"));
prime(1000000);
int T = sc.nextInt();while (T-- != 0)
{
solve();
}
endTime = currentTimeMillis();
long duration = endTime - startTime;
//out.println(duration);
if (false) {
out.println(max(3, 2));
Integer[] ARRAY = new Integer[2];
ArrayList<Integer> LIST = new ArrayList<>();
LIST.add(2);
// sort(LIST, reverseOrder());
LIST.sort(reverseOrder());
sort(ARRAY, reverseOrder());
out.println(LIST + " " + Arrays.toString(ARRAY));
}
} while (LOCAL);
}
public static void solve(){
/////////////////////////////////////////////////////////////////////
int n =sc.nextInt(),e=sc.nextInt();
int [] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i]=sc.nextInt();
}
int limit = 0;
long ans = 0;
while (limit<e) {
ArrayList<Integer> list = new ArrayList<>();
boolean isPrime = false;
long left = 0,right = 0;
for (int i = limit; i < n; i+=e) {
list.add(arr[i]);
}
list.add(0);
// list.add(4);
// list.add(12);
for (Integer integer : list) {
if (integer == 1) {
if (isPrime) {
right++;
} else {
left++;
}
} else if (prime[integer]) {
if (isPrime) {
ans += (left + 1) * right + left;
left = right;
right = 0;
} else {
isPrime = true;
}
} else {
if(isPrime)
ans += (left + 1) * right + left;
left = 0;
right = 0;
isPrime = false;
}
}
limit++;
}
out.println(ans);
///////////////////////////////////////////////////////////////////////
}
static void dfs(int src) {
visited[src] = 1;
for (Integer integer : graph.get(src)) {
if (visited[integer] != 1) {
dfs(integer);
}
}
dfs.add(src);
}
static void detectCycle(int src) {
visited[src] = 2;
for (Integer integer : graph.get(src)) {
if (visited[integer] == 0) {
detectCycle(integer);
} else if (visited[integer] == 2) {
isCyclic = true;
return;
}
}
visited[src] = 1;
}
static <Array, Target> int count(Array[] array, Target target) {
int cn = 0;
for (Array value : array) {
if (value.equals(target)) cn++;
}
return cn;
}
static int count(String string, char target) {
int cn = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == target) cn++;
}
return cn;
}
static <Array> void revArray(Array[] arr) {
int n = arr.length;
for (int i = 0; i < n / 2; i++) {
Array temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
}
static <Array> void printArray(Array[] arr) {
int n = arr.length;
for (Array array : arr) out.print(array + " ");
out.println();
}
static void yes() {
out.println("YES");
}
static void no() {
out.println("NO");
}
public static boolean isPal(String s) {
StringBuilder stringBuilder = new StringBuilder(s);
return s.equals(stringBuilder.reverse().toString());
}
public static int lowerBound(Integer[] array, int length, int value) {
int low = -1;
int hi = length;
while (low + 1 < hi) {
int mid = (low + hi) / 2;
if (array[mid] <= value) {
low = mid;
} else {
hi = mid;
}
}
return low;
}
public static int upperBound(Integer[] array, int length, int value) {
int low = -1;
int hi = length;
while (low + 1 < hi) {
int mid = (low + hi) >> 1;
if (array[mid] >= value) {
hi = mid;
} else low = mid;
}
return hi;
}
public static int binarySearch(Integer[] arr, int length, int value) {
int low = 0;
int hi = length - 1;
int ans = -1;
while (low <= hi) {
int mid = (low + hi) >> 1;
if (arr[mid] > value) {
hi = mid - 1;
} else if (arr[mid] < value) {
low = mid + 1;
} else {
ans = mid;
break;
}
}
return ans;
}
public static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
public static int countDivs(int n) {
int cn = 0;
int sqr = (int) Math.sqrt(n);
for (int i = 1; i <= sqr; ++i) {
if (n % i == 0) {
++cn;
}
}
cn *= 2;
if (sqr * sqr == n) cn--;
return cn;
}
static void prime(int x) {
//sieve algorithm. nlog(log(n)).
prime = new boolean[(x + 1)];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i * i <= x; i++)
if (prime[i])
for (int j = i * i; j <= x; j += i)
prime[j] = false;
}
static boolean isEven(long x) {
return x % 2 == 0;
}
static boolean isPrime(long x) {
boolean flag = true;
int sqr = (int) Math.sqrt(x);
if (x < 2) return false;
for (int i = 2; i <= sqr; i++) {
if (x % i == 0) {
flag = false;
break;
}
}
return flag;
}
static long factorial(long x) {
long total = 1;
for (int i = 2; i <= x; i++)
total = (total * i) % M;
return total;
}
static long power(long x, long n) {
if (n == 0) {
return 1;
}
long pow = power(x, n / 2L);
if ((n & 1) == 1) {
return (x * (pow) * (pow));
}
return (pow * pow);
}
private static <T> String ts(T t) {
if (t == null) {
return "null";
}
try {
return ts((Iterable) t);
} catch (ClassCastException e) {
if (t instanceof int[]) {
String s = Arrays.toString((int[]) t);
return "{" + s.substring(1, s.length() - 1) + "}";
} else if (t instanceof long[]) {
String s = Arrays.toString((long[]) t);
return "{" + s.substring(1, s.length() - 1) + "}";
} else if (t instanceof char[]) {
String s = Arrays.toString((char[]) t);
return "{" + s.substring(1, s.length() - 1) + "}";
} else if (t instanceof double[]) {
String s = Arrays.toString((double[]) t);
return "{" + s.substring(1, s.length() - 1) + "}";
} else if (t instanceof boolean[]) {
String s = Arrays.toString((boolean[]) t);
return "{" + s.substring(1, s.length() - 1) + "}";
}
try {
return ts((Object[]) t);
} catch (ClassCastException e1) {
return t.toString();
}
}
}
private static <T> String ts(T[] arr) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for (T t : arr) {
if (!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
private static <T> String ts(Iterable<T> iter) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for (T t : iter) {
if (!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
public static void dbg(Object... o) {
if (LOCAL) {
System.err.print("Line #" + Thread.currentThread().getStackTrace()[2].getLineNumber() + ": [");
for (int i = 0; i < o.length; i++) {
if (i != 0) {
System.err.print(", ");
}
System.err.print(ts(o[i]));
}
System.err.println("]");
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair implements Comparable<Pair> {
private final int key;
private final int value;
public Pair(int key, int value) {
this.key = key;
this.value = value;
}
public int second() {
return value;
}
@Override
public int compareTo(Pair o) {
return -Integer.compare(key, o.key);
}
@Override
public String toString() {
return "Pair{" + "x=" + key + ", y=" + value + '}';
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 4cbdae9e52c7e2f328209c02a547dc44 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.io.*;
////***************************************************************************
/* public class E_Gardener_and_Tree implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start();
}
public void run(){
WRITE YOUR CODE HERE!!!!
JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!!
}
}
*/
/////**************************************************************************
public class C_Complex_Market_Analysis{
public static void main(String[] args) {
FastScanner s= new FastScanner();
//PrintWriter out=new PrintWriter(System.out);
//end of program
//out.println(answer);
//out.close();
StringBuilder res = new StringBuilder();
long size=(long)Math.pow(10,6)+5;
boolean prime[]= new boolean[(int)size];
prime[1]=true;
for(int i=2;i<size;i++){
if(prime[i]==true){
continue;
}
long j=2;
while((i*j)<size){
prime[(int)(i*j)]=true;
j++;
}
}
int t=s.nextInt();
int p=0;
while(p<t){
int n=s.nextInt();
int e=s.nextInt();
long array[]= new long[n+1];
for(int i=1;i<=n;i++){
array[i]=s.nextLong();
}
long ones[]=new long[n+1];
long dp[]= new long[n+1];
for(int i=n;i>=1;i--){
if(array[i]==1){
int yo=i+e;
ones[i]=1;
if((yo)<=n){
long num=array[yo];
if(num==1){
dp[i]=dp[yo];
ones[i]+=ones[yo];
}
else if(prime[(int)num]==false){
dp[i]=(1+dp[yo]);
}
}
}
else if(prime[(int)array[i]]==false){
int yo=i+e;
if(yo<=n){
dp[i]=ones[yo];
}
}
}
long ans=0;
for(int i=1;i<=n;i++){
ans+=dp[i];
}
res.append(ans+" \n");
p++;
}
System.out.println(res);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | e6a6e7c6a0ff319051efe77d8fa12087 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.Scanner;
public class C {
static boolean[] prime = new boolean[1000001];
static int getOneLength(int[] arr, int pos) {
if (pos < 0 || pos >= arr.length) {
return 0;
}
return arr[pos];
}
static boolean isOk(int n, int pos) {
return pos >= 0 && pos < n;
}
static int getRightNonOne(int[] arr, int pos) {
if (pos < 0 || pos >= arr.length) {
return -1;
}
return arr[pos];
}
static long solve(int e, int[] arr) {
int n = arr.length;
int[] oneLength = new int[n];
int[] rightNonOne = new int[n];
for (int i = n - 1; i >= 0; i--) {
if (arr[i] == 1) {
oneLength[i] = 1 + getOneLength(oneLength, i + e);
rightNonOne[i] = getRightNonOne(rightNonOne, i + e);
} else {
rightNonOne[i] = i;
}
}
long count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 1) {
if (rightNonOne[i] == -1) {
continue;
}
if (!prime[arr[rightNonOne[i]]]) {
continue;
}
count += 1;
int firstNonOnePos = rightNonOne[i];
if (!isOk(n, firstNonOnePos + e)) {
continue;
}
if (arr[firstNonOnePos + e] != 1) {
continue;
}
count += oneLength[firstNonOnePos + e];
} else {
if (!prime[arr[i]]) {
continue;
}
count += getOneLength(oneLength, i + e);
}
}
return count;
}
public static void main(String[] args) {
for (int i = 2; i < prime.length; i++) {
prime[i] = true;
}
for (int i = 2; i < prime.length; ++i) {
if (prime[i]) {
if ((long) i * i < prime.length) {
for (int j = i * i; j < prime.length; j += i) {
prime[j] = false;
}
}
}
}
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int e = sc.nextInt();
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = sc.nextInt();
}
System.out.println(solve(e, arr));
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 6b8e19ed6f263e406b7e6d27c8672401 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void solve() {
int n = in.nextInt();
int e = in.nextInt();
int[] a = array(n);
long count = 0;
for (int i = 0; i < n; i++) {
if (prime[a[i]]) {
// System.out.println(a[i]);
long onLeft = 1;
int j = i - e;;
while (j >= 0 && a[j] == 1) {
onLeft++;
j -= e;
}
long onRight = 1;
j = i + e;
while (j < n && a[j] == 1) {
onRight++;
j += e;
}
// System.out.println(onLef)
count += onLeft - 1 + onRight - 1 + (onLeft - 1) * (onRight - 1);
}
}
out.append(count + "\n");
}
public static void main (String[] args) throws java.lang.Exception {
// your code goes here
for (int i = 0; i < prime.length; i++) {
prime[i] = true;
}
prime[0] = false;
prime[1] = false;
for (int i = 2; i * i < prime.length; i++) {
if (prime[i]) {
for (int j = i * i ; j < prime.length; j += i) {
prime[j] = false;
}
}
}
// for (int i = 0 ; i <= 100; i++) {
// if (prime[i])
// System.out.println(i);
// }
int t = in.nextInt();
while (t-- != 0) {
solve();
}
System.out.print(out);
}
public static <T> void print(T s) {
System.out.print(s);
}
public static <T> void println(T s) {
System.out.println(s);
}
public static void print(int s) {
System.out.print(s);
}
public static void println(int s) {
System.out.println(s);
}
public static void print(int[] a) {
int k = 0;
for (int i : a) {
print(k++ +":" + i + " ");
}
println("");
}
public static int[] array(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
return a;
}
public static int[] array1(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
return a;
}
static boolean prime[] = new boolean[1000001];
static FastReader in = new FastReader();
static StringBuffer out = new StringBuffer();
static final int MAX = Integer.MAX_VALUE;
static final int MIN = Integer.MIN_VALUE;
static int mod = 1000000007;
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | fa9c01cc5169b285c9e2b56584d702e8 | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.beans.DesignMode;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.CompletableFuture.AsynchronousCompletionTask;
import org.xml.sax.ErrorHandler;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.DataInputStream;
public class Solution {
//TEMPLATE -------------------------------------------------------------------------------------
public static boolean Local(){
try{
return System.getenv("LOCAL_SYS")!=null;
}catch(Exception e){
return false;
}
}
public static boolean LOCAL;
static class FastScanner {
BufferedReader br;
StringTokenizer st ;
FastScanner(){
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
FastScanner(String file) {
try{
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
st = new StringTokenizer("");
}catch(FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("file not found");
e.printStackTrace();
}
}
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String readLine() throws IOException{
return br.readLine();
}
}
static class Pair<T,X> {
T first;
X second;
Pair(T first,X second){
this.first = first;
this.second = second;
}
@Override
public int hashCode(){
return Objects.hash(first,second);
}
@Override
public boolean equals(Object obj){
return obj.hashCode() == this.hashCode();
}
}
static PrintStream debug = null;
static long mod = (long)(Math.pow(10,9) + 7);
//TEMPLATE -------------------------------------------------------------------------------------END//
public static void main(String[] args) throws Exception {
FastScanner s = new FastScanner();
LOCAL = Local();
//PrintWriter pw = new PrintWriter(System.out);
if(LOCAL){
s = new FastScanner("src/input.txt");
PrintStream o = new PrintStream("src/sampleout.txt");
debug = new PrintStream("src/debug.txt");
System.setOut(o);
// pw = new PrintWriter(o);
} long mod = 1000000007;
int tcr = s.nextInt();
StringBuilder sb = new StringBuilder();
List<Integer> sieve = sieve();
Set<Integer> prime = new HashSet<>(sieve);
for(int tc=0;tc<tcr;tc++){
int n = s.nextInt();
int e = s.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = s.nextInt();
}
boolean done[] = new boolean[n];
long ans = 0l;
for(int i=0;i<n;i++){
if(done[i]){continue;}
if(prime.contains(arr[i])){
int k = 0;
int index = i;
index+=e;
int prev = 0;
int next = 0;
while(index < n){
if(arr[index] == 1){next++;k++;done[index]=true;index+=e;}
else{break;}
}
index = i - e;
while(index >= 0){
if(arr[index] == 1){prev++;k++;done[index]=true;index-=e;}
else{break;}
}
// dbg(debug,i+" "+next+" "+prev);
if(k >= 1){
ans = ans + (prev*1l*next);
ans = ans + next + prev;
}
}
}
sb.append(ans+"\n");
}
print(sb.toString());
}
public static boolean check(char arr[],int index){
if(index - 2 >=0 && ((arr[index-2]=='a') && (arr[index-1]=='b') && (arr[index]=='c'))){
return true;
}
if((index - 1 >= 0) && (index + 1 < arr.length) && (arr[index-1]=='a') && (arr[index]=='b') && (arr[index+1]=='c')){
return true;
}
if((index + 2 < arr.length) && (arr[index]=='a') && (arr[index+1]=='b') && (arr[index+2] == 'c')){
return true;
}
return false;
}
public static List<int[]> print_prime_factors(int n){
List<int[]> list = new ArrayList<>();
for(int i=2;i<=(int)(Math.sqrt(n));i++){
if(n % i == 0){
int cnt = 0;
while( (n % i) == 0){
n = n/i;
cnt++;
}
list.add(new int[]{i,cnt});
}
}
if(n!=1){
list.add(new int[]{n,1});
}
return list;
}
public static List<int[]> prime_factors(int n,List<Integer> sieve){
List<int[]> list = new ArrayList<>();
int index = 0;
while(n > 1 && sieve.get(index) <= Math.sqrt(n)){
int curr = sieve.get(index);
int cnt = 0;
while((n % curr) == 0){
n = n/curr;
cnt++;
}
if(cnt >= 1){
list.add(new int[]{curr,cnt});
}
index++;
}
if(n > 1){
list.add(new int[]{n,1});
}
return list;
}
public static boolean inRange(int r1,int r2,int val){
return ((val >= r1) && (val <= r2));
}
static int len(long num){
return Long.toString(num).length();
}
static long mulmod(long a, long b,long mod)
{
long ans = 0l;
while(b > 0){
long curr = (b & 1l);
if(curr == 1l){
ans = ((ans % mod) + a) % mod;
}
a = (a + a) % mod;
b = b >> 1;
}
return ans;
}
public static void dbg(PrintStream ps,Object... o) throws Exception{
if(ps == null){
return;
}
Debug.dbg(ps,o);
}
public static long modpow(long num,long pow,long mod){
long val = num;
long ans = 1l;
while(pow > 0l){
long bit = pow & 1l;
if(bit == 1){
ans = (ans * (val%mod))%mod;
}
val = (val * val) % mod;
pow = pow >> 1;
}
return ans;
}
public static char get(int n){
return (char)('a' + n);
}
public static long[] sort(long arr[]){
List<Long> list = new ArrayList<>();
for(long n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
public static int[] sort(int arr[]){
List<Integer> list = new ArrayList<>();
for(int n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
// return the (index + 1)
// where index is the pos of just smaller element
// i.e count of elemets strictly less than num
public static int justSmaller(long arr[],long num){
// System.out.println(num+"@");
int st = 0;
int e = arr.length - 1;
int ans = -1;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] >= num){
e = mid - 1;
}else{
ans = mid;
st = mid + 1;
}
}
return ans + 1;
}
public static int justSmaller(int arr[],int num){
// System.out.println(num+"@");
int st = 0;
int e = arr.length - 1;
int ans = -1;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] >= num){
e = mid - 1;
}else{
ans = mid;
st = mid + 1;
}
}
return ans + 1;
}
//return (index of just greater element)
//count of elements smaller than or equal to num
public static int justGreater(long arr[],long num){
int st = 0;
int e = arr.length - 1;
int ans = arr.length;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] <= num){
st = mid + 1;
}else{
ans = mid;
e = mid - 1;
}
}
return ans;
}
public static int justGreater(int arr[],int num){
int st = 0;
int e = arr.length - 1;
int ans = arr.length;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] <= num){
st = mid + 1;
}else{
ans = mid;
e = mid - 1;
}
}
return ans;
}
public static void println(Object obj){
System.out.println(obj.toString());
}
public static void print(Object obj){
System.out.print(obj.toString());
}
public static int gcd(int a,int b){
if(b == 0){return a;}
return gcd(b,a%b);
}
public static long gcd(long a,long b){
if(b == 0l){
return a;
}
return gcd(b,a%b);
}
public static int find(int parent[],int v){
if(parent[v] == v){
return v;
}
return parent[v] = find(parent, parent[v]);
}
public static List<Integer> sieve(){
List<Integer> prime = new ArrayList<>();
int arr[] = new int[1000001];
Arrays.fill(arr,1);
arr[1] = 0;
arr[2] = 1;
for(int i=2;i<1000001;i++){
if(arr[i] == 1){
prime.add(i);
for(long j = (i*1l*i);j<1000001;j+=i){
arr[(int)j] = 0;
}
}
}
return prime;
}
static boolean isPower(long n,long a){
long log = (long)(Math.log(n)/Math.log(a));
long power = (long)Math.pow(a,log);
if(power == n){return true;}
return false;
}
private static int mergeAndCount(int[] arr, int l,int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l, swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static int mergeSortAndCount(int[] arr, int l,int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
int count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static class Debug{
//change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces
public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null;
private static <T> String ts(T t) {
if(t==null) {
return "null";
}
try {
return ts((Iterable) t);
}catch(ClassCastException e) {
if(t instanceof int[]) {
String s = Arrays.toString((int[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof long[]) {
String s = Arrays.toString((long[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof char[]) {
String s = Arrays.toString((char[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof double[]) {
String s = Arrays.toString((double[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof boolean[]) {
String s = Arrays.toString((boolean[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}
try {
return ts((Object[]) t);
}catch(ClassCastException e1) {
return t.toString();
}
}
}
private static <T> String ts(T[] arr) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: arr) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
private static <T> String ts(Iterable<T> iter) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: iter) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}\n");
return ret.toString();
}
public static void dbg(PrintStream ps,Object... o) throws Exception {
if(LOCAL) {
System.setErr(ps);
System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": [\n");
for(int i = 0; i<o.length; i++) {
if(i!=0) {
System.err.print(", ");
}
System.err.print(ts(o[i]));
}
System.err.println("]");
}
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | e421b3556660226c3b5d01bfee5fe94c | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
ConsoleIO io = new ConsoleIO(new InputStreamReader(System.in), new PrintWriter(System.out));
// String fileName = "C-large-practice";
// ConsoleIO io = new ConsoleIO(new FileReader("D:\\Dropbox\\code\\practice\\jb\\src\\" + fileName + ".in"), new PrintWriter(new File("D:\\Dropbox\\code\\practice\\jb\\src\\" + fileName + ".out")));
new Main(io).solve();
// new Main(io).solveLocal();
io.close();
}
ConsoleIO io;
Main(ConsoleIO io) {
this.io = io;
}
ConsoleIO opt;
Main(ConsoleIO io, ConsoleIO opt) {
this.io = io;
this.opt = opt;
}
List<List<Integer>> gr = new ArrayList<>();
//long MOD = 1_000_000_007;
class Point {
public Point(long x, long y){
this.x = x;
this.y = y;
}
public long x;
public long y;
public long prod(Point p){
return x*p.y - y*p.x;
}
}
public void solve() {
int t = io.ri();
boolean[] p = new boolean[1000001];
p[0] = p[1] = true;
for(int i = 2;i < p.length;i++) {
if (!p[i]) {
for (int j = i + i; j < p.length; j += i)
p[j] = true;
}
}
for(int i = 0 ; i < t; i++) {
int n = io.ri();
int e = io.ri();
int[] a = new int[n];
for(int j = 0;j<n;j++) {
int v = io.ri();
a[j] = v;
}
int[] prev1 = new int[n];
for(int j = 0; j<n;j++) {
if(a[j] == 1) {
prev1[j] = 1;
if(j>=e)
prev1[j]+=prev1[j-e];
}
}
int[] prev2 = new int[n];
for(int j = n-1; j>=0;j--) {
if(a[j] == 1) {
prev2[j] = 1;
if(j+e<n)
prev2[j]+=prev2[j+e];
}
}
long res = 0;
for(int j = 0; j < n; j++) {
long q = 1, w = 1;
if (!p[a[j]]) {
if (j >= e) q = prev1[j - e] + 1;
if (j + e < n) w = prev2[j + e] + 1;
}
if (q * w > 1)
res += q * w - 1;
}
io.writeLine(Long.toString(res));
}
}
public boolean good(char[] s, int p){
char v = s[p];
if(v=='a' && p < s.length-2 && s[p+1] == 'b' && s[p+2] == 'c') {
return true;
}
if(v=='b' && p < s.length-1 && p > 0 && s[p-1] == 'a' && s[p+1] == 'c') {
return true;
}
if(v=='c' && p > 1 && s[p-2] == 'a' && s[p-1] == 'b') {
return true;
}
return false;
}
public long choose4(long v){
if(v<4)
return 0;
return v * (v-1) * (v-2) * (v-3) / 24;
}
public long choose3(long v){
if(v<3)
return 0;
return v * (v-1) * (v-2) / 6;
}
}
class ConsoleIO {
BufferedReader br;
PrintWriter out;
public ConsoleIO(Reader reader, PrintWriter writer){br = new BufferedReader(reader);out = writer;}
public void flush(){this.out.flush();}
public void close(){this.out.close();}
public void writeLine(String s) {this.out.println(s);}
public void writeInt(int a) {this.out.print(a);this.out.print(' ');}
public void writeWord(String s){
this.out.print(s);
}
public void writeIntArray(int[] a, int k, String separator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < k; i++) {
if (i > 0) sb.append(separator);
sb.append(a[i]);
}
this.writeLine(sb.toString());
}
public void writeLongArray(long[] a, int k, String separator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < k; i++) {
if (i > 0) sb.append(separator);
sb.append(a[i]);
}
this.writeLine(sb.toString());
}
public int read(char[] buf, int len){try {return br.read(buf,0,len);}catch (Exception ex){ return -1; }}
public String readLine() {try {return br.readLine();}catch (Exception ex){ return "";}}
public long[] readLongArray() {
String[]n=this.readLine().trim().split("\\s+");long[]r=new long[n.length];
for(int i=0;i<n.length;i++)r[i]=Long.parseLong(n[i]);
return r;
}
public int[] readIntArray() {
String[]n=this.readLine().trim().split("\\s+");int[]r=new int[n.length];
for(int i=0;i<n.length;i++)r[i]=Integer.parseInt(n[i]);
return r;
}
public int[] readIntArray(int n) {
int[] res = new int[n];
char[] all = this.readLine().toCharArray();
int cur = 0;boolean have = false;
int k = 0;
boolean neg = false;
for(int i = 0;i<all.length;i++){
if(all[i]>='0' && all[i]<='9'){
cur = cur*10+all[i]-'0';
have = true;
}else if(all[i]=='-') {
neg = true;
}
else if(have){
res[k++] = neg?-cur:cur;
cur = 0;
have = false;
neg = false;
}
}
if(have)res[k++] = neg?-cur:cur;
return res;
}
public int ri() {
try {
int r = 0;
boolean start = false;
boolean neg = false;
while (true) {
int c = br.read();
if (c >= '0' && c <= '9') {
r = r * 10 + c - '0';
start = true;
} else if (!start && c == '-') {
start = true;
neg = true;
} else if (start || c == -1) return neg ? -r : r;
}
} catch (Exception ex) {
return -1;
}
}
public long readLong() {
try {
long r = 0;
boolean start = false;
boolean neg = false;
while (true) {
int c = br.read();
if (c >= '0' && c <= '9') {
r = r * 10 + c - '0';
start = true;
} else if (!start && c == '-') {
start = true;
neg = true;
} else if (start || c == -1) return neg ? -r : r;
}
} catch (Exception ex) {
return -1;
}
}
public String readWord() {
try {
boolean start = false;
StringBuilder sb = new StringBuilder();
while (true) {
int c = br.read();
if (c!= ' ' && c!= '\r' && c!='\n' && c!='\t') {
sb.append((char)c);
start = true;
} else if (start || c == -1) return sb.toString();
}
} catch (Exception ex) {
return "";
}
}
public char readSymbol() {
try {
while (true) {
int c = br.read();
if (c != ' ' && c != '\r' && c != '\n' && c != '\t') {
return (char) c;
}
}
} catch (Exception ex) {
return 0;
}
}
//public char readChar(){try {return (char)br.read();}catch (Exception ex){ return 0; }}
}
class Pair {
public Pair(int a, int b) {this.a = a;this.b = b;}
public int a;
public int b;
}
class PairLL {
public PairLL(long a, long b) {this.a = a;this.b = b;}
public long a;
public long b;
}
class Triple {
public Triple(int a, int b, int c) {this.a = a;this.b = b;this.c = c;}
public int a;
public int b;
public int c;
}
class TripleLL {
public TripleLL(long a, long b, long c) {this.a = a;this.b = b;this.c = c;}
public long a;
public long b;
public long c;
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 04630a140c656c54c945d790f8c98e6b | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | //package codeforces.deltix_autumn;
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
//Think through the entire logic before jump into coding!
//If you are out of ideas, take a guess! It is better than doing nothing!
//Read both C and D, it is possible that D is easier than C for you!
//Be aware of integer overflow!
//If you find an answer and want to return immediately, don't forget to flush before return!
public class C {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) {
//initReaderPrinter(true);
initReaderPrinter(false);
solve(in.nextInt());
//solve(1);
}
static boolean[] prime;
static int N = 1000000;
static void solve(int testCnt) {
prime = generatePrimeStatus(N);
for (int testNumber = 0; testNumber < testCnt; testNumber++) {
int n = in.nextInt(), e = in.nextInt();
int[] a = in.nextIntArrayPrimitive(n);
//boolean[] processed = new boolean[n];
long ans = 0;
for(int i = 0; i < n; i++) {
if(prime[a[i]]) {
long l = 0, r = 0;
for(int j = i - e; j >= 0; j -= e) {
if(a[j] == 1) l++;
else break;
}
for(int j = i + e; j < n; j += e) {
if(a[j] == 1) r++;
else break;
}
ans += (l * r + l + r);
// if(l != 0 || r != 0) {
// if(l == 0) l = 1;
// if(r == 0) r = 1;
// ans += l * r;
// }
}
}
out.println(ans);
}
out.close();
}
static boolean[] generatePrimeStatus(int n) {
boolean prime[] = new boolean[n + 1];
Arrays.fill(prime, true);
prime[1] = false;
for(int p = 2; p * p <= n; p++) {
if(prime[p]) {
for(int i = p * p; i <= n; i += p) {
prime[i] = false;
}
}
}
return prime;
}
static void initReaderPrinter(boolean test) {
if (test) {
try {
in = new InputReader(new FileInputStream("src/input.in"));
out = new PrintWriter(new FileOutputStream("src/output.out"));
} catch (IOException e) {
e.printStackTrace();
}
} else {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
InputReader(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream), 32768);
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
Integer[] nextIntArray(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitive(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
int[] nextIntArrayPrimitiveOneIndexed(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextInt();
return a;
}
Long[] nextLongArray(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitive(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long[] nextLongArrayPrimitiveOneIndexed(int n) {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++) a[i] = nextLong();
return a;
}
String[] nextStringArray(int n) {
String[] g = new String[n];
for (int i = 0; i < n; i++) g[i] = next();
return g;
}
List<Integer>[] readGraphOneIndexed(int n, int m) {
List<Integer>[] adj = new List[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt();
int v = nextInt();
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
List<Integer>[] readGraphZeroIndexed(int n, int m) {
List<Integer>[] adj = new List[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
adj[u].add(v);
adj[v].add(u);
}
return adj;
}
/*
A more efficient way of building a graph using int[] instead of ArrayList to store each node's neighboring nodes.
1-indexed.
*/
int[][] buildGraph(int nodeCnt, int edgeCnt) {
int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt];
int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1];
for (int i = 0; i < edgeCnt; i++) {
int u = in.nextInt(), v = in.nextInt();
edgeCntForEachNode[u]++;
edgeCntForEachNode[v]++;
end1[i] = u;
end2[i] = v;
}
int[][] adj = new int[nodeCnt + 1][];
for (int i = 1; i <= nodeCnt; i++) {
adj[i] = new int[edgeCntForEachNode[i]];
}
for (int i = 0; i < edgeCnt; i++) {
adj[end1[i]][idxForEachNode[end1[i]]] = end2[i];
idxForEachNode[end1[i]]++;
adj[end2[i]][idxForEachNode[end2[i]]] = end1[i];
idxForEachNode[end2[i]]++;
}
return adj;
}
}
} | Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 68c7870fdaae8ed6c38f507392e2eecc | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CSlozhniiAnalizRinka solver = new CSlozhniiAnalizRinka();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CSlozhniiAnalizRinka {
int N = 1000010;
boolean[] prime = new boolean[N + 1];
public CSlozhniiAnalizRinka() {
prime[0] = true;
prime[1] = true;
for (int i = 2; i * i < N; i++) {
for (int h = i + i; h < N; h += i) {
prime[h] = true;
}
}
}
long query(long[] cum, int l, int r) {
if (l == 0) {
return cum[r];
} else {
return cum[r] - cum[l - 1];
}
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int m = in.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
long[] cum = new long[n];
long ans = 0;
for (int q = 0; q < m; q++) {
List<Long> v = new ArrayList<>();
for (int i = q; i < n; i += m) {
v.add(a[i]);
}
cum[0] = v.get(0);
for (int i = 1; i < v.size(); i++) {
cum[i] = cum[i - 1] + v.get(i);
}
for (int i = 0; i < v.size(); i++) {
if (prime[v.get(i).intValue()]) {
continue;
}
int l = 0;
if (i != 0) {
int j1 = 0;
int j2 = i - 1;
while (j1 < j2) {
int mid = (j1 + j2) / 2;
if (query(cum, mid, i - 1) == (i - 1 - mid + 1)) {
j2 = mid;
} else {
j1 = mid + 1;
}
}
l = j1;
if (v.get(l) != 1) {
l = i;
}
}
int r = v.size() - 1;
if (i != v.size() - 1) {
int j1 = i + 1;
int j2 = v.size() - 1;
while (j1 < j2) {
int mid = (j1 + j2 + 1) / 2;
if (query(cum, i + 1, mid) == (mid - (i + 1) + 1)) {
j1 = mid;
} else {
j2 = mid - 1;
}
}
r = j1;
if (v.get(r) != 1) {
r = i;
}
}
ans += (i - l + 1L) * (r - i + 1L) - 1;
}
}
out.println(ans);
}
}
static class InputReader {
private static final int BUFFER_LENGTH = 1 << 10;
private InputStream stream;
private byte[] buf = new byte[BUFFER_LENGTH];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int nextC() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String next() {
int c = nextC();
while (isSpaceChar(c)) {
c = nextC();
}
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = nextC();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() {
int c = skipWhileSpace();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextC();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = nextC();
} while (!isSpaceChar(c));
return res * sgn;
}
public int skipWhileSpace() {
int c = nextC();
while (isSpaceChar(c)) {
c = nextC();
}
return c;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output | |
PASSED | 97220ee573d54c6e04730aa2b01e021d | train_110.jsonl | 1638110100 | While performing complex market analysis William encountered the following problem:For a given array $$$a$$$ of size $$$n$$$ and a natural number $$$e$$$, calculate the number of pairs of natural numbers $$$(i, k)$$$ which satisfy the following conditions: $$$1 \le i, k$$$ $$$i + e \cdot k \le n$$$. Product $$$a_i \cdot a_{i + e} \cdot a_{i + 2 \cdot e} \cdot \ldots \cdot a_{i + k \cdot e} $$$ is a prime number. A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class C {
static BufferedReader br;
static StringTokenizer st;
static PrintWriter pw;
static String nextToken() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
static int nextInt() {
return Integer.parseInt(nextToken());
}
public static void main(String[] args) {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
run();
pw.close();
}
private static void run() {
int t = nextInt();
TreeSet<Integer> set = new TreeSet<>();
boolean[] used = new boolean[(int) (1e6 + 10)];
for (int i = 2; i < used.length; i++) {
if (!used[i]) {
int k = i + i;
while (k < used.length) {
used[k] = true;
k += i;
}
set.add(i);
}
}
for (int i = 0; i < t; i++) {
int n = nextInt();
int e = nextInt();
int[] a = new int[n];
int[] one = new int[e];
for (int j = 0; j < n; j++) {
a[j] = nextInt();
if (a[j] == 1) {
one[j % e]++;
}
}
long ans = 0;
for (int j = 0; j < n; j++) {
if (a[j] == 1) {
one[j % e]--;
} else if (set.contains(a[j])) {
int c = 1;
while (j + c * e < n && a[j + c * e] == 1) {
ans++;
c++;
}
int cc = c;
c = 1;
while (j - c * e >= 0 && a[j - c * e] == 1) {
ans += cc;
c++;
}
}
}
pw.println(ans);
}
}
}
| Java | ["6\n7 3\n10 2 1 3 1 19 3\n3 2\n1 13 1\n9 3\n2 4 2 1 1 1 1 4 2\n3 1\n1 1 1\n4 1\n1 2 1 1\n2 2\n1 2"] | 2 seconds | ["2\n0\n4\n0\n5\n0"] | NoteIn the first example test case two pairs satisfy the conditions: $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{5} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 19$$$ which is a prime number. In the second example test case there are no pairs that satisfy the conditions.In the third example test case four pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{4} \cdot a_{7} = 2$$$ which is a prime number. $$$i = 3, k = 1$$$, for which the product is: $$$a_{3} \cdot a_{6} = 2$$$ which is a prime number. $$$i = 6, k = 1$$$, for which the product is: $$$a_{6} \cdot a_{9} = 2$$$ which is a prime number. In the fourth example test case there are no pairs that satisfy the conditions.In the fifth example test case five pairs satisfy the conditions: $$$i = 1, k = 1$$$, for which the product is: $$$a_{1} \cdot a_{2} = 2$$$ which is a prime number. $$$i = 1, k = 2$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 1, k = 3$$$, for which the product is: $$$a_{1} \cdot a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. $$$i = 2, k = 1$$$, for which the product is: $$$a_{2} \cdot a_{3} = 2$$$ which is a prime number. $$$i = 2, k = 2$$$, for which the product is: $$$a_{2} \cdot a_{3} \cdot a_{4} = 2$$$ which is a prime number. In the sixth example test case there are no pairs that satisfy the conditions. | Java 8 | standard input | [
"binary search",
"dp",
"implementation",
"number theory",
"schedules",
"two pointers"
] | 32130f939336bb6f2deb4dfa5402867d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$e$$$ $$$(1 \le e \le n \le 2 \cdot 10^5)$$$, the number of items in the array and number $$$e$$$, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^6)$$$, the contents of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,400 | For each test case output the answer in the following format: Output one line containing the number of pairs of numbers $$$(i, k)$$$ which satisfy the conditions. | standard output |
Subsets and Splits