message stringlengths 2 44.5k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 42 109k | cluster float64 5 5 | __index_level_0__ int64 84 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).
In particular, any integer sequence is similar to itself.
You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.
How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?
Constraints
* 1 \leq N \leq 10
* 1 \leq A_i \leq 100
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of integer sequences that satisfy the condition.
Examples
Input
2
2 3
Output
7
Input
3
3 3 3
Output
26
Input
1
100
Output
1
Input
10
90 52 56 71 44 8 13 30 57 84
Output
58921
Submitted Solution:
```
N = int(input())
A = list(map(int, input().split()))
A.sort()
even_cnt = []
cnt_1 = 0
for i in range(N):
if A[i] == 1:
even_cnt.append(1)
A[i] = 1
cnt_1 += 1
else:
c = 0
if (A[i]-1) % 2 == 0: c += 1
if A[i] % 2 == 0: c += 1
if (A[i]+1) % 2 == 0: c += 1
even_cnt.append(c)
A[i] = 3 - c
if cnt_1 > 0: x = 2 ** cnt_1 * 3 ** (N-cnt_1)
else: x = 3 ** N
ans = 0
for i in range(N):
if A[i] + even_cnt[i] == 2: x = x // 2
else: x = x // 3
ans += even_cnt[i] * x
x *= A[i]
print(ans)
``` | instruction | 0 | 34,125 | 5 | 68,250 |
No | output | 1 | 34,125 | 5 | 68,251 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).
In particular, any integer sequence is similar to itself.
You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.
How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?
Constraints
* 1 \leq N \leq 10
* 1 \leq A_i \leq 100
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of integer sequences that satisfy the condition.
Examples
Input
2
2 3
Output
7
Input
3
3 3 3
Output
26
Input
1
100
Output
1
Input
10
90 52 56 71 44 8 13 30 57 84
Output
58921
Submitted Solution:
```
N = int(input())
A = [int(i) for i in input().split()]
ans = 2**N
tmp = 1
for a in A:
if a%2==0:
tmp *= 2
print(ans - tmp)
``` | instruction | 0 | 34,126 | 5 | 68,252 |
No | output | 1 | 34,126 | 5 | 68,253 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).
In particular, any integer sequence is similar to itself.
You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.
How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?
Constraints
* 1 \leq N \leq 10
* 1 \leq A_i \leq 100
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the number of integer sequences that satisfy the condition.
Examples
Input
2
2 3
Output
7
Input
3
3 3 3
Output
26
Input
1
100
Output
1
Input
10
90 52 56 71 44 8 13 30 57 84
Output
58921
Submitted Solution:
```
N = int(input())
A = list(map(int, input().split()))
kisuu = 0
for i in range(N):
if A[i] % 2 == 0:
kisuu += 2
print(2**N-kisuu)
``` | instruction | 0 | 34,127 | 5 | 68,254 |
No | output | 1 | 34,127 | 5 | 68,255 |
Provide a correct Python 3 solution for this coding contest problem.
You survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department.
The following problems were described in the pamphlet.
> How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5.
If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input.
Input
The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≤ N ≤ 1000.
The end of the input is indicated by a single line of zeros.
Output
The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output.
Example
Input
9
500
0
Output
2
3 | instruction | 0 | 34,208 | 5 | 68,416 |
"Correct Solution:
```
while True:
n = int(input())
ans = 0
if n == 0:
break
count = n
for i in range(1, n):
a = 0
for j in range (i, n):
a += j
if a == n:
ans += 1
break
if a > n:
break
print(ans)
``` | output | 1 | 34,208 | 5 | 68,417 |
Provide a correct Python 3 solution for this coding contest problem.
You survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department.
The following problems were described in the pamphlet.
> How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5.
If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input.
Input
The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≤ N ≤ 1000.
The end of the input is indicated by a single line of zeros.
Output
The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output.
Example
Input
9
500
0
Output
2
3 | instruction | 0 | 34,209 | 5 | 68,418 |
"Correct Solution:
```
while True:
N = int(input())
if N==0:
break
a = 0
for i in range(1,N):
s = 0
for j in range(i,N+1):
s += j
if s == N:
a += 1
elif s > N:
break
print(a)
``` | output | 1 | 34,209 | 5 | 68,419 |
Provide a correct Python 3 solution for this coding contest problem.
You survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department.
The following problems were described in the pamphlet.
> How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5.
If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input.
Input
The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≤ N ≤ 1000.
The end of the input is indicated by a single line of zeros.
Output
The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output.
Example
Input
9
500
0
Output
2
3 | instruction | 0 | 34,210 | 5 | 68,420 |
"Correct Solution:
```
while True:
C=0
n=int(input())
if n==0:
break
for i in range(n-1):
A=i
B=0
while True:
A=A+1
B=B+A
if B==n:
C=C+1
if B>=n:
break
print(C)
``` | output | 1 | 34,210 | 5 | 68,421 |
Provide a correct Python 3 solution for this coding contest problem.
You survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department.
The following problems were described in the pamphlet.
> How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5.
If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input.
Input
The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≤ N ≤ 1000.
The end of the input is indicated by a single line of zeros.
Output
The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output.
Example
Input
9
500
0
Output
2
3 | instruction | 0 | 34,211 | 5 | 68,422 |
"Correct Solution:
```
if __name__ == "__main__":
while True:
n = int(input())
if n ==0:break
count = 0
for i in range(1,n):
for j in range(i+1,n):
sum = (i+j)*(j-i+1)/2
if sum> n:break
if sum == n:count +=1
print(count)
``` | output | 1 | 34,211 | 5 | 68,423 |
Provide a correct Python 3 solution for this coding contest problem.
You survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department.
The following problems were described in the pamphlet.
> How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5.
If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input.
Input
The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≤ N ≤ 1000.
The end of the input is indicated by a single line of zeros.
Output
The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output.
Example
Input
9
500
0
Output
2
3 | instruction | 0 | 34,212 | 5 | 68,424 |
"Correct Solution:
```
while True:
n = int(input())
if n == 0:
break
else:
cnt = 0
for i in range(1, n // 2 + 1):
sum = i
for j in range(i + 1, n):
sum += j
if sum == n:
cnt += 1
break
elif sum > n:
break
print(cnt)
``` | output | 1 | 34,212 | 5 | 68,425 |
Provide a correct Python 3 solution for this coding contest problem.
You survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department.
The following problems were described in the pamphlet.
> How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5.
If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input.
Input
The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≤ N ≤ 1000.
The end of the input is indicated by a single line of zeros.
Output
The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output.
Example
Input
9
500
0
Output
2
3 | instruction | 0 | 34,213 | 5 | 68,426 |
"Correct Solution:
```
while True:
x=int(input())
if x==0:
break
y=0
for i in range(2,50):
if x/i - i/2<0.1:
break
if i%2==0:
if x/i-x//i==0.5:
y+=1
else:
if x%i==0:
y+=1
print(y)
``` | output | 1 | 34,213 | 5 | 68,427 |
Provide a correct Python 3 solution for this coding contest problem.
You survived several months of exam wars and were able to enter ICPC University on a sunny day. On the day of admission, there was an enthusiastic recruitment of circles on the campus of the university, and you received a large number of pamphlets and returned. When you return to your room, you find one of the pamphlets you received that interests you. The pamphlet was given by the university's public relations department.
The following problems were described in the pamphlet.
> How many combinations of two or more consecutive positive integers have a sum of N? For example, 9 has two combinations, 2 + 3 + 4 and 4 + 5.
If you were curious about the answer to this question, you decided to write a program to find out the answer. Therefore, your job is to write a program that outputs the answer to the problem for the positive integer N given as the input.
Input
The input is a sequence of datasets. Each dataset is a line of one integer N. Where 1 ≤ N ≤ 1000.
The end of the input is indicated by a single line of zeros.
Output
The output is the order of the input datasets, which is the answer to the question for the positive integers represented by each dataset of the inputs. No other characters should be in the output.
Example
Input
9
500
0
Output
2
3 | instruction | 0 | 34,214 | 5 | 68,428 |
"Correct Solution:
```
while True:
n = int(input())
if n == 0: break
ans = 0
for i in range(1,n//2+2):
sumnum = i
if sumnum >= n:
continue
j = i+1
while j <= n//2+1:
sumnum += j
j += 1
if sumnum >= n:
if sumnum == n:
ans += 1
break
print(ans)
``` | output | 1 | 34,214 | 5 | 68,429 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
mx = max(a)
temp = 0
sol = 0
for i in range(n):
if a[i] == mx:
temp += 1
else:
temp = 0
sol = max(sol,temp)
print(sol)
``` | instruction | 0 | 34,293 | 5 | 68,586 |
Yes | output | 1 | 34,293 | 5 | 68,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
c=0
m=max(a)
x=0
for i in range(n):
if a[i]==m:
x+=1
if a[i]!=m:
x=0
c=max(x,c)
print(c)
``` | instruction | 0 | 34,294 | 5 | 68,588 |
Yes | output | 1 | 34,294 | 5 | 68,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().split()))
maxxn = max(a)
l = []
summ =0
f = -123
maxxi=-1
x = []
for i in range(len(a)):
summ +=a[i]
l.append(summ)
if a[i]==maxxn:
x.append(i)
# print("f: "+str(f))
# print(f,maxxi)
# print(x)
t = []
for i in range(len(x)):
if i!=len(x)-1:
t.append(x[i+1]-x[i])
b = False
# cnt = 0
cntlst =[]
for i in range(len(t)):
if t[i] ==1 and b==False:
b = True
cnt=1
if i==len(t)-1:
cntlst.append(cnt)
elif t[i]==1 and b==True:
cnt+=1
if i==len(t)-1:
cntlst.append(cnt)
elif t[i]!=1 and b ==False:
pass
elif t[i] !=1 and b==True:
cntlst.append(cnt)
b=False
if cntlst:
print(max(cntlst)+1)
else:
print(1)
``` | instruction | 0 | 34,295 | 5 | 68,590 |
Yes | output | 1 | 34,295 | 5 | 68,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
Submitted Solution:
```
t=int(input())
a=list(map(int,input().split()))
p=max(a)
c=1
j=0
m=1
while(j<(t-1)):
if a[j]==p:
if a[j]==a[j+1]:
c+=1
m=max(m,c)
else:
c=1
else:
c=1
j+=1
print(m)
``` | instruction | 0 | 34,296 | 5 | 68,592 |
Yes | output | 1 | 34,296 | 5 | 68,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
Submitted Solution:
```
m = int(input())
lis = list(map(int, input().split()))
ma = max(lis)
ct = 0
li2 = []
if(len(lis) == 1):
print(1)
else :
if(lis[0] == ma):
ct = 1
for i in range(m-1):
if(lis[i] == ma and lis[i+1] != ma):
li2.append(ct)
elif(lis[i] == ma and lis[i+1] == ma):
ct+=1
elif(lis[i] != ma and lis[i+1] == ma):
ct = 1
print(max(li2))
``` | instruction | 0 | 34,297 | 5 | 68,594 |
No | output | 1 | 34,297 | 5 | 68,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
Submitted Solution:
```
n = int(input())
arr = [int(x) for x in input().split()]
s = arr.copy()
mean = 0
ans = 0
for i in range(n - 1):
s[i+1] += s[i]
for i in range(n):
for j in range(i+1, n):
if (s[j] - s[i]) / (j-i) > mean:
mean = max(mean, (s[j] - s[i]) / (j-i))
ans = j-i
elif (s[j] - s[i]) / (j-i) == mean:
ans = max(ans, j-i)
print(ans)
``` | instruction | 0 | 34,299 | 5 | 68,598 |
No | output | 1 | 34,299 | 5 | 68,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given array a_1, a_2, ..., a_n. Find the subsegment a_l, a_{l+1}, ..., a_r (1 ≤ l ≤ r ≤ n) with maximum arithmetic mean (1)/(r - l + 1)∑_{i=l}^{r}{a_i} (in floating-point numbers, i.e. without any rounding).
If there are many such subsegments find the longest one.
Input
The first line contains single integer n (1 ≤ n ≤ 10^5) — length of the array a.
The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9) — the array a.
Output
Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.
Example
Input
5
6 1 6 6 0
Output
2
Note
The subsegment [3, 4] is the longest among all subsegments with maximum arithmetic mean.
Submitted Solution:
```
n = int(input())
l = list(map(int,input().split()))
max_length = 1
max_value = 0
j = 0
while(j<n):
if(l[j]>=max_value):
max_value = l[j]
length = 0
while(j<n and l[j]==max_value):
length+=1
j+=1
if(length>max_length):
max_length = length
else:
j+=1
print(max_length)
``` | instruction | 0 | 34,300 | 5 | 68,600 |
No | output | 1 | 34,300 | 5 | 68,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>.
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Submitted Solution:
```
import math
n=int(input());
if(n==1):
print("YES",1);
elif(n==2):
print("YES",1,2,sep="\n");
elif(n==4):
print("YES",1,3,2,4,sep="\n");
else:
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
print("NO");
exit();
print("YES",1,sep="\n");
for i in range (2,n):
print((i*pow(i-1,n-2,n))%n);
print(n);
``` | instruction | 0 | 34,673 | 5 | 69,346 |
Yes | output | 1 | 34,673 | 5 | 69,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>.
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Submitted Solution:
```
import sys
input = sys.stdin.readline
def check(a, n):
x = 1
s = set()
for i in a:
x = (x*i) % n
if x in s:
return False
s.add(x)
return True
def solve():
n = int(input())
if n == 4:
print('YES\n1\n3\n2\n4\n')
return
if n == 1:
print('YES\n1\n')
return
if n == 2:
print('YES\n1\n2\n')
return
for i in range(2,n):
if n % i == 0:
print('NO')
return
m = n - 1
d = []
for i in range(2,m):
if i * i > m:
break
if m % i == 0:
d.append(i)
m //= i
while m % i == 0:
m //= i
if m != 1:
d.append(m)
#print(d)
m = n-1
for w in range(2,n):
ok = True
for i in d:
if pow(w, n//i, n) == 1:
ok = False
break
if ok:
p = [None]*(n-1)
p[0] = 1
p[1] = w
x = w
for i in range(2,n-1):
x = (x*w) % n
p[i] = x
#print(len(set(p)))
#print(w)
print('YES')
#ans = []
for i in range(0,m):
print(p[(i*((-1)**i))%m])
print(n)
#print(check(ans, n))
return
raise Exception('wut')
solve()
``` | instruction | 0 | 34,674 | 5 | 69,348 |
Yes | output | 1 | 34,674 | 5 | 69,349 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>.
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Submitted Solution:
```
import math
n=int(input());
if(n==1):
print("YES",1);
elif(n==4):
print("YES",1,3,2,4,sep="\n");
else:
for i in range(2,int(math.sqrt(n))+1):
if n%i==0:
print("NO");
exit();
print("YES",1,sep="\n");
for i in range (2,n):
print((i*pow(i-1,n-2,n))%n);
print(n);
``` | instruction | 0 | 34,675 | 5 | 69,350 |
Yes | output | 1 | 34,675 | 5 | 69,351 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>.
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Submitted Solution:
```
def is_prime(n):
if n < 2:
return False
if n<4:
return True
if n%2 == 0 or n%3 == 0:
return False
i = 5
while i <= n**0.5:
if n%i == 0 or n%(i+2) == 0:
return False
i += 6
return True
n = int(input())
if n==4:
print("YES\n1\n3\n2\n4")
elif n==1:
print("YES\n1")
elif(not is_prime(n)):
print("NO")
else:
print("YES\n1 %s"%(" ".join(str((pow(i+1,n-2,n)*(i+2))%n or n) for i in range(n-1))))
``` | instruction | 0 | 34,676 | 5 | 69,352 |
Yes | output | 1 | 34,676 | 5 | 69,353 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>.
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Submitted Solution:
```
n = int(input())
if n == 4:
print ("YES\n1 3 2 4")
elif [i for i in range(2,n) if n%i==0]:
print("NO")
else:
print("YES\n1 %s"%(" ".join(str((pow(x+1,n-2,n)*(x+2))%n or n) for x in range(n-2))))
``` | instruction | 0 | 34,678 | 5 | 69,356 |
No | output | 1 | 34,678 | 5 | 69,357 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>.
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Submitted Solution:
```
import math
n = 17
def isprime(x):
if x == 1:
return False
if x == 2:
return true
if x % 2 == 0:
return False
for i in range(3, round(math.sqrt(n))):
if n % i == 0:
return False
return True
def multi(a, b):
ans = 0
a %= n
while b:
if b & 1:
ans = (ans + a) % n
b -= 1
b >>= 1
a = (a + a) % n
return ans
def quick_mod(a, b):
ans = 1
a %= n
while b:
if b & 1:
ans = multi(ans,a)
b -= 1
b >>= 1
a = multi(a,a)
return ans
def main():
# 'return None to exit function'
n = int(input())
if n == 4:
print("YES\n1\n3\n2\n4")
return None
elif n == 1:
print("YES\n1")
return None
elif not isprime(n):
print("NO")
return None
print("YES\n1")
for i in range(2, n):
print(i * quick_mod(i - 1, n - 2) % n)
print(n)
if __name__ == '__main__':
main()
``` | instruction | 0 | 34,679 | 5 | 69,358 |
No | output | 1 | 34,679 | 5 | 69,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence <image>.
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
Input
The only input line contains an integer n (1 ≤ n ≤ 105).
Output
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
7
Output
YES
1
4
3
6
5
2
7
Input
6
Output
NO
Note
For the second sample, there are no valid sequences.
Submitted Solution:
```
n=int(input());
if(n==1):
print("YES",1);
elif(n==2):
print("YES",1,2,sep="\n");
elif(n==4):
print("YES",1,3,2,4,sep="\n");
else:
for i in range(2,n):
if n%i==0:
print("NO");
exit();
print("YES",1,sep="\n");
for i in range (2,n):
print((i*pow(i-1,n-2,n))%n);
print(n);
``` | instruction | 0 | 34,680 | 5 | 69,360 |
No | output | 1 | 34,680 | 5 | 69,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
# 同じインデックスの重複を除くので、関数の呼び出し回数が少し少ないはず。
# ただ、重複を除く分のオーバーヘッドがあるので、
# fmm, fmo, fooが軽い関数だと、スピードはそこまで出ない。
class LazySegmentTree:
__slots__ = ["n", "data", "lazy", "me", "oe", "fmm", "fmo", "foo"]
def __init__(self, monoid_data, monoid_identity, operator_identity, func_monoid_monoid, func_monoid_operator, func_operator_operator):
self.me = monoid_identity
self.oe = operator_identity
self.fmm = func_monoid_monoid
self.fmo = func_monoid_operator
self.foo = func_operator_operator
self.n = len(monoid_data)
self.data = monoid_data * 2
for i in range(self.n-1, 0, -1):
self.data[i] = self.fmm(self.data[2*i], self.data[2*i+1])
self.lazy = [self.oe] * (self.n * 2)
def replace(self, index, value):
index += self.n
# propagation
for shift in range(index.bit_length()-1, 0, -1):
i = index >> shift
self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i])
self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
self.data[i] = self.fmo(self.data[i], self.lazy[i])
self.lazy[i] = self.oe
# update
self.data[index] = value
self.lazy[index] = self.oe
# recalculation
i = index
while i > 1:
i //= 2
self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
self.lazy[i] = self.oe
def effect(self, l, r, operator):
l += self.n
r += self.n
l0 = l
r0 = r - 1
while l0 % 2 == 0:
l0 //= 2
while r0 % 2 == 1:
r0 //= 2
l0 //= 2
r0 //= 2
# preparing indices
indices = []
while r0 > l0:
indices.append(r0)
r0 //= 2
while l0 > r0:
indices.append(l0)
l0 //= 2
while l0 and l0 != r0:
indices.append(r0)
r0 //= 2
if l0 == r0:
break
indices.append(l0)
l0 //= 2
while r0:
indices.append(r0)
r0 //= 2
# propagation
for i in reversed(indices):
self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i])
self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
self.data[i] = self.fmo(self.data[i], self.lazy[i])
self.lazy[i] = self.oe
# effect
while l < r:
if l % 2:
self.lazy[l] = self.foo(self.lazy[l], operator)
l += 1
if r % 2:
r -= 1
self.lazy[r] = self.foo(self.lazy[r], operator)
l //= 2
r //= 2
# recalculation
for i in indices:
self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) )
self.lazy[i] = self.oe
def folded(self, l, r):
l += self.n
r += self.n
l0 = l
r0 = r - 1
while l0 % 2 == 0:
l0 //= 2
while r0 % 2 == 1:
r0 //= 2
l0 //= 2
r0 //= 2
# preparing indices
indices = []
while r0 > l0:
indices.append(r0)
r0 //= 2
while l0 > r0:
indices.append(l0)
l0 //= 2
while l0 and l0 != r0:
indices.append(r0)
r0 //= 2
if l0 == r0:
break
indices.append(l0)
l0 //= 2
while r0:
indices.append(r0)
r0 //= 2
# propagation
for i in reversed(indices):
self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i])
self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i])
self.data[i] = self.fmo(self.data[i], self.lazy[i])
self.lazy[i] = self.oe
# fold
left_folded = self.me
right_folded = self.me
while l < r:
if l % 2:
left_folded = self.fmm(left_folded, self.fmo(self.data[l], self.lazy[l]))
l += 1
if r % 2:
r -= 1
right_folded = self.fmm(self.fmo(self.data[r], self.lazy[r]), right_folded)
l //= 2
r //= 2
return self.fmm(left_folded, right_folded)
def atc2():
# Monoid: ((0の数), (1の数), (転倒数))
# Operator: 反転するか? (1 or 0)
import sys
input = sys.stdin.buffer.readline
N, Q = map(int, input().split())
monoid_data = [(0, 1, 0) if A == b'1' else (1, 0, 0) for A in input().split()]
def fmm(m1, m2):
return (m1[0] + m2[0], m1[1] + m2[1], m1[2] + m2[2] + m1[1] * m2[0])
def fmo(m1, o1):
if o1:
return (m1[1], m1[0], m1[0] * m1[1] - m1[2])
else:
return m1
def foo(o1, o2):
return o1 ^ o2
lst = LazySegmentTree(monoid_data, (0, 0, 0), 0, fmm, fmo, foo)
ans = []
for _ in range(Q):
T, L, R = map(int, input().split())
if T == 1:
lst.effect(L-1, R, 1)
else:
ans.append(lst.folded(L-1, R)[2])
print('\n'.join(map(str, ans)))
if __name__ == "__main__":
atc2()
``` | instruction | 0 | 34,905 | 5 | 69,810 |
Yes | output | 1 | 34,905 | 5 | 69,811 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
class LazySegmentTree():
def __init__(self, n, op, e, mapping, composition, id):
self.n = n
self.op = op
self.e = e
self.mapping = mapping
self.composition = composition
self.id = id
self.log = (n - 1).bit_length()
self.size = 1 << self.log
self.d = [e] * (2 * self.size)
self.lz = [id] * (self.size)
def update(self, k):
self.d[k] = self.op(self.d[2 * k], self.d[2 * k + 1])
def all_apply(self, k, f):
self.d[k] = self.mapping(f, self.d[k])
if k < self.size:
self.lz[k] = self.composition(f, self.lz[k])
def push(self, k):
self.all_apply(2 * k, self.lz[k])
self.all_apply(2 * k + 1, self.lz[k])
self.lz[k] = self.id
def build(self, arr):
#assert len(arr) == self.n
for i, a in enumerate(arr):
self.d[self.size + i] = a
for i in range(1, self.size)[::-1]:
self.update(i)
def set(self, p, x):
#assert 0 <= p < self.n
p += self.size
for i in range(1, self.log + 1)[::-1]:
self.push(p >> i)
self.d[p] = x
for i in range(1, self.log + 1):
self.update(p >> i)
def get(self, p):
#assert 0 <= p < self.n
p += self.size
for i in range(1, self.log + 1):
self.push(p >> i)
return self.d[p]
def prod(self, l, r):
#assert 0 <= l <= r <= self.n
if l == r: return self.e
l += self.size
r += self.size
for i in range(1, self.log + 1)[::-1]:
if ((l >> i) << i) != l: self.push(l >> i)
if ((r >> i) << i) != r: self.push(r >> i)
sml = smr = self.e
while l < r:
if l & 1:
sml = self.op(sml, self.d[l])
l += 1
if r & 1:
r -= 1
smr = self.op(self.d[r], smr)
l >>= 1
r >>= 1
return self.op(sml, smr)
def all_prod(self):
return self.d[1]
def apply(self, p, f):
#assert 0 <= p < self.n
p += self.size
for i in range(1, self.log + 1)[::-1]:
self.push(p >> i)
self.d[p] = self.mapping(f, self.d[p])
for i in range(1, self.log + 1):
self.update(p >> i)
def range_apply(self, l, r, f):
#assert 0 <= l <= r <= self.n
if l == r: return
l += self.size
r += self.size
for i in range(1, self.log + 1)[::-1]:
if ((l >> i) << i) != l: self.push(l >> i)
if ((r >> i) << i) != r: self.push((r - 1) >> i)
l2 = l
r2 = r
while l < r:
if l & 1:
self.all_apply(l, f)
l += 1
if r & 1:
r -= 1
self.all_apply(r, f)
l >>= 1
r >>= 1
l = l2
r = r2
for i in range(1, self.log + 1):
if ((l >> i) << i) != l: self.update(l >> i)
if ((r >> i) << i) != r: self.update((r - 1) >> i)
def max_right(self, l, g):
#assert 0 <= l <= self.n
#assert g(self.e)
if l == self.n: return self.n
l += self.size
for i in range(1, self.log + 1)[::-1]:
self.push(l >> i)
sm = self.e
while True:
while l % 2 == 0: l >>= 1
if not g(self.op(sm, self.d[l])):
while l < self.size:
self.push(l)
l = 2 * l
if g(self.op(sm, self.d[l])):
sm = self.op(sm, self.d[l])
l += 1
return l - self.size
sm = self.op(sm, self.d[l])
l += 1
if (l & -l) == l: return self.n
def min_left(self, r, g):
#assert 0 <= r <= self.n
#assert g(self.e)
if r == 0: return 0
r += self.size
for i in range(1, self.log + 1)[::-1]:
self.push((r - 1) >> i)
sm = self.e
while True:
r -= 1
while r > 1 and r % 2: r >>= 1
if not g(self.op(self.d[r], sm)):
while r < self.size:
self.push(r)
r = 2 * r + 1
if g(self.op(self.d[r], sm)):
sm = self.op(self.d[r], sm)
r -= 1
return r + 1 - self.size
sm = self.op(self.d[r], sm)
if (r & -r) == r: return 0
import sys
input = sys.stdin.buffer.readline
from operator import xor
N, Q = map(int, input().split())
def op(x, y):
x0, x1, x2 = x
y0, y1, y2 = y
return x0 + y0, x1 + y1, x2 + y2 + x1 * y0
def mapping(p, x):
x0, x1, x2 = x
if p:
return x1, x0, x0 * x1 - x2
else:
return x0, x1, x2
arr = ((1 - a, a, 0) for a in map(int, input().split()))
lst = LazySegmentTree(N, op, (0, 0, 0), mapping, xor, 0)
lst.build(arr)
res = list()
for _ in range(Q):
t, l, r = map(int, input().split())
if t == 1:
lst.range_apply(l - 1, r, 1)
else:
res.append(lst.prod(l - 1, r)[2])
print('\n'.join(map(str, res)))
``` | instruction | 0 | 34,906 | 5 | 69,812 |
Yes | output | 1 | 34,906 | 5 | 69,813 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
import sys
sys.setrecursionlimit(10**7)
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
class LazySegTree(): # モノイドに対して適用可能、Nが2冪でなくても良い
def __init__(self,N,X_func,A_func,operate,X_unit,A_unit):
self.N = N
self.X_func = X_func
self.A_func = A_func
self.operate = operate
self.X_unit = X_unit
self.A_unit = A_unit
self.X = [self.X_unit]*(2*self.N)
self.A = [self.A_unit]*(2*self.N)
self.size = [0]*(2*self.N)
def build(self,init_value): # 初期値を[N,2N)に格納
for i in range(self.N):
self.X[self.N+i] = init_value[i]
self.size[self.N+i] = 1
for i in range(self.N-1,0,-1):
self.X[i] = self.X_func(self.X[i << 1],self.X[i << 1 | 1])
self.size[i] = self.size[i << 1] + self.size[i << 1 | 1]
def update(self,i,x): # i番目(0-index)の値をxに変更
i += self.N
self.X[i] = x
i >>= 1
while i:
self.X[i] = self.X_func(self.X[i << 1],self.X[i << 1 | 1])
i >>= 1
def eval_at(self,i): # i番目で作用を施した値を返す
return self.operate(self.X[i],self.A[i],self.size[i])
def eval_above(self,i): # i番目より上の値を再計算する
i >>= 1
while i:
self.X[i] = self.X_func(self.eval_at(i << 1),self.eval_at(i << 1 | 1))
i >>= 1
def propagate_at(self,i): # i番目で作用を施し、1つ下に作用の情報を伝える
self.X[i] = self.operate(self.X[i],self.A[i],self.size[i])
self.A[i << 1] = self.A_func(self.A[i << 1],self.A[i])
self.A[i << 1 | 1] = self.A_func(self.A[i << 1 | 1], self.A[i])
self.A[i] = self.A_unit
def propagate_above(self,i): # i番目より上で作用を施す
H = i.bit_length()
for h in range(H,0,-1):
self.propagate_at(i >> h)
def fold(self,L,R): # [L,R)の区間取得
L += self.N
R += self.N
L0 = L // (L & -L)
R0 = R // (R & -R) - 1
self.propagate_above(L0)
self.propagate_above(R0)
vL = self.X_unit
vR = self.X_unit
while L < R:
if L & 1:
vL = self.X_func(vL,self.eval_at(L))
L += 1
if R & 1:
R -= 1
vR = self.X_func(self.eval_at(R),vR)
L >>= 1
R >>= 1
return self.X_func(vL,vR)
def operate_range(self,L,R,x): # [L,R)にxを作用
L += self.N
R += self.N
L0 = L // (L & -L)
R0 = R // (R & -R) - 1
self.propagate_above(L0)
self.propagate_above(R0)
while L < R:
if L & 1:
self.A[L] = self.A_func(self.A[L],x)
L += 1
if R & 1:
R -= 1
self.A[R] = self.A_func(self.A[R],x)
L >>= 1
R >>= 1
self.eval_above(L0)
self.eval_above(R0)
N,Q = MI()
A = LI()
B = []
for i in range(N):
if A[i] == 0:
B.append((0,1,0))
else:
B.append((0,0,1))
# (その区間の転倒数,0の個数,1の個数)
def X_func(x,y):
x0,x1,x2 = x
y0,y1,y2 = y
return (x0+y0+x2*y1,x1+y1,x2+y2)
def A_func(a,b):
return a ^ b
def operate(x,a,r): # 右作用
if a == 0:
return x
x0,x1,x2 = x
return (x1*x2-x0,x2,x1)
X_unit = (0,0,0)
A_unit = 0
LST = LazySegTree(N,X_func,A_func,operate,X_unit,A_unit)
LST.build(B)
for i in range(Q):
T,L,R = MI()
if T == 1:
LST.operate_range(L-1,R,1)
else:
print(LST.fold(L-1,R)[0])
``` | instruction | 0 | 34,907 | 5 | 69,814 |
Yes | output | 1 | 34,907 | 5 | 69,815 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
# self.data: 1-indexed
# __1__
# _2_ _3_
# 4 5 6 7
# f(f(a, b), c) == f(a, f(b, c))
class LazySegmentTree:
# a = [default] * n
# O(n)
def __init__(self, n, f, default=(0, 0, 1)):
self.num_leaf = 2 ** (n-1).bit_length()
self.data = [default] * (2*self.num_leaf)
self.lazy = [True] * (2*self.num_leaf)
self.f = f
# You can use first_update before you use update.
# a[i] = x
# O(log(n))
def first_update(self, i, x):
i += self.num_leaf
self.data[i] = x
i >>= 1
while i > 0:
self.data[i] = self.f(self.data[2*i], self.data[2*i+1])
i >>= 1
def gindex(self, l, r):
lis = []
l += self.num_leaf
r += self.num_leaf
lm = (l // (l & -l)) >> 1
rm = (r // (r & -r)) >> 1
while l < r:
if l <= lm:
lis.append(l)
if r <= rm:
lis.append(r)
l >>= 1
r >>= 1
while l:
lis.append(l)
l >>= 1
lis.reverse()
return lis
def lazy_processing(self, i):
inversion, num_0, num_1 = self.data[i]
self.data[i] = (num_0 * num_1 - inversion, num_1, num_0)
self.lazy[i] = not self.lazy[i]
# from parent to children
def propagate(self, lis):
for i in lis:
if self.lazy[i]:
continue
self.lazy_processing(2*i)
self.lazy_processing(2*i+1)
self.lazy[i] = True
# update a[l:r]
def update(self, l, r):
lis = self.gindex(l, r)
# top-down propagation
self.propagate(lis)
l += self.num_leaf
r += self.num_leaf - 1
while l < r:
if l & 1:
self.lazy_processing(l)
l += 1
if not r & 1:
self.lazy_processing(r)
r -= 1
l >>= 1
r >>= 1
if l == r:
self.lazy_processing(l)
# bottom-up propagation
lis.reverse()
for i in lis:
self.data[i] = self.f(self.data[2*i], self.data[2*i+1])
# return f(a[l:r])
def query(self, l, r):
# top-down propagation
self.propagate(self.gindex(l, r))
l += self.num_leaf
r += self.num_leaf - 1
lres, rres = (0, 1, 0), self.data[0] # self.data[0] == default
while l < r:
if l & 1:
lres = self.f(lres, self.data[l])
l += 1
if not r & 1:
rres = self.f(self.data[r], rres)
r -= 1
l >>= 1
r >>= 1
if l == r:
res = self.f(self.f(lres, self.data[l]), rres)
else:
res = self.f(lres, rres)
return res
from sys import stdin
input = stdin.buffer.readline
def main():
n, q = map(int, input().split())
a = list(map(int, input().split()))
# tup = (inversion, num_0, num_1)
def f(tup1, tup2):
return (tup1[0] + tup2[0] + tup1[2] * tup2[1], tup1[1] + tup2[1], tup1[2] + tup2[2])
lst = LazySegmentTree(n, f=f)
for i, x in enumerate(a):
lst.first_update(i, (0, 1-x, x))
ans = []
for _ in range(q):
t, l, r = list(map(int, input().split()))
if t == 1:
lst.update(l-1, r)
else:
ans.append(lst.query(l-1, r)[0])
for i in ans:
print(i)
main()
``` | instruction | 0 | 34,908 | 5 | 69,816 |
Yes | output | 1 | 34,908 | 5 | 69,817 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
from copy import *
def m(X,Y):
return [X[0]+Y[0],X[1]+Y[1],X[2]+Y[2]+X[1]*Y[0]]
def r(X,F):
if F:
return [X[1],X[0],X[0]*X[1]-X[2]]
else:
return X[:]
def init(N,node,func,first,unit,unitr,refl,prop):
while len(node):
del node[-1]
node.append([])
node.append([])
n=1
while n<N:
n<<=1
for j in range((n<<1)-1):
node[0].append(deepcopy(first))
node[1].append(deepcopy(unitr))
for j in range(N,n):
node[0][n-1+j]=deepcopy(unit)
for j in range(n-2,-1,-1):
node[0][j]=func(node[0][(j<<1)+1],node[0][(j<<1)+2])
node[0].append(func)
node[0].append(deepcopy(unit))
node[0].append(n)
node[1].append(refl)
node[1].append(deepcopy(unitr))
node[1].append(prop)
def refl(node,k):
node[0][k]=node[1][-3](node[0][k],node[1][k])
if k<node[0][-1]-1:
node[1][(k<<1)+1]=node[1][-1](node[1][(k<<1)+1],node[1][k])
node[1][(k<<1)+2]=node[1][-1](node[1][(k<<1)+2],node[1][k])
node[1][k]=deepcopy(node[1][-2])
def upd(node,x,a,b):
q=[[0,0,node[0][-1],0]]
k=0
while len(q):
k=q[-1][:]
del q[-1]
if k[3]:
node[0][k[0]]=node[0][-3](node[0][(k[0]<<1)+1],node[0][(k[0]<<1)+2])
continue
refl(node,k[0])
if b<=k[1] or k[2]<=a:
continue
if a<=k[1] and k[2]<=b:
node[1][k[0]]=deepcopy(x)
refl(node,k[0])
else:
q.append([k[0],k[1],k[2],1])
q.append([(k[0]<<1)+1,k[1],(k[1]+k[2])>>1,0])
q.append([(k[0]<<1)+2,(k[1]+k[2])>>1,k[2],0])
def query(node,a,b):
r=deepcopy(node[0][-2])
q=[[0,0,node[0][-1]]]
k=0
while len(q):
k=q[-1][:]
del q[-1]
if b<=k[1] or k[2]<=a:
continue
refl(node,k[0])
if a<=k[1] and k[2]<=b:
r=node[0][-3](node[0][k[0]],r)
else:
q.append([(k[0]<<1)+1,k[1],(k[1]+k[2])>>1])
q.append([(k[0]<<1)+2,(k[1]+k[2])>>1,k[2]])
return r
N,Q=map(int,input().split())
A=list(map(int,input().split()))
X=[]
init(N,X,lambda x,y:m(x,y),[1,0,0],[0,0,0],0,lambda x,y:r(x,y),lambda x,y:x^y)
for i in range(N):
if A[i]:
upd(X,1,i,i+1)
a,b,c=0,0,0
for i in range(Q):
a,b,c=map(int,input().split())
if a==1:
upd(X,1,b-1,c)
else:
print(query(X,b-1,c)[2])
``` | instruction | 0 | 34,909 | 5 | 69,818 |
No | output | 1 | 34,909 | 5 | 69,819 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip()
class LazySegmentTree():
def __init__(self, init, unitX, unitA, f, g, h):
self.f = f # (X, X) -> X
self.g = g # (X, A, size) -> X
self.h = h # (A, A) -> A
self.unitX = unitX
self.unitA = unitA
self.f = f
if type(init) == int:
self.n = init
# self.n = 1 << (self.n - 1).bit_length()
self.X = [unitX] * (self.n * 2)
self.size = [1] * (self.n * 2)
else:
self.n = len(init)
# self.n = 1 << (self.n - 1).bit_length()
self.X = [unitX] * self.n + init + [unitX] * (self.n - len(init))
self.size = [0] * self.n + [1] * len(init) + [0] * (self.n - len(init))
for i in range(self.n-1, 0, -1):
self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
for i in range(self.n - 1, 0, -1):
self.size[i] = self.size[i*2] + self.size[i*2|1]
self.A = [unitA] * (self.n * 2)
def update(self, i, x):
i += self.n
self.X[i] = x
i >>= 1
while i:
self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
i >>= 1
def calc(self, i):
return self.g(self.X[i], self.A[i], self.size[i])
def calc_above(self, i):
i >>= 1
while i:
self.X[i] = self.f(self.calc(i*2), self.calc(i*2|1))
i >>= 1
def propagate(self, i):
self.X[i] = self.g(self.X[i], self.A[i], self.size[i])
self.A[i*2] = self.h(self.A[i*2], self.A[i])
self.A[i*2|1] = self.h(self.A[i*2|1], self.A[i])
self.A[i] = self.unitA
def propagate_above(self, i):
H = i.bit_length()
for h in range(H, 0, -1):
self.propagate(i >> h)
def propagate_all(self):
for i in range(1, self.n):
self.propagate(i)
def getrange(self, l, r):
l += self.n
r += self.n
l0, r0 = l // (l & -l), r // (r & -r) - 1
self.propagate_above(l0)
self.propagate_above(r0)
al = self.unitX
ar = self.unitX
while l < r:
if l & 1:
al = self.f(al, self.calc(l))
l += 1
if r & 1:
r -= 1
ar = self.f(self.calc(r), ar)
l >>= 1
r >>= 1
return self.f(al, ar)
def getvalue(self, i):
i += self.n
self.propagate_above(i)
return self.calc(i)
def operate_range(self, l, r, a):
l += self.n
r += self.n
l0, r0 = l // (l & -l), r // (r & -r) - 1
self.propagate_above(l0)
self.propagate_above(r0)
while l < r:
if l & 1:
self.A[l] = self.h(self.A[l], a)
l += 1
if r & 1:
r -= 1
self.A[r] = self.h(self.A[r], a)
l >>= 1
r >>= 1
self.calc_above(l0)
self.calc_above(r0)
# Find r s.t. calc(l, ..., r-1) = True and calc(l, ..., r) = False
def max_right(self, l, z):
if l >= self.n: return self.n
l += self.n
s = self.unitX
while 1:
while l % 2 == 0:
l >>= 1
if not z(self.f(s, self.calc(l))):
while l < self.n:
l *= 2
if z(self.f(s, self.calc(l))):
s = self.f(s, self.calc(l))
l += 1
return l - self.n
s = self.f(s, self.calc(l))
l += 1
if l & -l == l: break
return self.n
# Find l s.t. calc(l, ..., r-1) = True and calc(l-1, ..., r-1) = False
def min_left(self, r, z):
if r <= 0: return 0
r += self.n
s = self.unitX
while 1:
r -= 1
while r > 1 and r % 2:
r >>= 1
if not z(self.f(self.calc(r), s)):
while r < self.n:
r = r * 2 + 1
if z(self.f(self.calc(r), s)):
s = self.f(self.calc(r), s)
r -= 1
return r + 1 - self.n
s = self.f(self.calc(r), s)
if r & -r == r: break
return 0
def debug(self):
X = self.X
print("X =", [self.calc(i) for i in range(self.n, self.n * 2)])
if False:
f = lambda x, y: (x[0] + y[0] + x[2] * y[1], x[1] + y[1], x[2] + y[2])
g = lambda x, a, s: (x[1] * x[2] - x[0], x[2], x[1]) if a else x
h = lambda a, b: a ^ b
unitX = (0, 0, 0)
unitA = 0
# (inversion, number of zeros, number of ones)
mm = (1 << 15) - 1
mmm = ((1 << 15) - 1) << 15
def f(x, y):
return x + y + ((x & mm) * (y & mmm) << 15)
def g(x, a, s):
x0, x1, x2 = x >> 30, (x >> 15) & mm, x & mm
return (x1 * x2 - x0 << 30) + (x2 << 15) + x1 if a else x
def h(a, b):
return a ^ b
unitX = 0
unitA = 0
N, Q = map(int, input().split())
A = [(1 << 15) if int(a) == 0 else 1 for a in input().split()]
st = LazySegmentTree(A, unitX, unitA, f, g, h)
for _ in range(Q):
t, l, r = map(int, input().split())
if t == 1:
st.operate_range(l - 1, r, 1)
else:
print(st.getrange(l - 1, r) >> 30)
``` | instruction | 0 | 34,910 | 5 | 69,820 |
No | output | 1 | 34,910 | 5 | 69,821 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
class lazy_segtree:
#遅延評価セグメント木
def __init__(s, op, e, mapping, composition, id, v):
if type(v) is int: v = [e()] * v
s._n = len(v)
s.log = s.ceil_pow2(s._n)
s.size = 1 << s.log
s.d = [e()] * (2 * s.size)
s.lz = [id()] * s.size
s.e = e
s.op = op
s.mapping = mapping
s.composition = composition
s.id = id
for i in range(s._n): s.d[s.size + i] = v[i]
for i in range(s.size - 1, 0, -1): s.update(i)
# 1点更新
def set(s, p, x):
p += s.size
for i in range(s.log, 0, -1): s.push(p >> i)
s.d[p] = x
for i in range(1, s.log + 1): s.update(p >> i)
# 1点取得
def get(s, p):
p += s.size
for i in range(s.log, 0, -1): s.push(p >> i)
return s.d[p]
# 区間演算
def prod(s, l, r):
if l == r: return s.e()
l += s.size
r += s.size
for i in range(s.log, 0, -1):
if (((l >> i) << i) != l): s.push(l >> i)
if (((r >> i) << i) != r): s.push(r >> i)
sml, smr = s.e(), s.e()
while (l < r):
if l & 1:
sml = s.op(sml, s.d[l])
l += 1
if r & 1:
r -= 1
smr = s.op(s.d[r], smr)
l >>= 1
r >>= 1
return s.op(sml, smr)
# 全体演算
def all_prod(s): return s.d[1]
# 1点写像
def apply(s, p, f):
p += s.size
for i in range(s.log, 0, -1): s.push(p >> i)
s.d[p] = s.mapping(f, s.d[p])
for i in range(1, s.log + 1): s.update(p >> i)
# 区間写像
def apply(s, l, r, f):
if l == r: return
l += s.size
r += s.size
for i in range(s.log, 0, -1):
if (((l >> i) << i) != l): s.push(l >> i)
if (((r >> i) << i) != r): s.push((r - 1) >> i)
l2, r2 = l, r
while l < r:
if l & 1:
sml = s.all_apply(l, f)
l += 1
if r & 1:
r -= 1
smr = s.all_apply(r, f)
l >>= 1
r >>= 1
l, r = l2, r2
for i in range(1, s.log + 1):
if (((l >> i) << i) != l): s.update(l >> i)
if (((r >> i) << i) != r): s.update((r - 1) >> i)
# L固定時の最長区間のR
def max_right(s, l, g):
if l == s._n: return s._n
l += s.size
for i in range(s.log, 0, -1): s.push(l >> i)
sm = s.e()
while True:
while (l % 2 == 0): l >>= 1
if not g(s.op(sm, s.d[l])):
while l < s.size:
s.push(l)
l = 2 * l
if g(s.op(sm, s.d[l])):
sm = s.op(sm, s.d[l])
l += 1
return l - s.size
sm = s.op(sm, s.d[l])
l += 1
if (l & -l) == l: break
return s._n
# R固定時の最長区間のL
def min_left(s, r, g):
if r == 0: return 0
r += s.size
for i in range(s.log, 0, -1): s.push((r - 1) >> i)
sm = s.e()
while True:
r -= 1
while r > 1 and (r % 2): r >>= 1
if not g(s.op(s.d[r], sm)):
while r < s.size:
s.push(r)
r = 2 * r + 1
if g(s.op(s.d[r], sm)):
sm = s.op(s.d[r], sm)
r -= 1
return r + 1 - s.size
sm = s.op(s.d[r], sm)
if (r & - r) == r: break
return 0
def update(s, k): s.d[k] = s.op(s.d[2 * k], s.d[2 * k + 1])
def all_apply(s, k, f):
s.d[k] = s.mapping(f, s.d[k])
if k < s.size: s.lz[k] = s.composition(f, s.lz[k])
def push(s, k):
s.all_apply(2 * k, s.lz[k])
s.all_apply(2 * k + 1, s.lz[k])
s.lz[k] = s.id()
def ceil_pow2(s, n):
x = 0
while (1 << x) < n: x += 1
return x
def op(l, r):
x1, x2 = l >> 54, r >> 54
y1, y2 = (l >> 36) & 0x3FFFF, (r >> 36) & 0x3FFFF
z1, z2 = l & 0x3FFFF, r & 0x3FFFF
return ((x1 + x2) << 54) + ((y1 + y2) << 36) + z1 + z2 + y1 * x2
def e():
return 0
def mapping(l, r):
if (not l): return r
x = r >> 54
y = (r >> 36) & 0x3FFFF
z = r & 0x3FFFF
return (y << 54) + (x << 36) + x * y - z
def composition(l, r):
return (l and not r) or (not l and r)
def id():
return False
N, Q = list(map(int, input().split()))
A = list(map(int, input().split()))
TLR = [list(map(int, input().split())) for _ in range(Q)]
a = []
for i in A:
if i == 0:
a.append(1 << 54)
else:
a.append(1 << 36)
_ = 0
seg = lazy_segtree(op, e, mapping, composition, id, a)
for i in range(Q):
t, l, r = TLR[i]
l -= 1
if t == 1:
seg.apply(l, r, True)
else:
print(seg.prod(l, r) & 0x3FFFF)
``` | instruction | 0 | 34,911 | 5 | 69,822 |
No | output | 1 | 34,911 | 5 | 69,823 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
Process Q queries of the following types. The i-th query is represented by three integers T_i,L_i,R_i.
* T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
* T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}.
Note:The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
Constraints
* 1 \leq N \leq 2 \times 10^5
* 0 \leq A_i \leq 1
* 1 \leq Q \leq 2 \times 10^5
* 1 \leq T_i \leq 2
* 1 \leq L_i \leq R_i \leq N
* All values in Input are integer.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \cdots A_N
T_1 L_1 R_1
T_2 L_2 R_2
\vdots
T_Q L_Q R_Q
Output
For each query with T_i=2, print the answer.
Example
Input
5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2
Output
2
0
1
Submitted Solution:
```
# TODO: メモリリーク確認
# TODO: max_right とかが正しく動くか検証
# TODO: 更新ルールの異なる複数のセグ木を作ったときに正しく動くか検証
code_lazy_segtree = r"""
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
//#define ALLOW_MEMORY_LEAK // メモリリーク許容して高速化
// >>> AtCoder >>>
#ifndef ATCODER_LAZYSEGTREE_HPP
#define ATCODER_LAZYSEGTREE_HPP 1
#include <algorithm>
#ifndef ATCODER_INTERNAL_BITOP_HPP
#define ATCODER_INTERNAL_BITOP_HPP 1
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
} // namespace atcoder
#endif // ATCODER_INTERNAL_BITOP_HPP
#include <cassert>
#include <iostream>
#include <vector>
namespace atcoder {
template <class S,
S (*op)(S, S),
S (*e)(),
class F,
S (*mapping)(F, S),
F (*composition)(F, F),
F (*id)()>
struct lazy_segtree {
public:
lazy_segtree() : lazy_segtree(0) {}
lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
lz = std::vector<F>(size, id());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
return d[p];
}
S prod(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return e();
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push(r >> i);
}
S sml = e(), smr = e();
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() { return d[1]; }
void apply(int p, F f) {
assert(0 <= p && p < _n);
p += size;
for (int i = log; i >= 1; i--) push(p >> i);
d[p] = mapping(f, d[p]);
for (int i = 1; i <= log; i++) update(p >> i);
}
void apply(int l, int r, F f) {
assert(0 <= l && l <= r && r <= _n);
if (l == r) return;
l += size;
r += size;
for (int i = log; i >= 1; i--) {
if (((l >> i) << i) != l) push(l >> i);
if (((r >> i) << i) != r) push((r - 1) >> i);
}
{
int l2 = l, r2 = r;
while (l < r) {
if (l & 1) all_apply(l++, f);
if (r & 1) all_apply(--r, f);
l >>= 1;
r >>= 1;
}
l = l2;
r = r2;
}
for (int i = 1; i <= log; i++) {
if (((l >> i) << i) != l) update(l >> i);
if (((r >> i) << i) != r) update((r - 1) >> i);
}
}
template <bool (*g)(S)> int max_right(int l) {
return max_right(l, [](S x) { return g(x); });
}
template <class G> int max_right(int l, G g) {
assert(0 <= l && l <= _n);
assert(g(e()));
if (l == _n) return _n;
l += size;
for (int i = log; i >= 1; i--) push(l >> i);
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!g(op(sm, d[l]))) {
while (l < size) {
push(l);
l = (2 * l);
if (g(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*g)(S)> int min_left(int r) {
return min_left(r, [](S x) { return g(x); });
}
template <class G> int min_left(int r, G g) {
assert(0 <= r && r <= _n);
assert(g(e()));
if (r == 0) return 0;
r += size;
for (int i = log; i >= 1; i--) push((r - 1) >> i);
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!g(op(d[r], sm))) {
while (r < size) {
push(r);
r = (2 * r + 1);
if (g(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
std::vector<F> lz;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
void all_apply(int k, F f) {
d[k] = mapping(f, d[k]);
if (k < size) lz[k] = composition(f, lz[k]);
}
void push(int k) {
all_apply(2 * k, lz[k]);
all_apply(2 * k + 1, lz[k]);
lz[k] = id();
}
};
} // namespace atcoder
#endif // ATCODER_LAZYSEGTREE_HPP
// <<< AtCoder <<<
using namespace std;
using namespace atcoder;
#define PARSE_ARGS(types, ...) if(!PyArg_ParseTuple(args, types, __VA_ARGS__)) return NULL
struct AutoDecrefPtr{
PyObject* p;
AutoDecrefPtr(PyObject* _p) : p(_p) {};
#ifndef ALLOW_MEMORY_LEAK
AutoDecrefPtr(const AutoDecrefPtr& rhs) : p(rhs.p) { Py_INCREF(p); };
~AutoDecrefPtr(){ Py_DECREF(p); }
AutoDecrefPtr &operator=(const AutoDecrefPtr& rhs){
Py_DECREF(p);
p = rhs.p;
Py_INCREF(p);
return *this;
}
#endif
};
// >>> functions for laze_segtree constructor >>>
static PyObject* lazy_segtree_op_py;
static AutoDecrefPtr lazy_segtree_op(AutoDecrefPtr a, AutoDecrefPtr b){
PyObject* res(PyObject_CallFunctionObjArgs(lazy_segtree_op_py, a.p, b.p, NULL));
Py_INCREF(res); // ???????????????
return AutoDecrefPtr(res);
}
static PyObject* lazy_segtree_e_py;
static AutoDecrefPtr lazy_segtree_e(){
Py_INCREF(lazy_segtree_e_py);
return AutoDecrefPtr(lazy_segtree_e_py);
}
static PyObject* lazy_segtree_mapping_py;
static AutoDecrefPtr lazy_segtree_mapping(AutoDecrefPtr f, AutoDecrefPtr x){
return AutoDecrefPtr(PyObject_CallFunctionObjArgs(lazy_segtree_mapping_py, f.p, x.p, NULL));
}
static PyObject* lazy_segtree_composition_py;
static AutoDecrefPtr lazy_segtree_composition(AutoDecrefPtr f, AutoDecrefPtr g){
return AutoDecrefPtr(PyObject_CallFunctionObjArgs(lazy_segtree_composition_py, f.p, g.p, NULL));
}
static PyObject* lazy_segtree_id_py;
static AutoDecrefPtr lazy_segtree_id(){
Py_INCREF(lazy_segtree_id_py);
return AutoDecrefPtr(lazy_segtree_id_py);
}
using lazyseg = lazy_segtree<AutoDecrefPtr,
lazy_segtree_op,
lazy_segtree_e,
AutoDecrefPtr,
lazy_segtree_mapping,
lazy_segtree_composition,
lazy_segtree_id>;
// <<< functions for laze_segtree constructor <<<
static PyObject* lazy_segtree_f_py;
static bool lazy_segtree_f(AutoDecrefPtr x){
PyObject* pyfunc_res = PyObject_CallFunctionObjArgs(lazy_segtree_f_py, x.p, NULL);
int res = PyObject_IsTrue(pyfunc_res);
if(res == -1) PyErr_Format(PyExc_ValueError, "error in LazySegTree f");
return (bool)res;
}
struct LazySegTree{
PyObject_HEAD
lazyseg* seg;
PyObject* op;
PyObject* e;
PyObject* mapping;
PyObject* composition;
PyObject* id;
int n;
};
static inline void set_rules(LazySegTree* self){
lazy_segtree_op_py = self->op;
lazy_segtree_e_py = self->e;
lazy_segtree_mapping_py = self->mapping;
lazy_segtree_composition_py = self->composition;
lazy_segtree_id_py = self->id;
}
// >>> LazySegTree functions >>>
extern PyTypeObject LazySegTreeType;
static void LazySegTree_dealloc(LazySegTree* self){
delete self->seg;
Py_DECREF(self->op);
Py_DECREF(self->e);
Py_DECREF(self->mapping);
Py_DECREF(self->composition);
Py_DECREF(self->id);
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject* LazySegTree_new(PyTypeObject* type, PyObject* args, PyObject* kwds){
LazySegTree* self;
self = (LazySegTree*)type->tp_alloc(type, 0);
return (PyObject*)self;
}
static int LazySegTree_init(LazySegTree* self, PyObject* args){
if(Py_SIZE(args) != 6){
self->op = Py_None; // 何か入れておかないとヤバいことになる
Py_INCREF(Py_None);
self->e = Py_None;
Py_INCREF(Py_None);
self->mapping = Py_None;
Py_INCREF(Py_None);
self->composition = Py_None;
Py_INCREF(Py_None);
self->id = Py_None;
Py_INCREF(Py_None);
PyErr_Format(PyExc_TypeError,
"LazySegTree constructor expected 6 arguments (op, e, mapping, composition, identity, n), got %d", Py_SIZE(args));
return -1;
}
PyObject* arg;
if(!PyArg_ParseTuple(args, "OOOOOO",
&self->op, &self->e,
&self->mapping, &self->composition, &self->id, &arg)) return -1;
Py_INCREF(self->op);
Py_INCREF(self->e);
Py_INCREF(self->mapping);
Py_INCREF(self->composition);
Py_INCREF(self->id);
set_rules(self);
if(PyLong_Check(arg)){
int n = (int)PyLong_AsLong(arg);
if(PyErr_Occurred()) return -1;
if(n < 0 || n > (int)1e8) {
PyErr_Format(PyExc_ValueError, "constraint error in LazySegTree constructor (got %d)", n);
return -1;
}
self->seg = new lazyseg(n);
self->n = n;
}else{
PyObject *iterator = PyObject_GetIter(arg);
if(iterator==NULL) return -1;
PyObject *item;
vector<AutoDecrefPtr> vec;
if(Py_TYPE(arg)->tp_as_sequence != NULL) vec.reserve((int)Py_SIZE(arg));
while(item = PyIter_Next(iterator)) {
vec.emplace_back(item);
}
Py_DECREF(iterator);
if (PyErr_Occurred()) return -1;
self->seg = new lazyseg(vec);
self->n = (int)vec.size();
}
return 0;
}
static PyObject* LazySegTree_set(LazySegTree* self, PyObject* args){
long p;
PyObject* x;
PARSE_ARGS("lO", &p, &x);
if(p < 0 || p >= self->n){
PyErr_Format(PyExc_IndexError, "LazySegTree set index out of range (size=%d, index=%d)", self->n, p);
return (PyObject*)NULL;
}
Py_INCREF(x);
set_rules(self);
self->seg->set((int)p, AutoDecrefPtr(x));
Py_RETURN_NONE;
}
static PyObject* LazySegTree_get(LazySegTree* self, PyObject* args){
long p;
PARSE_ARGS("l", &p);
if(p < 0 || p >= self->n){
PyErr_Format(PyExc_IndexError, "LazySegTree get index out of range (size=%d, index=%d)", self->n, p);
return (PyObject*)NULL;
}
set_rules(self);
PyObject* res = self->seg->get((int)p).p;
return Py_BuildValue("O", res);
}
static PyObject* LazySegTree_prod(LazySegTree* self, PyObject* args){
long l, r;
PARSE_ARGS("ll", &l, &r);
set_rules(self);
PyObject* res = self->seg->prod((int)l, (int)r).p;
return Py_BuildValue("O", res);
}
static PyObject* LazySegTree_all_prod(LazySegTree* self, PyObject* args){
PyObject* res = self->seg->all_prod().p;
return Py_BuildValue("O", res);
}
static PyObject* LazySegTree_apply(LazySegTree* self, PyObject* args){
if(Py_SIZE(args) == 3){
long l, r;
PyObject* x;
PARSE_ARGS("llO", &l, &r, &x);
Py_INCREF(x);
set_rules(self);
self->seg->apply(l, r, AutoDecrefPtr(x));
Py_RETURN_NONE;
}else if(Py_SIZE(args) == 2){
long p;
PyObject* x;
PARSE_ARGS("lO", &p, &x);
if(p < 0 || p >= self->n){
PyErr_Format(PyExc_IndexError, "LazySegTree apply index out of range (size=%d, index=%d)", self->n, p);
return (PyObject*)NULL;
}
Py_INCREF(x);
set_rules(self);
self->seg->apply(p, AutoDecrefPtr(x));
Py_RETURN_NONE;
}else{
PyErr_Format(PyExc_TypeError,
"LazySegTree apply expected 2 (p, x) or 3 (l, r, x) arguments, got %d", Py_SIZE(args));
return (PyObject*)NULL;
}
}
static PyObject* LazySegTree_max_right(LazySegTree* self, PyObject* args){
long l;
PARSE_ARGS("lO", &l, &lazy_segtree_f_py);
if(l < 0 || l > self->n){
PyErr_Format(PyExc_IndexError, "LazySegTree max_right index out of range (size=%d, l=%d)", self->n, l);
return (PyObject*)NULL;
}
set_rules(self);
int res = self->seg->max_right<lazy_segtree_f>((int)l);
return Py_BuildValue("l", res);
}
static PyObject* LazySegTree_min_left(LazySegTree* self, PyObject* args){
long r;
PARSE_ARGS("lO", &r, &lazy_segtree_f_py);
if(r < 0 || r > self->n){
PyErr_Format(PyExc_IndexError, "LazySegTree max_right index out of range (size=%d, r=%d)", self->n, r);
return (PyObject*)NULL;
}
set_rules(self);
int res = self->seg->min_left<lazy_segtree_f>((int)r);
return Py_BuildValue("l", res);
}
static PyObject* LazySegTree_to_list(LazySegTree* self){
PyObject* list = PyList_New(self->n);
for(int i=0; i<self->n; i++){
PyObject* val = self->seg->get(i).p;
Py_INCREF(val);
PyList_SET_ITEM(list, i, val);
}
return list;
}
static PyObject* LazySegTree_repr(PyObject* self){
PyObject* list = LazySegTree_to_list((LazySegTree*)self);
PyObject* res = PyUnicode_FromFormat("LazySegTree(%R)", list);
Py_ReprLeave(self);
Py_DECREF(list);
return res;
}
// <<< LazySegTree functions <<<
static PyMethodDef LazySegTree_methods[] = {
{"set", (PyCFunction)LazySegTree_set, METH_VARARGS, "Set item"},
{"get", (PyCFunction)LazySegTree_get, METH_VARARGS, "Get item"},
{"prod", (PyCFunction)LazySegTree_prod, METH_VARARGS, "Get item"},
{"all_prod", (PyCFunction)LazySegTree_all_prod, METH_VARARGS, "Get item"},
{"apply", (PyCFunction)LazySegTree_apply, METH_VARARGS, "Apply function"},
{"max_right", (PyCFunction)LazySegTree_max_right, METH_VARARGS, "Binary search on lazy segtree"},
{"min_left", (PyCFunction)LazySegTree_min_left, METH_VARARGS, "Binary search on lazy segtree"},
{"to_list", (PyCFunction)LazySegTree_to_list, METH_VARARGS, "Convert to list"},
{NULL} /* Sentinel */
};
PyTypeObject LazySegTreeType = {
PyObject_HEAD_INIT(NULL)
"atcoder.LazySegTree", /*tp_name*/
sizeof(LazySegTree), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)LazySegTree_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*reserved*/
LazySegTree_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
LazySegTree_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
(initproc)LazySegTree_init, /*tp_init*/
0, /*tp_alloc*/
LazySegTree_new, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
0, /*tp_bases*/
0, /*tp_mro*/
0, /*tp_cache*/
0, /*tp_subclasses*/
0, /*tp_weaklist*/
0, /*tp_del*/
0, /*tp_version_tag*/
0, /*tp_finalize*/
};
static PyModuleDef atcodermodule = {
PyModuleDef_HEAD_INIT,
"atcoder",
NULL,
-1,
};
PyMODINIT_FUNC PyInit_atcoder(void)
{
PyObject* m;
if(PyType_Ready(&LazySegTreeType) < 0) return NULL;
m = PyModule_Create(&atcodermodule);
if(m == NULL) return NULL;
Py_INCREF(&LazySegTreeType);
if (PyModule_AddObject(m, "LazySegTree", (PyObject*)&LazySegTreeType) < 0) {
Py_DECREF(&LazySegTreeType);
Py_DECREF(m);
return NULL;
}
return m;
}
"""
code_setup = r"""
from distutils.core import setup, Extension
module = Extension(
"atcoder",
sources=["atcoder_library_wrapper.cpp"],
extra_compile_args=["-O3", "-march=native", "-std=c++14"]
)
setup(
name="atcoder-library",
version="0.0.1",
description="wrapper for atcoder library",
ext_modules=[module]
)
"""
import os
import sys
if sys.argv[-1] == "ONLINE_JUDGE" or os.getcwd() != "/imojudge/sandbox":
with open("atcoder_library_wrapper.cpp", "w") as f:
f.write(code_lazy_segtree)
with open("setup.py", "w") as f:
f.write(code_setup)
os.system(f"{sys.executable} setup.py build_ext --inplace")
from atcoder import LazySegTree
code_fastio = r"""
#define PY_SSIZE_T_CLEAN
#include <Python.h>
PyObject* fastio_readint(PyObject* self, PyObject* args){
long long in;
scanf("%lld", &in);
return PyLong_FromLongLong(in);
}
PyObject* fastio_readints(PyObject* self, PyObject* args){
long n;
if(!PyArg_ParseTuple(args, "l", &n)) return NULL;
PyObject* list = PyList_New(n);
long long in;
for(int i = 0; i < n; i++){
scanf("%lld", &in);
PyList_SET_ITEM(list, i, PyLong_FromLongLong(in));
}
return list;
}
PyObject* fastio_printintline(PyObject* self, PyObject* args){
long long val;
if(!PyArg_ParseTuple(args, "L", &val)) return NULL;
printf("%lld\n", val);
Py_RETURN_NONE;
}
PyObject* fastio_printintlines(PyObject* self, PyObject* args){
PyObject* list;
if(!PyArg_ParseTuple(args, "O", &list)) return NULL;
PyObject *iterator = PyObject_GetIter(list);
if(iterator==NULL) return NULL;
PyObject *item;
while(item = PyIter_Next(iterator)) {
printf("%lld\n", PyLong_AsLongLong(item));
Py_DECREF(item);
}
Py_DECREF(iterator);
if(PyErr_Occurred()) return NULL;
Py_RETURN_NONE;
}
static PyMethodDef fastiomethods[] = {
{"readint", fastio_readint, METH_VARARGS, "call scanf"},
{"readints", fastio_readints, METH_VARARGS, "call scanf n times"},
{"printintline", fastio_printintline, METH_VARARGS, "call printf"},
{"printintlines", fastio_printintlines, METH_VARARGS, "call printf"},
{NULL}
};
static struct PyModuleDef fastiomodule = {
PyModuleDef_HEAD_INIT,
"fastio",
NULL,
-1,
fastiomethods,
};
PyMODINIT_FUNC PyInit_fastio(void){
return PyModule_Create(&fastiomodule);
}
"""
code_fastio_setup = r"""
from distutils.core import setup, Extension
module = Extension(
"fastio",
sources=["fastio.cpp"],
extra_compile_args=["-O3", "-march=native", "-std=c++14"]
)
setup(
name="FastIOMethod",
version="0.0.1",
description="fastio",
ext_modules=[module]
)
"""
import os
import sys
if sys.argv[-1] == "ONLINE_JUDGE" or os.getcwd() != "/imojudge/sandbox":
with open("fastio.cpp", "w") as f:
f.write(code_fastio)
with open("fastio_setup.py", "w") as f:
f.write(code_fastio_setup)
os.system(f"{sys.executable} fastio_setup.py build_ext --inplace")
if sys.argv[-1] == "ONLINE_JUDGE":
exit()
from fastio import readint, readints, printintline, printintlines
from operator import xor
def main():
N, Q = readints(2)
A = readints(N)
TLR = readints(3*Q)
def op(l, r):
l_zero, l_one, l_inversion = l
r_zero, r_one, r_inversion = r
return l_zero+r_zero, l_one+r_one, l_inversion+r_inversion+l_one*r_zero
e = 0, 0, 0
def mapping(l, r):
if l:
r_zero, r_one, r_inversion = r
return r_one, r_zero, r_one*r_zero-r_inversion
return r
composition = xor
identity = False
A = [(1, 0, 0) if a == 0 else (0, 1, 0) for a in A]
seg = LazySegTree(op, e, mapping, composition, identity, A)
Ans = []
for t, l, r in zip(TLR[::3], TLR[1::3], TLR[2::3]):
l -= 1
if t == 1:
seg.apply(l, r, True)
else:
Ans.append(seg.prod(l, r)[2])
printintlines(Ans)
main()
``` | instruction | 0 | 34,912 | 5 | 69,824 |
No | output | 1 | 34,912 | 5 | 69,825 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,929 | 5 | 69,858 |
"Correct Solution:
```
ans = 6
for _ in range(2):
ans -= int(input())
print(ans)
``` | output | 1 | 34,929 | 5 | 69,859 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,930 | 5 | 69,860 |
"Correct Solution:
```
A = {int(input()) for _ in range(2)}
print(*{1,2,3}-A)
``` | output | 1 | 34,930 | 5 | 69,861 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,931 | 5 | 69,862 |
"Correct Solution:
```
n=int(input())
t=int(input())
print(6-n-t)
``` | output | 1 | 34,931 | 5 | 69,863 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,932 | 5 | 69,864 |
"Correct Solution:
```
A, B = [int(_) for _ in open(0).read().split()]
print(6 - A - B)
``` | output | 1 | 34,932 | 5 | 69,865 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,933 | 5 | 69,866 |
"Correct Solution:
```
# ABC148A
a = int(input())
b = int(input())
print(6 - a - b)
``` | output | 1 | 34,933 | 5 | 69,867 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,934 | 5 | 69,868 |
"Correct Solution:
```
A = int(input())
B = int(input())
print(int(6/A/B))
``` | output | 1 | 34,934 | 5 | 69,869 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,935 | 5 | 69,870 |
"Correct Solution:
```
ans=6
for i in [0]*2:
ans-=int(input())
print(ans)
``` | output | 1 | 34,935 | 5 | 69,871 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi is solving quizzes. He has easily solved all but the last one.
The last quiz has three choices: 1, 2, and 3.
With his supernatural power, Takahashi has found out that the choices A and B are both wrong.
Print the correct choice for this problem.
Constraints
* Each of the numbers A and B is 1, 2, or 3.
* A and B are different.
Input
Input is given from Standard Input in the following format:
A
B
Output
Print the correct choice.
Examples
Input
3
1
Output
2
Input
1
2
Output
3 | instruction | 0 | 34,936 | 5 | 69,872 |
"Correct Solution:
```
a = int(input())
b = int(input())
goukei = a + b
print(6-a-b)
``` | output | 1 | 34,936 | 5 | 69,873 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,043 | 5 | 70,086 |
"Correct Solution:
```
mod = 10**9+7
N,X,Y,Z = map(int, input().split())
num = 1<<(X+Y+Z-1)
ng = ((((1<<X)+1)<<Y)+1)<<(Z-1)
dp = [[0]*num for i in range(N+1)]
dp[0][0] = 1
for i in range(N):
for j in range(1,11):
for k in range(num):
K = (k*2+1)<<(j-1)
if K&ng != ng:
K &= num-1
dp[i+1][K] += dp[i][k]
dp[i+1][K] %= mod
print((10**N%mod-sum(dp[N])%mod)%mod)
``` | output | 1 | 35,043 | 5 | 70,087 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,044 | 5 | 70,088 |
"Correct Solution:
```
N,x,y,z=map(int,input().split())
mask=2**(x+y+z)-1
mod=10**9+7
ng=2**(x+y+z-1)+2**(y+z-1)+2**(z-1)
dp=[[0 for i in range(mask+1)] for i in range(N+1)]
dp[0][0]=1
for i in range(N):
for j in range(mask+1):
for k in range(1,11):
if (mask&((j<<k)+(1<<(k-1))))&ng!=ng:
dp[i+1][mask&((j<<k)+(1<<(k-1)))]+=dp[i][j]
dp[i+1][j]%=mod
ans=(10**N)%mod
for j in range(mask+1):
ans-=dp[N][j]
ans%=mod
print(ans)
``` | output | 1 | 35,044 | 5 | 70,089 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,045 | 5 | 70,090 |
"Correct Solution:
```
N,X,Y,Z=map(int,input().split())
mod=10**9+7
forbit=(1<<(X+Y+Z -1 )) +(1<<(Y+Z -1 ))+(1<<(Z -1 ))
mask = ((1<<(X+Y+Z)) -1)
s=set()
for i in range(mask+1):
if i & forbit:
s.add(i)
dp=[[0]*(mask+1) for i in range(N+1)]
dp[0][0]=1
for n in range(1,N+1):
for i in range(mask+1):
#if i in s:
#continue
for k in range(10):
t=((i <<(k+1)) | 1<<k) & mask
#if t in s:
#continue
if (t & forbit) == forbit:
continue
dp[n][t]+=dp[n-1][i]
dp[n][t]%=mod
ans=pow(10,N,mod)
for i in range(mask+1):
#if i in s:
#continue
ans-=dp[N][i]
ans%=mod
print(ans)
``` | output | 1 | 35,045 | 5 | 70,091 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,046 | 5 | 70,092 |
"Correct Solution:
```
mod = 10**9+7
N,X,Y,Z = map(int, input().split())
Num = 1<<(X+Y+Z-1)
ng = ((((1<<X)+1)<<Y)+1)<<(Z-1)
dp = [[0]*Num for i in range(N+1)]
dp[0][0] = 1
for i in range(N):
for k in range(Num):
for j in range(1,11):
a = (k<<j)|(1<<(j-1))
if a&ng != ng:
a &= Num-1
dp[i+1][a] += dp[i][k]
dp[i+1][a] %= mod
print((10**N%mod-sum(dp[N])%mod)%mod)
``` | output | 1 | 35,046 | 5 | 70,093 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,047 | 5 | 70,094 |
"Correct Solution:
```
mod = 10**9+7
n, x, y, z = map(int,input().split())
xyz = 2**(x+y+z-1) + 2**(y+z-1) + 2**(z-1)
w = 2**(x+y+z-1)
dp = [[0]*w for _ in range(n+1)]
dp[0][0] = 1
for i in range(n):
for j in range(w):
for k in range(1,11):
nj = ((j+j+1)<<(k-1))
if nj&xyz != xyz:
dp[i+1][nj%w] = (dp[i+1][nj%w]+dp[i][j])%mod
print((pow(10,n,mod)-sum(dp[-1])%mod)%mod)
``` | output | 1 | 35,047 | 5 | 70,095 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,048 | 5 | 70,096 |
"Correct Solution:
```
n,x,y,z=map(int,input().split())
mod=10**9+7
mask=1<<(x+y+z-1)
dp=[[0 for i in range(mask+1)] for j in range(n+1)]
dp[0][0]=1
ng=(1<<(x+y+z-1))|(1<<(y+z-1))|(1<<(z-1))
for i in range(n):
dp[i+1][mask]=(dp[i][mask]*10)%mod
for j in range(mask):
for k in range(1,11):
nmask=(j<<k)|(1<<(k-1))
if nmask&ng==ng:
nmask=mask
else:
nmask&=mask-1
dp[i+1][nmask]=(dp[i+1][nmask]+dp[i][j])%mod
print(dp[n][mask])
``` | output | 1 | 35,048 | 5 | 70,097 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,049 | 5 | 70,098 |
"Correct Solution:
```
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random
def I():
return int(input())
def MI():
return map(int,input().split())
def LI():
return [int(i) for i in input().split()]
def LI_():
return [int(i)-1 for i in input().split()]
def StoI():
return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
if show_flg:
print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=sys.stdin.readline
show_flg=False
#show_flg=True
n,X,Y,Z=MI()
t=1<<(X+Y+Z)
haiku=( (1<<(X+Y+Z)) + (1<<(Y+Z)) + (1<<Z) )>>1
dp=[0]*t
dp[0]=1
for i in range(n):
tm=[i for i in dp]
dp=[0]*t
for k in range(t):
for d in range(10):
m=((k<<(d+1))+(1<<d))
if m&haiku == haiku:
continue
else:
dp[int(m)%t]+=tm[k]
dp[int(m)%t]%=mo
sub=0
for i in range(t):
sub+=dp[i]
ans=pow(10,n,mo)-sub
print(ans%mo)
show(ans,sub)
show(dp)
show(n,t,n*t*10)
``` | output | 1 | 35,049 | 5 | 70,099 |
Provide a correct Python 3 solution for this coding contest problem.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100 | instruction | 0 | 35,050 | 5 | 70,100 |
"Correct Solution:
```
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random
def I():
return int(input())
def MI():
return map(int,input().split())
def LI():
return [int(i) for i in input().split()]
def LI_():
return [int(i)-1 for i in input().split()]
def StoI():
return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
if show_flg:
print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=sys.stdin.readline
show_flg=False
#show_flg=True
n,X,Y,Z=MI()
t=1<<(X+Y+Z)
haiku=( (1<<(X+Y+Z)) + (1<<(Y+Z)) + (1<<Z) )>>1
dp=[[0]*t for _ in range(n+1)]
dp[0][0]=1
for i in range(n):
for k in range(t):
for d in range(10):
m=((k<<(d+1))+(1<<d))&(t-1)
if m&haiku == haiku:
continue
else:
dp[i+1][m]+=dp[i][k]
dp[i+1][m]%=mo
sub=0
for i in range(t):
sub+=dp[n][i]
ans=pow(10,n,mo)-sub
print(ans%mo)
``` | output | 1 | 35,050 | 5 | 70,101 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100
Submitted Solution:
```
mod = 10**9+7
N,X,Y,Z = map(int, input().split())
Num = 1<<(X+Y+Z-1)
ng = ((((1<<X)+1)<<Y)+1)<<(Z-1)
dp = [[0]*Num for i in range(N+1)]
dp[0][0] = 1
for i in range(N):
for k in range(Num):
for j in range(1,11):
a = (k<<j)|(1<<(j-1))
if a&ng != ng:
a &= Num-1
dp[i+1][a] += dp[i][k]
dp[i+1][a] %= mod
cnt = 1
for i in range(N):
cnt *= 10
cnt %= mod
for x in dp[N]:
cnt -= x
cnt %= mod
print(cnt)
``` | instruction | 0 | 35,051 | 5 | 70,102 |
Yes | output | 1 | 35,051 | 5 | 70,103 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.
Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.
Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?
Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:
* a_x + a_{x+1} + ... + a_{y-1} = X
* a_y + a_{y+1} + ... + a_{z-1} = Y
* a_z + a_{z+1} + ... + a_{w-1} = Z
Since the answer can be extremely large, print the number modulo 10^9+7.
Constraints
* 3 ≦ N ≦ 40
* 1 ≦ X ≦ 5
* 1 ≦ Y ≦ 7
* 1 ≦ Z ≦ 5
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.
Examples
Input
3 5 7 5
Output
1
Input
4 5 7 5
Output
34
Input
37 4 2 3
Output
863912418
Input
40 5 7 5
Output
562805100
Submitted Solution:
```
MOD = pow(10, 9) + 7
def main():
n, x, y, z = map(int, input().split())
ngnum = pow(2, z-1) + pow(2, y+z-1) + pow(2, x+y+z-1)
mask = pow(2, x+y+z) - 1
dp = [[0 for _ in range(mask + 1)] for _ in range(n + 1)]
ans = 1
for _ in range(n):
ans = ans * 10 % MOD
dp[0][0] = 1
for i in range(n):
for j in range(mask + 1):
if dp[i][j] == 0:
continue
for d in range(1, 11):
t = j*pow(2, d) | pow(2, d-1)
if (t & ngnum) == ngnum:
continue
t &= mask
dp[i+1][t] = (dp[i+1][t] + dp[i][j]) % MOD
for i in range(mask + 1):
ans = (ans + MOD - dp[n][i]) % MOD
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 35,052 | 5 | 70,104 |
Yes | output | 1 | 35,052 | 5 | 70,105 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.