text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Tags: brute force, math, number theory
Correct Solution:
```
a,b = map(int,input().split())
lst = list(map(int,input().split()))
n=0
dct = {}
for x in lst:
n += dct.get(x^b,0)
dct[x] = dct.get(x,0)+1
print(n)
```
| 98,300 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Tags: brute force, math, number theory
Correct Solution:
```
n, x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
ax=[i^x for i in a]
a.sort()
ax.sort()
pos=0
posx=0
c=1
cx=1
total=0
while pos<n and posx<n:
if a[pos]==ax[posx]:
if pos<n-1 and a[pos+1]==a[pos]:
pos+=1
c+=1
elif posx<n-1 and ax[posx+1]==ax[posx]:
posx+=1
cx+=1
else:
#print(c, cx, pos, posx)
total+=c*cx
c=1
cx=1
pos+=1
posx+=1
elif a[pos]>ax[posx]:
posx+=1
else:
pos+=1
#print(total, a, ax)
if x>0:
print(total//2)
else:
print((total-n)//2)
```
| 98,301 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Tags: brute force, math, number theory
Correct Solution:
```
#http://codeforces.com/problemset/problem/742/B
#works but is too slow
def sum(x):
return int(x*(x+1)/2)
from collections import Counter
from math import factorial
answer = 0
seen = {}
ri = input()
ri = ri.split()
n = int(ri[0])
x = int(ri[1])
ri = input()
ri = ri.split()
d = Counter(ri)
for v1 in list(d.keys()):
v2 = str(int(v1) ^ x)
if v2 in d:
if v1 == v2:
answer += sum(d[v1] - 1)
del d[v1]
else:
answer += d[v1] * d[v2]
del d[v1]
del d[v2]
print(answer)
```
| 98,302 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
n, x = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
la = [0]*int(10e5+5)
ans = 0
for i in l:
ans += la[i ^ x]
la[i] += 1
print(ans)
```
Yes
| 98,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
n, x = map(int, input().split())
a, s = [0] * 100001, 0
for i in map(int, input().split()):
a[i] += 1
for i in range(100001):
if not x:
s += a[i] * (a[i] - 1) // 2
a[i] = 0
elif x ^ i < 100001:
s += a[i] * a[x ^ i]
a[i] = a[x ^ i] = 0
print(s)
```
Yes
| 98,304 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
def solve():
n,x=map(int,input().split())
a=list(map(int,input().split()))
d={}
g=[]
cnt=0
for i in a:
if(d.get(i)==None):
d[i]=1
else:
d[i]+=1
for i in a:
r=i^x
if(d.get(r)!=None and i!=r):
cnt+=d[r]
elif(d.get(r)!=None and i==r):
cnt+=d[r]-1
print(cnt//2)
solve()
```
Yes
| 98,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
from sys import stdin, stdout
import math
n,x=map(int,stdin.readline().split())
arr=list(map(int,stdin.readline().split()))
d=[0 for i in range(100005)]
count=0
for i in range(len(arr)):
if(x^arr[i]<100005):
count+=d[x^arr[i]]
d[arr[i]]+=1
stdout.write(str(count)+"\n")
```
Yes
| 98,306 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
read = lambda: map(int, input().split())
from collections import Counter as C
n, x = read()
a = list(read())
c = C()
for i in a: c[i ^ x] += 1
cnt = 0
for i in a: cnt += c[i ^ x]
print(cnt // 2)
```
No
| 98,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
a, b = map(int, input().split())
s = list(map(int, input().split()))
from collections import Counter
s = Counter(s)
res = 0
for i in s:
x = b^i
if x in s: res += s[x]*s[i]
print(res//2)
```
No
| 98,308 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
n,x=map(int,input().split())
k=0
l=list(map(int,input().split()))
for i in set(l):
k+=l.count(x^i)
print(k//2)
```
No
| 98,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are some beautiful girls in Arpaβs land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 β€ i < j β€ n) such that <image>, where <image> is bitwise xor operation (see notes for explanation).
<image>
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 β€ n β€ 105, 0 β€ x β€ 105) β the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105) β the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. <image> so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since <image>) and i = 1, j = 5 (since <image>).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: <https://en.wikipedia.org/wiki/Bitwise_operation#XOR>.
Submitted Solution:
```
i = input().split(' ')
numbers = input().split(' ')
def result(numbers,x):
result = 0
i = 0
numberset = list(set(map(int,numbers)))
while i < len(numberset):
if int(numberset[i]) ^ int(x) in numberset:
print(numbers.count(str(int(numberset[i]) ^ int(x))),numbers.count(str(numberset[i])))
result += min(numbers.count(str(int(numberset[i]) ^ int(x))),numbers.count(numberset[i]))
i += 1
return result//2
print(result(numbers,i[1]))
```
No
| 98,310 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souvenir shops along the street. In i-th shop Artsem can buy one souvenir for ai dollars, and he cannot buy more than one souvenir in one shop. He doesn't want to introduce envy in his team, so he wants to buy two souvenirs with least possible difference in price.
Artsem has visited the shopping street m times. For some strange reason on the i-th day only shops with numbers from li to ri were operating (weird? yes it is, but have you ever tried to come up with a reasonable legend for a range query problem?). For each visit, Artsem wants to know the minimum possible difference in prices of two different souvenirs he can buy in the opened shops.
In other words, for each Artsem's visit you should find the minimum possible value of |as - at| where li β€ s, t β€ ri, s β t.
Input
The first line contains an integer n (2 β€ n β€ 105).
The second line contains n space-separated integers a1, ..., an (0 β€ ai β€ 109).
The third line contains the number of queries m (1 β€ m β€ 3Β·105).
Next m lines describe the queries. i-th of these lines contains two space-separated integers li and ri denoting the range of shops working on i-th day (1 β€ li < ri β€ n).
Output
Print the answer to each query in a separate line.
Example
Input
8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7
Output
0
1
1
3
Submitted Solution:
```
a=int(input())
b=input().split()
for i in range (len(b)):
b[i]=int(b[i])
c=int(input())
#b.sort()
for i in range(c):
a=input().split()
x=int(a[0])-1
y=int(a[1])
d=b[x:y]
d.sort()
m=[]
for i in range(len(d)-1):
m.append(abs(int(d[i+1])-int(d[i])))
m.sort()
print(m[0]," lol ",d)
```
No
| 98,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n - 1):
b.append(abs(a[i] - a[i + 1]))
c = []
s = 1
summ = 0
for i in range(n - 1):
summ += s * b[i]
s = -s
c.append(summ)
c.sort()
if c[0] < 0:
print(c[n - 2] - c[0])
else:
print(c[n - 2])
```
| 98,312 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
da,p=[],1
for i in range(n-1):
da.append(p*abs(a[i]-a[i+1]));p*=-1
m1,m2,s1,s2=0,0,0,0
for x in da:
s1+=x
if s1<0: s1=0
s2-=x
if s2<0: s2=0
m1=max(m1,s1);m2=max(m2,s2)
print(max(m1,m2))
```
| 98,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
def main():
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(len(a) - 1):
b.append(abs(a[i] - a[i + 1]))
max_odd = b[0]
max_even = 0
all_values = [max_odd, max_even]
for bi in b[1:]:
new_odd = max(max_even + bi, bi)
new_even = max(max_odd - bi, 0)
max_odd = new_odd
max_even = new_even
all_values += [max_odd, max_even]
print(max(all_values))
if __name__ == '__main__':
# import sys
# sys.stdin = open("C.txt")
main()
```
| 98,314 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
import math
from collections import defaultdict as dd
def main():
n = int(input())
A = list(map(int, input().split()))
# print(A)
B = []
for i in range(1, len(A)):
B.append(abs(A[i]-A[i-1]))
# print(B)
Dp = dd(int)
Dm = dd(int)
Dp[0]=0
MAX = 0
for i in range(n-1):
Dm[i] = Dp[i-1] + B[i]
Dp[i] = max(Dm[i-1] - B[i], 0)
MAX = max(Dm[i], Dp[i], MAX)
print(MAX)
if __name__ == "__main__":
main()
```
| 98,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = []
sign = 1
for i in range(len(a)-1):
b.append( abs(a[i]-a[i+1]) * sign )
sign *= -1
max_ = 0
max1, max2 = 0, 0
a1, a2 = 0, 0
for i in range(n-1):
if a1+b[i]>0:
a1 += b[i]
else:
a1 = 0
max1 = max(a1, max1)
if a2-b[i]>0:
a2 -= b[i]
else:
a2 = 0
max2 = max(a2, max2)
print(max(max1,max2))
```
| 98,316 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
def maxSubArray( A):
if not A:
return 0
curSum = maxSum = A[0]
for num in A[1:]:
curSum = max(num, curSum + num)
maxSum = max(maxSum, curSum)
return maxSum
n = int(input())
L = list(map(int, input().split()))
D1 = []
D2 = []
for i in range(1, len(L)):
D1.append(abs(L[i] - L[i-1]) * (-1)**i)
D2.append(abs(L[i] - L[i-1]) * (-1)**(i+1))
print(max(maxSubArray(D1), maxSubArray(D2)))
```
| 98,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
n = int(input())
num = list(map(int, input().split()))
diff = [abs(j-i) for i, j in zip(num, num[1:])]
s1 = 0
s2 = 0
m1 = 0
m2 = 0
for i, x in enumerate(diff):
if s1 < 0:
s1 = 0
if s2 < 0:
s2 = 0
if i % 2 == 0:
s1 += x
s2 -= x
else:
s1 -= x
s2 += x
if s1 > m1:
m1 = s1
if s2 > m2:
m2 = s2
print(max(m1, m2))
```
| 98,318 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Tags: dp, two pointers
Correct Solution:
```
def max_subarray(A):
max_ending_here = max_so_far = A[0]
for x in A[1:]:
max_ending_here = max(x, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
n = int(input())
a = [int(v) for v in input().split()]
b = [abs(a[i]-a[i+1])*(-1)**i for i in range(n-1)]
print(max(max_subarray(b), max_subarray([-v for v in b])))
```
| 98,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
n = int(input())
R = [int(i) for i in input().split()]
L = [abs(R[i]-R[i+1]) for i in range(n-1)]
ans = [0 for _ in range(n)]
ans[0] = L[0]
for i in range(1, n-1):
ans[i] = max(L[i], L[i]-ans[i-1])
if i - 2 >= 0:
ans[i] = max(ans[i], L[i]-L[i-1]+ans[i-2])
print(max(ans))
```
Yes
| 98,320 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=998244353
EPS=1e-6
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
def Answer(a):
till=0
ans=-inf
for i in a:
till+=i
ans=max(till,ans)
till=max(0,till)
return ans
n=Int()
a=array()
a=[abs(a[i]-a[i+1]) for i in range(n-1)]
# print(*a)
n-=1
a=[a[i]*(-1)**i for i in range(n)]
b=[-i for i in a]
print(max(Answer(a),Answer(b)))
```
Yes
| 98,321 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
def f(l, r, p):
if l > r: return 0
return p[r] - p[l - 1] if l % 2 == 1 else -f(l - 1, r, p) + p[l - 1]
def main():
read = lambda: tuple(map(int, input().split()))
n = read()[0]
v = read()
p = [0]
pv = 0
for i in range(n - 1):
cp = abs(v[i] - v[i + 1]) * (-1) ** i
pv += cp
p += [pv]
mxc, mxn = 0, 0
mnc, mnn = 0, 0
for i in range(n):
cc, cn = f(1, i, p), f(2, i, p)
mxc, mxn = max(mxc, cc - mnc), max(mxn, cn - mnn)
mnc, mnn = min(mnc, cc), min(mnn, cn)
return max(mxc, mxn)
print(main())
```
Yes
| 98,322 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
from sys import stdin, stdout
n = int(stdin.readline())
values = list(map(int, stdin.readline().split()))
first = []
second = []
ans = float('-inf')
for i in range(n - 1):
first.append(abs(values[i] - values[i + 1]) * (-1) ** i)
second.append(abs(values[i] - values[i + 1]) * (-1) ** (i + 1))
cnt = 0
for i in range(n - 1):
cnt += first[i]
ans = max(ans, cnt)
if cnt < 0:
cnt = 0
cnt = 0
for i in range(n - 1):
cnt += second[i]
ans = max(ans, cnt)
if cnt < 0:
cnt = 0
stdout.write(str(ans))
```
Yes
| 98,323 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
n = int(input())
a = list(map(int,input().strip().split()))
b = [0]*(n-1)
c = [0]*(n-1)
for i in range(n-1):
if (i+1)%2 == 0:
b[i] = (abs(a[i] - a[i+1]))
else:
b[i] = (abs(a[i] - a[i+1]))*(-1)
for i in range(1,n-1):
if i%2 == 0:
c[i] = (abs(a[i] - a[i+1]))
else:
c[i] = (abs(a[i] - a[i+1]))*(-1)
d1 = [-1000000001]*(n-1)
d2 = [-1000000001]*(n-1)
for i in range(n-1):
if i > 0:
d1[i] = max(b[i]+d1[i-1],max(0,b[i]))
else:
d1[i] = max(b[i],0)
for i in range(n-1):
if i > 0:
d2[i] = max(c[i]+d2[i-1],max(0,c[i]))
else:
d2[i] = max(c[i],0)
print(max(max(d1),max(d2)))
## print(b)
## print(d1)
## print(c)
## print(d2)
```
No
| 98,324 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
from collections import defaultdict as dd
def main():
n = int(input())
A = list(map(int, input().split()))
# print(A)
B = []
for i in range(1, len(A)):
B.append(abs(A[i]-A[i-1]))
# print(B)
Dp = dd(int)
Dm = dd(int)
Dp[0]=0
for i in range(n-1):
Dm[i] = Dp[i-1] + B[i]
Dp[i] = max(Dm[i-1] - B[i], 0)
print(max(Dm[n-2], Dp[n-2]))
if __name__ == "__main__":
main()
```
No
| 98,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
n = I()
a = LI()
r = t = abs(a[0]-a[1])
for i in range(3,n,2):
t += abs(a[i]-a[i-1]) - abs(a[i-1]-a[i-2])
if r < t:
r = t
elif t < 0:
t = 0
if n < 3:
return r
t = abs(a[1]-a[2])
for i in range(4,n,2):
t += abs(a[i]-a[i-1]) - abs(a[i-1]-a[i-2])
if r < t:
r = t
elif t < 0:
t = 0
return r
print(main())
```
No
| 98,326 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array.
Submitted Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
n = I()
a = LI()
r = t = abs(a[0]-a[1])
for i in range(3,n,2):
t += abs(a[i]-a[i-1]) - abs(a[i-1]-a[i-2])
if r < t:
r = t
elif t < 0:
t = 0
t = 0
for i in range(2,n,2):
t += abs(a[i]-a[i-1]) - abs(a[i-1]-a[i-2])
if r < t:
r = t
elif t < 0:
t = 0
return r
print(main())
```
No
| 98,327 |
Provide tags and a correct Python 3 solution for this coding contest problem.
"Eat a beaver, save a tree!" β That will be the motto of ecologists' urgent meeting in Beaverley Hills.
And the whole point is that the population of beavers on the Earth has reached incredible sizes! Each day their number increases in several times and they don't even realize how much their unhealthy obsession with trees harms the nature and the humankind. The amount of oxygen in the atmosphere has dropped to 17 per cent and, as the best minds of the world think, that is not the end.
In the middle of the 50-s of the previous century a group of soviet scientists succeed in foreseeing the situation with beavers and worked out a secret technology to clean territory. The technology bears a mysterious title "Beavermuncher-0xFF". Now the fate of the planet lies on the fragile shoulders of a small group of people who has dedicated their lives to science.
The prototype is ready, you now need to urgently carry out its experiments in practice.
You are given a tree, completely occupied by beavers. A tree is a connected undirected graph without cycles. The tree consists of n vertices, the i-th vertex contains ki beavers.
"Beavermuncher-0xFF" works by the following principle: being at some vertex u, it can go to the vertex v, if they are connected by an edge, and eat exactly one beaver located at the vertex v. It is impossible to move to the vertex v if there are no beavers left in v. "Beavermuncher-0xFF" cannot just stand at some vertex and eat beavers in it. "Beavermuncher-0xFF" must move without stops.
Why does the "Beavermuncher-0xFF" works like this? Because the developers have not provided place for the battery in it and eating beavers is necessary for converting their mass into pure energy.
It is guaranteed that the beavers will be shocked by what is happening, which is why they will not be able to move from a vertex of the tree to another one. As for the "Beavermuncher-0xFF", it can move along each edge in both directions while conditions described above are fulfilled.
The root of the tree is located at the vertex s. This means that the "Beavermuncher-0xFF" begins its mission at the vertex s and it must return there at the end of experiment, because no one is going to take it down from a high place.
Determine the maximum number of beavers "Beavermuncher-0xFF" can eat and return to the starting vertex.
Input
The first line contains integer n β the number of vertices in the tree (1 β€ n β€ 105). The second line contains n integers ki (1 β€ ki β€ 105) β amounts of beavers on corresponding vertices. Following n - 1 lines describe the tree. Each line contains two integers separated by space. These integers represent two vertices connected by an edge. Vertices are numbered from 1 to n. The last line contains integer s β the number of the starting vertex (1 β€ s β€ n).
Output
Print the maximum number of beavers munched by the "Beavermuncher-0xFF".
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).
Examples
Input
5
1 3 1 3 2
2 5
3 4
4 5
1 5
4
Output
6
Input
3
2 1 1
3 2
1 2
3
Output
2
Tags: dfs and similar, dp, dsu, greedy, trees
Correct Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
beaver = list(map(int, input().split()))
adj = [[] for _ in range(n)]
deg = [0] * n
for u, v in (map(int, input().split()) for _ in range(n - 1)):
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
deg[u - 1] += 1
deg[v - 1] += 1
start = int(input()) - 1
deg[start] += 1000000
if n == 1:
print(0)
exit()
dp = [0] * n
stack = [i for i in range(n) if i != start and deg[i] == 1]
while stack:
v = stack.pop()
deg[v] = 0
child = []
child_dp = []
for dest in adj[v]:
if deg[dest] == 0:
child.append(dest)
child_dp.append(dp[dest])
else:
deg[dest] -= 1
if deg[dest] == 1:
stack.append(dest)
child_dp.sort(reverse=True)
x = min(beaver[v] - 1, len(child))
dp[v] = 1 + sum(child_dp[:x]) + x
beaver[v] -= x + 1
for c in child:
x = min(beaver[v], beaver[c])
beaver[v] -= x
dp[v] += 2 * x
x = min(beaver[start], len(adj[start]))
child_dp = sorted((dp[v] for v in adj[start]), reverse=True)
ans = sum(child_dp[:x]) + x
beaver[start] -= x
for c in adj[start]:
x = min(beaver[start], beaver[c])
beaver[start] -= x
ans += 2 * x
print(ans)
```
| 98,328 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"Eat a beaver, save a tree!" β That will be the motto of ecologists' urgent meeting in Beaverley Hills.
And the whole point is that the population of beavers on the Earth has reached incredible sizes! Each day their number increases in several times and they don't even realize how much their unhealthy obsession with trees harms the nature and the humankind. The amount of oxygen in the atmosphere has dropped to 17 per cent and, as the best minds of the world think, that is not the end.
In the middle of the 50-s of the previous century a group of soviet scientists succeed in foreseeing the situation with beavers and worked out a secret technology to clean territory. The technology bears a mysterious title "Beavermuncher-0xFF". Now the fate of the planet lies on the fragile shoulders of a small group of people who has dedicated their lives to science.
The prototype is ready, you now need to urgently carry out its experiments in practice.
You are given a tree, completely occupied by beavers. A tree is a connected undirected graph without cycles. The tree consists of n vertices, the i-th vertex contains ki beavers.
"Beavermuncher-0xFF" works by the following principle: being at some vertex u, it can go to the vertex v, if they are connected by an edge, and eat exactly one beaver located at the vertex v. It is impossible to move to the vertex v if there are no beavers left in v. "Beavermuncher-0xFF" cannot just stand at some vertex and eat beavers in it. "Beavermuncher-0xFF" must move without stops.
Why does the "Beavermuncher-0xFF" works like this? Because the developers have not provided place for the battery in it and eating beavers is necessary for converting their mass into pure energy.
It is guaranteed that the beavers will be shocked by what is happening, which is why they will not be able to move from a vertex of the tree to another one. As for the "Beavermuncher-0xFF", it can move along each edge in both directions while conditions described above are fulfilled.
The root of the tree is located at the vertex s. This means that the "Beavermuncher-0xFF" begins its mission at the vertex s and it must return there at the end of experiment, because no one is going to take it down from a high place.
Determine the maximum number of beavers "Beavermuncher-0xFF" can eat and return to the starting vertex.
Input
The first line contains integer n β the number of vertices in the tree (1 β€ n β€ 105). The second line contains n integers ki (1 β€ ki β€ 105) β amounts of beavers on corresponding vertices. Following n - 1 lines describe the tree. Each line contains two integers separated by space. These integers represent two vertices connected by an edge. Vertices are numbered from 1 to n. The last line contains integer s β the number of the starting vertex (1 β€ s β€ n).
Output
Print the maximum number of beavers munched by the "Beavermuncher-0xFF".
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).
Examples
Input
5
1 3 1 3 2
2 5
3 4
4 5
1 5
4
Output
6
Input
3
2 1 1
3 2
1 2
3
Output
2
Submitted Solution:
```
import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n = int(input())
beaver = tuple(map(int, input().split()))
adj = [[] for _ in range(n)]
deg = [0] * n
for u, v in (map(int, input().split()) for _ in range(n - 1)):
adj[u - 1].append(v - 1)
adj[v - 1].append(u - 1)
deg[u - 1] += 1
deg[v - 1] += 1
start = int(input()) - 1
deg[start] += 1000000
if n == 1:
print(0)
exit()
dp = [0] * n
stack = [i for i in range(n) if i != start and deg[i] == 1]
while stack:
v = stack.pop()
deg[v] = 0
child = []
for dest in adj[v]:
if deg[dest] == 0:
child.append(dp[dest])
else:
deg[dest] -= 1
if deg[dest] == 1:
stack.append(dest)
child.sort(reverse=True)
dp[v] = 1 + sum(child[:beaver[v] - 1]) + min(beaver[v] - 1, len(child))
child = sorted((dp[v] for v in adj[start]), reverse=True)
ans = sum(child[:beaver[start]]) + min(beaver[start], len(child))
print(ans)
```
No
| 98,329 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
import sys
from collections import defaultdict
input=sys.stdin.readline
n,q,c=map(int,input().split())
d=defaultdict(lambda:[0]*11)
for _ in range(n):
x,y,s=map(int,input().split())
if (x,y) not in d:
d[(x,y)]=[0]*11
d[(x,y)][s]+=1
g=[[[0]*11 for i in range(101)] for i in range(101)]
g[1][1]=d[(1,1)]
for i in range(1,101):
for j in range(1,101):
if i==1 and j==1:continue
for k in range(11):
if i==1:
g[i][j][k]=g[i][j-1][k]+d[(i,j)][k]
elif j==1:
g[i][j][k]=g[i-1][j][k]+d[(i,j)][k]
else:
g[i][j][k]=g[i-1][j][k]+g[i][j-1][k]-g[i-1][j-1][k]+d[(i,j)][k]
for _ in range(q):
t,x1,y1,x2,y2=map(int,input().split())
ans=0
for k in range(11):
cnt=g[x2][y2][k]-g[x1-1][y2][k]-g[x2][y1-1][k]+g[x1-1][y1-1][k]
ans+=((t+k)%(c+1))*cnt
print(ans)
```
| 98,330 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
import sys
input=sys.stdin.readline
n,q,c=map(int,input().split())
g=[[[0]*11 for i in range(101)] for i in range(101)]
for _ in range(n):
x,y,s=map(int,input().split())
g[x][y][s]+=1
for i in range(1,101):
for j in range(1,101):
for k in range(11):
g[i][j][k]+=g[i-1][j][k]+g[i][j-1][k]-g[i-1][j-1][k]
for _ in range(q):
t,x1,y1,x2,y2=map(int,input().split())
ans=0
for k in range(11):
cnt=g[x2][y2][k]-g[x1-1][y2][k]-g[x2][y1-1][k]+g[x1-1][y1-1][k]
ans+=((t+k)%(c+1))*cnt
print(ans)
```
| 98,331 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
n,q,c = map(int,input().split())
sky = [[[0] * 11 for j in range(101)] for i in range(101)]
c += 1
for i in range(n):
x,y,s = map(int,input().split())
sky[y][x][s % c] += 1
for k in range(11):
i = 1
while i < 101:
j = 1
while j < 101:
sky[i][j][k] += (sky[i-1][j][k] + sky[i][j-1][k] - sky[i-1][j-1][k])
j += 1
i += 1
req = [list(map(int,input().split())) for i in range(q)]
for i in range(q):
t,x,y,x2,y2 = req[i]
ans = 0
j = 0
while j < c:
ans += (sky[y2][x2][j] - sky[y2][x-1][j] - sky[y-1][x2][j] + sky[y-1][x-1][j]) * ((j + t) % c)
j += 1
print(ans)
```
| 98,332 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
"""
Author : Arif Ahmad
Date :
Algo :
Difficulty :
"""
from sys import stdin, stdout
def main():
n, q, c = [int(_) for _ in stdin.readline().strip().split()]
g = [[[0 for i in range(102)] for j in range(102)] for k in range(12)]
for _ in range(n):
x, y, s = [int(_) for _ in stdin.readline().strip().split()]
for t in range(c+1):
brightness = (s + t) % (c + 1)
g[t][x][y] += brightness
# dp stores cummulative brightness at time t
dp = [[[0 for i in range(102)] for j in range(102)] for k in range(12)]
for t in range(c+1):
for x in range(1, 101):
for y in range(1, 101):
dp[t][x][y] = dp[t][x-1][y] + dp[t][x][y-1] - dp[t][x-1][y-1] + g[t][x][y]
for _ in range(q):
t, x1, y1, x2, y2 = [int(_) for _ in stdin.readline().strip().split()]
t = t % (c + 1)
ans = dp[t][x2][y2] - dp[t][x1-1][y2] - dp[t][x2][y1-1] + dp[t][x1-1][y1-1]
stdout.write(str(ans) + '\n')
if __name__ == '__main__':
main()
```
| 98,333 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
def fun(n,q,c,stars,querry):
m=100
precom=[[[0]*m for _ in range(m)] for _ in range(c+1)]
for x,y,p in stars:
for i in range(c+1):
temp=(p+i)%(c+1)
precom[i][x-1][y-1]+=temp
for k in range(c+1):
for i in range(0,m):
for j in range(1,m):
precom[k][i][j]+=precom[k][i][j-1]
for k in range(c+1):
for j in range(0,m):
for i in range(1,m):
precom[k][i][j]+= precom[k][i-1][j]
for t,x1,y1,x2,y2 in querry:
t=t%(c+1)
x1,y1,x2,y2,a,b,C,d=x1-1,y1-1,x2-1,y2-1,0,0,0,0
a=precom[t][x2][y2]
if x1!=0:
b=precom[t][x1-1][y2]
if y1!=0:
C=precom[t][x2][y1-1]
if x1!=0 and y1!=0:
d=precom[t][x1-1][y1-1]
print(a-b-C+d)
n,q,c=list(map(lambda x:int(x),input().split()))
stars=[list(map(lambda x:int(x),input().split())) for _ in range(n)]
querry=[list(map(lambda x:int(x),input().split())) for _ in range(q)]
fun(n,q,c,stars,querry)
```
| 98,334 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
###### ### ####### ####### ## # ##### ### #####
# # # # # # # # # # # # # ###
# # # # # # # # # # # # # ###
###### ######### # # # # # # ######### #
###### ######### # # # # # # ######### #
# # # # # # # # # # #### # # #
# # # # # # # ## # # # # #
###### # # ####### ####### # # ##### # # # #
from __future__ import print_function # for PyPy2
from itertools import permutations as perm
# from fractions import Fraction
from collections import *
from sys import stdin
from bisect import *
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1e9)+7
inf = float("inf")
n, q, c = gil()
f = [ [[0 for _ in range(101)] for _ in range(101)] for _ in range(c+1) ]
for _ in range(n):
x, y, s = gil()
f[s][x][y] += 1
# pre computation
for y in range(1, 101):
for x in range(1, 101):
for ci in range(c+1):
f[ci][x][y] += f[ci][x][y-1] + f[ci][x-1][y] - f[ci][x-1][y-1]
for _ in range(q):
t, x1, y1, x2, y2 = gil()
t %= (c+1)
val = 0
for ci in range(c+1):
fi = f[ci][x2][y2] + f[ci][x1-1][y1-1] - f[ci][x2][y1-1] - f[ci][x1-1][y2]
val += fi*( (t+ci)%(c+1) )
print(val)
```
| 98,335 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
import sys
from collections import defaultdict
input=sys.stdin.readline
n,q,c=map(int,input().split())
d=defaultdict(lambda:[0]*11)
for _ in range(n):
x,y,s=map(int,input().split())
if (x,y) not in d:
d[(x,y)]=[0]*11
d[(x,y)][s]+=1
g=[[[0]*11 for i in range(101)] for i in range(101)]
g[1][1]=d[(1,1)]
for i in range(1,101):
for j in range(1,101):
for k in range(11):
g[i][j][k]=g[i-1][j][k]+g[i][j-1][k]-g[i-1][j-1][k]+d[(i,j)][k]
for _ in range(q):
t,x1,y1,x2,y2=map(int,input().split())
ans=0
for k in range(11):
cnt=g[x2][y2][k]-g[x1-1][y2][k]-g[x2][y1-1][k]+g[x1-1][y1-1][k]
ans+=((t+k)%(c+1))*cnt
print(ans)
```
| 98,336 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Tags: dp, implementation
Correct Solution:
```
import sys
from bisect import bisect_left, bisect_right
n, q, c = list(map(int, input().split()))
values = [[[0 for _ in range(11)] for _ in range(0, 101)] for _ in range(0, 101)]
# print(dp)
for i in range(n):
x, y, s = list(map(int, sys.stdin.readline().split()))
values[x][y][s] += 1
dp = [[[-1 for _ in range(11)] for _ in range(0, 101)] for _ in range(0, 101)]
for i in range(0,101):
for p in range(11):
dp[0][i][p]=0
dp[i][0][p]=0
def recurse(p, x, y):
if x==0 or y==0:
return 0
if dp[x][y][p]!=-1:
return dp[x][y][p]
if x == 1 and y == 1:
dp[x][y][p]=values[x][y][p]
return values[x][y][p]
else:
# print(x,y,p)
dp[x][y][p]=recurse(p,x-1,y)+recurse(p,x,y-1)-recurse(p,x-1,y-1)+values[x][y][p]
dp[x][y][p]=max(dp[x][y][p],0)
return dp[x][y][p]
for i in range(1,101):
for j in range(1,101):
for p in range(0,c+1):
recurse(p,i,j)
# print(dp)
for i in range(q):
t,x1,y1,x2,y2=list(map(int, sys.stdin.readline().split()))
ans=0
for k in range(0,c+1):
# print(dp[x2][y2][k])
# print(dp[x1-1][y2][k])
# print(dp[x1][y2-1][k])
# print(dp[x1-1][y1-1][k])
ans+=((t+k)%(c+1))*(dp[x2][y2][k]-dp[x1-1][y2][k]-dp[x2][y1-1][k]+dp[x1-1][y1-1][k])
print(ans)
```
| 98,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def add(x,y):
for i in range(len(x)):
x[i] += y[i]
def sub(x,y):
for i in range(len(x)):
x[i] -= y[i]
def main():
n,q,c = map(int,input().split())
arr = [[[0]*(c+1) for _ in range(101)] for _ in range(101)]
for _ in range(n):
x,y,s = map(int,input().split())
arr[x][y][s] += 1
dp = [[[0]*(c+1) for _ in range(101)] for _ in range(101)]
for i in range(1,101):
for j in range(1,101):
add(dp[i][j],arr[i][j])
add(dp[i][j],dp[i-1][j])
add(dp[i][j],dp[i][j-1])
sub(dp[i][j],dp[i-1][j-1])
for _ in range(q):
t,x1,y1,x2,y2 = map(int,input().split())
x = dp[x2][y2][:]
add(x,dp[x1-1][y1-1])
sub(x,dp[x1-1][y2])
sub(x,dp[x2][y1-1])
ans = 0
for ind,j in enumerate(x):
ans += ((ind+t)%(c+1))*j
print(ans)
#Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == '__main__':
main()
```
Yes
| 98,338 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
from sys import stdin,stdout
stdin.readline
def mp(): return list(map(int, stdin.readline().strip().split()))
def it():return int(stdin.readline().strip())
# def cal_brightness_at_any_instant(ib,t,c):
# while t:
# ib = ib+1
# ib%=(c+1)
# t-=1
# return ib
# print(cal_brightness_at_any_instant(1,5,3))
n,q,c = mp()
dp = [[[0]*11 for _ in range(101)] for _ in range(101)]
# print(dp)
for _ in range(n):
x,y,ib = mp()
dp[x][y][ib] += 1
for i in range(1,101):
for j in range(1,101):
for k in range(11):
dp[i][j][k]+=dp[i-1][j][k]+dp[i][j-1][k]-dp[i-1][j-1][k]
for _ in range(q):
t,x1,y1,x2,y2=mp()
ans=0
for k in range(11):
cnt=dp[x2][y2][k]-dp[x1-1][y2][k]-dp[x2][y1-1][k]+dp[x1-1][y1-1][k]
ans+=((t+k)%(c+1))*cnt
print(ans)
```
Yes
| 98,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
###### ### ####### ####### ## # ##### ### #####
# # # # # # # # # # # # # ###
# # # # # # # # # # # # # ###
###### ######### # # # # # # ######### #
###### ######### # # # # # # ######### #
# # # # # # # # # # #### # # #
# # # # # # # ## # # # # #
###### # # ####### ####### # # ##### # # # #
from __future__ import print_function # for PyPy2
from itertools import permutations as perm
# from fractions import Fraction
from collections import *
from sys import stdin
from bisect import *
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1e9)+7
inf = float("inf")
n, q, c = gil()
f = [ [[0 for _ in range(101)] for _ in range(101)] for _ in range(c+1) ]
for _ in range(n):
x, y, s = gil()
f[s][x][y] += 1
# pre computation
for y in range(1, 101):
for x in range(1, 101):
for ci in range(c+1):
f[ci][x][y] += f[ci][x][y-1] + f[ci][x-1][y] - f[ci][x-1][y-1]
for _ in range(q):
t, x1, y1, x2, y2 = gil()
store = t
# t %= (c+1)
val = 0
for ci in range(c+1):
fi = f[ci][x2][y2] + f[ci][x1-1][y1-1] - f[ci][x2][y1-1] - f[ci][x1-1][y2]
# if fi:print("at time", store, "seeing stars with brightness", ci, ' > ', (t+ci)%(c+1), " : ", fi)
val += fi*( (t+ci)%(c+1) )
print(val)
# print("*************")
```
Yes
| 98,340 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
n, q, c = map(int,input().split())
stars = [ list( map(int,input().split()) ) for i in range(n)]
queries = [ list( map(int,input().split()) ) for i in range(q)]
prec = [[[0 for y in range(101)]for x in range(101)] for i in range(c+1)] #0, c
for x, y, s in stars:
for t in range(c+1):
prec[t][x][y] += s+t if s+t <= c else s+t-(c+1)
for s in range(len(prec)):
for x in range(len(prec[s])):
for y in range(len(prec[s][x])):
if x and y:
prec[s][x][y] += prec[s][x-1][y]+prec[s][x][y-1]-prec[s][x-1][y-1]
for t, x1, y1, x2, y2 in queries:
s = t%(c+1)
res = ( prec[s][x2][y2]-prec[s][x1-1][y2]-prec[s][x2][y1-1]+prec[s][x1-1][y1-1] )
print(res)
```
Yes
| 98,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
#!/usr/bin/python3
def main():
n, q, c = [int(i) for i in input().split()]
plano = [[-1]*101 for i in range(101)]
for i in range(n):
a, b, d = [int(i) for i in input().split()]
plano[b][a] = d
for i in range(q):
t, x1, y1, x2, y2 = [int(i) for i in input().split()]
atual = 0
for y in range(y1, y2+1):
for x in range(x1, x2+1):
val = plano[x][y]
if val >= 0:
atual += (val + t) % (c+1)
# don't quite understand why plano[x][y] instead of plano[y][x]
print(atual)
if __name__ == "__main__": main()
```
No
| 98,342 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations
from bisect import *
def main():
n,q,c=map(int,input().split())
a=[[[0 for i in range(101)] for j in range(101)] for k in range(11)]
for i in range(n):
x,y,s=map(int,input().split())
for k in range(11):
a[k][x][y]=(s+k if s+k<=c else 0)
for k in range(11):
for i in range(101):
for j in range(1,101):
a[k][i][j]+=a[k][i][j-1]
for k in range(11):
for i in range(1,101):
for j in range(101):
a[k][i][j]+=a[k][i-1][j]
for i in range(q):
t,x1,y1,x2,y2=map(int,input().split())
z=t%(c+1)
print(a[z][x2][y2]-a[z][x1-1][y2]-a[z][x2][y1-1]+a[z][x1-1][y1-1])
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
```
No
| 98,343 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
n, q, c = [int(i) for i in input().split()]
c += 1
stars = [[[0 for u in range(c)] for i in range(101)] for j in range(101)]
for i in range(n):
x, y, s = [int(j) for j in input().split()]
stars[y - 1][x - 1][s] += 1
for i in range(101):
for j in range(101):
'''
if i:
for u in range(c):
stars[i][j][u] += stars[i - 1][j][u]
if j:
for u in range(c):
stars[i][j][u] += stars[i][j - 1][u]
if i and j:
for u in range(c):
stars[i][j][u] -= stars[i - 1][j - 1][u]
'''
for u in range(c):
if i:
stars[i][j][u] += stars[i - 1][j][u]
if j:
stars[i][j][u] += stars[i][j - 1][u]
if i and j:
stars[i][j][u] -= stars[i - 1][j - 1][u]
for i in range(q):
t, x1, y1, x2, y2 = [int(j) for j in input().split()]
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1
'''
data = [0 for i in range(c)]
for j in range(c):
data[j] = stars[y2][x2][j]
if y1:
for j in range(c):
data[j] -= stars[y1 - 1][x2][j]
if x1:
for j in range(c):
data[j] -= stars[y2][x1 - 1][j]
if x1 and y1:
for j in range(c):
data[j] += stars[y1 - 1][x1 - 1][j]
ret = 0
for j in range(c):
ret += ((j + t) % c) * data[j]
'''
ret = 0
for j in range(c):
box = stars[y2][x2][j]
if y1:
box -= stars[y1 - 1][x2][j]
if x1:
box -= stars[y2][x1 - 1][j]
if y1 and x2:
box += stars[y1 - 1][x1 - 1][j]
ret += (j + t) % c * box
print(ret)
```
No
| 98,344 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Cartesian coordinate system is set in the sky. There you can see n stars, the i-th has coordinates (xi, yi), a maximum brightness c, equal for all stars, and an initial brightness si (0 β€ si β€ c).
Over time the stars twinkle. At moment 0 the i-th star has brightness si. Let at moment t some star has brightness x. Then at moment (t + 1) this star will have brightness x + 1, if x + 1 β€ c, and 0, otherwise.
You want to look at the sky q times. In the i-th time you will look at the moment ti and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (x1i, y1i) and the upper right β (x2i, y2i). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it.
Input
The first line contains three integers n, q, c (1 β€ n, q β€ 105, 1 β€ c β€ 10) β the number of the stars, the number of the views and the maximum brightness of the stars.
The next n lines contain the stars description. The i-th from these lines contains three integers xi, yi, si (1 β€ xi, yi β€ 100, 0 β€ si β€ c β€ 10) β the coordinates of i-th star and its initial brightness.
The next q lines contain the views description. The i-th from these lines contains five integers ti, x1i, y1i, x2i, y2i (0 β€ ti β€ 109, 1 β€ x1i < x2i β€ 100, 1 β€ y1i < y2i β€ 100) β the moment of the i-th view and the coordinates of the viewed rectangle.
Output
For each view print the total brightness of the viewed stars.
Examples
Input
2 3 3
1 1 1
3 2 0
2 1 1 2 2
0 2 1 4 5
5 1 1 5 5
Output
3
0
3
Input
3 4 5
1 1 2
2 3 0
3 3 1
0 1 1 100 100
1 2 2 4 4
2 2 1 4 7
1 50 50 51 51
Output
3
3
5
0
Note
Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
Submitted Solution:
```
from sys import stdin, stdout
n,q,c=map(int,stdin.readline().split())
cs=[[[0]*101 for _ in range(101)] for _ in range(11)]
for k in range(n):
x,y,s=map(int,stdin.readline().split())
cs[s][x][y]+=1
for i in range(1,101):
for j in range(1,101):
for k in range(11):
cs[k][i][j]+=max(cs[k][i-1][j-1],cs[k][i-1][j],cs[k][i][j-1])
for _ in range(q):
t,x1,y1,x2,y2=map(int,stdin.readline().split())
totalb=0
for k in range(11):
totalb+=((k+t)%(c+1))*(cs[k][x2][y2]-cs[k][x2][y1-1]-cs[k][x1-1][y2]+cs[k][x1-1][y1-1])
print(totalb)
```
No
| 98,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A long time ago in some country in Asia were civil wars.
Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city.
Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads.
Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them.
Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees.
Input
The first line contains an integer n (2 β€ n β€ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 β€ ai, bi β€ n), connected by the i-th road and the number of days di the army spends to go on it (1 β€ di β€ 109). The lengths of several roads may coincide.
Output
Print on the first line two integers β the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data.
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
Examples
Input
2
2 1 5
Output
2 1
1
Input
6
1 2 1
1 3 5
3 4 2
3 5 3
3 6 4
Output
16 1
2
Submitted Solution:
```
from collections import deque
from operator import itemgetter
class DisjointSet:
def __init__(self, n):
self.padre = [x for x in range(0, n)]
self. cantidad_vertices = [1]*n
def Padre(self, x):
if x == self.padre[x]:
return x
self.padre[x] = self.Padre(self.padre[x])
return self.padre[x]
def Union(self, x, y):
raiz_x = self.Padre(x)
raiz_y = self.Padre(y)
if self.cantidad_vertices[raiz_x] > self.cantidad_vertices[raiz_y]:
self.padre[raiz_y] = raiz_x
self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y]
else:
self.padre[raiz_x] = raiz_y
self.cantidad_vertices[raiz_y] += self.cantidad_vertices[raiz_x]
# Dfs para saber el numero de aristas a la derecha de un vertice
def DFSsumando(x, ady, num, i, DS):
num[x] = i
for y in ady[x]:
if num[y] == 0:
DFSsumando(y, ady, num, i, DS)
DS.Union(x, y)
def Solucion():
# organizar las aristas por valor d
edges.sort(key=itemgetter(2))
maximo = -1
cant_maximos = 0
aristas_maximos = [-1]*m
DS = DisjointSet(n)
i = 0
while i < m:
min = edges[i][2]
k = i+1
# si se repite un valor d
if k < m and edges[k][2] == min:
adyacentes_temp = [[] for _ in range(0, n)]
edges_temp = deque()
# ir agregando todas las aristas con peso min
while True:
x, y, _, posI = edges[k-1]
r_a = DS.Padre(x)
r_b = DS.Padre(y)
edges_temp.append((r_a, r_b, posI))
adyacentes_temp[r_a].append(r_b)
adyacentes_temp[r_b].append(r_a)
if k >= m or edges[k][2] != min:
break
k += 1
num_cc = [0]*n
contador = 1
# por cada arista tempoar el dfs
for x, y, i_inicio in edges_temp:
if num_cc[x] == 0:
DFSsumando(x, adyacentes_temp, num_cc, contador, DS)
# una nueva cc
contador += 1
# calculando el valor de cada arista
for x, y, i_inicio in edges_temp:
li = total = 0
if DS.Padre(x) == x:
li = DS.cantidad_vertices[y]
total = DS.cantidad_vertices[x]
else:
li = DS.cantidad_vertices[y]
total = DS.cantidad_vertices[x]
# cantidad de vertices en esa cc
ld = total-li
res = li*ld*2
if res < maximo:
continue
if res > maximo:
maximo = res
cant_maximos = 0
aristas_maximos[cant_maximos] = i_inicio+1
cant_maximos += 1
# saltar las aristas q ya se analizaron
i = k
# si no ay varias aristas con ese valor d
else:
x, y, _, i_inicio = edges[i]
px = DS.Padre(x)
py = DS.Padre(y)
res = DS.cantidad_vertices[px]*DS.cantidad_vertices[py]*2
DS.Union(px, py)
i += 1
if res < maximo:
continue
if res > maximo:
maximo = res
cant_maximos = 0
aristas_maximos[cant_maximos] = i_inicio+1
cant_maximos += 1
# poner las aristas con mas arboles hacia delante
print(str(maximo)+' '+str(cant_maximos))
# imprime cada arista hermosa
aristas_maximos[0:cant_maximos].sort()
print(*aristas_maximos[0:cant_maximos], sep=" ")
n = int(input())
m = n-1
edges = [-1]*m
adyacentes = [[] for _ in range(0, n)]
for i in range(0, m):
x, y, d = map(int, input().split())
edges[i] = (x-1, y-1, d, i)
adyacentes[x-1].append(y-1)
adyacentes[y-1].append(x-1)
Solucion()
```
No
| 98,346 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A long time ago in some country in Asia were civil wars.
Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city.
Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads.
Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them.
Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees.
Input
The first line contains an integer n (2 β€ n β€ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 β€ ai, bi β€ n), connected by the i-th road and the number of days di the army spends to go on it (1 β€ di β€ 109). The lengths of several roads may coincide.
Output
Print on the first line two integers β the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data.
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
Examples
Input
2
2 1 5
Output
2 1
1
Input
6
1 2 1
1 3 5
3 4 2
3 5 3
3 6 4
Output
16 1
2
Submitted Solution:
```
from collections import deque
from operator import itemgetter
class DisjointSet:
def __init__(self, n):
self.padre = [x for x in range(0, n)]
self. cantidad_vertices = [1]*n
def Padre(self, x):
if x == self.padre[x]:
return x
self.padre[x] = self.Padre(self.padre[x])
return self.padre[x]
def Union(self, x, y):
raiz_x = self.Padre(x)
raiz_y = self.Padre(y)
self.padre[raiz_y] = raiz_x
self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y]
# if self.cantidad_vertices[raiz_x] > self.cantidad_vertices[raiz_y]:
# self.padre[raiz_y] = raiz_x
#
# self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y]
# else:
# self.padre[raiz_x] = raiz_y
# self.cantidad_vertices[raiz_y] += self.cantidad_vertices[raiz_x]
#
# Dfs para saber el numero de aristas a la derecha de un vertice
def DFSsumando(x, ady, num, i, DS):
num[x] = i
for y in ady[x]:
if num[y] == 0:
DFSsumando(y, ady, num, i, DS)
DS.Union(x, y)
def Solucion():
# organizar las aristas por valor d
edges.sort(key=itemgetter(2))
maximo = -1
cant_maximos = 0
aristas_maximos = [-1]*m
DS = DisjointSet(n)
i = 0
while i < m:
min = edges[i][2]
k = i+1
# si se repite un valor d
if k < m and edges[k][2] == min:
adyacentes_temp = [[] for _ in range(0, n)]
edges_temp = deque()
# ir agregando todas las aristas con peso min
while True:
x, y, _, posI = edges[k-1]
r_a = DS.Padre(x)
r_b = DS.Padre(y)
edges_temp.append((r_a, r_b, posI))
adyacentes_temp[r_a].append(r_b)
adyacentes_temp[r_b].append(r_a)
if k >= m or edges[k][2] != min:
break
k += 1
num_cc = [0]*n
contador = 1
# por cada arista tempoar el dfs
for x, y, i_inicio in edges_temp:
if num_cc[x] == 0:
DFSsumando(x, adyacentes_temp, num_cc, contador, DS)
# una nueva cc
contador += 1
# calculando el valor de cada arista
for x, y, i_inicio in edges_temp:
li = total = 0
if DS.Padre(x) == x:
li = DS.cantidad_vertices[y]
total = DS.cantidad_vertices[x]
else:
li = DS.cantidad_vertices[x]
total = DS.cantidad_vertices[y]
# cantidad de vertices en esa cc
ld = total-li
res = li*ld*2
if res < maximo:
continue
if res > maximo:
maximo = res
cant_maximos = 0
aristas_maximos[cant_maximos] = i_inicio+1
cant_maximos += 1
# saltar las aristas q ya se analizaron
i = k
# si no ay varias aristas con ese valor d
else:
x, y, _, i_inicio = edges[i]
px = DS.Padre(x)
py = DS.Padre(y)
res = DS.cantidad_vertices[px]*DS.cantidad_vertices[py]*2
DS.Union(px, py)
i += 1
if res < maximo:
continue
if res > maximo:
maximo = res
cant_maximos = 0
aristas_maximos[cant_maximos] = i_inicio+1
cant_maximos += 1
# poner las aristas con mas arboles hacia delante
print(str(maximo)+' '+str(cant_maximos))
# imprime cada arista hermosa
aristas_maximos[0:cant_maximos] = sorted(aristas_maximos[0:cant_maximos])
a=aristas_maximos[0:cant_maximos]
print(*a, sep=" ")
n = int(input())
m = n-1
edges = [-1]*m
adyacentes = [[] for _ in range(0, n)]
for i in range(0, m):
x, y, d = map(int, input().split())
edges[i] = (x-1, y-1, d, i)
adyacentes[x-1].append(y-1)
adyacentes[y-1].append(x-1)
Solucion()
```
No
| 98,347 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A long time ago in some country in Asia were civil wars.
Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city.
Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads.
Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them.
Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees.
Input
The first line contains an integer n (2 β€ n β€ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 β€ ai, bi β€ n), connected by the i-th road and the number of days di the army spends to go on it (1 β€ di β€ 109). The lengths of several roads may coincide.
Output
Print on the first line two integers β the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data.
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
Examples
Input
2
2 1 5
Output
2 1
1
Input
6
1 2 1
1 3 5
3 4 2
3 5 3
3 6 4
Output
16 1
2
Submitted Solution:
```
from collections import deque
from operator import itemgetter
class DisjointSet:
def __init__(self, n):
self.padre = [x for x in range(0, n)]
self. cantidad_vertices = [1]*n
def Padre(self, x):
if x == self.padre[x]:
return x
self.padre[x] = self.Padre(self.padre[x])
return self.padre[x]
def Union(self, x, y):
raiz_x = self.Padre(x)
raiz_y = self.Padre(y)
if self.cantidad_vertices[raiz_x] > self.cantidad_vertices[raiz_y]:
self.padre[raiz_y] = raiz_x
self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y]
else:
self.padre[raiz_x] = raiz_y
self.cantidad_vertices[raiz_y] += self.cantidad_vertices[raiz_x]
# Dfs para saber el numero de aristas a la derecha de un vertice
def DFSsumando(x, ady, num, i, DS):
num[x] = i
for y in ady[x]:
if num[y] == 0:
DS.Union(x, y)
DFSsumando(y, ady, num, i, DS)
def Solucion():
# organizar las aristas por valor d
edges.sort(key=itemgetter(2))
maximo = -1
cant_maximos = 0
aristas_maximos = [-1]*m
DS = DisjointSet(n)
i = 0
while i < m:
min = edges[i][2]
k = i+1
# si se repite un valor d
if k < m and edges[k][2] == min:
adyacentes_temp = [[] for _ in range(0, n)]
edges_temp = deque()
# ir agregando todas las aristas con peso min
while True:
x, y, _, posI = edges[k-1]
r_a = DS.Padre(x)
r_b = DS.Padre(y)
edges_temp.append((r_a, r_b, posI))
adyacentes_temp[r_a].append(r_b)
adyacentes_temp[r_b].append(r_a)
if k >= m or edges[k][2] != min:
break
k += 1
num_cc = [0]*n
contador = 1
# por cada arista tempoar el dfs
for x, y, i_inicio in edges_temp:
if num_cc[x] == 0:
DFSsumando(x, adyacentes_temp, num_cc, contador, DS)
# una nueva cc
contador += 1
# calculando el valor de cada arista
for x, y, i_inicio in edges_temp:
li = total = 0
if DS.Padre(x) == x:
li = DS.cantidad_vertices[y]
total = DS.cantidad_vertices[x]
else:
li = DS.cantidad_vertices[x]
total = DS.cantidad_vertices[y]
# cantidad de vertices en esa cc
ld = total-li
res = li*ld*2
if res < maximo:
continue
if res > maximo:
maximo = res
cant_maximos = 0
aristas_maximos[cant_maximos] = i_inicio+1
cant_maximos += 1
# saltar las aristas q ya se analizaron
i = k
# si no ay varias aristas con ese valor d
else:
x, y, _, i_inicio = edges[i]
px = DS.Padre(x)
py = DS.Padre(y)
res = DS.cantidad_vertices[px]*DS.cantidad_vertices[py]*2
DS.Union(px, py)
i += 1
if res < maximo:
continue
if res > maximo:
maximo = res
cant_maximos = 0
aristas_maximos[cant_maximos] = i_inicio+1
cant_maximos += 1
# poner las aristas con mas arboles hacia delante
print(str(maximo)+' '+str(cant_maximos))
# imprime cada arista hermosa
aristas_maximos[0:cant_maximos].sort()
print(*aristas_maximos[0:cant_maximos], sep=" ")
n = int(input())
m = n-1
edges = [-1]*m
adyacentes = [[] for _ in range(0, n)]
for i in range(0, m):
x, y, d = map(int, input().split())
edges[i] = (x-1, y-1, d, i)
adyacentes[x-1].append(y-1)
adyacentes[y-1].append(x-1)
Solucion()
```
No
| 98,348 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A long time ago in some country in Asia were civil wars.
Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city.
Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads.
Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them.
Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees.
Input
The first line contains an integer n (2 β€ n β€ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 β€ ai, bi β€ n), connected by the i-th road and the number of days di the army spends to go on it (1 β€ di β€ 109). The lengths of several roads may coincide.
Output
Print on the first line two integers β the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data.
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
Examples
Input
2
2 1 5
Output
2 1
1
Input
6
1 2 1
1 3 5
3 4 2
3 5 3
3 6 4
Output
16 1
2
Submitted Solution:
```
from collections import deque
from operator import itemgetter
# Para encontrar el representante o padre de una componente conexa
def Padre(x, P):
while x != P[x]:
x = P[x]
return x
# Dfs para saber el numero de aristas a la derecha de un vertice
def DFSsumando(x, suma, color, ady, num, i):
color[x] = 1
num[x] = i
for adyacente in ady[x]:
v = adyacente[0]
if color[v] == 1:
continue
elif color[v] == 0:
suma[x] += DFSsumando(v, suma, color, ady, num, i)
color[x] = 2
return suma[x]
def Solucion():
# organizar las aristas por valor d
edges.sort(key=itemgetter(2))
maximo = 1
lista_maximos = deque()
# caso 1 en que todas las aristas tienen el mismo valor d
inicio = edges[0]
final = edges[n-2]
if inicio[2] == final[2]:
valorI = [1]*n
color= num = [0]*n
for x, y, d, pos in edges:
if color[x] == 0:
DFSsumando(x, valorI, color, adyacentes, num, 1)
for i in range(0, m):
x, y, d, pos = edges[i]
vix = valorI[x]
viy = valorI[y]
li = vix if vix < viy else viy
ld = n-li
res = li*ld*2
if res > maximo:
maximo = res
lista_maximos = deque()
lista_maximos.append(edges[i][3])
elif res == maximo:
lista_maximos.append(edges[i][3])
# existen al menos dos aristas con d diferente
else:
i = 0
padre = [x for x in range(0, n)]
cc = [1]*n
while i < m:
min = edges[i][2]
k = i+1
# si se repite un valor d
if k < m and edges[k][2] == min:
adyacentes_temp = [[] for _ in range(0, n)]
edges_temp = []
bol = True
# ir agregando todas las aristas con peso min
while bol:
x, y, d, posI = edges[k-1]
r_a = Padre(x, padre)
r_b = Padre(y, padre)
edges_temp.append([r_a, r_b, i, posI])
adyacentes_temp[r_a].append([r_b, k-1])
adyacentes_temp[r_b].append([r_a, k-1])
k += 1
bol = k < m and edges[k][2] == min
cant = deque()
num_cc = [0]*n
color = [0]*n
copia = cc.copy()
# por cada arista tempoar el dfs
for x, y, i_edg, i_inicio in edges_temp:
contador = 1
if color[x] == 0:
r = DFSsumando(x, copia, color,
adyacentes_temp, num_cc, contador)
# una nueva cc
cant.append(r)
contador += 1
# calculando el valor de cada arista
for x, y, i_edg, i_inicio in edges_temp:
vx = copia[x]
vy = copia[y]
li = vx if vx < vy else vy
h = num_cc[li]
ld = cant[h-1]-li
res = li*ld*2
if res > maximo:
maximo = res
lista_maximos = deque()
lista_maximos.append(i_inicio)
elif res == maximo:
lista_maximos.append(i_inicio)
padre[y] = x
cc[x] += cc[y]
# saltar las aristas q ya se analizaron
i = k
# si no ay varias aristas con ese valor d
else:
x, y, i_edg, i_inicio = edges[i]
px = padre[x]
py = padre[y]
res = cc[px]*cc[py]*2
if res > maximo:
maximo = res
lista_maximos = deque()
lista_maximos.append(i_inicio)
elif res == maximo:
lista_maximos.append(i_inicio)
padre[py] = px
cc[px] += cc[py]
i += 1
# poner las aristas con mas arboles hacia delante
cantidad = len(lista_maximos)
print(str(maximo)+' '+str(cantidad))
# imprime cada arista hermosa
for x in lista_maximos:
print(str(x+1))
n = int(input())
m = n-1
edges = [-1]*m
adyacentes = [[] for _ in range(0, n)]
for i in range(0, m):
x, y, d = map(int, input().split())
edges[i] = [x-1, y-1, d, i]
adyacentes[x-1].append([y-1, d, i])
adyacentes[y-1].append([x-1, d, i])
Solucion()
```
No
| 98,349 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a matrix f with 4 rows and n columns. Each element of the matrix is either an asterisk (*) or a dot (.).
You may perform the following operation arbitrary number of times: choose a square submatrix of f with size k Γ k (where 1 β€ k β€ 4) and replace each element of the chosen submatrix with a dot. Choosing a submatrix of size k Γ k costs ak coins.
What is the minimum number of coins you have to pay to replace all asterisks with dots?
Input
The first line contains one integer n (4 β€ n β€ 1000) β the number of columns in f.
The second line contains 4 integers a1, a2, a3, a4 (1 β€ ai β€ 1000) β the cost to replace the square submatrix of size 1 Γ 1, 2 Γ 2, 3 Γ 3 or 4 Γ 4, respectively.
Then four lines follow, each containing n characters and denoting a row of matrix f. Each character is either a dot or an asterisk.
Output
Print one integer β the minimum number of coins to replace all asterisks with dots.
Examples
Input
4
1 10 8 20
***.
***.
***.
...*
Output
9
Input
7
2 1 8 2
.***...
.***..*
.***...
....*..
Output
3
Input
4
10 10 1 10
***.
*..*
*..*
.***
Output
2
Note
In the first example you can spend 8 coins to replace the submatrix 3 Γ 3 in the top-left corner, and 1 coin to replace the 1 Γ 1 submatrix in the bottom-right corner.
In the second example the best option is to replace the 4 Γ 4 submatrix containing columns 2 β 5, and the 2 Γ 2 submatrix consisting of rows 2 β 3 and columns 6 β 7.
In the third example you can select submatrix 3 Γ 3 in the top-left corner and then submatrix 3 Γ 3 consisting of rows 2 β 4 and columns 2 β 4.
Tags: bitmasks, dp
Correct Solution:
```
import sys
n = int(sys.stdin.buffer.readline().decode('utf-8'))
cost = [0] + \
list(map(int, sys.stdin.buffer.readline().decode('utf-8').split()))
a = [line.decode('utf-8').rstrip() for line in sys.stdin.buffer]
mask = [0, 1, 51, 1911]
inf, bs_size, full_bit = 10**9, 1 << 12, (1 << 12) - 1
dp = [[inf]*bs_size for _ in range(4*n+1)]
dp[0][0] = 0
for i in range(4*n):
y, x = i & 3, i >> 2
is_dot = 1 if a[y][x] == '.' else 0
for bitset in range(bs_size):
if y == 0:
'''
01234 01234
0 s**** .t..*
1 ***** -> ....*
2 ***** -> ....*
3 ***** ....*
'''
if dp[i+4][full_bit] > dp[i][bitset] + cost[4]:
dp[i+4][full_bit] = dp[i][bitset] + cost[4]
if (is_dot | bitset & 1) and\
dp[i+1][bitset >> 1] > dp[i][bitset]:
dp[i+1][bitset >> 1] = dp[i][bitset]
for k in range(1, min(4-y, 3)+1):
if dp[i][bitset | mask[k]] > dp[i][bitset] + cost[k]:
dp[i][bitset | mask[k]] = dp[i][bitset] + cost[k]
print(min(dp[4*n]))
```
| 98,350 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
l, r = input().split()
s = 0
for x in range(0, 34):
for y in range(0, 34):
m = (2**x)*(3**y)
if int(l) <= m <= int(r):
s += 1
print(s)
```
| 98,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
l, r = input().split()
l = int(l)
r = int(r)
a, b = [0] * 50, [0] * 50
a[0] = 1
b[0] = 1
for i in range(1, 40):
a[i], b[i] = a[i - 1] * 2, b[i - 1] * 3
ans = 0
for i in range(40):
for j in range(40):
if a[i] * b[j] >= l and a[i] * b[j] <= r:
ans += 1
print(ans)
```
| 98,352 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
a = [0, 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27, 32, 36, 48, 54, 64, 72, 81, 96, 108, 128, 144, 162, 192, 216, 243, 256, 288, 324, 384, 432, 486, 512, 576, 648, 729, 768, 864, 972, 1024, 1152, 1296, 1458, 1536, 1728, 1944, 2048, 2187, 2304, 2592, 2916, 3072, 3456, 3888, 4096, 4374, 4608, 5184, 5832, 6144, 6561, 6912, 7776, 8192, 8748, 9216, 10368, 11664, 12288, 13122, 13824, 15552, 16384, 17496, 18432, 19683, 20736, 23328, 24576, 26244, 27648, 31104, 32768, 34992, 36864, 39366, 41472, 46656, 49152, 52488, 55296, 59049, 62208, 65536, 69984, 73728, 78732, 82944, 93312, 98304, 104976, 110592, 118098, 124416, 131072, 139968, 147456, 157464, 165888, 177147, 186624, 196608, 209952, 221184, 236196, 248832, 262144, 279936, 294912, 314928, 331776, 354294, 373248, 393216, 419904, 442368, 472392, 497664, 524288, 531441, 559872, 589824, 629856, 663552, 708588, 746496, 786432, 839808, 884736, 944784, 995328, 1048576, 1062882, 1119744, 1179648, 1259712, 1327104, 1417176, 1492992, 1572864, 1594323, 1679616, 1769472, 1889568, 1990656, 2097152, 2125764, 2239488, 2359296, 2519424, 2654208, 2834352, 2985984, 3145728, 3188646, 3359232, 3538944, 3779136, 3981312, 4194304, 4251528, 4478976, 4718592, 4782969, 5038848, 5308416, 5668704, 5971968, 6291456, 6377292, 6718464, 7077888, 7558272, 7962624, 8388608, 8503056, 8957952, 9437184, 9565938, 10077696, 10616832, 11337408, 11943936, 12582912, 12754584, 13436928, 14155776, 14348907, 15116544, 15925248, 16777216, 17006112, 17915904, 18874368, 19131876, 20155392, 21233664, 22674816, 23887872, 25165824, 25509168, 26873856, 28311552, 28697814, 30233088, 31850496, 33554432, 34012224, 35831808, 37748736, 38263752, 40310784, 42467328, 43046721, 45349632, 47775744, 50331648, 51018336, 53747712, 56623104, 57395628, 60466176, 63700992, 67108864, 68024448, 71663616, 75497472, 76527504, 80621568, 84934656, 86093442, 90699264, 95551488, 100663296, 102036672, 107495424, 113246208, 114791256, 120932352, 127401984, 129140163, 134217728, 136048896, 143327232, 150994944, 153055008, 161243136, 169869312, 172186884, 181398528, 191102976, 201326592, 204073344, 214990848, 226492416, 229582512, 241864704, 254803968, 258280326, 268435456, 272097792, 286654464, 301989888, 306110016, 322486272, 339738624, 344373768, 362797056, 382205952, 387420489, 402653184, 408146688, 429981696, 452984832, 459165024, 483729408, 509607936, 516560652, 536870912, 544195584, 573308928, 603979776, 612220032, 644972544, 679477248, 688747536, 725594112, 764411904, 774840978, 805306368, 816293376, 859963392, 905969664, 918330048, 967458816, 1019215872, 1033121304, 1073741824, 1088391168, 1146617856, 1162261467, 1207959552, 1224440064, 1289945088, 1358954496, 1377495072, 1451188224, 1528823808, 1549681956, 1610612736, 1632586752, 1719926784, 1811939328, 1836660096, 1934917632, 2038431744, 2066242608, 2147483648, 2176782336, 2293235712, 2324522934, 2415919104, 2448880128, 2579890176, 2717908992, 2754990144, 2902376448, 3057647616, 3099363912, 3221225472, 3265173504, 3439853568, 3486784401, 3623878656, 3673320192, 3869835264, 4076863488, 4132485216, 4294967296, 4353564672, 4586471424, 4649045868, 4831838208, 4897760256, 5159780352, 5435817984, 5509980288, 5804752896, 6115295232, 6198727824, 6442450944, 6530347008, 6879707136, 6973568802, 7247757312, 7346640384, 7739670528, 8153726976, 8264970432, 8589934592, 8707129344, 9172942848, 9298091736, 9663676416, 9795520512, 10319560704, 10460353203, 10871635968, 11019960576, 11609505792, 12230590464, 12397455648, 12884901888, 13060694016, 13759414272, 13947137604, 14495514624, 14693280768, 15479341056, 16307453952, 16529940864, 17179869184, 17414258688, 18345885696, 18596183472, 19327352832, 19591041024]
l, r = map(int, input().split())
ansr = 0
ansl = 398
while a[ansr] < r:
ansr += 1
if a[ansr] > r:
ansr -= 1
while a[ansl] > l:
ansl -= 1
if a[ansl] < l:
ansl += 1
print(ansr - ansl + 1)
```
| 98,353 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
l, r = map(int, input().split())
two = []
three = []
for i in range(31):
two.append(2 ** i)
for i in range(20):
three.append(3 ** i)
s = set()
for i in range(31):
for j in range(20):
s.add(two[i] * three[j])
k = 0
for i in s:
if l <= i <= r:
k += 1
print(k)
```
| 98,354 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
count = 0
l, r = map(int, input().split())
for i in range(50):
for j in range(50):
if l <= (2 ** i) * (3 ** j) <= r:
count += 1
if (2 ** i) * (3 ** j) > r:
break
print(count)
```
| 98,355 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
#import time
l, r = map(int, input().split())
checked = set()
def howNums(num):
if num in checked: return 0
checked.add(num)
c = 0
subnum1 = num * 2
subnum2 = num * 3
if(subnum1 <= r and (subnum1 not in checked)):
if(subnum1 >= l): c+=1
c+=howNums(subnum1)
#if(subnum1 >= l): print(subnum1)
if(subnum2 <= r and (subnum2 not in checked)):
if(subnum2 >= l): c+=1
c+=howNums(subnum2)
#if(subnum2 >= l): print(subnum2)
if((subnum1 > r) and (subnum2 > r)):
return 0
return c
#start_time = time.time()
count = howNums(1)
if(l is 1): count+=1
#print("ans:" + str(count))
print(count)
#print(str(time.time() - start_time))
```
| 98,356 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
l,r=list(map(int,input().split()))
count=0
for i in range(0,32):
temp=(1<<i)
if((temp >= l) and(temp<=r)):
count+=1
for j in range(1,21):
n=3**j
if(((n*temp)>= l) and((n*temp)<=r)):
count+=1
print(count)
```
| 98,357 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Tags: implementation, math
Correct Solution:
```
from collections import defaultdict
d = {}
left, right = map(int, input().split())
N = max(left, right) + 1
stack = [1]
d[1] = 1
while stack:
p = stack.pop()
for i in ( 2*p, 3*p):
if i < N:
if not i in d:
d[i] = 1
stack.append(i)
ans = 0
for a in d:
if left <= a <= right:
ans += 1
print(ans)
```
| 98,358 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
l,r=map(int,input().split())
counter=0
for i in range(50):
for j in range(50):
if l<=(2**i)*(3**j)<=r:
counter+=1
print(counter)
```
Yes
| 98,359 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
l, r = map(int, input().split())
counter = 0
for x in range(31):
for y in range(20):
num = 2 ** x * 3 ** y
if l <= num <= r:
counter += 1
elif num > r:
break
print(counter)
```
Yes
| 98,360 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
n, m = input().split()
n = int(n)
m = int(m)
ans = 0
for i in range(31):
for j in range(20):
if 2 ** i * 3 ** j >= n and 2 ** i * 3 ** j <= m:
ans = ans + 1
print(ans)
```
Yes
| 98,361 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
import math
l, r=map(int,input().split())
ans = 0
if (l<=1) & (r>=1) : ans = ans + 1
if (l<=2) & (r>=2) : ans = ans + 1
if (l<=3) & (r>=3) : ans = ans + 1
if (l<=4) & (r>=4) : ans = ans + 1
if (l<=6) & (r>=6) : ans = ans + 1
if (l<=8) & (r>=8) : ans = ans + 1
if (l<=9) & (r>=9) : ans = ans + 1
if (l<=12) & (r>=12) : ans = ans + 1
if (l<=16) & (r>=16) : ans = ans + 1
if (l<=18) & (r>=18) : ans = ans + 1
if (l<=24) & (r>=24) : ans = ans + 1
if (l<=27) & (r>=27) : ans = ans + 1
if (l<=32) & (r>=32) : ans = ans + 1
if (l<=36) & (r>=36) : ans = ans + 1
if (l<=48) & (r>=48) : ans = ans + 1
if (l<=54) & (r>=54) : ans = ans + 1
if (l<=64) & (r>=64) : ans = ans + 1
if (l<=72) & (r>=72) : ans = ans + 1
if (l<=81) & (r>=81) : ans = ans + 1
if (l<=96) & (r>=96) : ans = ans + 1
if (l<=108) & (r>=108) : ans = ans + 1
if (l<=128) & (r>=128) : ans = ans + 1
if (l<=144) & (r>=144) : ans = ans + 1
if (l<=162) & (r>=162) : ans = ans + 1
if (l<=192) & (r>=192) : ans = ans + 1
if (l<=216) & (r>=216) : ans = ans + 1
if (l<=243) & (r>=243) : ans = ans + 1
if (l<=256) & (r>=256) : ans = ans + 1
if (l<=288) & (r>=288) : ans = ans + 1
if (l<=324) & (r>=324) : ans = ans + 1
if (l<=384) & (r>=384) : ans = ans + 1
if (l<=432) & (r>=432) : ans = ans + 1
if (l<=486) & (r>=486) : ans = ans + 1
if (l<=512) & (r>=512) : ans = ans + 1
if (l<=576) & (r>=576) : ans = ans + 1
if (l<=648) & (r>=648) : ans = ans + 1
if (l<=729) & (r>=729) : ans = ans + 1
if (l<=768) & (r>=768) : ans = ans + 1
if (l<=864) & (r>=864) : ans = ans + 1
if (l<=972) & (r>=972) : ans = ans + 1
if (l<=1024) & (r>=1024) : ans = ans + 1
if (l<=1152) & (r>=1152) : ans = ans + 1
if (l<=1296) & (r>=1296) : ans = ans + 1
if (l<=1458) & (r>=1458) : ans = ans + 1
if (l<=1536) & (r>=1536) : ans = ans + 1
if (l<=1728) & (r>=1728) : ans = ans + 1
if (l<=1944) & (r>=1944) : ans = ans + 1
if (l<=2048) & (r>=2048) : ans = ans + 1
if (l<=2187) & (r>=2187) : ans = ans + 1
if (l<=2304) & (r>=2304) : ans = ans + 1
if (l<=2592) & (r>=2592) : ans = ans + 1
if (l<=2916) & (r>=2916) : ans = ans + 1
if (l<=3072) & (r>=3072) : ans = ans + 1
if (l<=3456) & (r>=3456) : ans = ans + 1
if (l<=3888) & (r>=3888) : ans = ans + 1
if (l<=4096) & (r>=4096) : ans = ans + 1
if (l<=4374) & (r>=4374) : ans = ans + 1
if (l<=4608) & (r>=4608) : ans = ans + 1
if (l<=5184) & (r>=5184) : ans = ans + 1
if (l<=5832) & (r>=5832) : ans = ans + 1
if (l<=6144) & (r>=6144) : ans = ans + 1
if (l<=6561) & (r>=6561) : ans = ans + 1
if (l<=6912) & (r>=6912) : ans = ans + 1
if (l<=7776) & (r>=7776) : ans = ans + 1
if (l<=8192) & (r>=8192) : ans = ans + 1
if (l<=8748) & (r>=8748) : ans = ans + 1
if (l<=9216) & (r>=9216) : ans = ans + 1
if (l<=10368) & (r>=10368) : ans = ans + 1
if (l<=11664) & (r>=11664) : ans = ans + 1
if (l<=12288) & (r>=12288) : ans = ans + 1
if (l<=13122) & (r>=13122) : ans = ans + 1
if (l<=13824) & (r>=13824) : ans = ans + 1
if (l<=15552) & (r>=15552) : ans = ans + 1
if (l<=16384) & (r>=16384) : ans = ans + 1
if (l<=17496) & (r>=17496) : ans = ans + 1
if (l<=18432) & (r>=18432) : ans = ans + 1
if (l<=19683) & (r>=19683) : ans = ans + 1
if (l<=20736) & (r>=20736) : ans = ans + 1
if (l<=23328) & (r>=23328) : ans = ans + 1
if (l<=24576) & (r>=24576) : ans = ans + 1
if (l<=26244) & (r>=26244) : ans = ans + 1
if (l<=27648) & (r>=27648) : ans = ans + 1
if (l<=31104) & (r>=31104) : ans = ans + 1
if (l<=32768) & (r>=32768) : ans = ans + 1
if (l<=34992) & (r>=34992) : ans = ans + 1
if (l<=36864) & (r>=36864) : ans = ans + 1
if (l<=39366) & (r>=39366) : ans = ans + 1
if (l<=41472) & (r>=41472) : ans = ans + 1
if (l<=46656) & (r>=46656) : ans = ans + 1
if (l<=49152) & (r>=49152) : ans = ans + 1
if (l<=52488) & (r>=52488) : ans = ans + 1
if (l<=55296) & (r>=55296) : ans = ans + 1
if (l<=59049) & (r>=59049) : ans = ans + 1
if (l<=62208) & (r>=62208) : ans = ans + 1
if (l<=65536) & (r>=65536) : ans = ans + 1
if (l<=69984) & (r>=69984) : ans = ans + 1
if (l<=73728) & (r>=73728) : ans = ans + 1
if (l<=78732) & (r>=78732) : ans = ans + 1
if (l<=82944) & (r>=82944) : ans = ans + 1
if (l<=93312) & (r>=93312) : ans = ans + 1
if (l<=98304) & (r>=98304) : ans = ans + 1
if (l<=104976) & (r>=104976) : ans = ans + 1
if (l<=110592) & (r>=110592) : ans = ans + 1
if (l<=118098) & (r>=118098) : ans = ans + 1
if (l<=124416) & (r>=124416) : ans = ans + 1
if (l<=131072) & (r>=131072) : ans = ans + 1
if (l<=139968) & (r>=139968) : ans = ans + 1
if (l<=147456) & (r>=147456) : ans = ans + 1
if (l<=157464) & (r>=157464) : ans = ans + 1
if (l<=165888) & (r>=165888) : ans = ans + 1
if (l<=177147) & (r>=177147) : ans = ans + 1
if (l<=186624) & (r>=186624) : ans = ans + 1
if (l<=196608) & (r>=196608) : ans = ans + 1
if (l<=209952) & (r>=209952) : ans = ans + 1
if (l<=221184) & (r>=221184) : ans = ans + 1
if (l<=236196) & (r>=236196) : ans = ans + 1
if (l<=248832) & (r>=248832) : ans = ans + 1
if (l<=262144) & (r>=262144) : ans = ans + 1
if (l<=279936) & (r>=279936) : ans = ans + 1
if (l<=294912) & (r>=294912) : ans = ans + 1
if (l<=314928) & (r>=314928) : ans = ans + 1
if (l<=331776) & (r>=331776) : ans = ans + 1
if (l<=354294) & (r>=354294) : ans = ans + 1
if (l<=373248) & (r>=373248) : ans = ans + 1
if (l<=393216) & (r>=393216) : ans = ans + 1
if (l<=419904) & (r>=419904) : ans = ans + 1
if (l<=442368) & (r>=442368) : ans = ans + 1
if (l<=472392) & (r>=472392) : ans = ans + 1
if (l<=497664) & (r>=497664) : ans = ans + 1
if (l<=524288) & (r>=524288) : ans = ans + 1
if (l<=531441) & (r>=531441) : ans = ans + 1
if (l<=559872) & (r>=559872) : ans = ans + 1
if (l<=589824) & (r>=589824) : ans = ans + 1
if (l<=629856) & (r>=629856) : ans = ans + 1
if (l<=663552) & (r>=663552) : ans = ans + 1
if (l<=708588) & (r>=708588) : ans = ans + 1
if (l<=746496) & (r>=746496) : ans = ans + 1
if (l<=786432) & (r>=786432) : ans = ans + 1
if (l<=839808) & (r>=839808) : ans = ans + 1
if (l<=884736) & (r>=884736) : ans = ans + 1
if (l<=944784) & (r>=944784) : ans = ans + 1
if (l<=995328) & (r>=995328) : ans = ans + 1
if (l<=1048576) & (r>=1048576) : ans = ans + 1
if (l<=1062882) & (r>=1062882) : ans = ans + 1
if (l<=1119744) & (r>=1119744) : ans = ans + 1
if (l<=1179648) & (r>=1179648) : ans = ans + 1
if (l<=1259712) & (r>=1259712) : ans = ans + 1
if (l<=1327104) & (r>=1327104) : ans = ans + 1
if (l<=1417176) & (r>=1417176) : ans = ans + 1
if (l<=1492992) & (r>=1492992) : ans = ans + 1
if (l<=1572864) & (r>=1572864) : ans = ans + 1
if (l<=1594323) & (r>=1594323) : ans = ans + 1
if (l<=1679616) & (r>=1679616) : ans = ans + 1
if (l<=1769472) & (r>=1769472) : ans = ans + 1
if (l<=1889568) & (r>=1889568) : ans = ans + 1
if (l<=1990656) & (r>=1990656) : ans = ans + 1
if (l<=2097152) & (r>=2097152) : ans = ans + 1
if (l<=2125764) & (r>=2125764) : ans = ans + 1
if (l<=2239488) & (r>=2239488) : ans = ans + 1
if (l<=2359296) & (r>=2359296) : ans = ans + 1
if (l<=2519424) & (r>=2519424) : ans = ans + 1
if (l<=2654208) & (r>=2654208) : ans = ans + 1
if (l<=2834352) & (r>=2834352) : ans = ans + 1
if (l<=2985984) & (r>=2985984) : ans = ans + 1
if (l<=3145728) & (r>=3145728) : ans = ans + 1
if (l<=3188646) & (r>=3188646) : ans = ans + 1
if (l<=3359232) & (r>=3359232) : ans = ans + 1
if (l<=3538944) & (r>=3538944) : ans = ans + 1
if (l<=3779136) & (r>=3779136) : ans = ans + 1
if (l<=3981312) & (r>=3981312) : ans = ans + 1
if (l<=4194304) & (r>=4194304) : ans = ans + 1
if (l<=4251528) & (r>=4251528) : ans = ans + 1
if (l<=4478976) & (r>=4478976) : ans = ans + 1
if (l<=4718592) & (r>=4718592) : ans = ans + 1
if (l<=4782969) & (r>=4782969) : ans = ans + 1
if (l<=5038848) & (r>=5038848) : ans = ans + 1
if (l<=5308416) & (r>=5308416) : ans = ans + 1
if (l<=5668704) & (r>=5668704) : ans = ans + 1
if (l<=5971968) & (r>=5971968) : ans = ans + 1
if (l<=6291456) & (r>=6291456) : ans = ans + 1
if (l<=6377292) & (r>=6377292) : ans = ans + 1
if (l<=6718464) & (r>=6718464) : ans = ans + 1
if (l<=7077888) & (r>=7077888) : ans = ans + 1
if (l<=7558272) & (r>=7558272) : ans = ans + 1
if (l<=7962624) & (r>=7962624) : ans = ans + 1
if (l<=8388608) & (r>=8388608) : ans = ans + 1
if (l<=8503056) & (r>=8503056) : ans = ans + 1
if (l<=8957952) & (r>=8957952) : ans = ans + 1
if (l<=9437184) & (r>=9437184) : ans = ans + 1
if (l<=9565938) & (r>=9565938) : ans = ans + 1
if (l<=10077696) & (r>=10077696) : ans = ans + 1
if (l<=10616832) & (r>=10616832) : ans = ans + 1
if (l<=11337408) & (r>=11337408) : ans = ans + 1
if (l<=11943936) & (r>=11943936) : ans = ans + 1
if (l<=12582912) & (r>=12582912) : ans = ans + 1
if (l<=12754584) & (r>=12754584) : ans = ans + 1
if (l<=13436928) & (r>=13436928) : ans = ans + 1
if (l<=14155776) & (r>=14155776) : ans = ans + 1
if (l<=14348907) & (r>=14348907) : ans = ans + 1
if (l<=15116544) & (r>=15116544) : ans = ans + 1
if (l<=15925248) & (r>=15925248) : ans = ans + 1
if (l<=16777216) & (r>=16777216) : ans = ans + 1
if (l<=17006112) & (r>=17006112) : ans = ans + 1
if (l<=17915904) & (r>=17915904) : ans = ans + 1
if (l<=18874368) & (r>=18874368) : ans = ans + 1
if (l<=19131876) & (r>=19131876) : ans = ans + 1
if (l<=20155392) & (r>=20155392) : ans = ans + 1
if (l<=21233664) & (r>=21233664) : ans = ans + 1
if (l<=22674816) & (r>=22674816) : ans = ans + 1
if (l<=23887872) & (r>=23887872) : ans = ans + 1
if (l<=25165824) & (r>=25165824) : ans = ans + 1
if (l<=25509168) & (r>=25509168) : ans = ans + 1
if (l<=26873856) & (r>=26873856) : ans = ans + 1
if (l<=28311552) & (r>=28311552) : ans = ans + 1
if (l<=28697814) & (r>=28697814) : ans = ans + 1
if (l<=30233088) & (r>=30233088) : ans = ans + 1
if (l<=31850496) & (r>=31850496) : ans = ans + 1
if (l<=33554432) & (r>=33554432) : ans = ans + 1
if (l<=34012224) & (r>=34012224) : ans = ans + 1
if (l<=35831808) & (r>=35831808) : ans = ans + 1
if (l<=37748736) & (r>=37748736) : ans = ans + 1
if (l<=38263752) & (r>=38263752) : ans = ans + 1
if (l<=40310784) & (r>=40310784) : ans = ans + 1
if (l<=42467328) & (r>=42467328) : ans = ans + 1
if (l<=43046721) & (r>=43046721) : ans = ans + 1
if (l<=45349632) & (r>=45349632) : ans = ans + 1
if (l<=47775744) & (r>=47775744) : ans = ans + 1
if (l<=50331648) & (r>=50331648) : ans = ans + 1
if (l<=51018336) & (r>=51018336) : ans = ans + 1
if (l<=53747712) & (r>=53747712) : ans = ans + 1
if (l<=56623104) & (r>=56623104) : ans = ans + 1
if (l<=57395628) & (r>=57395628) : ans = ans + 1
if (l<=60466176) & (r>=60466176) : ans = ans + 1
if (l<=63700992) & (r>=63700992) : ans = ans + 1
if (l<=67108864) & (r>=67108864) : ans = ans + 1
if (l<=68024448) & (r>=68024448) : ans = ans + 1
if (l<=71663616) & (r>=71663616) : ans = ans + 1
if (l<=75497472) & (r>=75497472) : ans = ans + 1
if (l<=76527504) & (r>=76527504) : ans = ans + 1
if (l<=80621568) & (r>=80621568) : ans = ans + 1
if (l<=84934656) & (r>=84934656) : ans = ans + 1
if (l<=86093442) & (r>=86093442) : ans = ans + 1
if (l<=90699264) & (r>=90699264) : ans = ans + 1
if (l<=95551488) & (r>=95551488) : ans = ans + 1
if (l<=100663296) & (r>=100663296) : ans = ans + 1
if (l<=102036672) & (r>=102036672) : ans = ans + 1
if (l<=107495424) & (r>=107495424) : ans = ans + 1
if (l<=113246208) & (r>=113246208) : ans = ans + 1
if (l<=114791256) & (r>=114791256) : ans = ans + 1
if (l<=120932352) & (r>=120932352) : ans = ans + 1
if (l<=127401984) & (r>=127401984) : ans = ans + 1
if (l<=129140163) & (r>=129140163) : ans = ans + 1
if (l<=134217728) & (r>=134217728) : ans = ans + 1
if (l<=136048896) & (r>=136048896) : ans = ans + 1
if (l<=143327232) & (r>=143327232) : ans = ans + 1
if (l<=150994944) & (r>=150994944) : ans = ans + 1
if (l<=153055008) & (r>=153055008) : ans = ans + 1
if (l<=161243136) & (r>=161243136) : ans = ans + 1
if (l<=169869312) & (r>=169869312) : ans = ans + 1
if (l<=172186884) & (r>=172186884) : ans = ans + 1
if (l<=181398528) & (r>=181398528) : ans = ans + 1
if (l<=191102976) & (r>=191102976) : ans = ans + 1
if (l<=201326592) & (r>=201326592) : ans = ans + 1
if (l<=204073344) & (r>=204073344) : ans = ans + 1
if (l<=214990848) & (r>=214990848) : ans = ans + 1
if (l<=226492416) & (r>=226492416) : ans = ans + 1
if (l<=229582512) & (r>=229582512) : ans = ans + 1
if (l<=241864704) & (r>=241864704) : ans = ans + 1
if (l<=254803968) & (r>=254803968) : ans = ans + 1
if (l<=258280326) & (r>=258280326) : ans = ans + 1
if (l<=268435456) & (r>=268435456) : ans = ans + 1
if (l<=272097792) & (r>=272097792) : ans = ans + 1
if (l<=286654464) & (r>=286654464) : ans = ans + 1
if (l<=301989888) & (r>=301989888) : ans = ans + 1
if (l<=306110016) & (r>=306110016) : ans = ans + 1
if (l<=322486272) & (r>=322486272) : ans = ans + 1
if (l<=339738624) & (r>=339738624) : ans = ans + 1
if (l<=344373768) & (r>=344373768) : ans = ans + 1
if (l<=362797056) & (r>=362797056) : ans = ans + 1
if (l<=382205952) & (r>=382205952) : ans = ans + 1
if (l<=387420489) & (r>=387420489) : ans = ans + 1
if (l<=402653184) & (r>=402653184) : ans = ans + 1
if (l<=408146688) & (r>=408146688) : ans = ans + 1
if (l<=429981696) & (r>=429981696) : ans = ans + 1
if (l<=452984832) & (r>=452984832) : ans = ans + 1
if (l<=459165024) & (r>=459165024) : ans = ans + 1
if (l<=483729408) & (r>=483729408) : ans = ans + 1
if (l<=509607936) & (r>=509607936) : ans = ans + 1
if (l<=516560652) & (r>=516560652) : ans = ans + 1
if (l<=536870912) & (r>=536870912) : ans = ans + 1
if (l<=544195584) & (r>=544195584) : ans = ans + 1
if (l<=573308928) & (r>=573308928) : ans = ans + 1
if (l<=603979776) & (r>=603979776) : ans = ans + 1
if (l<=612220032) & (r>=612220032) : ans = ans + 1
if (l<=644972544) & (r>=644972544) : ans = ans + 1
if (l<=679477248) & (r>=679477248) : ans = ans + 1
if (l<=688747536) & (r>=688747536) : ans = ans + 1
if (l<=725594112) & (r>=725594112) : ans = ans + 1
if (l<=764411904) & (r>=764411904) : ans = ans + 1
if (l<=774840978) & (r>=774840978) : ans = ans + 1
if (l<=805306368) & (r>=805306368) : ans = ans + 1
if (l<=816293376) & (r>=816293376) : ans = ans + 1
if (l<=859963392) & (r>=859963392) : ans = ans + 1
if (l<=905969664) & (r>=905969664) : ans = ans + 1
if (l<=918330048) & (r>=918330048) : ans = ans + 1
if (l<=967458816) & (r>=967458816) : ans = ans + 1
if (l<=1019215872) & (r>=1019215872) : ans = ans + 1
if (l<=1033121304) & (r>=1033121304) : ans = ans + 1
if (l<=1073741824) & (r>=1073741824) : ans = ans + 1
if (l<=1088391168) & (r>=1088391168) : ans = ans + 1
if (l<=1146617856) & (r>=1146617856) : ans = ans + 1
if (l<=1162261467) & (r>=1162261467) : ans = ans + 1
if (l<=1207959552) & (r>=1207959552) : ans = ans + 1
if (l<=1224440064) & (r>=1224440064) : ans = ans + 1
if (l<=1289945088) & (r>=1289945088) : ans = ans + 1
if (l<=1358954496) & (r>=1358954496) : ans = ans + 1
if (l<=1377495072) & (r>=1377495072) : ans = ans + 1
if (l<=1451188224) & (r>=1451188224) : ans = ans + 1
if (l<=1528823808) & (r>=1528823808) : ans = ans + 1
if (l<=1549681956) & (r>=1549681956) : ans = ans + 1
if (l<=1610612736) & (r>=1610612736) : ans = ans + 1
if (l<=1632586752) & (r>=1632586752) : ans = ans + 1
if (l<=1719926784) & (r>=1719926784) : ans = ans + 1
if (l<=1811939328) & (r>=1811939328) : ans = ans + 1
if (l<=1836660096) & (r>=1836660096) : ans = ans + 1
if (l<=1934917632) & (r>=1934917632) : ans = ans + 1
print(ans)
```
Yes
| 98,362 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
import math
l,r = map(int,input().split())
a,b = math.log(2),math.log(3)
t = math.log(r)
z = math.log(r)
i = 1
toe = 0
while (t>=0):
toe += int(t/b)+1
t = z - i*a
i += 1
if l == 1:
print(toe)
else:
t = math.log(l-1)
z = math.log(l-1)
i = 1
toi = 0
while (t>=0):
y = t/b
toi += int(t/b)+1
t = z - i*a
i += 1
print(toe-toi)
```
No
| 98,363 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
l, r = map(int, input().split())
print(sum(l <= 2 ** i * 3 ** j <= r for i in range(29) for j in range(20)))
```
No
| 98,364 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
import math
l,r = map(int,input().split())
a,b = math.log(2),math.log(3)
if l == 1:
t,z = math.log(r), math.log(r)
i,toe = 1,0
while (t>=0):
toe += int(t/b)+1
t = z - i*a
i += 1
print(toe)
else:
t,z = math.log(l-1), math.log(l-1)
i,toi = 1,0
while (t>=0):
toi += int(t/b)+1
t = z - i*a
i += 1
print(toe-toi)
```
No
| 98,365 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer is called a 2-3-integer, if it is equal to 2xΒ·3y for some non-negative integers x and y. In other words, these integers are such integers that only have 2 and 3 among their prime divisors. For example, integers 1, 6, 9, 16 and 108 β are 2-3 integers, while 5, 10, 21 and 120 are not.
Print the number of 2-3-integers on the given segment [l, r], i. e. the number of sich 2-3-integers t that l β€ t β€ r.
Input
The only line contains two integers l and r (1 β€ l β€ r β€ 2Β·109).
Output
Print a single integer the number of 2-3-integers on the segment [l, r].
Examples
Input
1 10
Output
7
Input
100 200
Output
5
Input
1 2000000000
Output
326
Note
In the first example the 2-3-integers are 1, 2, 3, 4, 6, 8 and 9.
In the second example the 2-3-integers are 108, 128, 144, 162 and 192.
Submitted Solution:
```
l, r = input().split()
l = (int(l))
r = (int(r))
a = set()
sc = 0
for i in range (0, 30):
cur = 2**i
for j in range (0, 20):
if cur > r:
break
if cur >= l:
a.add(cur)
cur *= 3
print(len(a))
```
No
| 98,366 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
s=input()
res = 0
l = len(s)
for i in range(l):
for j in range(i + 1, l+1):
t = s[i:j]
tt = t[::-1]
if t != tt:
res = max(res, j - i)
print(res)
```
| 98,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
### @author egaeus
### @mail jsbeltran.valhalla@gmail.com
### @veredict
### @url https://codeforces.com/problemset/problem/981/A
### @category strings
### @date 02/12/2019
s = input()
res = 0
for i in range(len(s)):
for j in range(i+1, len(s) + 1):
ws = True
for k in range(0, j - i):
if s[i+k] != s[j-k-1]:
ws = False
if not ws:
res = max(res, j - i)
print(res)
```
| 98,368 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
n = input()
s = {x for x in n}
r = n[::-1]
print(0) if len(s) == 1 else print(len(n)-1) if r == n else print(len(n))
```
| 98,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
def reverse(str):
new_str = ''
for i in range(len(str)-1, -1, -1):
new_str += str[i]
return new_str
def is_palindrome(str):
return str == reverse(str)
s = input()
if is_palindrome(s):
if is_palindrome(s[:len(s)-1]):
print(0)
else:
print(len(s)-1)
else:
print(len(s))
```
| 98,370 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
s = input()
if len(set(s))==1:
print(0)
elif s==s[::-1]:
print(len(s)-1)
else:
print(len(s))
```
| 98,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
s = input()
t = s[::-1]
if s == t:
if len(set(s)) == 1:
print("0")
else:
print(len(s)-1)
else:
print(len(s))
```
| 98,372 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
def is_palindrome(word):
return word == word[::-1]
word = input()
sem_palindromo = True
for i in range(len(word)):
temp = word[i:]
temp2 = word[:len(word)-i]
temp3 = word[i:len(word)-i]
if not is_palindrome(temp):
print(len(temp))
sem_palindromo = False
break
elif not is_palindrome(temp2):
print(len(temp2))
sem_palindromo = False
break
elif not is_palindrome(temp3):
print(len(temp3))
sem_palindromo = False
break
if sem_palindromo:
print(0)
```
| 98,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Tags: brute force, implementation, strings
Correct Solution:
```
import sys
import math
import bisect
import itertools
import random
def main():
s = input()
if len(set(list(s))) == 1:
print(0)
elif s == s[::-1]:
print(len(s)-1)
else:
print(len(s))
if __name__ == "__main__":
main()
```
| 98,374 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
x = input()
y = x[::-1]
def asd(x):
a = x
b = x[::-1]
if a == b and x!= "":
x = x[:len(x) - 1]
asd(x)
elif a!= b or x == "":
print(len(x))
asd(x)
```
Yes
| 98,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
def reverse(s):
return s[::-1]
def pal(s):
rev = reverse(s)
if (s == rev):
return 1
return 0
str = input()
l = len(str)
c=0
for i in range(0,l//2+1):
sub = str[i:]
if pal(sub)==0:
print(len(sub))
c=1
break
if c==0:
print(0)
```
Yes
| 98,376 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 9 21:00:18 2020
@author: MridulSachdeva
"""
s = input()
def is_palindrome(x):
n = len(x) // 2
for i in range(n):
if x[i] != x[-i-1]:
return False
return True
if len(set(s)) == 1:
print(0)
elif is_palindrome(s):
print(len(s) - 1)
else:
print(len(s))
```
Yes
| 98,377 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
s=input()
if s!=s[::-1]:
ans=len(s)
elif len(set(s))<2:
ans=0
else: ans=len(s)-1
print(ans)
```
Yes
| 98,378 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
def reverse(string):
string1 = string[::-1]
return string1
n = 0
fl = 0
string = input()
s1 = reverse(string)
if s1 == string:
for e in string:
for j in s1:
if e == j:
n=n+1
if n==len(string):
break
if n==len(string) and e==string[0]:
print("0")
else:
y = len(string)-1
print(y)
else:
print(len(string))
```
No
| 98,379 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
string = input()
for i in range(len(string)):
for j in range(len(string),i-1,-1):
# print(string[i:j])
# print(string[j-len(string)-1:-len(string)+i-1:-1])
if string[i:j] == string[j-len(string)-1:-len(string)+i-1:-1]:
continue
else:
print(len(string[i:j]))
exit()
```
No
| 98,380 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
s=input()
n=len(s)
def ap(s):
for i in range(n//2):
if(s[i]!=s[n-i-1]):
return False
return True
if(ap(s)):
print(n-1)
elif(s.count(s[0])==len(s)):
print('0')
else:
print(n)
```
No
| 98,381 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l β¦ r] (1 β€ l β€ r β€ |s|) of a string s = s_{1}s_{2} β¦ s_{|s|} is the string s_{l}s_{l + 1} β¦ s_{r}.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word s is changed into its longest substring that is not a palindrome. If all the substrings of s are palindromes, she skips the word at all.
Some time ago Ann read the word s. What is the word she changed it into?
Input
The first line contains a non-empty string s with length at most 50 characters, containing lowercase English letters only.
Output
If there is such a substring in s that is not a palindrome, print the maximum length of such a substring. Otherwise print 0.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
Input
mew
Output
3
Input
wuffuw
Output
5
Input
qqqqqqqq
Output
0
Note
"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is 3.
The string "uffuw" is one of the longest non-palindrome substrings (of length 5) of the string "wuffuw", so the answer for the second example is 5.
All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is 0.
Submitted Solution:
```
import sys
s=input()
left=0
right=len(s)-1
temp=0
check=0
flag=0
while left<int(len(s)/2):
if left+1==len(s)-right and s[left]!=s[right]:
print(len(s))
sys.exit(0)
if s[left]!=s[right] and check == 0:
if len(s)-right>left+1:
temp=right+1
check=1
else:
temp=len(s)-left
check=1
if int(len(s))%2!=0 and left ==int(len(s)/2)-1 and right == int(len(s)/2)+1 and check==0:
if s[left]==s[right] and s[int(len(s)/2)]!=s[left]:
print(len(s)-1)
sys.exit(0)
if flag==0:
left+=1
else:
right-=1
flag=~flag
print(temp)
```
No
| 98,382 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
n, x, m = map(int, input().split())
g = x * 1
ans = x
arr = []
res = set([])
loop = (0, 0)
k = 0
for i in range(m + 1):
tmp = x ** 2 % m
if tmp in res:
loop = (i, tmp)
break
res.add(tmp)
arr.append(tmp)
ans += tmp
x = tmp
for i, y in enumerate(arr):
if y == loop[1]:
k = i
break
ini = g + sum(arr[:k])
mul = ans - ini
t, v = divmod(n - k - 1, loop[0] - k)
print(ini + mul * t + sum(arr[k:k + v]))
```
| 98,383 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
n, x, m = map(int, input().split())
s = set([x])
t = [x]
p = 1
for i in range(n):
x = x * x % m
if x in s:
break
else:
s.add(x)
t.append(x)
p += 1
if p == n:
print(sum(s))
exit()
q = t.index(x)
l = p - q
b = sum(t[q:p])
ans = sum(t[:q])
n -= q
ans += n // l * b + sum(t[q:q + n % l])
print(ans)
```
| 98,384 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
n, x, m = map(int, input().split())
mn = min(n, m)
P = [] # pre_sum
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check & pre_len
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
cyc = (sum_p - P[X[x]]) * ((n - X[x]) // cyc_len)
remain = P[X[x] + (n - X[x]) % cyc_len]
print(cyc + remain)
exit()
P.append(sum_p)
sum_p += x
X[x] = i
x = x*x % m
print(sum_p)
```
| 98,385 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
def resolve():
n,x,m = map(int,input().split())
ans = x
y = [x]
for i in range(n-1):
x = (x**2)%m
if x not in y:
y.append(x)
else:
break
b = y.index(x)
N = len(y[b:])
c = sum(y[b:])
ans = sum(y[:b]) + c*((n-b)//N)+sum(y[b:b+(n-b)%N])
print(ans)
resolve()
```
| 98,386 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
N, X, M = map(int, input().split())
ans = X
ALL_cal = [False] * M
ALL = []
rou = False
for i in range(N-1):
X = pow(X, 2, M)
if ALL_cal[X]:
num = ALL_cal[X]
now = i
rou = True
break
ALL.append(X)
ALL_cal[X] = i
ans += X
if rou :
roupe = now - num
nokori = N - now - 1
print(sum(ALL[num:])*(nokori//roupe) + ans + sum(ALL[num:num + nokori%roupe]))
else:
print(ans)
```
| 98,387 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
N,X,M=map(int,input().split())
seen=[-2]*M
seen[X]
A=[X]
i=1
while(i<N):
T=A[-1]**2%M
if seen[T]!=-2:
Roop=i-seen[T]
Left,Right=seen[T],i
break
A.append(T)
seen[T]=i
i+=1
if i==N:
print(sum(A))
exit()
Roopsum=0
for i in range(Left,Right):
Roopsum+=A[i]
Rest=N-len(A)
ans=sum(A)
ans+=Rest//Roop*Roopsum
for i in range(Rest%Roop):
ans+=T
T=T**2%M
print(ans)
```
| 98,388 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
N, X, M = map(int, input().split())
nxt = X
lst = []
dic = {}
for i in range(M + 1):
if nxt in dic:
loop_st = dic[nxt]
loop_ed = i - 1
break
lst.append(nxt)
dic[nxt] = i
nxt = (nxt ** 2) % M
v = N - loop_st
q, r = divmod(v, loop_ed - loop_st + 1)
pre_sum = sum(lst[:loop_st])
loop_sum = q * sum(lst[loop_st:])
post_sum = sum(lst[loop_st:loop_st + r])
print(pre_sum + loop_sum + post_sum)
```
| 98,389 |
Provide a correct Python 3 solution for this coding contest problem.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
"Correct Solution:
```
n, x, m = map(int, input().split())
ans = []
c = [0]*m
flag = False
for i in range(n):
if c[x] == 1:
flag = True
break
ans.append(x)
c[x] = 1
x = x**2 % m
if flag:
p = ans.index(x)
l = len(ans) - p
d, e = divmod(n-p, l)
print(sum(ans[:p]) + d*sum(ans[p:]) + sum(ans[p:p+e]))
else:
print(sum(ans))
```
| 98,390 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
n, x, m = map(int, input().split())
lst, num, flag = set(), [], False
for i in range(1, n + 1):
lst.add(x), num.append(x)
x = x ** 2 % m
if x in lst:
flag = True
break
ans = sum(num)
if flag:
cnt, idx = i, num.index(x)
div, mod = divmod(n - cnt, len(num) - idx)
ans += sum(num[idx:idx + mod])
ans += sum(num[idx:]) * div
print(ans)
```
Yes
| 98,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
N, X, M = list(map(int, input().split()))
i = 1
a = X
s = 0
L = []
while i < N and a > 0:
L.append(a)
s += a
a = (a**2) % M
if a in L:
j = L.index(a)
break
i += 1
else:
s += a
print(s)
exit()
# print(L)
t = sum(L[:j])
L = L[j:]
u = s-t
N = N-j
v = t+u*(N//(i-j))
if N % (i-j) == 0:
pass
else:
v += sum(L[:N % (i-j)])
print(v)
```
Yes
| 98,392 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
n,x,m=map(int,input().split())
s=x
t={x}
u=[x]
for i in range(1,n):
x=pow(x,2,m)
if x==0:break
if x==1:
s+=n-i
break
if x in t:
j=u.index(x)
s=sum(u[:j])
u=u[j:]
s+=(n-j)//len(u)*sum(u)
s+=sum(u[:(n-j)%len(u)])
break
s+=x
t|={x}
u+=x,
print(s)
```
Yes
| 98,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
N, X, M = map(int,input().split())
def cal(x):
return pow(x,2,M)
memo = set()
lis = []
while X not in memo:
memo.add(X)
lis.append(X)
X = cal(X)
pos = lis.index(X)
if pos >= N:
print(sum(lis[:N]))
else:
print(sum(lis[:pos]) + ((N-pos)//(len(memo)-pos)) * sum(lis[pos:]) + sum(lis[pos:(N-pos)%(len(memo)-pos)+pos]))
```
Yes
| 98,394 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
while True:
try:
n,x,m = map(int,input().split())
#print(n,x,m)
prev = x
ans = x
for i in range(1,n):
ans += (prev**2)%m
prev = (prev**2)%m
print(ans)
except:
break
```
No
| 98,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
from collections import defaultdict
N, X, M = map(int, input().split())
A = [X]
visited = set()
visited.add(X)
idx = defaultdict()
idx[X] = 0
iii = -1
for i in range(1, M):
tmp = (A[-1]**2) % M
if tmp not in visited:
A.append(tmp)
visited.add(tmp)
idx[tmp] = i
else:
iii = idx[tmp]
print(A)
print(len(A))
print(iii)
ans = 0
ans += sum(A[:iii])
N -= iii
l = len(A) - iii
ans += (N // l) * sum(A[iii:])
N -= N // l * l
ans += sum(A[iii:iii + N])
print(ans)
exit()
if A[-1] == 0:
print(sum(A))
exit()
else:
l = len(A)
ans = 0
if N % l == 0:
ans += sum(A) * (N // l)
else:
ans += sum(A) * (N // l)
rest = N % l
if iii == -1:
ans += sum(A[:rest])
else:
ans += sum(A[i:iii + rest])
print(ans)
```
No
| 98,396 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
n, x, m = map(int, input().split())
mn = min(n, m)
S = set()
A = []
sum_9 = 0 # sum of pre + cycle
for len_9 in range(mn):
if x in S: break
S.add(x)
A.append(x)
sum_9 += x
x = x*x % m
pre_len = A.index(x)
cyc_len = len(A) - pre_len
nxt_len = (n - pre_len) % cyc_len
cyc_num = (n - pre_len) // cyc_len
pre = sum(A[:pre_len])
cyc = sum_9 - pre
nxt = sum(A[pre_len: pre_len + nxt_len])
print(pre + cyc * cyc_num + nxt)
```
No
| 98,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let us denote by f(x, m) the remainder of the Euclidean division of x by m.
Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.
Constraints
* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X M
Output
Print \displaystyle{\sum_{i=1}^N A_i}.
Examples
Input
6 2 1001
Output
1369
Input
1000 2 16
Output
6
Input
10000000000 10 99959
Output
492443256176507
Submitted Solution:
```
n,x,m=map(int,input().split())
l=[0]*m
s=[0]*m
t=p=0
while l[x]<1:
l[x]=t
s[x]=s[p]+x
p=x
x=pow(p,2,m)
t+=1
T=t-l[x]
S=s[p]+x-s[x]
if n<t:
print(s[l.index(n-1)])
else:
print(S*((n-l[x])//T)+s[l.index(l[x]+(n-l[x])%T-1)])
```
No
| 98,398 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi wants to be a member of some web service.
He tried to register himself with the ID S, which turned out to be already used by another user.
Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.
He is now trying to register with the ID T. Determine whether this string satisfies the property above.
Constraints
* S and T are strings consisting of lowercase English letters.
* 1 \leq |S| \leq 10
* |T| = |S| + 1
Input
Input is given from Standard Input in the following format:
S
T
Output
If T satisfies the property in Problem Statement, print `Yes`; otherwise, print `No`.
Examples
Input
chokudai
chokudaiz
Output
Yes
Input
snuke
snekee
Output
No
Input
a
aa
Output
Yes
"Correct Solution:
```
s = input()
t = input()
print('Yes' if s == t[:len(s)] else 'No')
```
| 98,399 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.