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 positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,498 | 22 | 214,996 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
'''
Hey why peeping here -_'_- ?
I believe on myself and I will achieve
this->author = Fuad Ashraful Mehmet, CSE ,University of Asia Pacific
Todo:
'''
import sys,math
input=sys.stdin.readline
def getSum(k):
return k*(k-1)//2
def HalfDead():
n,k=map(int,input().split())
ans=-1
for i in range(1,int(math.sqrt(n))+1):
if not n%i:
x=i
y=n//i
sum=x*getSum(k)
rem=n-sum
if rem>x*(k-1):
if x>ans:
ans=x
sum=y*getSum(k)
rem=n-sum
if rem>y*(k-1):
if y>ans:
ans=y
if ans==-1:
print(ans)
else:
ar=[]
for i in range(1,k):
ar.append(ans*i)
ar.append(n-ans*getSum(k))
print(*ar)
if __name__=='__main__':
HalfDead()
``` | output | 1 | 107,498 | 22 | 214,997 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,499 | 22 | 214,998 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
n,k = map(int,input().split())
if 2*n<k*(k+1):
print(-1)
exit(0)
mx = 0
def ok(d):
sm = k*(k-1)//2
sm *= d
if sm+k>n:
return False
if (n-sm>((k-1)*d)):
return True
else:
return False
i = 1
while i*i<=n:
if n%i==0:
if ok(i): mx = max(mx,i)
if ok(n//i): mx = max(mx,n//i)
i+= 1
ans = ''
for i in range(1,k):
ans += str(i*mx)+' '
ans += str(n-((k*(k-1))//2)*mx)
print(ans)
``` | output | 1 | 107,499 | 22 | 214,999 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,500 | 22 | 215,000 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
[n,k]=[int(x) for x in input().split()]
m=k*(k+1)/2
limit=n//m
lul="-1"
def nice(x):
global lul
lul=""
sum=0
a=[0]*k
for i in range(1,k):
a[i-1]=str(i*x)
sum+=i*x
a[k-1]=str(n-sum)
print(" ".join(a))
pep=-1
for i in range(1,1000000):
if i*i>n:
break
if i>limit:
break
if n%i>0:
continue
if n//i<=limit:
nice(n//i)
break
pep=i
if lul=="-1" and pep!=-1:
nice(pep)
print(lul)
``` | output | 1 | 107,500 | 22 | 215,001 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,501 | 22 | 215,002 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import math
import sys
array=list(map(int,input().split()))
n = array[0]
k = array[1]
maxi = -1
for i in range(1, int(math.sqrt(n)) + 1):
if n % i != 0:
continue
del1 = int(i)
del2 = int(n / i)
sum1 = del1 * k * (k - 1) / 2
sum2 = del2 * k * (k - 1) / 2
if n - sum1 > (k - 1) * del1:
maxi = max(maxi, del1)
if n - sum2 > (k - 1) * del2:
maxi = max(maxi, del2)
if maxi == -1:
print(-1)
sys.exit()
sum = 0
ans = []
for i in range(1, k):
ans.append(i * maxi)
sum += i * maxi
ans.append(n - sum)
print(" ".join(map(str,ans)))
``` | output | 1 | 107,501 | 22 | 215,003 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,502 | 22 | 215,004 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
#Library Info(ACL for Python/Pypy) -> https://github.com/not522/ac-library-python
def input():
return sys.stdin.readline().rstrip()
DXY = [(0, -1), (1,0), (0, 1), (-1,0)] #L,D,R,Uの順番
def divisors(n : int):
res = []
for i in range(1,n + 1):
if i * i > n:
break
if n % i == 0:
if i * i == n:
res.append(i)
else:
res.append(n // i)
res.append(i)
res.sort(reverse = True)
return res
def main():
n,k = map(int,input().split())
for d in divisors(n):
if n // d >= k * (k + 1) // 2:
ans = [0]*(k)
tot = n // d
for i in range(k):
if i != k - 1:
ans[i] = (i + 1) * d
tot -= (i + 1)
else:
ans[i] = tot * d
print(*ans)
return 0
print(-1)
return 0
if __name__ == "__main__":
main()
``` | output | 1 | 107,502 | 22 | 215,005 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,503 | 22 | 215,006 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import sys
#import random
from bisect import bisect_left as lb
from collections import deque
#sys.setrecursionlimit(10**8)
from queue import PriorityQueue as pq
from math import *
input_ = lambda: sys.stdin.readline().strip("\r\n")
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab,bc,cd : ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x : print(x)
prinT = lambda x : print(x)
f = lambda : sys.stdout.flush()
inv =lambda x:pow(x,mod-2,mod)
mod = 10**9 + 7
n,k = il()
s = k*(k+1)//2
if (s > n) :
print(-1)
exit(0)
if (s == n) :
for i in range (1,k+1) :
print(i,end=" ")
print()
exit(0)
p = 1
a = []
while (p*p <= n) :
if (n%p) == 0 :
a.append(p)
if (p*p) != n :
a.append(n//p)
p += 1
a.sort()
ans = 0
for d in a :
if (n//d) >= s :
ans = d
else :
break
if (ans) :
for i in range(1,k) :
print(i*ans,end=" ")
n -= i*ans
print(n)
else :
print(-1)
``` | output | 1 | 107,503 | 22 | 215,007 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,504 | 22 | 215,008 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
import math
n, k = map(int, input().split())
if (k*(k+1))/2 > n:
print(-1)
else:
c = int( n/ ((k*(k+1))/2))
a = []
for i in range(1, int( math.sqrt(n) + 1 ) ):
if i*i == n:
a.append(i)
elif n%i == 0:
a.append(i)
a.append(n//i)
a = sorted(a)
s = 0
for i in range(len(a)):
s+=1
if a[i] > c:
break
c = a[ s - 2]
for i in range(1, k):
print(c*i, end= " ")
print(str( int(n - c*(k*(k-1)/2) ) ))
``` | output | 1 | 107,504 | 22 | 215,009 |
Provide tags and a correct Python 2 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,505 | 22 | 215,010 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
def fun(x,n,k):
sm=n-((x*k*(k-1))/2)
return (sm>0 and sm/x>=k)
n,k=inp()
div=0
i=1
while i*i<=n:
if n%i==0:
if fun(i,n,k):
div=max(div,i)
if fun(n/i,n,k):
div=max(div,n/i)
i+=1
if not div:
pr_num(-1)
else:
sm=0
i=div
while i<=div*(k-1):
pr(str(i)+' ')
sm+=i
i+=div
pr(str(n-sm))
``` | output | 1 | 107,505 | 22 | 215,011 |
Provide tags and a correct Python 2 solution for this coding contest problem.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1 | instruction | 0 | 107,506 | 22 | 215,012 |
Tags: constructive algorithms, greedy, math
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
def fun(x,n,k):
sm=n-((x*k*(k-1))/2)
return (sm>0 and sm/x>=k)
n,k=in_arr()
div=0
i=1
while i*i<=n:
if n%i==0:
if fun(i,n,k):
div=max(div,i)
if fun(n/i,n,k):
div=max(div,n/i)
i+=1
if not div:
pr_num(-1)
else:
sm=0
i=div
while i<=div*(k-1):
pr(str(i)+' ')
sm+=i
i+=div
pr(str(n-sm))
``` | output | 1 | 107,506 | 22 | 215,013 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import math
n, k = map(int, input().split())
def solve(i):
a = i * k * (k + 1) // 2
if(n >= a):
return (n - a)
else:
return -1
i = 1
r = -1
while(i * i <= n):
if(n % i == 0):
if(solve(i) > -1):
r = max(r, i);
if(i * i != n):
if(solve(n / i) > -1):
r = max(r, n / i);
i = i + 1;
if(r == -1):
print(r)
else:
b = []
for i in range(1, k):
b.append(i * r)
b.append(k * r + solve(r))
for i in b:
print(int(i), end=' ')
print()
``` | instruction | 0 | 107,507 | 22 | 215,014 |
Yes | output | 1 | 107,507 | 22 | 215,015 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
def factor(n):
rtn = []
p = 2
tmp = n
while p * p <= tmp:
q = 0
while tmp % p == 0:
tmp //= p
q += 1
if 0 < q:
rtn.append((p, q))
p += 1
if 1 < tmp:
rtn.append((tmp, 1))
return rtn
def divs(n):
rtn = [1]
arr = factor(n)
for p, q in arr:
ds = [p**i for i in range(1, q + 1)]
tmp = rtn[:]
for d in ds:
for t in tmp:
rtn.append(d * t)
return list(sorted(rtn))
n, k = map(int, input().split())
ds = divs(n)
l = 0
r = len(ds) - 1
while l + 1 < r:
c = (l + r) // 2
if ds[c] * k * (k + 1) // 2 <= n:
l = c
else:
r = c
if l == 0 and n < k * (k + 1) // 2:
print(-1)
else:
d = ds[l]
ans = [d * (i + 1) for i in range(k)]
ans[-1] += n - sum(ans)
print(' '.join(map(str, ans)))
``` | instruction | 0 | 107,508 | 22 | 215,016 |
Yes | output | 1 | 107,508 | 22 | 215,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
# cook your dish here
import math
import sys
n, k = map(int, input().strip().split())
divlist=[]
rev=[]
for i in range(1, int(math.sqrt(n))+1):
if n%i==0:
divlist.append(i)
rev.append(n//i)
for i in range(len(rev)):
divlist.append(rev[len(rev)-i-1])
if 2*n < k*(k+1):
print(-1)
else:
beta=-1
for i in divlist:
if 2*i>=k*(k+1):
beta = i
break
alpha = n//beta
for i in range(k-1):
sys.stdout.write(str(alpha*(i+1)) + " ")
beta -= (i+1)
print(alpha*beta)
``` | instruction | 0 | 107,509 | 22 | 215,018 |
Yes | output | 1 | 107,509 | 22 | 215,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import sys
inf = 1 << 30
def solve():
n, k = map(int, input().split())
lim = k * (k + 1) // 2
if n < lim:
print(-1)
return
d_max = 1
for d in range(1, n + 1):
if d*d > n:
break
if n % d != 0:
continue
q = n // d
if d >= lim:
d_max = q
break
elif q >= lim:
d_max = d
else:
break
ans = []
j = 1
for i in range(k - 1):
ans.append(d_max * j)
j += 1
ans.append(n - sum(ans))
print(*ans)
if __name__ == '__main__':
solve()
``` | instruction | 0 | 107,510 | 22 | 215,020 |
Yes | output | 1 | 107,510 | 22 | 215,021 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
n,a= map(int,input().split())
if((a*(a+1)/2) > n):
print(-1)
exit(0)
c = a*(a+1)/2;
res = 1
q = int((n+10)**(1/2))
for i in range(1,q):
if(n%i): continue
if(c*i > n): continue
i1 = n/i
if(i1*c <= n): res = max(res,i1)
res=max(res,i)
res = int(res)
s1 = 0
for i in range(a-1):
print((i+1)*res,end=' ')
s1 += (i+1)*res
print(n-s1)
``` | instruction | 0 | 107,511 | 22 | 215,022 |
No | output | 1 | 107,511 | 22 | 215,023 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import math
n, k = map(int, input().split())
Ans = []
if n < (k+1)*k//2:
print(-1)
else:
d = n
nd = 1
for i in range(int(math.sqrt(n)), 1, -1):
if n%i == 0:
if i > nd and n//i >=(k+1)*k//2:
nd = i
elif n//i > nd and i >=(k+1)*k//2:
nd = n//i
d = n//nd
for x in range(1, k):
Ans.append(nd*x)
d -= x
Ans.append(d*nd)
if len(Ans) != 0:
print(*Ans)
else:
print(-1)
``` | instruction | 0 | 107,512 | 22 | 215,024 |
No | output | 1 | 107,512 | 22 | 215,025 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
import math
n, k = map(int, input().split())
Ans = []
d = (k+1)*k//2
if n < d:
print(-1)
elif d > n//2:
for x in range(1, k):
Ans.append(x)
n -= x
Ans.append(n)
print(*Ans)
else:
if d >= int(math.sqrt(n)):
for i in range(int(math.sqrt(n)), n//2+1):
if n % i == 0:
l = i
nod = n//i
if l >= d:
for x in range(1, k):
Ans.append(nod*x)
l -= x
Ans.append(l*nod)
break
else:
for i in range(1, int(math.sqrt(n))):
if n % i == 0:
l = i
nod = n//l
if l >= d:
for x in range(1, k):
Ans.append(nod*x)
l -= x
Ans.append(l*nod)
break
if len(Ans) != 0:
print(*Ans)
else:
print(-1)
``` | instruction | 0 | 107,513 | 22 | 215,026 |
No | output | 1 | 107,513 | 22 | 215,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given positive integer number n. You should create such strictly increasing sequence of k positive numbers a1, a2, ..., ak, that their sum is equal to n and greatest common divisor is maximal.
Greatest common divisor of sequence is maximum of such numbers that every element of sequence is divisible by them.
If there is no possible sequence then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n, k ≤ 1010).
Output
If the answer exists then output k numbers — resulting sequence. Otherwise output -1. If there are multiple answers, print any of them.
Examples
Input
6 3
Output
1 2 3
Input
8 2
Output
2 6
Input
5 3
Output
-1
Submitted Solution:
```
from math import sqrt
n , k = (int(i) for i in input().split())
def Provera (n, k):
if (k * (k + 1)) // 2 > n:
return True
if (k == 1):
print (n)
exit()
if (Provera(n,k)):
print (-1)
else:
for i in range(int(sqrt(n)) + 1, 2, -1):
if (n % i == 0):
if not (Provera (i , k)):
if (k * (k + 1)) // 2 == i:
for j in range(1, k + 1):
print ((n // i) * j,end = ' ')
exit()
if (k * (k + 1)) // 2 < i:
for j in range(1, k):
print ((n // i) * j,end = ' ')
print (n - (n // i) * ((k * (k - 1)) // 2))
exit()
for i in range(2, int(sqrt(n)) + 1):
if (n % i == 0):
if not (Provera (n // i , k)):
if (k * (k + 1)) // 2 == (n // i):
for j in range(1, k + 1):
print (i * j,end = ' ')
exit()
if (k * (k + 1)) // 2 < (n // i):
for j in range(1, k):
print (i * j,end = ' ')
print (n - i * ((k * (k - 1)) // 2))
exit()
if (k * (k + 1)) // 2 == n:
for i in range(1, k + 1):
print (i, end = ' ')
else:
for i in range(1, k):
print (i, end = ' ')
print (n - (k * (k + 1)) // 2)
``` | instruction | 0 | 107,514 | 22 | 215,028 |
No | output | 1 | 107,514 | 22 | 215,029 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,562 | 22 | 215,124 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import sys
ints = (int(x) for x in sys.stdin.read().split())
a,b,p,x = (next(ints) for i in range(4))
def ext_gcd(a, b):
"""return (g, x, y) such that a*x + b*y = g = gcd(a, b) and abs(x)<=b+1 ans abs(y)<=a+1"""
x, x1, y, y1 = 0, 1, 1, 0
while a != 0:
(q, a), b = divmod(b, a), a
y, y1 = y1, y - q * y1
x, x1 = x1, x - q * x1
return b, x, y
def mod_inv(a, mod):
"""return x such that (x * a) % mod = gcd(a, mod)"""
g, x, y = ext_gcd(a, mod)
return x % mod
ans, x = divmod(x, (p-1)*p)
ans = (p-1)*ans-(b==0)
an = 1
for i in range(p-1):
k = (mod_inv((an*(p-1))%p, p)*(b-i*an)) % p
ans += k*(p-1)+i<=x
an = (an*a)%p
print(ans)
if 0:
a, b, p = 2, 1, 5
for k in range(p+1):
h = [((k*(p-1)+i)*(a**i))%p for i in range(0, p-1)]
print(*h)
``` | output | 1 | 107,562 | 22 | 215,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,563 | 22 | 215,126 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))
def gcd2(a, b):
l = []
while b:
l.append(divmod(a,b))
a, b = b, a%b
x, y = 1, 0
for aa,bb in l[::-1]:
x, y = y, x - aa*y
return a, x, y
def modinv(x, M):
a,xx,yy = gcd2(x,M)
return a,xx%M
def solve(a,b,n):
g,xx,yy = gcd2(a,n)
if b%g!=0:
return None
a //= g
n //= g
b //= g
ainv = modinv(a, n)[1]
x = ainv*b%n
return x
def crt(rs, ms):
r0 = 0
m0 = 1
for r1,m1 in zip(rs, ms):
if m0<m1:
m0, m1 = m1, m0
r0, r1 = r1, r0
if m0%m1==0:
if r0%m1 != r1:
return None,None
else:
continue
# print(m0,m1)
g,im = modinv(m0, m1)
u1 = m1//g
if (r1-r0)%g!=0:
return None,None
x = (r1-r0) // g % u1 * im % u1
r0 += x * m0
m0 *= u1
if r0<0:
r0 += m0
return r0,m0
a,b,p,x = list(map(int, input().split()))
c = 1
index = [[] for _ in range(p)]
for i in range(p-1):
index[c].append(i)
c *= a
c %= p
ans = 0
for i in range(1,p):
j = b*pow(i, p-2, p)%p
for jj in index[j]:
res = crt((i, jj), (p, p-1))
if res[0] is not None:
r,m = res
# print(res)
ans += max(0, x-r+m)//m
print(ans)
``` | output | 1 | 107,563 | 22 | 215,127 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,564 | 22 | 215,128 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
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
def main():
a, b, p, x = mpint()
ans = 0
# 5: 4, 3, 2, 1
by = b
for j in reversed(range(p - 1)):
by = by * a % p
# y = pow(a, p - 1 - j, p)
# by = b * y
i = (j - by) % p # smallest i s.t. the equation
if i > (x - j) // (p - 1):
continue
ans += 1 + ((x - j) // (p - 1) - i) // p
print(ans)
DEBUG = 0
URL = 'https://codeforces.com/contest/919/problem/E'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: interactive, 3: debug
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 != 3:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
_print = print
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
``` | output | 1 | 107,564 | 22 | 215,129 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,565 | 22 | 215,130 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
a,b,p,x=map(int,input().split())
ans=0
for j in range(p-1):
y=b*pow(pow(a,j,p),p-2,p)%p
i=(j-y+p)%p
t=i*(p-1)+j
if(x-t>=0):
ans+=((x-t)//(p*(p-1))+1)
print(ans)
``` | output | 1 | 107,565 | 22 | 215,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,566 | 22 | 215,132 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
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
def main():
a, b, p, x = mpint()
ans = 0
for j in range(p - 1):
y = pow(a, p - 1 - j, p)
by = b * y
i = (j - by) % p # smallest i s.t. the equation
if i > (x - j) // (p - 1):
continue
ans += 1 + ((x - j) // (p - 1) - i) // p
print(ans)
DEBUG = 0
URL = 'https://codeforces.com/contest/919/problem/E'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: interactive, 3: debug
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 != 3:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
_print = print
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
``` | output | 1 | 107,566 | 22 | 215,133 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,567 | 22 | 215,134 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
a,b,p,x = [int(x) for x in input().split()]
ainv = pow(a,p-2,p)
count = 0
n = 1
exp = ainv*b%p
while n<p:
m = n
if exp>n:
m += (n+p-exp)*(p-1)
else:
m += (n-exp)*(p-1)
if m<=x: count += 1 + (x-m)//(p*(p-1))
n += 1
exp = (exp*ainv)%p
print(count)
``` | output | 1 | 107,567 | 22 | 215,135 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,568 | 22 | 215,136 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
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
def main():
a, b, p, x = mpint()
ans = 0
by = b
for j in reversed(range(p - 1)):
by = by * a % p
# y = pow(a, p - 1 - j, p)
# by = b * y
i = (j - by) % p # smallest i s.t. the equation
# if i > (x - j) // (p - 1):
# continue
ans += 1 + ((x - j) // (p - 1) - i) // p
print(ans)
DEBUG = 0
URL = 'https://codeforces.com/contest/919/problem/E'
if __name__ == '__main__':
# 0: normal, 1: runner, 2: interactive, 3: debug
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 != 3:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
if DEBUG:
_print = print
def print(*args, **kwargs):
_print(*args, **kwargs)
sys.stdout.flush()
main()
# Please check!
``` | output | 1 | 107,568 | 22 | 215,137 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers. | instruction | 0 | 107,569 | 22 | 215,138 |
Tags: chinese remainder theorem, math, number theory
Correct Solution:
```
# import sys
# sys.stdin = open('in.txt', 'r')
a, b, p, x = map(int, input().split())
# A = [0, 1, 2, .. p-2, p-1] .. length = p
# B = [1, a, a^2, .. a^(p-2)] .. length = p - 1
# x * inv(x) * b = b
# a^x -> inv(a*x) * b
# 4 6 7 13
# [0, 1, 2, 3, 4, 5, 6]
# [0, 1, 2, 3, 4, 5]
# 2 3 5 8
# [0, 1, 2, 3]
# [0, 1, 2]
x += 1
res = 0
res += x//(p*(p-1)) * (p-1)
x -= x//(p*(p-1)) * (p*(p-1))
for i in range(p-1):
ap = (pow(a, (i)*(p-2)%(p-1), p) * b) % p
l = (x // (p-1)) + (1 if i < x%(p-1) else 0)
if i >= ap and i-ap < l:
res += 1
elif i < ap and i+p-ap < l:
res += 1
# print(ap, l, res)
print(res)
# for i in range(1, x+1):
# print(i*pow(a,i,p)%p, end=' ')
# if i % (p-1) == 0:
# print()
# print()
``` | output | 1 | 107,569 | 22 | 215,139 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a, b, p, x = map(int, input().split())
print(sum(map(lambda j: 1 + ((x - j) // (p - 1) - (j - b * pow(a, p - 1 - j, p)) % p) // p, range(p - 1))))
``` | instruction | 0 | 107,570 | 22 | 215,140 |
Yes | output | 1 | 107,570 | 22 | 215,141 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a,b,p,x = map(int,input().split())
res = 0
for i in range(1,p):
n = (b * pow(pow(a,i,p),p-2,p))%p
u = (n - i + p )%p
u = (u * pow(p-1,p-2,p))%p
me = u * (p-1) + i
if me>x: continue
res+= (x-me) // (p*(p-1))+1
print(res)
# (u*(p-1) + i) * a^i = b mod p
# u*(p-1) + i = b * inv (a^i) % mod p
``` | instruction | 0 | 107,571 | 22 | 215,142 |
Yes | output | 1 | 107,571 | 22 | 215,143 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
heap.append(item)
heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
if heap and item < heap[0]:
item, heap[0] = heap[0], item
heapq._siftup_max(heap, 0)
return item
from math import gcd as GCD, modf
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
def LCM(n,m):
if n or m:
return abs(n)*abs(m)//math.gcd(n,m)
return 0
def CRT(lst_r,lst_m):
r,m=lst_r[0],lst_m[0]
for r0,m0 in zip(lst_r[1:],lst_m[1:]):
if (r0,m0)==(-1,0):
r,m=-1,0
break
r0%=m0
g=math.gcd(m,m0)
l=LCM(m,m0)
if r%g!=r0%g:
r,m=-1,0
break
r,m=(r0+m0*(((r-r0)//g)*Extended_Euclid(m0//g,m//g)[0]%(m//g)))%l,l
return r,m
def Extended_Euclid(n,m):
stack=[]
while m:
stack.append((n,m))
n,m=m,n%m
if n>=0:
x,y=1,0
else:
x,y=-1,0
for i in range(len(stack)-1,-1,-1):
n,m=stack[i]
x,y=y,x-(n//m)*y
return x,y
class MOD:
def __init__(self,mod):
self.mod=mod
def Pow(self,a,n):
a%=self.mod
if n>=0:
return pow(a,n,self.mod)
else:
assert math.gcd(a,self.mod)==1
x=Extended_Euclid(a,self.mod)[0]
return pow(x,-n,self.mod)
def Build_Fact(self,N):
assert N>=0
self.factorial=[1]
for i in range(1,N+1):
self.factorial.append((self.factorial[-1]*i)%self.mod)
self.factorial_inv=[None]*(N+1)
self.factorial_inv[-1]=self.Pow(self.factorial[-1],-1)
for i in range(N-1,-1,-1):
self.factorial_inv[i]=(self.factorial_inv[i+1]*(i+1))%self.mod
return self.factorial_inv
def Fact(self,N):
return self.factorial[N]
def Fact_Inv(self,N):
return self.factorial_inv[N]
def Comb(self,N,K):
if K<0 or K>N:
return 0
s=self.factorial[N]
s=(s*self.factorial_inv[K])%self.mod
s=(s*self.factorial_inv[N-K])%self.mod
return s
a,b,p,x=map(int,readline().split())
if a%p==0 and b%p==0:
ans=x
elif a%p==0:
ans=0
elif b%p==0:
ans=x//p
else:
ans=0
MD=MOD(p)
for i in range(p-1):
R,M=[i,b*MD.Pow(a,-i)],[p-1,p]
r,m=CRT(R,M)
if r==-1:
continue
ans+=(x+m-r)//m
print(ans)
``` | instruction | 0 | 107,572 | 22 | 215,144 |
Yes | output | 1 | 107,572 | 22 | 215,145 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
def AC():
a, b, p, x = map(int, input().split())
ans = 0
m = 1
for i in range(p - 2):
m = (m * a) % p
rem = 1
inv = 1
Ch = p * (p - 1)
for n in range(1, p):
rem = (rem * a) % p
inv = (inv * m) % p
cur = min(p, ((n * rem - b) * inv + p) % p)
rep = n + cur * (p - 1)
ans += max(0, (x - rep + Ch) // Ch)
print(ans)
AC()
``` | instruction | 0 | 107,573 | 22 | 215,146 |
Yes | output | 1 | 107,573 | 22 | 215,147 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a, b, p, x = map(int, input().split())
count = 0
for n in range(1,x+1):
left = (n%p*(((a%p)**n)%p))%p
right = b % p
if left == right:
count += 1
``` | instruction | 0 | 107,574 | 22 | 215,148 |
No | output | 1 | 107,574 | 22 | 215,149 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a, b, p, x = input().split()
a = int(a)
b = int(b)
p = int(p)
x = int(x)
count = 0
for n in range(1, x+1):
binary = "{0:b}".format(n)
mult = 1
total = 1
for i in range(len(binary)-1, -1, -1):
if binary[i]:
total *= a**mult
mult *= 2
if (n%p * total%p)%p == b%p:
count += 1
print(count)
``` | instruction | 0 | 107,575 | 22 | 215,150 |
No | output | 1 | 107,575 | 22 | 215,151 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
a,b,p,x = list(map(int,input().split()))
d = p*x+b
n = 1
k = n*a**n
t = 0
while(d > 1 and n <= x):
d //= k
n+=1
k = n*a**n
if(d): t+=1
print(t)
``` | instruction | 0 | 107,576 | 22 | 215,152 |
No | output | 1 | 107,576 | 22 | 215,153 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy $$$n ⋅ a^n ≡ b (mod\;p), where a, b, p$$$ are all known constants.
Input
The only line contains four integers a,b,p,x (2 ≤ p ≤ 10^6+3, 1 ≤ a,b < p, 1 ≤ x ≤ 10^{12}). It is guaranteed that p is a prime.
Output
Print a single integer: the number of possible answers n.
Examples
Input
2 3 5 8
Output
2
Input
4 6 7 13
Output
1
Input
233 233 10007 1
Output
1
Note
In the first sample, we can see that n=2 and n=8 are possible answers.
Submitted Solution:
```
inputt = [int(x) for x in input().split()]
a = inputt[0]
b = inputt[1]
p = inputt[2]
x = inputt[3]
N = 10 ** 6 + 10
A = [0] * (N)
A[0] = 1
ans = 0
for i in range(1, p):
A[i] = A[i - 1] * a % p
for j in range(1, p):
y = b * A[p - 1 - j] % p
i = (j - y + p) % p
tmp = i * (p - 1 ) + j
if x>=tmp:
ans += (x - tmp)/p/(p - 1) + 1
print(int(ans))
``` | instruction | 0 | 107,577 | 22 | 215,154 |
No | output | 1 | 107,577 | 22 | 215,155 |
Provide a correct Python 3 solution for this coding contest problem.
We have an integer sequence of length N: A_0,A_1,\cdots,A_{N-1}.
Find the following sum (\mathrm{lcm}(a, b) denotes the least common multiple of a and b):
* \sum_{i=0}^{N-2} \sum_{j=i+1}^{N-1} \mathrm{lcm}(A_i,A_j)
Since the answer may be enormous, compute it modulo 998244353.
Constraints
* 1 \leq N \leq 200000
* 1 \leq A_i \leq 1000000
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_0\ A_1\ \cdots\ A_{N-1}
Output
Print the sum modulo 998244353.
Examples
Input
3
2 4 6
Output
22
Input
8
1 2 3 4 6 8 12 12
Output
313
Input
10
356822 296174 484500 710640 518322 888250 259161 609120 592348 713644
Output
353891724 | instruction | 0 | 107,660 | 22 | 215,320 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
def primes(n):
is_prime = [True] * (n + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int((n+1)**0.5)+1):
if is_prime[i]:
for j in range(i *2, n + 1, i):
is_prime[j] = False
res = [i for i in range(n+1) if is_prime[i]]
return res
def make_modinv_list(n, mod=10**9+7):
inv_list = [0]*(n+1)
inv_list[1] = 1
for i in range(2, n+1):
inv_list[i] = (mod - mod//i * inv_list[mod%i] % mod)
return inv_list
def solve():
mod = 998244353
n = ni()
a = nl()
m = max(a)
s = -sum(a) % mod
l = [0]*(m+1)
for x in a:
l[x] += x
a = make_modinv_list(m, mod)
pr = primes(m)
for i in pr:
for j in range(m//i, 0, -1):
l[j] += l[j*i]
for i in range(m+1):
l[i] = l[i] * l[i] % mod
for i in pr:
for j in range(1, m//i + 1):
l[j] = (l[j] - l[j*i]) % mod
for i in range(1, m+1):
if l[i]:
s = (s + l[i] * a[i]) % mod
print(s * a[2] % mod)
return
solve()
``` | output | 1 | 107,660 | 22 | 215,321 |
Provide a correct Python 3 solution for this coding contest problem.
A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k, you should count the number of ways to express n as a sum of k different primes. Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5+ 3 but they are not distinguished.
When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are no other sets of three primes that sum up to 24. For n = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7,17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn't count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two diffrent primes whose sums are 4.
Your job is to write a program that reports the number of such ways for the given n and k.
Input
The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≤ 1120 and k ≤ 14.
Output
The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of ways for n and k specified in the corresponding dataset. You may assume that it is less than 231.
Example
Input
24 3
24 2
2 1
1 1
4 2
18 3
17 1
17 3
17 4
100 5
1000 10
1120 14
0 0
Output
2
3
1
0
0
2
1
0
1
55
200102899
2079324314 | instruction | 0 | 107,813 | 22 | 215,626 |
"Correct Solution:
```
primes = []
flag = [0 for i in range(1121)]
for i in range(2, 1121):
if flag[i] == 0:
primes.append(i)
count = 2
while i * count < 1121:
flag[i * count] = 1
count += 1
dp = [[0 for i in range(15)] for j in range(1121)]
dp[0][0] = 1
for p in primes:
for k in range(13, -1, -1):
for n in range(1121):
if n + p < 1121:
dp[n + p][k + 1] += dp[n][k]
else:
break
while True:
N, K = map(int, input().split())
if(N | K == 0):
break
print(dp[N][K])
``` | output | 1 | 107,813 | 22 | 215,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k, you should count the number of ways to express n as a sum of k different primes. Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5+ 3 but they are not distinguished.
When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are no other sets of three primes that sum up to 24. For n = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7,17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn't count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two diffrent primes whose sums are 4.
Your job is to write a program that reports the number of such ways for the given n and k.
Input
The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≤ 1120 and k ≤ 14.
Output
The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of ways for n and k specified in the corresponding dataset. You may assume that it is less than 231.
Example
Input
24 3
24 2
2 1
1 1
4 2
18 3
17 1
17 3
17 4
100 5
1000 10
1120 14
0 0
Output
2
3
1
0
0
2
1
0
1
55
200102899
2079324314
Submitted Solution:
```
for i in range(2,1201):
for j in range(2,int(i**0.5)+1):
if not i%j:break
else:p+=[i]
dp=[[0]*1200 for _ in range(15)]
dp[0][0]=1
for x,y in enumerate(p):
for i in range(min(x+1,14),0,-1):
for j in range(p[x],1200):
dp[i][j]+=dp[i-1][j-y]
while 1:
n,k=map(int,input().split())
if n==0:break
print(dp[k][n])
``` | instruction | 0 | 107,814 | 22 | 215,628 |
No | output | 1 | 107,814 | 22 | 215,629 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,459 | 22 | 216,918 |
"Correct Solution:
```
def gcd(a, b):
while b:
a, b = b, a % b
return a
def prime_decomposition(n):
i = 2
d = {}
while i * i <= n:
while n % i == 0:
n //= i
if i not in d:
d[i] = 0
d[i] += 1
i += 1
if n > 1:
if n not in d:
d[n] = 1
return d
def eratosthenes(n):
if n < 2:
return []
prime = []
limit = n**0.5
numbers = [i for i in range(2,n+1)]
while True:
p = numbers[0]
if limit <= p:
return prime + numbers
prime.append(p)
numbers = [i for i in numbers if i%p != 0]
return prime
def ok(p):
if A[0]%p != 0:
return False
B = [A[i]%p for i in range(1,N+1)]
mod = [0]*(p-1)
for i in range(N):
mod[i%(p-1)] += B[i]
mod[i%(p-1)] %= p
return sum(mod)==0
N = int(input())
A = [int(input()) for i in range(N+1)][::-1]
g = abs(A[0])
for a in A:
g = gcd(g,abs(a))
d = prime_decomposition(g)
ans = [p for p in d]
prime = eratosthenes(N+1)
for p in prime:
if ok(p):
ans.append(p)
ans = list(set(ans))
ans.sort()
for p in ans:
print(p)
``` | output | 1 | 108,459 | 22 | 216,919 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,460 | 22 | 216,920 |
"Correct Solution:
```
def gcd(a, b):
a, b = max(a, b), min(a, b)
while a % b > 0:
a, b = b, a%b
return b
def divisor(a):
i = 1
divset = set()
while i * i <= a:
if a % i == 0: divset |= {i, a//i}
i += 1
divset.remove(1)
return divset
def is_prime(a):
if a <= 3:
prime = [False, False, True, True]
return prime[a]
i = 2
while i * i <= a:
if a % i == 0: return False
i += 1
else: return True
N = int(input())
A = [None] * (N+1)
for i in reversed(range(N+1)):
A[i] = int(input())
primes_bool = [True] * (N + 1)
primes = []
for p in range(2, N+1):
if primes_bool[p]:
primes.append(p)
p_mult = p * 2
while p_mult <= N:
primes_bool[p_mult] = False
p_mult += p
ans = []
used = set()
gcd_of_A = abs(A[N])
for a in A[:N]:
if a != 0: gcd_of_A = gcd(abs(a), gcd_of_A)
commondiv = divisor(gcd_of_A)
for d in commondiv:
if d > 1 and is_prime(d):
ans.append(d)
used |= {d}
for p in primes:
if A[0] % p == 0:
for i in range(1, p):
coefficient = 0
index_a = i
while index_a <= N:
coefficient += A[index_a]
coefficient %= p
index_a += p-1
if coefficient > 0: break
else:
if p not in used: ans.append(p)
ans.sort()
for a in ans: print(a)
``` | output | 1 | 108,460 | 22 | 216,921 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,461 | 22 | 216,922 |
"Correct Solution:
```
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
sosuu=[2];n=int(input())
for L in range(3,n+100):
chk=True
for L2 in sosuu:
if L%L2 == 0:chk=False
if chk==True:sosuu.append(L)
S=set(sosuu)
A=[0]*(n+1)
g=0
for i in range(n+1):
A[i]=int(input())
P=[]
PP=factorization(abs(A[0]))
for i in range(len(PP)):
P.append(PP[i][0])
if P==[1]:
P=[]
P=set(P)
P=S|P
P=list(P)
P.sort()
for i in range(len(P)):
p=P[i]
B=A[0:n+2]
b=0
for j in range(n+1):
if p+j<n+1:
B[j+p-1]=(B[j+p-1]+B[j])%p
else:
if B[j]%p!=0:
b=1
break
if b==0:
print(p)
``` | output | 1 | 108,461 | 22 | 216,923 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,462 | 22 | 216,924 |
"Correct Solution:
```
def prime_fact(n: int)->list:
'''n の素因数を返す
'''
if n < 2:
return []
d = 2
res = []
while n > 1 and d * d <= n:
if n % d == 0:
res.append(d)
while n % d == 0:
n //= d
d += 1
if n > 1:
# n が素数
res.append(n)
return res
def gen_primes(n: int)->int:
'''n まで(n 含む)の素数を返します。
'''
if n < 2:
return []
primes = [2]
if n < 3:
return primes
primes.append(3)
def is_prime(n: int)->bool:
for p in primes:
if n % p == 0:
return False
return True
# 2,3 以降の素数は 6k+1 あるいは 6k-1
# の形をしていることを利用して列挙する。
for k in range((N+1)//6):
k += 1
if is_prime(6*k-1):
primes.append(6*k-1)
if is_prime(6*k+1):
primes.append(6*k+1)
return primes
def gcd(a: int, b: int)->int:
if a < b:
a, b = b, a
return a if b == 0 else gcd(b, a % b)
def gcd_list(A: list)->int:
if len(A) == 0:
return 0
if len(A) == 1:
return A[0]
g = abs(A[0])
for a in A[1:]:
g = gcd(g, abs(a))
return g
def polynominal_divisors(N: int, A: list)->list:
# primes = prime_fact(abs(A[-1]))
A.reverse()
# 候補となる素数は、N 以下の素数あるいは、aN の素因数
def check(p: int)->bool:
'''素数 p が任意の整数 x について f(x) を割り切れるかを
check する。具体的には、
- a0
- a1 + ap + a{2p-1} + ...
- a2 + a{p+1} + a{2p} + ...
...
- a{p-1} + a{2p-2} + ...
という項が p で割り切れるかを検査する。
'''
if A[0] % p != 0:
return False
for i in range(1, min(N, p)):
# ai + a{i+p-1} + ... を求めて
# p で割り切れるか検査する。
# print('p={}'.format(p))
b = 0
while i <= N:
# print('i={}'.format(i))
b = (b + A[i]) % p
i += p-1
if b != 0:
return False
return True
primes = set(gen_primes(N) + prime_fact(gcd_list(A)))
res = [p for p in primes if check(p)]
res.sort()
return res
if __name__ == "__main__":
N = int(input())
A = [int(input()) for _ in range(N+1)]
ans = polynominal_divisors(N, A)
for a in ans:
print(a)
``` | output | 1 | 108,462 | 22 | 216,925 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,463 | 22 | 216,926 |
"Correct Solution:
```
import sys
from fractions import gcd
n = int(input())
a = [int(input()) for _ in range(n+1)]
x = 0
while True:
m = abs(sum([a[i]*pow(x, n-i) for i in range(n+1)]))
if m != 0:
break
x += 1
ps = []
i = 2
while i**2 <= m and i <= n+1:
#for i in range(2, int(sqrt(m))+1):
if m%i == 0:
ps.append(i)
while m%i == 0:
m //= i
i += 1
if m != 1 and n != 0:
ps.append(m)
#print(ps)
g = a[0]
for i in range(1, n+1):
g = gcd(g, abs(a[i]))
#print(g)
ans = []
i = 2
while i**2 <= g:
#for i in range(2, int(sqrt(m))+1):
if g%i == 0:
if i >= n+1:
ans.append(i)
while g%i == 0:
g //= i
i += 1
if g != 1 and g >= n+1:
ans.append(g)
a = a[::-1]
for p in ps:
if p-1 > n:
ng = False
for i in range(1, n+1):
if a[i]%p != 0:
ng = True
break
if not ng:
ans.append(p)
else:
mods = [0 for _ in range(p-1)]
for i in range(1, n+1):
mods[i%(p-1)] += a[i]
mods[i%(p-1)] %= p
if mods[0] == 0:
ng = False
for i in range(1, p-1):
if mods[i] != 0:
ng = True
break
if not ng:
ans.append(p)
ans = sorted(list(set(ans)))
if ans:
print(*ans, sep="\n")
``` | output | 1 | 108,463 | 22 | 216,927 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,464 | 22 | 216,928 |
"Correct Solution:
```
from functools import reduce
import random
MOD = 59490679579998635868564022242503445659680322440679327938309703916140405638383434744833220555452130558140772894046169700747186409941317550665821619479140098743288335106272856533978719769677794737287419461899922456614256826709727133515885581878614846298251428096512495590545989536239739285290509870860732435109794045491640561011775744617218426011793283330497878491137999673760740717904979730106994180691956740996319139767941335979146924614249205862051182094081815447356600355657858936442589201739350474320966093143098768914226528498860747050474426418211313776073365498029087492308102876323562641651940555529817223923967479711293692697592800750806084802623966596666156361791014935954720420383555300213829134029560826306201326905674141090667002959274513478562033467485802590354539887133866434256562999666615074220857502563011098759348850149774083443643246907501430076141377431759984926248272553982527014011245310690986644461251576837779050124866641531488351016070300247918750613207952783437248236577750579092103591540592540838395240861443615842939673889972504776455114767522430347610500271801944701491874649306605557851914677316523034726044356368337787113190245601235705959816327016988315321850324065292964680148884818699916224411245926491625245571963741062587277736372090007125908660590314049976206281064703285728879581199596401313352724403492043120507597057004633720111008838673185885941472392623805512709896864520875740497617898566981218781313900004406341154884180472153171064617953661517880143988867189622824081729539392205701820144922307223627345876707465251206005262622236311161449785941834002795854996108322186230425179217618301526151712928790928933798369678844576216735378555233905935973195721247604933753363412045618703247367192610615234835041956551569232557323060407648325565048712478527583315981204846341857095134567470182330491262285172727037299211391244340592936174221176781260586188162350081192408213470101717320475998863222409777310443027421196981193126541663212124245716187453863438039402316877152286468198891603632606578778749292403571792687832081974134637014026451921536576338243322267400651083805535119025415817887075652758045539565968044552126338330231434466204888993650859153585380124240540573308417330478048240203241631072371322849430883727355239704116556046700749530006852187064160849175332758172150251213637470549781080491037088372092203085237973008861896576796238915011886636658033019385943299986285181723378096425117379056207797455963451889971988904466449319007760192467211209692128894691704353648198130409333996534250878389064152054828983825841234644875996912485916827004219887033833599723481903489316488764021700996686817244736947119285629049355809027206179193628292018441744552168286541735687924729198455883895791600170372255284216915442808139396541702893732917062958054499525549626455191658842064247047426187897146172971001949767308335268505414284088528125611263685734457560292833389995980698745893243832547007166243476192958601735336260255598581701267151224204461879782815468518040925292817115377696676461775120750971210951527384637825092221708015393564320979357698186262039029460777050248162599194429941464248920952161182024344007059684970270762248243899640259750891406957836740989312390963091260380990901672119736141666856448171380781556117025832312710039041398538035351795267732240730608951176127282191528457681241590895740457571038936173983449289126574141189374690274057472401359482497502067814596008557725079835212621242944853319496441084441343866446380876967613370281793088540430288658573281302876742323336651987699532240686371751448290351615451932054274752105688318766958111191822471878078268490672607804265064578569581247796205593441336042254502454646980009177290888726099355974892680373341371214123450598691915802122913613669446370194221846225597401405625536693874723700374068542217340621938985167525416725638580266200550048001242788847319217369432802469735271132107428204697172144851088692772696511622350096452418244968500432645305761138012204888798724453956720733374364721511323104353496583927094760495687785031050687852300161433757934364774351673531859394855389527851678630166084819717354779333605871648837631164630550613112327670353108353791304451834904017538583880634608113426156225757314283269948216017294095002859515842577481167417167637534527111130468710
def gcd(a, b):
b = abs(b)
while b != 0:
r = a%b
a,b = b,r
return a
def gcd_mult(numbers):
return reduce(gcd, numbers)
N = int(input())
A = [int(input()) for _ in range(N+1)][::-1]
g = gcd_mult(A)
A = [a//g for a in A]
def f(n):
ret = A[N]
for i in range(N-1, -1, -1):
ret = (ret * n + A[i]) % MOD
return ret
ans = MOD
for _ in range(20):
k = random.randrange(10**10)
ans = gcd(ans, f(k))
def primeFactor(N):
i = 2
ret = {}
n = N
if n < 0:
ret[-1] = 1
n = -n
if n == 0:
ret[0] = 1
d = 2
sq = int(n ** (1/2))
while i <= sq:
k = 0
while n % i == 0:
n //= i
k += 1
ret[i] = k
if k > 0:
sq = int(n**(1/2))
if i == 2:
i = 3
elif i == 3:
i = 5
elif d == 2:
i += 2
d = 4
else:
i += 4
d = 2
if n > 1:
ret[n] = 1
return ret
ANS = []
for i in primeFactor(ans*g):
ANS.append(i)
ANS = sorted(ANS)
for ans in ANS:
print(ans)
``` | output | 1 | 108,464 | 22 | 216,929 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,465 | 22 | 216,930 |
"Correct Solution:
```
import sys
from fractions import gcd
n = int(input())
a = [int(input()) for _ in range(n+1)]
x = 0
while True:
m = abs(sum([a[i]*pow(x, n-i) for i in range(n+1)]))
if m != 0:
break
x += 1
ps = []
i = 2
while i**2 <= m and i <= n+1:
if m%i == 0:
ps.append(i)
while m%i == 0:
m //= i
i += 1
if m != 1 and n != 0:
ps.append(m)
g = a[0]
for i in range(1, n+1):
g = gcd(g, abs(a[i]))
ans = []
i = 2
while i**2 <= g:
if g%i == 0:
if i >= n+1:
ans.append(i)
while g%i == 0:
g //= i
i += 1
if g != 1 and g >= n+1:
ans.append(g)
a = a[::-1]
for p in ps:
if p-1 > n:
ng = False
for i in range(1, n+1):
if a[i]%p != 0:
ng = True
break
if not ng:
ans.append(p)
else:
mods = [0 for _ in range(p-1)]
for i in range(1, n+1):
mods[i%(p-1)] += a[i]
mods[i%(p-1)] %= p
if mods[0] == 0:
ng = False
for i in range(1, p-1):
if mods[i] != 0:
ng = True
break
if not ng:
ans.append(p)
ans = sorted(list(set(ans)))
if ans:
print(*ans, sep="\n")
``` | output | 1 | 108,465 | 22 | 216,931 |
Provide a correct Python 3 solution for this coding contest problem.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353 | instruction | 0 | 108,466 | 22 | 216,932 |
"Correct Solution:
```
def factors(z):
ret = []
for i in range(2, int(z**(1/2))+1):
if z%i == 0:
ret.append(i)
while z%i == 0:
z //= i
if z != 1:
ret.append(z)
return ret
def eratosthenes(N):
if N == 0:
return []
from collections import deque
work = [True] * (N+1)
work[0] = False
work[1] = False
ret = []
for i in range(N+1):
if work[i]:
ret.append(i)
for j in range(2* i, N+1, i):
work[j] = False
return ret
N = int( input())
A = [ int( input()) for _ in range(N+1)]
Primes = eratosthenes(N)
ANS = []
F = factors( abs(A[0]))
for f in F:
if f >= N+1:
Primes.append(f)
for p in Primes:
if p >= N+1:
check = 1
for i in range(N+1): # f が恒等的に 0 であるかどうかのチェック
if A[i]%p != 0:
check = 0
break
if check == 1:
ANS.append(p)
else:
poly = [0]*(p-1)
for i in range(N+1): # フェルマーの小定理
poly[(N-i)%(p-1)] = (poly[(N-i)%(p-1)] + A[i])%p
check = 0
if sum(poly) == 0 and A[N]%p == 0: # a_0 が 0 かつ、g が恒等的に 0 であることをチェックしている
check = 1
if check == 1:
ANS.append(p)
for ans in ANS:
print(ans)
``` | output | 1 | 108,466 | 22 | 216,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353
Submitted Solution:
```
# E
# 入力
# input
N = int(input())
a_list = [0]*(N+1)
for i in range(N+1):
a_list[N-i] = int(input())
# 10^5までの素数を計算
# list primes <= 10^5
primes = list(range(10**5))
primes[0] = 0
primes[1] = 0
for p in range(2, 10**5):
if primes[p]:
for i in range(2*p, 10**5, p):
primes[i] = 0
primes = [p for p in primes if p>0]
# primes <= N
p_list_small = [p for p in primes if p <= N]
# A_Nを素因数分解、Nより大きい素因数をp_list_largeに追加
# A_N, ..., A_0の最大公約数の素因数でやっても同じ
# list prime factors of A_N, if greater then N, add to p_list_large
# you can use prime factors of gcd(A_N, ..., A_0) instead
p_list_large = []
A = abs(a_list[-1])
for p in primes:
if A % p == 0:
if p > N:
p_list_large.append(p)
while A % p == 0:
A = A // p
if A != 1:
p_list_large.append(A)
# 個別に条件確認
# check all p in p_list_small
# x^p ~ x (mod p)
res_list = []
for p in p_list_small:
r = 0
check_list = [0]*(N+1)
for i in range(p):
check_list[i] = a_list[i]
for i in range(p, N+1):
check_list[(i-1) % (p-1) + 1] += a_list[i]
for i in range(p):
if check_list[i] % p != 0:
r = 1
if r == 0:
res_list.append(p)
# 個別に条件確認
# check all p in p_list_large
# a_i % p == 0
for p in p_list_large:
r = 0
for i in range(N+1):
if a_list[i] % p != 0:
r = 1
if r == 0:
res_list.append(p)
for p in res_list:
print(p)
``` | instruction | 0 | 108,467 | 22 | 216,934 |
Yes | output | 1 | 108,467 | 22 | 216,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353
Submitted Solution:
```
def gcd(x,y):
if x<y:
x,y=y,x
if y==0:
return x
if x%y==0:
return y
else:
return gcd(x%y,y)
N=int(input())
a=[int(input()) for i in range(N+1)][::-1]
#N=10000
#a=[i+1 for i in range(N+1)]
g=abs(a[0])
for i in range(1,N+1):
g=gcd(g,abs(a[i]))
def check(P):
b=[a[i]%P for i in range(1,N+1)]
if a[0]%P!=0:
return False
if N<P:
for i in b:
if i!=0:
return False
return True
else:
c=[0 for i in range(P-1)]
for i in range(N):
c[i%(P-1)]+=b[i]
c[i%(P-1)]%=P
for i in c:
if i!=0:
return False
return True
Plist=set()
X=[1 for i in range(max(N+1,3))]
X[0]=0;X[1]=0
i=2
while(i*i<=N):
for j in range(i,N+1,i):
if i==j:
if X[i]==0:
break
else:
X[j]=0
i+=1
i=2
tmp=g
while(i*i<=g):
if tmp%i==0:
Plist.add(i)
while(1):
if tmp%i==0:
tmp=tmp//i
else:
break
i+=1
if tmp>1:
Plist.add(tmp)
for j in range(N+1):
if X[j]==1:
Plist.add(j)
Plist=sorted(list(Plist))
for p in Plist:
if check(p):
print(p)
``` | instruction | 0 | 108,468 | 22 | 216,936 |
Yes | output | 1 | 108,468 | 22 | 216,937 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353
Submitted Solution:
```
def p_factors(n):
if n < 2:
return []
d = 2
result = []
while n > 1 and d*d <= n:
if n % d == 0:
result.append(d)
while n % d == 0:
n //= d
d += 1
if n > 1:
# n is a prime
result.append(n)
return result
def sieve_of_eratosthenes(ub):
# O(n loglog n) implementation.
# returns prime numbers <= ub.
if ub < 2:
return []
prime = [True]*(ub+1)
prime[0] = False
prime[1] = False
p = 2
while p*p <= ub:
if prime[p]:
# don't need to check p+1 ~ p^2-1 (they've already been checked).
for i in range(p*p, ub+1, p):
prime[i] = False
p += 1
return [i for i in range(ub+1) if prime[i]]
def gcd(a, b):
if a < b:
a, b = b, a
while b:
a, b = b, a%b
return a
def gcd_list(lst):
if len(lst) == 0:
return 0
if len(lst) == 1:
return lst[0]
g = abs(lst[0])
for l in lst[1:]:
g = gcd(g, abs(l))
return g
N = int(input())
A = [int(input()) for _ in range(N+1)]
cands = set(sieve_of_eratosthenes(N) + p_factors(gcd_list(A)))
answer = []
A = list(reversed(A))
for p in cands:
flag = True
# f(x) = Q(x) (x^p - x) + \sum_{i = 0}^{p-1} h_i x^i (mod p),
# where
# h_0 = a_0 (i = 0)
# h_i = \sum_{j < N, j % (p-1) = i} a_j (i = 1, ..., p-2)
# h_{p-1} = \sum_{j < N, j % (p-1) = 0} a_j - a_0 (i = p-1)
if A[0] % p != 0:
flag = False
else:
for i in range(1, min(N, p-1) + 1):
h = 0
j = i
while j <= N:
h = (h + A[j]) % p
j += p - 1
if h != 0:
flag = False
break
if flag:
answer.append(p)
if answer:
answer = sorted(answer)
print(*answer, sep='\n')
``` | instruction | 0 | 108,469 | 22 | 216,938 |
Yes | output | 1 | 108,469 | 22 | 216,939 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353
Submitted Solution:
```
import sys
from fractions import gcd
def eratosthenes_generator():
yield 2
n = 3
h = {}
while True:
m = n
if n in h:
b = h[n]
del h[n]
else:
b = n
yield n
m += b << 1
while m in h:
m += b << 1
h[m] = b
n += 2
def prime(n):
ret = []
if n % 2 == 0:
ret.append(2)
while n % 2 == 0:
n >>= 1
for i in range(3, int(n ** 0.5) + 1, 2):
if n % i == 0:
ret.append(i)
while n % i == 0:
n = n // i
if n == 1:
break
if n > 1:
ret.append(n)
return ret
def solve(n, aaa):
g = abs(aaa[-1])
for a in aaa[:-1]:
if a != 0:
g = gcd(g, abs(a))
ans = set(prime(g))
for p in eratosthenes_generator():
if p > n + 2:
break
if p in ans or aaa[0] % p != 0:
continue
q = p - 1
tmp = [0] * q
for i, a in enumerate(aaa):
tmp[i % q] += a
if all(t % p == 0 for t in tmp):
ans.add(p)
ans = sorted(ans)
return ans
n = int(input())
aaa = list(map(int, sys.stdin))
aaa.reverse()
print('\n'.join(map(str, solve(n, aaa))))
``` | instruction | 0 | 108,470 | 22 | 216,940 |
Yes | output | 1 | 108,470 | 22 | 216,941 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353
Submitted Solution:
```
def prime_fact(n: int)->list:
'''n の素因数を返す
'''
if n < 2:
return []
d = 2
res = []
while n > 1 and d * d <= n:
if n % d == 0:
res.append(d)
while n % d == 0:
n //= d
d += 1
if n > 1:
# n が素数
res.append(n)
return res
def gen_primes(n: int)->int:
'''n まで(n 含む)の素数を返します。
'''
if n < 2:
return []
primes = [2]
if n < 3:
return primes
primes.append(3)
def is_prime(n: int)->bool:
for p in primes:
if n % p == 0:
return False
return True
# 2,3 以降の素数は 6k+1 あるいは 6k-1
# の形をしていることを利用して列挙する。
for k in range((N+1)//6):
k += 1
if is_prime(6*k-1):
primes.append(6*k-1)
if is_prime(6*k+1):
primes.append(6*k+1)
return primes
def polynominal_divisors(N: int, A: list)->list:
# primes = prime_fact(abs(A[-1]))
A.reverse()
# 候補となる素数は、N 以下の素数あるいは、aN の素因数
def check(p: int)->bool:
'''素数 p が任意の整数 x について f(x) を割り切れるかを
check する。具体的には、
- a0
- a1 + ap + a{2p-1} + ...
- a2 + a{p+1} + a{2p} + ...
...
- a{p-1} + a{2p-2} + ...
という項が p で割り切れるかを検査する。
'''
if A[0] % p != 0:
return False
for i in range(1, p):
# ai + a{i+p-1} + ... を求めて
# p で割り切れるか検査する。
b = 0
while i <= N:
b = (b + A[i]) % p
i += p-1
if b != 0:
return False
return True
primes = set([p for p in gen_primes(N) if check(p)] + prime_fact(A[-1]))
return sorted(primes)
if __name__ == "__main__":
N = int(input())
A = [int(input()) for _ in range(N+1)]
ans = polynominal_divisors(N, A)
for a in ans:
print(a)
``` | instruction | 0 | 108,471 | 22 | 216,942 |
No | output | 1 | 108,471 | 22 | 216,943 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.
Constraints
* 0 \leq N \leq 10^4
* |a_i| \leq 10^9(0\leq i\leq N)
* a_N \neq 0
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
a_N
:
a_0
Output
Print all prime numbers p that divide f(x) for every integer x, in ascending order.
Examples
Input
2
7
-7
14
Output
2
7
Input
3
1
4
1
5
Output
Input
0
998244353
Output
998244353
Submitted Solution:
```
def eratosthenes(n):
l=[1]*n
r=[]
for i in range(2,n+1):
#i=2
#while i<=n:
if l[i-1]!=0:
r+=[i]
j=2
while i*j<=n:
l[i*j-1]=0
j+=1
# i+=1
return r
Ps = eratosthenes(40000)
def divisor(n):
for p in Ps:
if p*p>n:return -1
if n%p==0:return p
def prime_division(n):
d={}
while n>1:
p=divisor(n)
if p==-1:d[n]=d.get(n,0)+1;break
else:d[p]=d.get(p,0)+1;n//=p
return d
import math
from functools import reduce
def gcd(*numbers):
return reduce(math.gcd, numbers)
N=int(input())
A=[int(input())for _ in[0]*-~N]
G=sorted(set(prime_division(gcd(*A)).keys())|set(eratosthenes(N)))
def div(p):
if A[-1]%p!=0:return A[-1]%p
l=[a%p for a in A]
r=[a%p for a in A[:p]]
ind=0
for i in range(N-p+1):
a=r[ind];ind+=1
r[-1]=(r[-1]+a)%p
r+=[l[p+i]]
return sum(r[-p:])
for p in G:
if div(p)==0:print(p)
``` | instruction | 0 | 108,472 | 22 | 216,944 |
No | output | 1 | 108,472 | 22 | 216,945 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.