message stringlengths 2 57.2k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 61 108k | cluster float64 22 22 | __index_level_0__ int64 122 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6). | instruction | 0 | 1,731 | 22 | 3,462 |
Tags: math, matrices, number theory, two pointers
Correct Solution:
```
from collections import Counter
def pr(a):return (a*(a-1))//2
n,p,k=map(int,input().split())
a=map(int,input().split())
a=Counter([(x**4-x*k+p)%p for x in a])
print(sum(pr(a[x]) for x in a))
``` | output | 1 | 1,731 | 22 | 3,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6). | instruction | 0 | 1,732 | 22 | 3,464 |
Tags: math, matrices, number theory, two pointers
Correct Solution:
```
from sys import stdin
n,p,k=map(int,stdin.readline().strip().split())
s=list(map(int,stdin.readline().strip().split()))
st=set()
d=dict()
for i in s:
x=(i**4-i*k)%p
if x in st:
d[x]+=1
else:
st.add(x)
d[x]=1
s.sort()
ans=0
for i in s:
x=(i**4-i*k)%p
if x in st:
ans+=(d[x]-1)*d[x]//2
d[x]=0
print(ans)
``` | output | 1 | 1,732 | 22 | 3,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6). | instruction | 0 | 1,733 | 22 | 3,466 |
Tags: math, matrices, number theory, two pointers
Correct Solution:
```
n,p,k=map(int,input().split())
arr=list(map(int,input().split()))
dict1={}
for i in range(n):
val=((pow(arr[i],4,p)-(k*arr[i])%p)+p)%p
try:
dict1[val]+=1
except:
KeyError
dict1[val]=1
ans=0
for i in dict1.keys():
ans+=(dict1[i]*(dict1[i]-1))//2
print(ans)
``` | output | 1 | 1,733 | 22 | 3,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
from collections import defaultdict as dc
n,p,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in range(n):
x=((a[i]**2)%p)**2%p-(k*a[i])%p
if(x<0):
b.append(x+p)
else:
b.append(x)
d=dc(int)
for i in b:
d[i]+=1
def c(n):
return (n*(n-1))//2
cnt=0
for i in list(d.values()):
cnt+=c(i)
print(cnt)
``` | instruction | 0 | 1,734 | 22 | 3,468 |
Yes | output | 1 | 1,734 | 22 | 3,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
cond = [int(x) for x in input().split()]
n = cond[0]
p = cond[1]
k = cond[2]
a = [int(x) for x in input().split()]
b = [(x**4-k*x)%p for x in a]
d = {}
r = 0
for x in b:
if x not in d:
d[x] = 0
r += d[x]
d[x] += 1
print(r)
``` | instruction | 0 | 1,735 | 22 | 3,470 |
Yes | output | 1 | 1,735 | 22 | 3,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
"""This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead."""
from collections import Counter
n, p, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [(x ** 4) % p for x in a]
c = [(x * k) % p for x in a]
d = [(b[i] - c[i] + p) % p for i in range(n)]
f = Counter(d)
s = 0
for _, e in f.items():
s += (e * (e - 1)) // 2
print(s)
``` | instruction | 0 | 1,736 | 22 | 3,472 |
Yes | output | 1 | 1,736 | 22 | 3,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
n,p,k = map(int,input().split())
l = list(map(int,input().split()))
d = dict()
for y in range(n):
x = pow(l[y],4) - l[y]*(k)
if(x%p not in d):
d[x%p] = 1
else:
d[x%p] += 1
ct = 0
#print(d)
for k,v in d.items():
ct += v*(v-1)//2
print(ct)
``` | instruction | 0 | 1,737 | 22 | 3,474 |
Yes | output | 1 | 1,737 | 22 | 3,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
# E. Count Pairs from Codeforces
n,p,k=input().split()
n=int(n)
p=int(p)
k=int(k)
c=0
a=list(map(int,input().split()))
for i in range(n) :
for j in range(i,n) :
if (((a[i]+a[j])*((a[i]*a[i])+(a[j]*a[j]))-k)%p) == 0:
c+=1
print(c)
``` | instruction | 0 | 1,738 | 22 | 3,476 |
No | output | 1 | 1,738 | 22 | 3,477 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
from collections import Counter
n, p, k = map(int, input().split())
a = list(map(int, input().split()))
freqs = Counter([(x ** 4 + k * x) % p for x in a])
ans = sum([(x - 1) * x // 2 for x in freqs.values()])
print(ans)
``` | instruction | 0 | 1,739 | 22 | 3,478 |
No | output | 1 | 1,739 | 22 | 3,479 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
#572_E
ln = [int(i) for i in input().split(" ")]
n = ln[0]
p = ln[1]
k = ln[2]
a = [int(i) for i in input().split(" ")]
cts = {}
ans = []
nm = 0
for i in range(0, len(a)):
an = ((a[i] ** 4) - (k * a[i])) % p
if an in cts:
cts[an] *= 2
nm += cts[an] // 2
else:
cts[an] = 1
print(nm)
``` | instruction | 0 | 1,740 | 22 | 3,480 |
No | output | 1 | 1,740 | 22 | 3,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be prime.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ p-1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(0^2 + 1^2) = 1 ≡ 1 mod 3.
(0+2)(0^2 + 2^2) = 8 ≡ 2 mod 3.
(1+2)(1^2 + 2^2) = 15 ≡ 0 mod 3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1, 5), (2, 3), (4, 6).
Submitted Solution:
```
import sys
n, p, k = list(map(int, sys.stdin.readline().strip().split()))
a = list(map(int, sys.stdin.readline().strip().split()))
for i in range (0, n):
a[i] = (a[i] ** 4 - k * a[i]) % p
a[i] = a[i] + 2 * p
a[i] = a[i] % p
a.sort()
print(a)
c = 1
x = a[0]
ans = 0
for i in range (1, n):
if a[i] == x:
c = c + 1
else:
ans = ans + (c * (c-1) // 2)
c = 1
x = a[i]
ans = ans + (c * (c-1) // 2)
print(ans)
``` | instruction | 0 | 1,741 | 22 | 3,482 |
No | output | 1 | 1,741 | 22 | 3,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,758 | 22 | 3,516 |
Tags: hashing, math, number theory
Correct Solution:
```
import sys
input=sys.stdin.readline
from collections import defaultdict
n,k=map(int,input().split())
a=list(map(int,input().split()))
dd=defaultdict(int)
ans=0
for aa in a:
primes=[]
b=aa
for j in range(2,int(b**0.5)+1):
if b%j==0:
cnt=0
while b%j==0:
cnt+=1
b//=j
primes.append([j,cnt])
if b>1:
primes.append([b,1])
key1,key2=1,1
for p,c in primes:
if c%k!=0:
key1*=p**(c%k)
key2*=p**(k-c%k)
ans+=dd[key2]
dd[key1]+=1
print(ans)
``` | output | 1 | 1,758 | 22 | 3,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,759 | 22 | 3,518 |
Tags: hashing, math, number theory
Correct Solution:
```
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = {}
ans = 0
for el in a:
i = 2
a = []
b = []
while i*i <= el:
cnt = 0
while not(el%i):
el //= i
cnt += 1
if cnt%k:
a.append((i, cnt%k))
b.append((i, k-(cnt%k)))
i += 1
if el > 1:
a.append((el, 1))
b.append((el, k-1))
a = tuple(a)
b = tuple(b)
ans += d.get(b, 0)
d[a] = d.get(a, 0)+1
print(ans)
``` | output | 1 | 1,759 | 22 | 3,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,760 | 22 | 3,520 |
Tags: hashing, math, number theory
Correct Solution:
```
from collections import defaultdict
# The largest prime of that divides i where 2 <= i <= N
largerPrime = list(range(10**5+1))
def largestPrime(N = 10**5):
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
#[0, 1, 2, 3, 2, 5, 3, 7, 2, 3, 5, 11, 3, 13, 7, 5, 2, 17, 3, 19, 5, 7, 11, 23, 3, 5, 13, 3, 7, 29, 5]
for i in range(2,N+1):
if(largerPrime[i] == i):
for j in range(i*2,N+1,i):
largerPrime[j] = i
largestPrime()
# Requires largerPrime Array (Type math.largestPrime to Access the array)
def primeFactors(n, k): # N Log log N
primeFactors = defaultdict(int)
while(n!=1):
primeFactors[largerPrime[n]] += 1
primeFactors[largerPrime[n]] %= k
n = int(n/largerPrime[n])
return primeFactors
def negate(pair, k):
neg = []
for key in pair:
if(pair[key]):
neg.append((key, k - pair[key]))
neg.sort()
if(neg):
return neg
return 1
n, k = map(int,input().split())
arr = [int(x) for x in input().split()]
one = 0
ans = 0
d = defaultdict(int)
for i in arr:
fac = primeFactors(i, k)
x = negate(fac, k)
if(x == 1):
ans += one
one += 1
else:
x = tuple(x)
ans += d[x]
save = []
for key in fac:
if(fac[key]):
save.append((key, fac[key]))
save.sort()
d[tuple(save)] += 1
print(ans)
``` | output | 1 | 1,760 | 22 | 3,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,761 | 22 | 3,522 |
Tags: hashing, math, number theory
Correct Solution:
```
from collections import defaultdict
n, k = map(int, input().split())
a = list(map(int, input().split()))
n = int(max(a)**.5)
mark = [True]*(n+1)
primes = []
for i in range(2,n+1):
if mark[i]:
primes.append(i)
for j in range(i, n+1, i): mark[j] = False
d = defaultdict(int)
ans = 0
for i in a:
t, t1 = [], []
for j in primes:
if i%j == 0:
x = 0
while i%j==0:
i//=j
x += 1
z = x%k
if z:
t.append((j,z))
t1.append((j,k-z))
elif i == 1:break
if i != 1:
t.append((i,1))
t1.append((i,k-1))
t = tuple(t)
t1 = tuple(t1)
ans += d[t1]
d[t] += 1
print(ans)
``` | output | 1 | 1,761 | 22 | 3,523 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,762 | 22 | 3,524 |
Tags: hashing, math, number theory
Correct Solution:
```
# -*- coding:utf-8 -*-
"""
created by shuangquan.huang at 2/11/20
"""
import collections
import time
import os
import sys
import bisect
import heapq
from typing import List
import math
def factors(val):
wc = []
for i in range(2, int(math.sqrt(val)) + 2):
if i > val:
break
if val % i == 0:
c = 0
while val % i == 0:
c += 1
val //= i
wc.append((i, c))
if val > 1:
wc.append((val, 1))
return wc
def expand(fc, maxd, k):
def dfs(index, mul):
if index >= len(fc):
return [mul]
w, c = fc[index]
d = k - (c % k) if c % k != 0 else 0
x = []
t = mul * (w ** d)
while t <= maxd:
x.extend(dfs(index + 1, t))
d += k
t *= w**k
return x
return dfs(0, 1)
def solve(N, K, A):
wc = collections.defaultdict(int)
ans = 0
for v in A:
fc = factors(v)
fc = [(f, c % K) for f, c in fc if c % K != 0]
key = '_'.join(['{}+{}'.format(f, c) for f, c in fc])
cov = [(f, K-c) for f, c in fc]
ckey = '_'.join(['{}+{}'.format(f, c) for f, c in cov])
ans += wc[ckey]
wc[key] += 1
return ans
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
print(solve(N, K, A))
``` | output | 1 | 1,762 | 22 | 3,525 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,763 | 22 | 3,526 |
Tags: hashing, math, number theory
Correct Solution:
```
import math as mt
from typing import Counter
# Calculando SPF (Smallest Prime Factor)
# para todo número hasta MAXN.
def sieve():
spf[1] = 1
for i in range(2, MAXN):
# Marcando como SPF de cada número
# para que sea el mismo
spf[i] = i
# Por separado se marca como spf
# de todo número par al 2
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, mt.ceil(mt.sqrt(MAXN))):
# Verificando si i es primo
if (spf[i] == i):
# Marcando spf para todo número
# divisible por i
for j in range(i * i, MAXN, i):
# Marcando spf[j] si no fue
# anteriormente marcado
if (spf[j] == j):
spf[j] = i
# Función que obtendrá la factorización
# prima módulo k de x, dividiendo por
# el spf en cada paso, y la retornará
# como tuplas (factor, exponente)
def getFactorization(x, k):
li = dict()
while (x != 1):
# Si no existe dicha llave se crea
if not spf[x] in li:
li[spf[x]] = 0
# Tantas veces como spf[x] divida a
# x será el valor de su exponente
li[spf[x]] += 1
# Si el exponente llega a k
# se elimina el factor pues
# al exponente se le efectúa mod k
if li[spf[x]] == k:
del li[spf[x]]
# Se divide x para encontrar
# el próximo factor
x = x // spf[x]
# Se computa la factorización módulo k
# del a_j necesario
li_prime = li.copy()
for i in li_prime.keys():
li_prime[i] = k - li_prime[i]
# Se devuelven ambas factorizaciones en forma de tupla de tuplas
return tuple(li.items()), tuple(li_prime.items())
# Función que devuelve la cantidad de pares
# que cumplan la condición del problema.
def Solution(n, k, array):
# Mapa (descomposición mod k, cant de apariciones)
lists = dict()
# Precálculo de la criba
sieve()
count = 0
for i in array:
# Comprueba si se ha calculado
# la descomposición mod k anteriormente
if len(mask_li[i]) == 0:
# Si no, hay que calcularlas
li, li_prime = getFactorization(i,k)
mask_li[i] = li
mask_li_prime[i] = li_prime
# Si la descomposición que machea con
# la de a_i está en el mapa sumamos a la respuesta
# la cantidad de apariciones
if mask_li_prime[i] in lists:
count += lists[mask_li_prime[i]]
# Se suma 1 a la cantidad de apariciones
# de la descomposición mod k de a_i pues
# ella puede ser el match para otra a_i
if mask_li[i] in lists:
lists[mask_li[i]] += 1
# Si la descomposición no existía
# se crea el nuevo par con aparición
# igua a 1
else : lists[mask_li[i]] = 1
return count
# Fragmento para computar la entrada
values = input().split()
n, k = int(values[0]), int(values[1])
a = list(map(int, input().split()))
# Se guarda el entero más grande
# de la entrada pues hasta ese valor
# se calculará la criba
maximun = max(a)
MAXN = maximun + 1
# Máscaras que guardarán las factorizaciones
# calculadas hasta el momento para evitar
# calcular factorizaciones que ya se hayan hecho
mask_li = [() for i in range(0, maximun + 1)]
mask_li_prime = [() for i in range(0, maximun + 1)]
# Almacena factor primo más
# pequeño para cada número
spf = [0 for i in range(MAXN)]
result = Solution(n, k, a)
print(result)
``` | output | 1 | 1,763 | 22 | 3,527 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,764 | 22 | 3,528 |
Tags: hashing, math, number theory
Correct Solution:
```
from math import sqrt
def gd(n, k):
ans = 1
obrans = 1
for i in range(2, int(sqrt(n) + 1)):
j = 0
while n % i == 0:
j += 1
n //= i
ans *= pow(i, j%k)
obrans *= pow(i, (-j)%k)
ans *= n
obrans *= pow(n, (k-1))
return ans, obrans
n, k = map(int,input().split())
oba = set()
dct = {}
for i in list(map(int,input().split())):
a,b = gd(i, k)
dct[a] = dct.get(a, 0) + 1
a,b = min(a,b), max(a,b)
oba.add((a,b))
ans = 0
for i, j in oba:
if i == j:
ans += (dct.get(i, 0) * dct.get(j, 0) - dct.get(i, 0)) // 2
else:
ans += dct.get(i, 0) * dct.get(j, 0)
print(ans)
``` | output | 1 | 1,764 | 22 | 3,529 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3. | instruction | 0 | 1,765 | 22 | 3,530 |
Tags: hashing, math, number theory
Correct Solution:
```
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
d = {}
for i in range(n):
x = 2
a = []
b = []
y = arr[i]
while x * x <= arr[i]:
if arr[i] % x == 0:
c = 0
while y % x == 0:
y = y // x
c += 1
if c % k > 0:
a.append((x, c % k))
b.append((x, k - (c % k)))
x += 1
if y > 1:
a.append((y, 1 % k))
b.append((y, k - (1 % k)))
try:
ans += d[tuple(b)]
except:
pass
try:
d[tuple(a)] += 1
except:
d[tuple(a)] = 1
print(ans)
``` | output | 1 | 1,765 | 22 | 3,531 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3.
Submitted Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from collections import Counter
def seieve_prime_factorisation(n):
p,i=[1]*(n+1),2
while i*i<=n:
if p[i]==1:
for j in range(i*i,n+1,i):
p[j]=i
i+=1
return p
def prime_factorisation_by_seive(p,x):
c=Counter()
while p[x]!=1:
c[p[x]]+=1
x=x//p[x]
c[x]+=1
return c
def main():
n,k=map(int,input().split())
a=list(map(int,input().split()))
ma=max(a)
p=seieve_prime_factorisation(ma)
b=Counter()
ans,z=0,0
for i in a:
if i!=1:
c,d=[],[]
e=prime_factorisation_by_seive(p,i)
for j in sorted(e.keys()):
y=e[j]%k
if y:
c.append((j,y))
d.append((j,k-y))
if c:
c,d=tuple(c),tuple(d)
b[c]+=1
ans+=b[d]-(c==d)
else:
z+=1
else:
z+=1
print(ans+z*(z-1)//2)
# 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()
``` | instruction | 0 | 1,768 | 22 | 3,536 |
Yes | output | 1 | 1,768 | 22 | 3,537 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3.
Submitted Solution:
```
#!/usr/bin/python3
import array
import math
import os
import sys
def main():
init_primes()
N, K = read_ints()
A = read_ints()
print(solve(N, K, A))
PRIMES = []
PMAX = 100000
def init_primes():
b = [0] * (PMAX + 1)
for i in range(2, PMAX + 1):
if not b[i]:
PRIMES.append(i)
for j in range(i + i, PMAX + 1, i):
b[j] = 1
def solve(N, K, A):
high = max(A)
D = dict()
for a in A:
if a not in D:
D[a] = 0
D[a] += 1
ans = 0
for a, c in D.items():
if a == 1:
ans += c * (c - 1)
continue
b = 1
x = a
for p in PRIMES:
if x == 1:
break
if x % p == 0:
b *= p
x //= p
while x % p == 0:
x //= p
assert x == 1
#dprint('a:', a, 'b:', b)
p = b ** K
i = 1
while True:
x = (i ** K) * p
#dprint(' x:', x)
if x > high * high:
break
if x % a == 0:
y = x // a
#dprint(' y:', y)
if a == y:
ans += c * (c - 1)
#dprint(' +', c * (c - 1))
elif y in D:
f = 1
if y == 1:
f = 2
ans += f * c * D[y]
#dprint(' +', f * c * D[y])
i += 1
return ans // 2
###############################################################################
# AUXILIARY FUNCTIONS
DEBUG = 'DEBUG' in os.environ
def inp():
return sys.stdin.readline().rstrip()
def read_int():
return int(inp())
def read_ints():
return [int(e) for e in inp().split()]
def dprint(*value, sep=' ', end='\n'):
if DEBUG:
print(*value, sep=sep, end=end)
if __name__ == '__main__':
main()
``` | instruction | 0 | 1,769 | 22 | 3,538 |
Yes | output | 1 | 1,769 | 22 | 3,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given n positive integers a_1, …, a_n, and an integer k ≥ 2. Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that a_i ⋅ a_j = x^k.
Input
The first line contains two integers n and k (2 ≤ n ≤ 10^5, 2 ≤ k ≤ 100).
The second line contains n integers a_1, …, a_n (1 ≤ a_i ≤ 10^5).
Output
Print a single integer — the number of suitable pairs.
Example
Input
6 3
1 3 9 8 24 1
Output
5
Note
In the sample case, the suitable pairs are:
* a_1 ⋅ a_4 = 8 = 2^3;
* a_1 ⋅ a_6 = 1 = 1^3;
* a_2 ⋅ a_3 = 27 = 3^3;
* a_3 ⋅ a_5 = 216 = 6^3;
* a_4 ⋅ a_6 = 8 = 2^3.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 12 16:24:41 2019
# =============================================================================
# @author: Acer
# """
#
# n = int(input())
# dp = [[0 for x in range(10)] for y in range(n)]
# for i in range (0, 10):
# if (i != 0) and (i != 8):
# dp[0][i] = 1
# for i in range (1, n):
# dp[i][0] = dp[i - 1][4] + dp[i - 1][6]
# dp[i][1] = dp[i - 1][8] + dp[i - 1][6]
# dp[i][2] = dp[i - 1][7] + dp[i - 1][9]
# dp[i][3] = dp[i - 1][4] + dp[i - 1][8]
# dp[i][4] = dp[i - 1][3] + dp[i - 1][9] + dp[i - 1][0]
# dp[i][6] = dp[i - 1][1] + dp[i - 1][7] + dp[i - 1][0]
# dp[i][7] = dp[i - 1][2] + dp[i - 1][6]
# dp[i][8] = dp[i - 1][1] + dp[i - 1][3]
# dp[i][9] = dp[i - 1][4] + dp[i - 1][2]
# ans = 0;
# for j in range (0, 10):
# ans += dp[n - 1][j]
# # =============================================================================
# # print(ans)n
# # =============================================================================
#
# =============================================================================
import math
def fact(a):
res = {}
for i in range (2, int(math.sqrt(a)) + 1):
while a % i == 0:
if i in res:
res[i] += 1
else:
res[i] = 1
a /= i
if len(res) == 0:
res[a] = 1
res[1] = 1
return res
def process_list(a : []):
res = {}
for i in range (0, len(a)):
cur_fact = fact(a[i])
for k in cur_fact: # простые числа
pow = cur_fact[k]
if not k in res:
res[k] = {}
if not pow in res[k]:
res[k][pow] = [i]
else:
res[k][pow].append(i)
return res
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
dict_primes = process_list(a) # простое число - степень - индексы
res = 0
for i in range (0, len(a)):
cur_fact = fact(a[i])
last_key = list(cur_fact.keys())[0]
last_val = cur_fact[last_key] % k
# print(last_key, last_val)
if last_key == 1:
last_val = k - 1
pos_vars = []
if last_val % k == 0:
pos_vars = list(range(i + 1, len(a)))
# print("!")
elif (k - last_val) in dict_primes[last_key]:
pos_vars = dict_primes[last_key][k - last_val]
for pv in pos_vars:
if pv <= i:
continue
pr = a[i] * a[pv]
# print (a[i], pv, pr)
x = int(round((math.pow(pr, 1 / k)), 0))
# print(x)
if (math.pow(x, k) == pr):
res += 1
# print(res)
print (res)
``` | instruction | 0 | 1,770 | 22 | 3,540 |
No | output | 1 | 1,770 | 22 | 3,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Bizon the Champion isn't just friendly, he also is a rigorous coder.
Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].
Let's determine the sequence Xi, for integer i (i ≥ 0): X0 = [X] ([X] is a sequence consisting of a single number X), Xi = f(Xi - 1) (i > 0). For example, at X = 6 we get X0 = [6], X1 = [1, 2, 3, 6], X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].
Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first 105 elements of this sequence.
Input
A single line contains two space-separated integers — X (1 ≤ X ≤ 1012) and k (0 ≤ k ≤ 1018).
Output
Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
Examples
Input
6 1
Output
1 2 3 6
Input
4 2
Output
1 1 2 1 2 4
Input
10 3
Output
1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10 | instruction | 0 | 1,982 | 22 | 3,964 |
Tags: brute force, dfs and similar, implementation, number theory
Correct Solution:
```
import sys
solSaved = {}
solSavedPropios = {}
def DivisorsN(n):
key = solSaved.get(str(n))
if key == None :
divisors = []
divisors2 = []
for i in range(1,int(n ** (1/2)) + 1):
if n % i == 0 :
if abs(i - (n/i)) < 10 ** (-7) : divisors.append(int(i))
else :
divisors.append(int(i))
temp = []
temp.append(int(n/i))
divisors2 = temp + divisors2
divisors = divisors + divisors2
solSaved[str(n)] = divisors.copy()
return divisors
return key
def TodosLosDivisores(arrayN) :
sol = []
if len(arrayN) > 10 ** 5 :
newArrayN = []
for k in range(len(arrayN)):
newArrayN.append(arrayN[k])
arrayN = newArrayN
for i in range(len(arrayN) - 1):
temp = DivisorsN(arrayN[i])
for t in range(len(temp)) : sol.append(temp[t])
#sol = sol + solSaved.get(str(arrayN[len(arrayN) - 1]))
temp = sol.copy()
temp2 = solSaved.get(str(arrayN[len(arrayN) - 1]))
sol.append(temp2)
for t in range(len(temp2)) : temp.append(temp2[t])
solSaved[str(temp2)] = temp
return sol
def DivisorsXk():
a, b = input().split()
x = int(a)
k = int(b)
if x == 1 : return 1
if k == 0 : return x
if k == 1 : return DivisorsN(x)
sol = []
sol.append(x)
solSaved[str(x)] = DivisorsN(x)
# index = 0
# for i in range(k):
# prev_index = len(sol)
# for j in range(index, len(sol)):
# divs = DivisorsN(sol[j])
# sol = sol + divs
# index = prev_index
i = 1
while i <= k :
# j = 0
# while j < len(sol):
# if type(sol[j]) == int : newSol.append(DivisorsN(sol[j]))
# else : newSol = newSol + DivisorAll(sol[j])
# j+=1
# sol = newSol
if type(sol[i - 1]) == int : sol.append(DivisorsN(sol[i - 1]))
else : sol.append(TodosLosDivisores(sol[i - 1]))
i += 1
temp = []
temp2 = sol[len(sol) - 1]
for t in range(len(temp2) - 1) : temp.append(temp2[t])
temp = temp + temp2[len(temp2) - 1]
return temp
def LosFuckingDivisoresDeUnArrayConLista(a) :
sol = []
if type(a) == int : return DivisorsN(a)
for i in range(len(a)) :
temp = []
key = solSaved.get(str(a[i]))
if key == None : temp = LosFuckingDivisoresDeUnArrayConLista(a[i])
else : temp = key
sol.append(temp)
solSaved[str(a)] = sol
return sol
def Divisors(x, k) :
if k == 0 : return x
if k > 10 ** 5 :
Ones = []
for i in range(10 ** 5) : Ones.append(1)
return Ones
key = solSaved.get(str((x,k)))
if key == None :
sol = []
if type(x) == int : return Divisors(DivisorsN(x), k - 1)
else :
sol = []
for i in range(len(x)) :
temp = []
temp.append(Divisors(x[i], k - 1))
sol = sol + temp
return sol
else : return key
def DivisorsPropios(n) :
key = solSavedPropios.get(str(n))
if key == None :
divisors = []
divisors2 = []
for i in range(2,int(n ** (1/2)) + 1):
if n % i == 0 :
if abs(i - (n/i)) < 10 ** (-7) : divisors.append(int(i))
else :
divisors.append(int(i))
temp = []
temp.append(int(n/i))
divisors2 = temp + divisors2 #divisors2.append(int(n / i))
divisors = divisors + divisors2
solSavedPropios[str(n)] = divisors.copy()
return divisors
return key
def UltimateDivisors(x, k) :
if x == 1 : return 1
if k == 0 : return x
if k == 1 : return DivisorsN(x)
if k >= 10 ** 5 :
a = []
for i in range(10 ** 5) : a.append(1)
return a
Unos = []
sol = []
sol.append(1)
temp = DivisorsPropios(x).copy()
for i in range(k - 1) : Unos.append(1)
if len(temp) == 0 : return [1] + Unos + [x]
temp.append(x)
for t in range(len(temp)) :
if len(sol) >= 10 ** 5 : return sol
temp3 = DivisorsPropios(temp[t]).copy()
if len(temp3) == 0 :
temp4 = UltimateDivisors(temp[t], k - 1)
if type(temp4) == int : sol = sol + [temp4]
else : sol = sol + temp4
else : sol = sol + NewUltimateDivisores(temp[t], k - 1)
return sol
def NewUltimateDivisores(x, k) :
temp = DivisorsPropios(x)
i = k - 1
sol = []
while i >= 1 :
if len(sol) >= 10**5 : return sol
sol = sol + [1]
for t in range(len(temp)) :
temp2 = UltimateDivisors(temp[t], i)
if type(temp2) == int : sol = sol + [temp2]
else : sol = sol + temp2
i -= 1
return sol + DivisorsN(x)
#print(UltimateDivisors(963761198400,65535))
#print(UltimateDivisors(549755813888,269))
a, b = input().split()
x = int(a)
k = int(b)
sol = UltimateDivisors(x,k)
if type(sol) == int : print(sol)
else :
if(len(sol) >= 10 ** 5):
for i in range(10 ** 5 - 1) :
sys.stdout.write(str(sol[i]) + ' ')
sys.stdout.write(str(sol[10**5 - 1]))
else :
for i in range(len(sol) - 1) :
sys.stdout.write(str(sol[i]) + ' ')
sys.stdout.write(str(sol[len(sol) - 1]))
``` | output | 1 | 1,982 | 22 | 3,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just friendly, he also is a rigorous coder.
Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].
Let's determine the sequence Xi, for integer i (i ≥ 0): X0 = [X] ([X] is a sequence consisting of a single number X), Xi = f(Xi - 1) (i > 0). For example, at X = 6 we get X0 = [6], X1 = [1, 2, 3, 6], X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].
Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first 105 elements of this sequence.
Input
A single line contains two space-separated integers — X (1 ≤ X ≤ 1012) and k (0 ≤ k ≤ 1018).
Output
Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
Examples
Input
6 1
Output
1 2 3 6
Input
4 2
Output
1 1 2 1 2 4
Input
10 3
Output
1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10
Submitted Solution:
```
def main():
x, k = [int(i) for i in input().split()]
if k > 10 ** 5:
print(' '.join('1' for i in range(10 ** 5)))
else:
cur = [x]
for times in range(k):
next = []
for i in cur:
if len(next) >= 10 ** 5:
break
for j in range(1, i + 1):
if i % j == 0:
next.append(j)
if len(next) >= 10 ** 5:
break
cur = next
print(' '.join(str(i) for i in cur))
main()
``` | instruction | 0 | 1,983 | 22 | 3,966 |
No | output | 1 | 1,983 | 22 | 3,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just friendly, he also is a rigorous coder.
Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].
Let's determine the sequence Xi, for integer i (i ≥ 0): X0 = [X] ([X] is a sequence consisting of a single number X), Xi = f(Xi - 1) (i > 0). For example, at X = 6 we get X0 = [6], X1 = [1, 2, 3, 6], X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].
Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first 105 elements of this sequence.
Input
A single line contains two space-separated integers — X (1 ≤ X ≤ 1012) and k (0 ≤ k ≤ 1018).
Output
Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
Examples
Input
6 1
Output
1 2 3 6
Input
4 2
Output
1 1 2 1 2 4
Input
10 3
Output
1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10
Submitted Solution:
```
def DivisorsN(n):
divisors = []
divisors2 = []
for i in range(1,int(n ** (1/2)) + 1):
if n % i == 0 :
if abs(i - (n/i)) < 10 ** (-7) : divisors.append(i)
else :
divisors.append(i)
divisors2.append(n / i)
i = -1*(len(divisors2) - 1)
while i < 1 :
divisors.append(divisors2[i])
i += 1
return divisors
def asd(n) :
sol = []
for i in range(len(n)) :
sol.append(DivisorsN(n[i]))
return sol
def DivisorsXk():
a, b = input().split()
x = int(a)
k = int(b)
sol = []
i = 2
sol.append(x)
while i <= k :
j = 0
temp = []
while j < len(sol):
a = DivisorsN(sol[j])
for t in range(len(a)): temp.append(int(a[t]))
j+=1
sol = temp
i+= 1
return sol
print(DivisorsXk())
``` | instruction | 0 | 1,984 | 22 | 3,968 |
No | output | 1 | 1,984 | 22 | 3,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just friendly, he also is a rigorous coder.
Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].
Let's determine the sequence Xi, for integer i (i ≥ 0): X0 = [X] ([X] is a sequence consisting of a single number X), Xi = f(Xi - 1) (i > 0). For example, at X = 6 we get X0 = [6], X1 = [1, 2, 3, 6], X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].
Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first 105 elements of this sequence.
Input
A single line contains two space-separated integers — X (1 ≤ X ≤ 1012) and k (0 ≤ k ≤ 1018).
Output
Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
Examples
Input
6 1
Output
1 2 3 6
Input
4 2
Output
1 1 2 1 2 4
Input
10 3
Output
1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10
Submitted Solution:
```
from __future__ import print_function
maxx = 100000
n, m = map(int, input().split())
a = []
a.clear()
def dfs(x, y):
global maxx
if maxx == 0 :
return
if x == 0 or y == m :
maxx -= 1
print(a[x],' ',end="")
return
for d in range(x + 1):
if a[x] % a[d] == 0:
dfs(d, y + 1)
def main():
i = 1
while i * i < n :
if n % i == 0 :
a.append(i)
i += 1
if i * i > n : a.pop()
while i > 0 :
if n % i ==0 :
a.append(n//i)
i -= 1
dfs(len(a) - 1, 0)
if __name__ == '__main__':
main()
``` | instruction | 0 | 1,985 | 22 | 3,970 |
No | output | 1 | 1,985 | 22 | 3,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Bizon the Champion isn't just friendly, he also is a rigorous coder.
Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].
Let's determine the sequence Xi, for integer i (i ≥ 0): X0 = [X] ([X] is a sequence consisting of a single number X), Xi = f(Xi - 1) (i > 0). For example, at X = 6 we get X0 = [6], X1 = [1, 2, 3, 6], X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].
Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first 105 elements of this sequence.
Input
A single line contains two space-separated integers — X (1 ≤ X ≤ 1012) and k (0 ≤ k ≤ 1018).
Output
Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
Examples
Input
6 1
Output
1 2 3 6
Input
4 2
Output
1 1 2 1 2 4
Input
10 3
Output
1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10
Submitted Solution:
```
def DivisorsN(n):
divisors = []
divisors2 = []
for i in range(1,int(n ** (1/2)) + 1):
if n % i == 0 :
if abs(i - (n/i)) < 10 ** (-7) : divisors.append(i)
else :
divisors.append(i)
divisors2.append(n / i)
i = -1*(len(divisors2) - 1)
while i < 1 :
divisors.append(divisors2[i])
i += 1
return divisors
def asd(n) :
sol = []
for i in range(len(n)) :
sol.append(DivisorsN(n[i]))
return sol
def DivisorsXk():
a, b = input().split()
x = int(a)
k = int(b)
sol = []
i = 2
sol.append(x)
while i <= k :
j = 0
temp = []
while j < len(sol):
a = DivisorsN(sol[j])
for t in range(len(a)): temp.append(a[t])
j+=1
sol = temp
i+= 1
return sol
print(DivisorsXk())
``` | instruction | 0 | 1,986 | 22 | 3,972 |
No | output | 1 | 1,986 | 22 | 3,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The only difference is that in this version k = 0.
There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.
Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important.
What is the minimum number of continuous segments you should use if you will make changes optimally?
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0).
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7).
It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case print a single integer — the answer to the problem.
Example
Input
3
5 0
18 6 2 4 1
5 0
6 8 1 24 8
1 0
1
Output
3
2
1
Note
In the first test case the division may be as follows:
* [18, 6]
* [2, 4]
* [1] | instruction | 0 | 2,654 | 22 | 5,308 |
Tags: data structures, dp, greedy, math, number theory, two pointers
Correct Solution:
```
import os,io,math;input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
prime=[1]*10000;prime2=[];curr=2
for i in range(2,10000):
if prime[i]:
for j in range(2*i,10000,i): prime[j]=0
prime2 = [(curr*curr) for curr in range(2,math.ceil(math.sqrt(10**7)) + 1) if prime[curr]]
for _ in range(int(input())):
n,k=map(int,input().split());a=list(map(int,input().split()));ans=1;kl=set()
for i in range(n):
r=a[i]
for q in prime2:
if r<q: break
while r%q==0: r=r//q
if r in kl: ans+=1;kl=set();kl.add(r)
else: kl.add(r)
print(ans)
``` | output | 1 | 2,654 | 22 | 5,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is the easy version of the problem. The only difference is that in this version k = 0.
There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.
Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important.
What is the minimum number of continuous segments you should use if you will make changes optimally?
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0).
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7).
It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case print a single integer — the answer to the problem.
Example
Input
3
5 0
18 6 2 4 1
5 0
6 8 1 24 8
1 0
1
Output
3
2
1
Note
In the first test case the division may be as follows:
* [18, 6]
* [2, 4]
* [1] | instruction | 0 | 2,659 | 22 | 5,318 |
Tags: data structures, dp, greedy, math, number theory, two pointers
Correct Solution:
```
import os,io,math;input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
prime=[1]*10000;prime2=[];curr=2
for i in range(2,10000):
if prime[i]:
for j in range(2*i,10000,i): prime[j]=0
for curr in range(2,math.ceil(math.sqrt(10**7)) + 1):
if prime[curr]:
prime2.append(curr*curr)
for _ in range(int(input())):
n,k=map(int,input().split());a=list(map(int,input().split()));ans=1;kl=set()
for i in range(n):
r=a[i]
for q in prime2:
if r<q: break
while r%q==0: r=r//q
if r in kl: ans+=1;kl=set();kl.add(r)
else: kl.add(r)
print(ans)
``` | output | 1 | 2,659 | 22 | 5,319 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The only difference is that in this version k = 0.
There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.
Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important.
What is the minimum number of continuous segments you should use if you will make changes optimally?
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0).
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7).
It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case print a single integer — the answer to the problem.
Example
Input
3
5 0
18 6 2 4 1
5 0
6 8 1 24 8
1 0
1
Output
3
2
1
Note
In the first test case the division may be as follows:
* [18, 6]
* [2, 4]
* [1]
Submitted Solution:
```
import sys
input = sys.stdin.readline
x=3200
import math
L=math.floor(math.sqrt(x)) # 平方根を求める
Primelist=[i for i in range(x+1)]
Primelist[1]=0 # 1は素数でないので0にする.
for i in Primelist:
if i>L:
break
if i==0:
continue
for j in range(2*i,x+1,i):
Primelist[j]=0
Primes=[Primelist[j] for j in range(x+1) if Primelist[j]!=0]
D=[p*p for p in Primes]
t=int(input())
for tests in range(t):
n,k=map(int,input().split())
A=list(map(int,input().split()))
for i in range(n):
for d in D:
while A[i]%d==0:
A[i]//=d
if d>A[i]:
break
NOW=set()
ANS=0
for a in A:
if a in NOW:
ANS+=1
NOW={a}
else:
NOW.add(a)
print(ANS+1)
``` | instruction | 0 | 2,662 | 22 | 5,324 |
Yes | output | 1 | 2,662 | 22 | 5,325 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is the easy version of the problem. The only difference is that in this version k = 0.
There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.
Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important.
What is the minimum number of continuous segments you should use if you will make changes optimally?
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0).
The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7).
It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
Output
For each test case print a single integer — the answer to the problem.
Example
Input
3
5 0
18 6 2 4 1
5 0
6 8 1 24 8
1 0
1
Output
3
2
1
Note
In the first test case the division may be as follows:
* [18, 6]
* [2, 4]
* [1]
Submitted Solution:
```
from sys import stdin
from collections import defaultdict
from copy import deepcopy
import heapq
import math
from functools import reduce
def get_prime_nums():
lst = [2]
for i in range(3, 10000):
is_prime = True
for x in lst:
if i % x == 0:
is_prime = False
break
if is_prime:
lst.append(i)
return (lst)
def get_mask(num, prime_nums):
dv = []
for p in prime_nums:
c = 0
while num % p == 0:
c += 1
num = num // p
if c % 2 == 1:
dv.append(p)
if num != 1:
dv.append(num)
return ' '.join((str(_) for _ in dv))
def get_ans(n, k, lst, prime_nums):
i = 0
ans = 0
o_set = set()
while i < n:
mask = get_mask(lst[i], prime_nums)
if mask in o_set:
o_set = set()
ans += 1
i += 1
o_set.add(mask)
return ans + 1
prime_nums = get_prime_nums()
t = int(stdin.buffer.readline())
for _ in range(t):
n, k = map(int, stdin.buffer.readline().split())
lst = list(map(int, stdin.buffer.readline().split()))
print(get_ans(n, k, lst, prime_nums))
``` | instruction | 0 | 2,665 | 22 | 5,330 |
Yes | output | 1 | 2,665 | 22 | 5,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).
You are given an array a of n integers. You have to process two types of queries:
1. REPLACE l r — for every <image> replace ai with D(ai);
2. SUM l r — calculate <image>.
Print the answer for each SUM query.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.
Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).
There is at least one SUM query.
Output
For each SUM query print the answer to it.
Example
Input
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
Output
30
13
4
22
Submitted Solution:
```
import sys
class FenwickTree(object):
"""Implementation of Fenwick Tree (a.k.a Binary Indexed Tree). [v1.0]
This implentation uses 0-based indexing.
"""
def __init__(self, n, lst=None):
"""Initialize Fenwick Tree for n elements. If lst is given, Fenwick
Tree is initiailzed from the list in O(n) time.
"""
if not lst:
self._data = [0] * n
else:
self._data = lst
for i in range(1, n + 1):
j = i + (i & -i)
if j <= n:
self._data[j - 1] += self._data[i - 1]
def sum(self, beg, end):
"""Return the sum of elements in range [beg, end)."""
if beg > 0:
return self.sum(0, end) - self.sum(0, beg)
s = 0
while end > 0:
s += self._data[end - 1]
end -= end & -end
return s
def add(self, idx, val):
"""Add val to the element at idx."""
idx += 1
while idx <= len(self._data):
self._data[idx - 1] += val
idx += idx & -idx
def main():
n, m = [int(x) for x in sys.stdin.readline().split()]
a = [int(x) for x in sys.stdin.readline().split()]
tr = FenwickTree(n, a[:])
nm = 1000001
tt = [2] * nm
for i in range(2, 1001):
for j in range(i+i, nm, i):
tt[j] += 1
tt[1]=1
dd = {}
for i in range(n):
if tt[a[i]] < a[i]:
dd[i] = a[i]
#print(dd)
for _ in range(m):
c, l, r = [int(x) for x in sys.stdin.readline().split()]
l -=1
r-=1
if c==2 :
print(tr.sum(l, r+1))
else:
xx=[]
for k,v in dd.items():
if l<=k<=r:
nnn = tt[v]
tr.add(k, nnn - v)
xx.append((k, nnn))
for aa,bb in xx:
if bb<=2:
del dd[aa]
else:
dd[aa] = bb
if __name__ == '__main__':
main()
``` | instruction | 0 | 3,053 | 22 | 6,106 |
No | output | 1 | 3,053 | 22 | 6,107 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).
You are given an array a of n integers. You have to process two types of queries:
1. REPLACE l r — for every <image> replace ai with D(ai);
2. SUM l r — calculate <image>.
Print the answer for each SUM query.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.
Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).
There is at least one SUM query.
Output
For each SUM query print the answer to it.
Example
Input
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
Output
30
13
4
22
Submitted Solution:
```
import math
def NOD(a):
k = int(a)
sum=0
i=1
for i in range(math.trunc(math.sqrt(k))):
if a % (i+1)==0:
sum+=1
return int(2*sum)
def Sum(a,d,p):
sum =0
i=d-1
while i<=p-1:
sum+=int(a[i])
i+=1
return sum
def change(a,l=1,r=7):
b = a
i = l-1
while i<=r-1:
s=NOD(int(a[i]))
b[i]=s
i+=1
return b
sum=0
str1=[]
mas=[]
s1= input()
kol=int(s1[0:s1.index(" ")])
m=int(s1[s1.index("")+1:s1.__len__()])#кол-во строк
str = input()# кол-во элементов
mas= mas+(str.split(" "))
mas1=[]
for i in range(m):
str1 = input()
if str1[0:str1.index(" ")]=="1":
l = str1[str1.find(" ") + 1:str1.rfind(" ")]
r = str1[str1.rfind(" ") + 1:str1.__len__()]
mas = change(mas, int(l), int(r))
else:
g = str1[str1.find(" ") + 1:str1.rfind(" ")]
r = str1[str1.rfind(" ") + 1:str1.__len__()]
sum += Sum(mas, int(g), int(r))
mas1.append(sum)
sum=0
for i in mas1:
print(i)
``` | instruction | 0 | 3,054 | 22 | 6,108 |
No | output | 1 | 3,054 | 22 | 6,109 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).
You are given an array a of n integers. You have to process two types of queries:
1. REPLACE l r — for every <image> replace ai with D(ai);
2. SUM l r — calculate <image>.
Print the answer for each SUM query.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.
Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).
There is at least one SUM query.
Output
For each SUM query print the answer to it.
Example
Input
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
Output
30
13
4
22
Submitted Solution:
```
def change(ar, l, r):
for i in range(int(l), int(r)+1):
count = 1
for k in range(1,ar[i]):
if ar[i] % k == 0:
count += 1
ar[i] = count
#return ar
print(ar)
def sum(ar, l, r):
su = 0
for i in range(int(l)-1, int(r)):
su = su + int(ar[i])
return su
n , m = input().split(' ')
ll = list(input().split(' '))
for i in range(int(m)):
x, l, r = input().split(' ')
if x == 1:
change(ll, l, r)
else:
y = sum(ll, l, r)
print(y)
``` | instruction | 0 | 3,055 | 22 | 6,110 |
No | output | 1 | 3,055 | 22 | 6,111 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).
You are given an array a of n integers. You have to process two types of queries:
1. REPLACE l r — for every <image> replace ai with D(ai);
2. SUM l r — calculate <image>.
Print the answer for each SUM query.
Input
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.
Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).
There is at least one SUM query.
Output
For each SUM query print the answer to it.
Example
Input
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
Output
30
13
4
22
Submitted Solution:
```
n,q = tuple(map(int,input().split()))
arr = list(map(int,input().split()))
import math
def get_factors(n):
if n<=2:
return 2
count = 0
for i in range(1,int(math.sqrt(n)) + 1):
if ( n%i) == 0:
if (n/i == i):
count += 1;
else:
count += 2;
return count;
def update(bit,index,val):
index+=1;
while index <= n:
bit[index] += val;
index += index & (-index)
def sum(bit, index):
SUM = 0
index+=1;
while index > 0:
SUM += bit[index]
index -= index & (-index)
return SUM
bit = [0 for _ in range(n+1)]
for i in range(n):
update(bit,i,arr[i])
for _ in range(q):
t,l,r = tuple(map(int,input().split()))
if t == 1: #replace
for i in range(l-1,r):
cal = get_factors(arr[i])
delta = cal - arr[i]
arr[i] = cal;
update(bit,i,delta)
else:
l-=1;r-=1;
if l == r:
print(arr[l])
continue;
if l==0:
left = 0
else:
left = sum(bit,l-1)
right = sum(bit,r)
print(right - left);
``` | instruction | 0 | 3,056 | 22 | 6,112 |
No | output | 1 | 3,056 | 22 | 6,113 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s.
Input
The first line contains two positive integers d and s (1 ≤ d ≤ 500, 1 ≤ s ≤ 5000) separated by space.
Output
Print the required number or -1 if it doesn't exist.
Examples
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1 | instruction | 0 | 3,363 | 22 | 6,726 |
Tags: dp, graphs, number theory, shortest paths
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
_print = print
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')
def inp():
return sys.stdin.readline().rstrip()
def mpint():
return map(int, inp().split(' '))
def itg():
return int(inp())
# ############################## import
def extended_gcd(a, b):
"""returns gcd(a, b), s, r s.t. a * s + b * r == gcd(a, b)"""
s, old_s = 0, 1
r, old_r = b, a
while r:
q = old_r // r
old_r, r = r, old_r - q * r
old_s, s = s, old_s - q * s
return old_r, old_s, (old_r - old_s * a) // b if b else 0
def modinv(a, m):
"""returns the modular inverse of a w.r.t. to m"""
amodm = a % m
g, x, _ = extended_gcd(amodm, m)
return x % m if g == 1 else None
# ############################## main
from collections import deque
def solve():
d, s = mpint()
dp = [[(-1, -1)] * (d + 1) for _ in range(s + 1)]
# last_digit, prev_mod
dq = deque([(0, 0)]) # (digit_sum, mod)
# bfs
while dq:
digit_sum, mod = dq.popleft()
for dg in range(10):
dg_sum = digit_sum + dg
m = (mod * 10 + dg) % d
if dg_sum <= s and dp[dg_sum][m][0] == -1:
dp[dg_sum][m] = dg, mod
dq.append((dg_sum, m))
# Found the answer
# Early termination to speed up
if dp[s][0][0] != -1:
break
# No such answer
if dp[s][0][0] == -1:
return -1
# backtrace to get answer
ans = [] # char list, reverse at the end
d = 0
while s:
dg, d = dp[s][d]
s -= dg
ans.append(chr(dg + 48))
return ''.join(reversed(ans))
def main():
print(solve())
DEBUG = 0
URL = 'https://codeforces.com/contest/1070/problem/A'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: debug, 3: interactive
if DEBUG == 1:
import requests
from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
runner = TestCaseRunner(main, URL)
inp = runner.input_stream
print = runner.output_stream
runner.checking()
else:
if DEBUG != 2:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
``` | output | 1 | 3,363 | 22 | 6,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s.
Input
The first line contains two positive integers d and s (1 ≤ d ≤ 500, 1 ≤ s ≤ 5000) separated by space.
Output
Print the required number or -1 if it doesn't exist.
Examples
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1 | instruction | 0 | 3,364 | 22 | 6,728 |
Tags: dp, graphs, number theory, shortest paths
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
_print = print
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')
def inp():
return sys.stdin.readline().rstrip()
def mpint():
return map(int, inp().split(' '))
def itg():
return int(inp())
# ############################## import
# https://codeforces.com/contest/1070/submission/71820555
# ############################## main
from collections import deque
D = 507
S = 5007
def solve():
d, sm = mpint()
dq = deque([(0, 0)])
mod = [i % d for i in range(S)]
dp = [[0] * D for _ in range(S)]
ar = [[(0, 0)] * D for _ in range(S)]
dp[0][0] = 1
while dq and not dp[sm][0]:
s, r = dq.popleft()
for i in range(10):
a = s + i
b = mod[10 * r + i]
if a == 0 or a > sm:
continue
if dp[a][b] == 0:
dq.append((a, b))
dp[a][b] = 1
ar[a][b] = s, r
if not dp[sm][0]:
return -1
s = sm
r = 0
ans = []
while s:
a, b = ar[s][r]
ans.append(chr(ord('0') + s - a))
s, r = ar[s][r]
ans.reverse()
return ''.join(ans)
def main():
print(solve())
DEBUG = 0
URL = 'https://codeforces.com/contest/1070/problem/A'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: debug, 3: interactive
if DEBUG == 1:
import requests
from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
runner = TestCaseRunner(main, URL)
inp = runner.input_stream
print = runner.output_stream
runner.checking()
else:
if DEBUG != 2:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
``` | output | 1 | 3,364 | 22 | 6,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s.
Input
The first line contains two positive integers d and s (1 ≤ d ≤ 500, 1 ≤ s ≤ 5000) separated by space.
Output
Print the required number or -1 if it doesn't exist.
Examples
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1 | instruction | 0 | 3,365 | 22 | 6,730 |
Tags: dp, graphs, number theory, shortest paths
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
_print = print
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')
def inp():
return sys.stdin.readline().rstrip()
def mpint():
return map(int, inp().split(' '))
def itg():
return int(inp())
# ############################## import
# ############################## main
from collections import deque
def solve():
d, s = mpint()
dp = [[(-1, -1)] * (d + 1) for _ in range(s + 1)]
# last_digit, prev_mod
dq = deque([(0, 0)]) # (digit_sum, mod)
# bfs
while dq:
digit_sum, mod = dq.popleft()
for dg in range(10):
dg_sum = digit_sum + dg
m = (mod * 10 + dg) % d
if dg_sum <= s and dp[dg_sum][m][0] == -1:
dp[dg_sum][m] = dg, mod
dq.append((dg_sum, m))
# Found the answer
# Early termination to speed up
if dp[s][0][0] != -1:
break
# No such answer
if dp[s][0][0] == -1:
return -1
# backtrace to get answer
ans = [] # char list, reverse at the end
d = 0
while s:
dg, d = dp[s][d]
s -= dg
ans.append(chr(dg + 48))
return ''.join(reversed(ans))
def main():
print(solve())
DEBUG = 0
URL = 'https://codeforces.com/contest/1070/problem/A'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: debug, 3: interactive
if DEBUG == 1:
import requests
from ACgenerator.Y_Test_Case_Runner import TestCaseRunner
runner = TestCaseRunner(main, URL)
inp = runner.input_stream
print = runner.output_stream
runner.checking()
else:
if DEBUG != 2:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
``` | output | 1 | 3,365 | 22 | 6,731 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s.
Input
The first line contains two positive integers d and s (1 ≤ d ≤ 500, 1 ≤ s ≤ 5000) separated by space.
Output
Print the required number or -1 if it doesn't exist.
Examples
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1
Submitted Solution:
```
def init(d):
n = []
while d > 0:
k = min(d, 9)
n.append(k)
d -= k
return n
def inc(n):
i = 0
while i < len(n):
if n[i] != 0:
break
i += 1
j = i + 1
while j < len(n):
if n[j] != 9:
break
j += 1
if j == len(n):
n.append(0)
v = n[i]
n[i] = 0
n[0] = v - 1
n[j] += 1
if j > i + 1:
n[:j] = n[:j][::-1]
def to_int(n):
a = 0
m = 1
for d in n:
a += m * d
m *= 10
return a
import time
def solve(d, s):
n = init(s)
start = time.monotonic()
while True:
if time.monotonic() - start > 2.5:
return -1
for _ in range(100):
m = to_int(n)
if m % d == 0:
return m
inc(n)
import sys
s, d = tuple(int(i) for i in sys.stdin.readline().split())
n = solve(s, d)
print(n)
``` | instruction | 0 | 3,366 | 22 | 6,732 |
No | output | 1 | 3,366 | 22 | 6,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s.
Input
The first line contains two positive integers d and s (1 ≤ d ≤ 500, 1 ≤ s ≤ 5000) separated by space.
Output
Print the required number or -1 if it doesn't exist.
Examples
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1
Submitted Solution:
```
def suma (x:int):
summ = 0
while x:
summ+=(x%10)
x/=10
return summ
def summm(x:int):
summmm = 0
x = str(x)
p = len(x) - 1
for i in range (0, len(x)//2, 1):
summmm += int(x[i])
summmm += int(x[p])
p-=1
return summmm
def gcd(a:int,b:int):
while b:
a %= b;
a,b = b,a
return a
d, s = input().split(' ')
d = int(d)
s = int(s)
n = 0
if gcd(d, s) != 1:
print(-1)
else:
while True:
n+=d
if ((n % d == 0) and (summm(n) == s)):
print(n)
break
``` | instruction | 0 | 3,367 | 22 | 6,734 |
No | output | 1 | 3,367 | 22 | 6,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s.
Input
The first line contains two positive integers d and s (1 ≤ d ≤ 500, 1 ≤ s ≤ 5000) separated by space.
Output
Print the required number or -1 if it doesn't exist.
Examples
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1
Submitted Solution:
```
n, m = map(int, input().split())
i = n
count = 0
while i%n != 0 and sum(list(map(int, list(str(i))))) % m != 0 and count < 100000:
i += n
count += 1
print (i)
``` | instruction | 0 | 3,368 | 22 | 6,736 |
No | output | 1 | 3,368 | 22 | 6,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two positive integers d and s. Find minimal positive integer n which is divisible by d and has sum of digits equal to s.
Input
The first line contains two positive integers d and s (1 ≤ d ≤ 500, 1 ≤ s ≤ 5000) separated by space.
Output
Print the required number or -1 if it doesn't exist.
Examples
Input
13 50
Output
699998
Input
61 2
Output
1000000000000000000000000000001
Input
15 50
Output
-1
Submitted Solution:
```
from collections import deque
import sys
# d, s = map(int, input().split())
# d = 500
# s = 4992
d = 13
s = 50
queue = deque([('0', 0, 0)])
cache = dict()
# (n*10 + i) mod d = ((n mod d) * (10 mod d) + (i mod d)) mod d
mod10 = 10%d
mod_i = [i%d for i in range(0,10)]
while len(queue):
n, n_mod, n_sum = queue.popleft()
# print(n, n_mod, n_sum)
if (n_mod, n_sum) in cache or n_sum > s:
continue
if n_mod == 0 and n_sum == s:
print(int(n))
break
cache[(n_mod,n_sum)] = 1
for i in range(0,10):
queue.append((n + str(i), (n_mod * mod10 + mod_i[i])%d, n_sum + i))
if len(queue) == 0:
print(-1)
``` | instruction | 0 | 3,369 | 22 | 6,738 |
No | output | 1 | 3,369 | 22 | 6,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,376 | 22 | 6,752 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
from collections import defaultdict
import math
def ncr(n,m):
return math.factorial(n)//((math.factorial(m)*math.factorial(n-m)))
def gcd(n,m):
return math.gcd(n,m)
power=[]
def cal():
temp=1
power.append(temp)
for i in range(2,30):
temp*=2
power.append(temp)
cal()
#print(*power)
t=int(input())
for t1 in range(0,t):
n=int(input())
ans=1
x=1
for i in range(0,len(power)):
if power[i]<=n:
ans=power[i]
x=i+1
else:
break
if(pow(2,x)-1 == n):
i=2
mx=1
while(i< int(math.sqrt(n))+1):
if n%i==0:
mx=max(mx,i,n//i)
i+=1
print(mx)
#print(math.gcd(n^mx,n&mx))
else:
print(pow(2,x)-1)
``` | output | 1 | 3,376 | 22 | 6,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,377 | 22 | 6,754 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
import math
def ans2(n):
if n==0:
return 1
res = 2**(math.floor(math.log2(n))+1)-1
if res == n:
lst = [1,1,1,5,1,21,1,85,73,341,89,1365,1,5461,4681,21845,1,87381,1,349525,299593,1398101,178481,5592405,1082401]
res = lst[math.ceil(math.log2(n))-1]
if n==2:
res = 3
if n==1:
res = 1
return res
def ans(n):
maxg = 1
g = 1
for i in range(1,n):
a = i ^ n
b = i & n
if math.gcd(a,b) > maxg:
maxg = math.gcd(a,b)
g = i
return maxg
#for i in range(1,26):
# print(ans(2**i-1))
#for i in range(33554430,33554434):
# if ans(i)!=ans2(i):
# print(i, ans(i), ans2(i))
#print("done")
q = int(input())
for p in range(q):
num = int(input())
print(ans2(num))
``` | output | 1 | 3,377 | 22 | 6,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,378 | 22 | 6,756 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
import math
def factors(num):
ans = 1
n = int(math.sqrt(num)) + 1
for i in range(1, n):
if num % i == 0:
a1 = num // i
a2 = i
if a1 < num:
if a1 > ans:
ans = a1
if a2 < num:
if a2 > ans:
ans = a2
return ans
t = int(input())
for _ in range(t):
a = int(input())
s1 = len(str(bin(a))) - 2
t1 = "0b" + ("1" * s1)
temp = int(t1, 2) ^ a
if temp != 0:
print(temp ^ a)
else:
x = factors(a)
print(x)
``` | output | 1 | 3,378 | 22 | 6,757 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,379 | 22 | 6,758 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
from math import *
q=int(input())
ansarr=[]
for l in range(q):
n=int(input())
b1=bin(n)[2:]
if(b1.count('1')==len(b1)):
flag=0
for i in range(2,ceil(sqrt(n))):
if(n%i==0):
ansarr.append(n//i)
flag=1
break
if(flag==0):
ansarr.append(1)
else:
i=len(b1)
ansarr.append((2**i)-1)
print(*ansarr)
``` | output | 1 | 3,379 | 22 | 6,759 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,380 | 22 | 6,760 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
import math
def f(p):
L = []
for i in range(1,int(math.sqrt(p))+1):
if p % i == 0:
L.append(i)
if i != p//i:
L.append(p//i)
L.sort()
return L[-2]
R = lambda: map(int, input().split())
for _ in range(int(input())):
n = int(input())
s = bin(n)[2:]
l = len(s)
p = s.count('1')
if p == l:
print(f(n))
else:
print((2**(l))-1)
``` | output | 1 | 3,380 | 22 | 6,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,381 | 22 | 6,762 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
import math
def isPowerOfTwo (x):
# First x in the below expression
# is for the case when x is 0
return (x and (not(x & (x - 1))) )
def main():
t = int(input())
while t > 0:
t -= 1
n = int(input())
if isPowerOfTwo(n + 1):
s = math.ceil(math.sqrt(n))
ans = 1
for i in range(2, s + 1):
if n % i == 0:
ans = n / i
break
ans = int(ans)
print(math.gcd(n ^ ans, n & ans))
else:
z = bin(n)[2:]
newstr = ''
for i in range(len(z)):
newstr += '1'
z = int(newstr, 2)
print(z)
if __name__ == '__main__':
main()
``` | output | 1 | 3,381 | 22 | 6,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,382 | 22 | 6,764 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
from math import log
q = int(input())
for i in range(q):
w = int(input())
if log(w+1,2)>int(log(w+1,2)):
print(2**(int(log(w+1,2))+1)-1)
else:
c = w
p = 3
k = False
while k == False and p<=int(w**0.5):
if w%p==0:
k = True
c = p
else:
p+=2
print(w//c)
``` | output | 1 | 3,382 | 22 | 6,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.
Suppose you are given a positive integer a. You want to choose some integer b from 1 to a - 1 inclusive in such a way that the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers a ⊕ b and a \> \& \> b is as large as possible. In other words, you'd like to compute the following function:
$$$f(a) = max_{0 < b < a}{gcd(a ⊕ b, a \> \& \> b)}.$$$
Here ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), and \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The greatest common divisor of two integers x and y is the largest integer g such that both x and y are divided by g without remainder.
You are given q integers a_1, a_2, …, a_q. For each of these integers compute the largest possible value of the greatest common divisor (when b is chosen optimally).
Input
The first line contains an integer q (1 ≤ q ≤ 10^3) — the number of integers you need to compute the answer for.
After that q integers are given, one per line: a_1, a_2, …, a_q (2 ≤ a_i ≤ 2^{25} - 1) — the integers you need to compute the answer for.
Output
For each integer, print the answer in the same order as the integers are given in input.
Example
Input
3
2
3
5
Output
3
1
7
Note
For the first integer the optimal choice is b = 1, then a ⊕ b = 3, a \> \& \> b = 0, and the greatest common divisor of 3 and 0 is 3.
For the second integer one optimal choice is b = 2, then a ⊕ b = 1, a \> \& \> b = 2, and the greatest common divisor of 1 and 2 is 1.
For the third integer the optimal choice is b = 2, then a ⊕ b = 7, a \> \& \> b = 0, and the greatest common divisor of 7 and 0 is 7. | instruction | 0 | 3,383 | 22 | 6,766 |
Tags: constructive algorithms, math, number theory
Correct Solution:
```
from math import gcd, sqrt, ceil
def check_pow2(a):
return 2 ** a.bit_length() == a + 1
def max_div(a):
for i in range(2, ceil(sqrt(a))+1):
if a % i == 0:
return max(i, a // i)
return 1
def solve_dumb(a):
mv = 0
for b in range(1, a):
v = gcd(a ^ b, a & b)
if v > mv:
mv = v
return mv
#print(a, bin(a), mv)
def solve(a):
if check_pow2(a):
return max_div(a)
else:
return 2 ** (a.bit_length()) - 1
#for i in range(2, 257):
# a = i
# sd, s = solve_dumb(a), solve(a)
# print(a, sd, s)
# assert sd == s
q = int(input())
for i in range(q):
n = int(input())
print(solve(n))
``` | output | 1 | 3,383 | 22 | 6,767 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.