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. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}.
instruction
0
105,634
22
211,268
Tags: math Correct Solution: ``` a, b = (int(x) for x in input().split()) ans = b*(b-1)*a//2 + b*(b-1)*b*(1 + a)*a//4 print(ans % 1000000007) ```
output
1
105,634
22
211,269
Provide tags and a correct Python 3 solution for this coding contest problem. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}.
instruction
0
105,635
22
211,270
Tags: math Correct Solution: ``` #input a,b=map(int,input().split()) #variables nicesum=(b*(b-1)//2)*(a*(a+1)//2)*b+(b*(b-1)//2)*a #main print(int(nicesum%1000000007)) ```
output
1
105,635
22
211,271
Provide tags and a correct Python 3 solution for this coding contest problem. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}.
instruction
0
105,636
22
211,272
Tags: math Correct Solution: ``` a, b = map(int, input().split()) print((b * (a * (a + 1) // 2) * (b * (b - 1) // 2 ) + a * b * (b - 1) // 2) % 1000000007) ```
output
1
105,636
22
211,273
Provide tags and a correct Python 3 solution for this coding contest problem. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}.
instruction
0
105,637
22
211,274
Tags: math Correct Solution: ``` a,b = map(int, input().split()) sum = 0 mod = 1000000007 def c(i): mi = i * b + i ma = i * b * a + i return ((mi+ma) * a)//2 sum = (((c(1) + c(b-1))*(b-1))//2)%mod print(sum) ```
output
1
105,637
22
211,275
Provide tags and a correct Python 3 solution for this coding contest problem. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}.
instruction
0
105,638
22
211,276
Tags: math Correct Solution: ``` a = input() a = a.split() a,b = int(a[0]),int(a[1]) k2 = b*(b-1)*a // 2 k = (b-1)*b//2 * b* (1+a)*a // 2 res = (k + k2) % 1000000007 print(res) ```
output
1
105,638
22
211,277
Provide tags and a correct Python 3 solution for this coding contest problem. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}.
instruction
0
105,639
22
211,278
Tags: math Correct Solution: ``` a, b = map(int, input().split()) r1 = (b - 1) * b // 2 r2 = (a * b + 1 + b + 1) * a // 2 print((r1 * r2) % 1000000007) ```
output
1
105,639
22
211,279
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` #Collaborated with Rudransh singh and Bhumi patel a, b = input().split(" ") a = int(a) b = int(b) print((b * (b - 1) // 2) * (((a * (a + 1) // 2) * b) % 1000000007 + a) % 1000000007) ```
instruction
0
105,640
22
211,280
Yes
output
1
105,640
22
211,281
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` a,b=map(int,input().split()) dangbra=(b*(b-1)//2)*(a*(a+1)//2)*b+(b*(b-1)//2)*a print(int(dangbra%1000000007)) ```
instruction
0
105,641
22
211,282
Yes
output
1
105,641
22
211,283
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` a, b = map(int, input().split()) print((b * (b - 1) * b * a * (a + 1) // 4 + (b - 1) * b * a // 2) % (10 ** 9 + 7)) ```
instruction
0
105,642
22
211,284
Yes
output
1
105,642
22
211,285
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` # Collaborated with no one input1 = list(map(int, input().split(" "))) a = input1[0] b = input1[1] quotient = b*(b-1)//2 remainder = a*(a+1)//2 answer = (quotient*((remainder*b)%(10**9+7)+a)%(10**9+7)) print(answer) ```
instruction
0
105,643
22
211,286
Yes
output
1
105,643
22
211,287
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` a,b=map(int,input().split()) print(b*(b-1)*a*(b*a+b+2)/4%(10**9+7)) ```
instruction
0
105,644
22
211,288
No
output
1
105,644
22
211,289
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` #Created By Minsol Jeong def N3(): a, b = [int(x) for x in input().split()] mod = 10 ** 9 + 7 x = ((b * (b-1))/2) % mod y = (a * (a+1)/2)% mod sum = (x * ((y * b) % mod + a) % mod) % mod print(sum) if __name__=="__main__": N3() ```
instruction
0
105,645
22
211,290
No
output
1
105,645
22
211,291
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict import heapq #range = xrange # not for python 3.0+ # main code mod=10**9+7 ans=0 a,b=map(int,stdin.read().split()) for i in range(1,b): #print val val=a t1=(val*(val+1))/2 t1%=mod t1=(t1*b)%mod temp=(t1*i)%mod temp=(temp+((i*val)%mod))%mod ans=(ans+temp)%mod print(ans) ```
instruction
0
105,646
22
211,292
No
output
1
105,646
22
211,293
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of x and y. By <image> we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT. The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon? Input The single line of the input contains two integers a, b (1 ≤ a, b ≤ 107). Output Print a single integer representing the answer modulo 1 000 000 007 (109 + 7). Examples Input 1 1 Output 0 Input 2 2 Output 8 Note For the first sample, there are no nice integers because <image> is always zero. For the second sample, the set of nice integers is {3, 5}. Submitted Solution: ``` a, b = map(int, input().split()) ans = ((a * a * b + 2 * a) * b * (b - 1) * (2 * b - 1)) // 12 + (a * b * b * (b - 1)) // 4 print(ans % 1000000007) ```
instruction
0
105,647
22
211,294
No
output
1
105,647
22
211,295
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of. Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about. Input A single line contains number n (1 ≤ n ≤ 103). Output Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them. Examples Input 4 Output 3 2 4 3 Input 6 Output 4 2 4 3 5 Note The sequence from the answer to the first sample test is actually correct. If the unknown number is not divisible by one of the sequence numbers, it is equal to 1. If the unknown number is divisible by 4, it is 4. If the unknown number is divisible by 3, then the unknown number is 3. Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
instruction
0
105,689
22
211,378
Tags: math, number theory Correct Solution: ``` def SieveOfEratosthenes(n): primes = [] prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 for p in range(2, n+1): if prime[p]: primes.append(p) return primes N = int(input()) primes = SieveOfEratosthenes(N) newList = primes.copy() for i in primes: power = 2 while i ** power <= N: newList.append(i ** power) power += 1 print(len(newList)) print(*newList) ```
output
1
105,689
22
211,379
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of. Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about. Input A single line contains number n (1 ≤ n ≤ 103). Output Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them. Examples Input 4 Output 3 2 4 3 Input 6 Output 4 2 4 3 5 Note The sequence from the answer to the first sample test is actually correct. If the unknown number is not divisible by one of the sequence numbers, it is equal to 1. If the unknown number is divisible by 4, it is 4. If the unknown number is divisible by 3, then the unknown number is 3. Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
instruction
0
105,690
22
211,380
Tags: math, number theory Correct Solution: ``` n = int(input()) primes = [] is_prime = (n + 1) * [ True ] is_prime[0] = is_prime[1] = False pos = 0 while True: while pos <= n and not is_prime[pos]: pos += 1 if pos > n: break for i in range(2 * pos, n + 1, pos): is_prime[i] = False primes.append(pos) pos += 1 def get_factors(x): factors = [] for factor in primes: if x % factor == 0: factors.append(factor) return factors ask = [] for x in range(2, n + 1): factors = get_factors(x) if len(factors) == 1: ask.append(x) print(len(ask)) print(' '.join(map(str, ask))) ```
output
1
105,690
22
211,381
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of. Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about. Input A single line contains number n (1 ≤ n ≤ 103). Output Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them. Examples Input 4 Output 3 2 4 3 Input 6 Output 4 2 4 3 5 Note The sequence from the answer to the first sample test is actually correct. If the unknown number is not divisible by one of the sequence numbers, it is equal to 1. If the unknown number is divisible by 4, it is 4. If the unknown number is divisible by 3, then the unknown number is 3. Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
instruction
0
105,692
22
211,384
Tags: math, number theory Correct Solution: ``` def gen_prime(): num = [0 for i in range(10**3+1)] num[2] = 1 num[3] = 1 for i in range(4, 10**3+1): isprime = True for j in range(2, int(i**0.5)+1): if num[j]: if not i%j: isprime = False break if isprime: num[i] = 1 prime = [] for i in range(2, 10**3+1): if num[i]: prime.append(i) return prime import sys def inp(): return sys.stdin.readline().strip() t=1 #t=int(inp()) for _ in range(t): #n,m =map(int, inp().split()) n=int(input()) primes=gen_prime() ans=[] a=0 for i in primes: a=1 ii=i while(ii<=n): ans.append(ii) ii=ii*i print(len(ans)) print(*ans) ```
output
1
105,692
22
211,385
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of. Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about. Input A single line contains number n (1 ≤ n ≤ 103). Output Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them. Examples Input 4 Output 3 2 4 3 Input 6 Output 4 2 4 3 5 Note The sequence from the answer to the first sample test is actually correct. If the unknown number is not divisible by one of the sequence numbers, it is equal to 1. If the unknown number is divisible by 4, it is 4. If the unknown number is divisible by 3, then the unknown number is 3. Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
instruction
0
105,693
22
211,386
Tags: math, number theory Correct Solution: ``` def gen_prime(): num = [0 for i in range(10**3+1)] num[2] = 1 num[3] = 1 for i in range(4, 10**3+1): isprime = True for j in range(2, int(i**0.5)+1): if num[j]: if not i%j: isprime = False break if isprime: num[i] = 1 prime = [] for i in range(2, 10**3+1): if num[i]: prime.append(i) return prime n = int(input()) prime = gen_prime() div = [] i = 0 while i < len(prime): for j in range(1, 10): if prime[i]**j > n: break div.append(prime[i]**j) i += 1 print(len(div)) for i in range(len(div)): print(div[i], end = " ") print() ```
output
1
105,693
22
211,387
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of. Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about. Input A single line contains number n (1 ≤ n ≤ 103). Output Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them. Examples Input 4 Output 3 2 4 3 Input 6 Output 4 2 4 3 5 Note The sequence from the answer to the first sample test is actually correct. If the unknown number is not divisible by one of the sequence numbers, it is equal to 1. If the unknown number is divisible by 4, it is 4. If the unknown number is divisible by 3, then the unknown number is 3. Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter.
instruction
0
105,695
22
211,390
Tags: math, number theory Correct Solution: ``` def is_prime(n): for i in range(2, n): if ((n % i) == 0): return 0 break return 1 n = int(input()) if (n == 1): print(0) else: q = [] for i in range(2, n + 1): if (is_prime(i)): q.append(i) k = i while(1): k = k * i if (k <= n): q.append(k) else: break print(len(q)) for i in q: print(i, end=' ') ```
output
1
105,695
22
211,391
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of. Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about. Input A single line contains number n (1 ≤ n ≤ 103). Output Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them. Examples Input 4 Output 3 2 4 3 Input 6 Output 4 2 4 3 5 Note The sequence from the answer to the first sample test is actually correct. If the unknown number is not divisible by one of the sequence numbers, it is equal to 1. If the unknown number is divisible by 4, it is 4. If the unknown number is divisible by 3, then the unknown number is 3. Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter. Submitted Solution: ``` import math isprime = [0]*1000010 prime = [] def seive(): isprime[1] = isprime[0] = 1 prime.append(2) limit = int(math.sqrt(1000010))+2 for i in range(4,1000010,2): isprime[i] = 1 for i in range(3,1000010,2): if(not isprime[i]): prime.append(i) if(i <= limit): for j in range(i*i,1000010,i*2): isprime[j] = 1 seive() n = int(input()) output_list = [] index = 0 while prime[index]<=n: p = 1 x = pow(prime[index],p) while(x<=n): output_list.append(x) p+=1 x = pow(prime[index],p) index+=1 print(len(output_list)) for x in output_list: print(x,end=" ") print() ```
instruction
0
105,697
22
211,394
Yes
output
1
105,697
22
211,395
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Vasya and Petya are playing a simple game. Vasya thought of number x between 1 and n, and Petya tries to guess the number. Petya can ask questions like: "Is the unknown number divisible by number y?". The game is played by the following rules: first Petya asks all the questions that interest him (also, he can ask no questions), and then Vasya responds to each question with a 'yes' or a 'no'. After receiving all the answers Petya should determine the number that Vasya thought of. Unfortunately, Petya is not familiar with the number theory. Help him find the minimum number of questions he should ask to make a guaranteed guess of Vasya's number, and the numbers yi, he should ask the questions about. Input A single line contains number n (1 ≤ n ≤ 103). Output Print the length of the sequence of questions k (0 ≤ k ≤ n), followed by k numbers — the questions yi (1 ≤ yi ≤ n). If there are several correct sequences of questions of the minimum length, you are allowed to print any of them. Examples Input 4 Output 3 2 4 3 Input 6 Output 4 2 4 3 5 Note The sequence from the answer to the first sample test is actually correct. If the unknown number is not divisible by one of the sequence numbers, it is equal to 1. If the unknown number is divisible by 4, it is 4. If the unknown number is divisible by 3, then the unknown number is 3. Otherwise, it is equal to 2. Therefore, the sequence of questions allows you to guess the unknown number. It can be shown that there is no correct sequence of questions of length 2 or shorter. Submitted Solution: ``` def gen_prime(): num = [0 for i in range(10**3+1)] num[2] = 1 num[3] = 1 for i in range(4, 10**3+1): isprime = True for j in range(2, int(i**0.5)+1): if num[j]: if not i%j: isprime = False break if isprime: num[i] = 1 prime = [] for i in range(2, 10**3+1): if num[i]: prime.append(i) return prime n = int(input()) prime = gen_prime() div = []; high_div = 1 i = 0 while i < len(prime): for j in range(1, 10): if prime[i]**j > n: break div.append(prime[i]**j) if n%(prime[i]**j): break elif high_div < prime[i]**j: high_div = prime[i]**j i += 1 # if high_div*prime[i] > n: # if prime[i] < n: # div.append(prime[i]) # break print(len(div)) for i in range(len(div)): print(div[i], end = " ") print() ```
instruction
0
105,703
22
211,406
No
output
1
105,703
22
211,407
Provide a correct Python 3 solution for this coding contest problem. The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Example Input 10 11 27 2 492170 0 Output 4 0 6 0 114
instruction
0
106,053
22
212,106
"Correct Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- import math def sieve_of_erastosthenes(num): input_list = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)] input_list[0] = input_list[1] = False input_list[2] = input_list[3] = input_list[5] = True sqrt = math.sqrt(num) for serial in range(3, num, 2): if serial >= sqrt: return input_list for s in range(serial ** 2, num, serial): input_list[s] = False primeTable = sieve_of_erastosthenes(13*(10**5)) while True: k = int(input()) if k == 0: break if primeTable[k]: print(0) else: i = k while primeTable[i] is False: i += 1 j = i-1 while primeTable[j] is False: j -= 1 print(i-j) ```
output
1
106,053
22
212,107
Provide a correct Python 3 solution for this coding contest problem. The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Example Input 10 11 27 2 492170 0 Output 4 0 6 0 114
instruction
0
106,054
22
212,108
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 998244353 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) class Prime(): def __init__(self, n): self.M = m = int(math.sqrt(n)) + 10 self.A = a = [True] * m a[0] = a[1] = False self.T = t = [] for i in range(2, int(math.sqrt(m)) + 1): if not a[i]: continue t.append(i) for j in range(i*i,m,i): a[j] = False def is_prime(self, n): return self.A[n] def main(): rr = [] pr = Prime(1299709**2) while True: n = I() if n == 0: break l = r = n while not pr.is_prime(l): l -= 1 while not pr.is_prime(r): r += 1 rr.append(r-l) return '\n'.join(map(str, rr)) print(main()) ```
output
1
106,054
22
212,109
Provide a correct Python 3 solution for this coding contest problem. The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Example Input 10 11 27 2 492170 0 Output 4 0 6 0 114
instruction
0
106,055
22
212,110
"Correct Solution: ``` def make_prime_table(n): """n以下の非負整数が素数であるかを判定したリストを出力する 計算量: O(NloglogN) 入出力例: 6 -> [False, False, True, True, False, True, False] """ is_prime = [True] * (n + 1) is_prime[0] = False is_prime[1] = False for i in range(2, int(n ** 0.5) + 1): if not is_prime[i]: continue for j in range(2 * i, n + 1, i): is_prime[j] = False return is_prime is_prime = make_prime_table(2 * 10 ** 6) while True: n = int(input()) if n == 0: break l = n r = n while not is_prime[l]: l -= 1 while not is_prime[r]: r += 1 print(r - l) ```
output
1
106,055
22
212,111
Provide a correct Python 3 solution for this coding contest problem. The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Example Input 10 11 27 2 492170 0 Output 4 0 6 0 114
instruction
0
106,056
22
212,112
"Correct Solution: ``` primes = [0, 0] + 1299709*[1] for i in range(2, 1141): if primes[i]: for j in range(i*i, 1299710, i): primes[j] = 0 while True: n = int(input()) if n == 0: break if primes[n]: print(0) continue a = b = n while not primes[a]: a -= 1 while not primes[b]: b += 1 print(b-a) ```
output
1
106,056
22
212,113
Provide a correct Python 3 solution for this coding contest problem. The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Example Input 10 11 27 2 492170 0 Output 4 0 6 0 114
instruction
0
106,057
22
212,114
"Correct Solution: ``` def sieve(): L = 1299710 primes = [] isPrime = [ True for _ in range(L) ] for i in range(2, L): if isPrime[i]: primes.append(i) for j in range(i * i, L, i): isPrime[j] = False return primes def getPrimeGap(num, primes): if num in primes: return 0 for i in range(100000 - 1): if primes[i] < num and primes[i + 1] > num: return primes[i + 1] - primes[i] if __name__ == '__main__': primes = sieve() while True: num = int(input()) if num == 0: break print(getPrimeGap(num, primes)) ```
output
1
106,057
22
212,115
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Example Input 10 11 27 2 492170 0 Output 4 0 6 0 114 Submitted Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- import math def sieve_of_erastosthenes(num): input_list = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)] input_list[0] = input_list[1] = False input_list[2] = input_list[3] = input_list[5] = True sqrt = math.sqrt(num) for serial in range(3, num, 2): if serial >= sqrt: return input_list for s in range(serial ** 2, num, serial): input_list[s] = False primeTable = sieve_of_erastosthenes(13*(10**5)) while True: k = int(input()) if k == 0: break if primeTable[k]: print(0) else: i = 0 while i < k or primeTable[i] == False: if primeTable[i]: prevPrime = i i+= 1 print(i-prevPrime) ```
instruction
0
106,058
22
212,116
No
output
1
106,058
22
212,117
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6. Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. Input The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. Output The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. Example Input 10 11 27 2 492170 0 Output 4 0 6 0 114 Submitted Solution: ``` #!/usr/bin/env python # -*- coding: utf-8 -*- import math def sieve_of_erastosthenes(num): input_list = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)] input_list[0] = input_list[1] = False input_list[2] = input_list[3] = input_list[5] = True sqrt = math.sqrt(num) for serial in range(3, num, 2): if serial >= sqrt: return input_list for s in range(serial ** 2, num, serial): input_list[s] = False primeTable = sieve_of_erastosthenes(13*10**5) while True: k = int(input()) if k == 0: break if primeTable[k]: print(0) else: i = 0 while i < k or primeTable[i] == False: if primeTable[i]: prevPrime = i i+= 1 print(i-prevPrime) ```
instruction
0
106,059
22
212,118
No
output
1
106,059
22
212,119
Provide tags and a correct Python 3 solution for this coding contest problem. You are given integer n. You have to arrange numbers from 1 to 2n, using each of them exactly once, on the circle, so that the following condition would be satisfied: For every n consecutive numbers on the circle write their sum on the blackboard. Then any two of written on the blackboard 2n numbers differ not more than by 1. For example, choose n = 3. On the left you can see an example of a valid arrangement: 1 + 4 + 5 = 10, 4 + 5 + 2 = 11, 5 + 2 + 3 = 10, 2 + 3 + 6 = 11, 3 + 6 + 1 = 10, 6 + 1 + 4 = 11, any two numbers differ by at most 1. On the right you can see an invalid arrangement: for example, 5 + 1 + 6 = 12, and 3 + 2 + 4 = 9, 9 and 12 differ more than by 1. <image> Input The first and the only line contain one integer n (1 ≤ n ≤ 10^5). Output If there is no solution, output "NO" in the first line. If there is a solution, output "YES" in the first line. In the second line output 2n numbers — numbers from 1 to 2n in the order they will stay in the circle. Each number should appear only once. If there are several solutions, you can output any of them. Examples Input 3 Output YES 1 4 5 2 3 6 Input 4 Output NO Note Example from the statement is shown for the first example. It can be proved that there is no solution in the second example.
instruction
0
106,211
22
212,422
Tags: constructive algorithms, greedy, math Correct Solution: ``` def divisors(M): d=[] i=1 while M>=i**2: if M%i==0: d.append(i) if i**2!=M: d.append(M//i) i=i+1 return d def popcount(x): x = x - ((x >> 1) & 0x55555555) x = (x & 0x33333333) + ((x >> 2) & 0x33333333) x = (x + (x >> 4)) & 0x0f0f0f0f x = x + (x >> 8) x = x + (x >> 16) return x & 0x0000007f def eratosthenes(n): res=[0 for i in range(n+1)] prime=set([]) for i in range(2,n+1): if not res[i]: prime.add(i) for j in range(1,n//i+1): res[i*j]=1 return prime def factorization(n): res=[] for p in prime: if n%p==0: while n%p==0: n//=p res.append(p) if n!=1: res.append(n) return res def euler_phi(n): res = n for x in range(2,n+1): if x ** 2 > n: break if n%x==0: res = res//x * (x-1) while n%x==0: n //= x if n!=1: res = res//n * (n-1) return res def ind(b,n): res=0 while n%b==0: res+=1 n//=b return res def isPrimeMR(n): d = n - 1 d = d // (d & -d) L = [2, 3, 5, 7, 11, 13, 17] for a in L: t = d y = pow(a, t, n) if y == 1: continue while y != n - 1: y = (y * y) % n if y == 1 or t == n - 1: return 0 t <<= 1 return 1 def findFactorRho(n): from math import gcd m = 1 << n.bit_length() // 8 for c in range(1, 99): f = lambda x: (x * x + c) % n y, r, q, g = 2, 1, 1, 1 while g == 1: x = y for i in range(r): y = f(y) k = 0 while k < r and g == 1: ys = y for i in range(min(m, r - k)): y = f(y) q = q * abs(x - y) % n g = gcd(q, n) k += m r <<= 1 if g == n: g = 1 while g == 1: ys = f(ys) g = gcd(abs(x - ys), n) if g < n: if isPrimeMR(g): return g elif isPrimeMR(n // g): return n // g return findFactorRho(g) def primeFactor(n): i = 2 ret = {} rhoFlg = 0 while i*i <= n: k = 0 while n % i == 0: n //= i k += 1 if k: ret[i] = k i += 1 + i % 2 if i == 101 and n >= 2 ** 20: while n > 1: if isPrimeMR(n): ret[n], n = 1, 1 else: rhoFlg = 1 j = findFactorRho(n) k = 0 while n % j == 0: n //= j k += 1 ret[j] = k if n > 1: ret[n] = 1 if rhoFlg: ret = {x: ret[x] for x in sorted(ret)} return ret def divisors(n): res = [1] prime = primeFactor(n) for p in prime: newres = [] for d in res: for j in range(prime[p]+1): newres.append(d*p**j) res = newres res.sort() return res def xorfactorial(num): if num==0: return 0 elif num==1: return 1 elif num==2: return 3 elif num==3: return 0 else: x=baseorder(num) return (2**x)*((num-2**x+1)%2)+function(num-2**x) def xorconv(n,X,Y): if n==0: res=[(X[0]*Y[0])%mod] return res x=[X[i]+X[i+2**(n-1)] for i in range(2**(n-1))] y=[Y[i]+Y[i+2**(n-1)] for i in range(2**(n-1))] z=[X[i]-X[i+2**(n-1)] for i in range(2**(n-1))] w=[Y[i]-Y[i+2**(n-1)] for i in range(2**(n-1))] res1=xorconv(n-1,x,y) res2=xorconv(n-1,z,w) former=[(res1[i]+res2[i])*inv for i in range(2**(n-1))] latter=[(res1[i]-res2[i])*inv for i in range(2**(n-1))] former=list(map(lambda x:x%mod,former)) latter=list(map(lambda x:x%mod,latter)) return former+latter def merge_sort(A,B): pos_A,pos_B = 0,0 n,m = len(A),len(B) res = [] while pos_A < n and pos_B < m: a,b = A[pos_A],B[pos_B] if a < b: res.append(a) pos_A += 1 else: res.append(b) pos_B += 1 res += A[pos_A:] res += B[pos_B:] return res class UnionFindVerSize(): def __init__(self, N): self._parent = [n for n in range(0, N)] self._size = [1] * N self.group = N def find_root(self, x): if self._parent[x] == x: return x self._parent[x] = self.find_root(self._parent[x]) stack = [x] while self._parent[stack[-1]]!=stack[-1]: stack.append(self._parent[stack[-1]]) for v in stack: self._parent[v] = stack[-1] return self._parent[x] def unite(self, x, y): gx = self.find_root(x) gy = self.find_root(y) if gx == gy: return self.group -= 1 if self._size[gx] < self._size[gy]: self._parent[gx] = gy self._size[gy] += self._size[gx] else: self._parent[gy] = gx self._size[gx] += self._size[gy] def get_size(self, x): return self._size[self.find_root(x)] def is_same_group(self, x, y): return self.find_root(x) == self.find_root(y) class WeightedUnionFind(): def __init__(self,N): self.parent = [i for i in range(N)] self.size = [1 for i in range(N)] self.val = [0 for i in range(N)] self.flag = True self.edge = [[] for i in range(N)] def dfs(self,v,pv): stack = [(v,pv)] new_parent = self.parent[pv] while stack: v,pv = stack.pop() self.parent[v] = new_parent for nv,w in self.edge[v]: if nv!=pv: self.val[nv] = self.val[v] + w stack.append((nv,v)) def unite(self,x,y,w): if not self.flag: return if self.parent[x]==self.parent[y]: self.flag = (self.val[x] - self.val[y] == w) return if self.size[self.parent[x]]>self.size[self.parent[y]]: self.edge[x].append((y,-w)) self.edge[y].append((x,w)) self.size[x] += self.size[y] self.val[y] = self.val[x] - w self.dfs(y,x) else: self.edge[x].append((y,-w)) self.edge[y].append((x,w)) self.size[y] += self.size[x] self.val[x] = self.val[y] + w self.dfs(x,y) class Dijkstra(): class Edge(): def __init__(self, _to, _cost): self.to = _to self.cost = _cost def __init__(self, V): self.G = [[] for i in range(V)] self._E = 0 self._V = V @property def E(self): return self._E @property def V(self): return self._V def add_edge(self, _from, _to, _cost): self.G[_from].append(self.Edge(_to, _cost)) self._E += 1 def shortest_path(self, s): import heapq que = [] d = [10**15] * self.V d[s] = 0 heapq.heappush(que, (0, s)) while len(que) != 0: cost, v = heapq.heappop(que) if d[v] < cost: continue for i in range(len(self.G[v])): e = self.G[v][i] if d[e.to] > d[v] + e.cost: d[e.to] = d[v] + e.cost heapq.heappush(que, (d[e.to], e.to)) return d #Z[i]:length of the longest list starting from S[i] which is also a prefix of S #O(|S|) def Z_algorithm(s): N = len(s) Z_alg = [0]*N Z_alg[0] = N i = 1 j = 0 while i < N: while i+j < N and s[j] == s[i+j]: j += 1 Z_alg[i] = j if j == 0: i += 1 continue k = 1 while i+k < N and k + Z_alg[k]<j: Z_alg[i+k] = Z_alg[k] k += 1 i += k j -= k return Z_alg class BIT(): def __init__(self,n,mod=0): self.BIT = [0]*(n+1) self.num = n self.mod = mod def query(self,idx): res_sum = 0 mod = self.mod while idx > 0: res_sum += self.BIT[idx] if mod: res_sum %= mod idx -= idx&(-idx) return res_sum #Ai += x O(logN) def update(self,idx,x): mod = self.mod while idx <= self.num: self.BIT[idx] += x if mod: self.BIT[idx] %= mod idx += idx&(-idx) return class dancinglink(): def __init__(self,n,debug=False): self.n = n self.debug = debug self._left = [i-1 for i in range(n)] self._right = [i+1 for i in range(n)] self.exist = [True for i in range(n)] def pop(self,k): if self.debug: assert self.exist[k] L = self._left[k] R = self._right[k] if L!=-1: if R!=self.n: self._right[L],self._left[R] = R,L else: self._right[L] = self.n elif R!=self.n: self._left[R] = -1 self.exist[k] = False def left(self,idx,k=1): if self.debug: assert self.exist[idx] res = idx while k: res = self._left[res] if res==-1: break k -= 1 return res def right(self,idx,k=1): if self.debug: assert self.exist[idx] res = idx while k: res = self._right[res] if res==self.n: break k -= 1 return res class SparseTable(): def __init__(self,A,merge_func,ide_ele): N=len(A) n=N.bit_length() self.table=[[ide_ele for i in range(n)] for i in range(N)] self.merge_func=merge_func for i in range(N): self.table[i][0]=A[i] for j in range(1,n): for i in range(0,N-2**j+1): f=self.table[i][j-1] s=self.table[i+2**(j-1)][j-1] self.table[i][j]=self.merge_func(f,s) def query(self,s,t): b=t-s+1 m=b.bit_length()-1 return self.merge_func(self.table[s][m],self.table[t-2**m+1][m]) class BinaryTrie: class node: def __init__(self,val): self.left = None self.right = None self.max = val def __init__(self): self.root = self.node(-10**15) def append(self,key,val): pos = self.root for i in range(29,-1,-1): pos.max = max(pos.max,val) if key>>i & 1: if pos.right is None: pos.right = self.node(val) pos = pos.right else: pos = pos.right else: if pos.left is None: pos.left = self.node(val) pos = pos.left else: pos = pos.left pos.max = max(pos.max,val) def search(self,M,xor): res = -10**15 pos = self.root for i in range(29,-1,-1): if pos is None: break if M>>i & 1: if xor>>i & 1: if pos.right: res = max(res,pos.right.max) pos = pos.left else: if pos.left: res = max(res,pos.left.max) pos = pos.right else: if xor>>i & 1: pos = pos.right else: pos = pos.left if pos: res = max(res,pos.max) return res def solveequation(edge,ans,n,m): #edge=[[to,dire,id]...] x=[0]*m used=[False]*n for v in range(n): if used[v]: continue y = dfs(v) if y!=0: return False return x def dfs(v): used[v]=True r=ans[v] for to,dire,id in edge[v]: if used[to]: continue y=dfs(to) if dire==-1: x[id]=y else: x[id]=-y r+=y return r class SegmentTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num self.size = n for i in range(n): self.tree[self.num + i] = init_val[i] for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): if r==self.size: r = self.num res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res def bisect_l(self,l,r,x): l += self.num r += self.num Lmin = -1 Rmin = -1 while l<r: if l & 1: if self.tree[l] <= x and Lmin==-1: Lmin = l l += 1 if r & 1: if self.tree[r-1] <=x: Rmin = r-1 l >>= 1 r >>= 1 if Lmin != -1: pos = Lmin while pos<self.num: if self.tree[2 * pos] <=x: pos = 2 * pos else: pos = 2 * pos +1 return pos-self.num elif Rmin != -1: pos = Rmin while pos<self.num: if self.tree[2 * pos] <=x: pos = 2 * pos else: pos = 2 * pos +1 return pos-self.num else: return -1 import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import gcd,log input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) n = int(input()) if n%2==0: print("NO") else: print("YES") ans = [0 for i in range(2*n)] for i in range(n): if i%2==0: ans[i] = 2*i + 1 ans[i+n] = 2*i + 2 else: ans[i] = 2*i + 2 ans[i+n] = 2*i + 1 print(*ans) ```
output
1
106,211
22
212,423
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,540
22
213,080
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` class DisjointSet: def __init__(self, n): self._fa = list(range(n)) def union(self, x, y): x = self.get_father(x) y = self.get_father(y) self._fa[x] = y return y def get_father(self, x): y = self._fa[x] if self._fa[y] == y: return y else: z = self._fa[y] = self.get_father(y) return z def __repr__(self): return repr([self.get_father(i) for i in range(len(self._fa))]) def solve(n, a, b, xs): h = {x: i for i, x in enumerate(xs)} if a == b: if all(a - x in h for x in xs): return [0] * n return False g1 = n g2 = n + 1 ds = DisjointSet(n + 2) for i, x in enumerate(xs): for t in (a, b): if t - x in h: ds.union(i, h[t - x]) for i, x in enumerate(xs): b1 = (a - x) in h b2 = (b - x) in h if b1 + b2 == 0: return False if b1 + b2 == 1: if b1: ds.union(i, g1) else: ds.union(i, g2) if ds.get_father(g1) == ds.get_father(g2): return False group = [None] * n for i, x in enumerate(xs): f = ds.get_father(i) if f < n: return False group[i] = f - n return group n, a, b = map(int, input().split()) xs = list(map(int, input().split())) group = solve(n, a, b, xs) if isinstance(group, list): print('YES') print(' '.join(map(str, group))) else: print('NO') ```
output
1
106,540
22
213,081
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,541
22
213,082
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` import sys import queue sys.setrecursionlimit(100000) # global constant INF = int(1e7+1) MAX = 100005 # For testing #sys.stdin = open("INP.txt", 'r') #sys.stdout = open("OUT.txt", 'w') # global variables parents = [] ranks = [] size = [] n = 0 # classes class Pair: def __init__(self, a, b): self.first = a self.second = b # functions def init(): global parents, ranks, size parents = [i for i in range(n+2)] ranks = [0 for i in range(n+2)] size = [1 for i in range(n+2)] def findSet(u): if(parents[u] != u): parents[u] = findSet(parents[u]) return parents[u] def unionSet(u, v): up = findSet(u) vp = findSet(v) if up == vp: return if ranks[up] < ranks[vp]: parents[up] = vp elif ranks[vp] < ranks[up]: parents[vp] = up else: ranks[vp] += 1 parents[up] = vp # main function def main(): global n n, a, b = map(int, input().split()) init() M = {} l = list(map(int, input().split())) Max = -1 l.insert(0, -1) l.append(-1) for i in range(1, n+1): M[l[i]] = i parents[i] = i Max = max(Max, l[i]) if Max >= a and Max >= b: print("NO") return for i in range(1, n+1): # print(l[i], a-l[i], b-l[i], (a-l[i] in M), (b-l[i]) in M) if (a-l[i]) in M: unionSet(M[a - l[i]], i) else: unionSet(i, n+1) if(b-l[i]) in M: unionSet(M[b-l[i]], i) else: unionSet(i, 0) A = findSet(0) B = findSet(n+1) """ for parent in parents: print(parent, end = ' ') print() print(A, B) """ if(A == B): print("NO") return print("YES") for i in range(1, n+1): # print(l[i], findSet(l[i])) if (findSet(i) == A): print("%d" % (0), end=' ') else: print("%d" % (1), end=' ') main() ```
output
1
106,541
22
213,083
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,542
22
213,084
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` def process(X, a, b): X1 = set(X) Other = set([]) A = set([]) B = set([]) Both = set([]) for x in X: if a-x in X1 and b-x not in X1: A.add(x) A.add(a-x) elif a-x not in X1 and b-x in X1: B.add(x) B.add(b-x) elif a-x not in X1 and b-x not in X1: return 'NO' else: Both.add(x) start = A.copy() while len(start) > 0: next_s = set([]) for x in start: if b-x in Both: Both.remove(b-x) next_s.add(b-x) if a-b+x in Both: Both.remove(a-b+x) A.add(a-b+x) next_s.add(a-b+x) A.add(b-x) if a-x in Both: Both.remove(a-x) next_s.add(a-x) A.add(a-x) elif a-x in B or a-x not in A: return 'NO' start = next_s start = B.copy() while len(start) > 0: next_s = set([]) for x in start: if a-x in Both: Both.remove(a-x) next_s.add(a-x) if b-a+x in Both: Both.remove(b-a+x) B.add(b-a+x) next_s.add(b-a+x) B.add(a-x) if b-x in Both: Both.remove(b-x) next_s.add(b-x) B.add(b-x) elif b-x in A or b-x not in B: return 'NO' start = next_s answer = [] for x in X: if x in A: answer.append(0) else: answer.append(1) return answer n, a, b = [int(x) for x in input().split()] X = [int(x) for x in input().split()] answer = process(X, a, b) if answer=='NO': print('NO') else: print('YES') print(' '.join(map(str, answer))) ```
output
1
106,542
22
213,085
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,543
22
213,086
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` # Problem from Codeforces # http://codeforces.com/problemset/problem/468/B parent = [] ranks = [] def make_set(N): global parent, ranks parent = [i for i in range(N + 5)] ranks = [0 for i in range(N + 5)] def find_set(u): if parent[u] != u: parent[u] = find_set(parent[u]) else: parent[u] = u return parent[u] def union_set(u, v): up = find_set(u) vp = find_set(v) # if ranks[up] is None: # ranks[up] = 1 # if ranks.get(vp) is None: # ranks[vp] = 1 if ranks[up] > ranks[vp]: parent[vp] = up elif ranks[up] < ranks[vp]: parent[up] = vp else: parent[up] = vp ranks[vp] += 1 def solution(): set_indicators = dict() n, a, b = map(int, input().split()) numbers = list(map(int, input().split())) A = n + 1 B = A + 1 make_set(n) for i in range(n): set_indicators[numbers[i]] = i for i in range(n): if set_indicators.get(a - numbers[i]) is not None: union_set(i, set_indicators.get(a - numbers[i])) else: union_set(i, B) if set_indicators.get(b - numbers[i]) is not None: union_set(i, set_indicators.get(b - numbers[i])) else: union_set(i, A) if find_set(A) == find_set(B): print('NO') return print('YES') for i in range(n): print(1 if find_set(i) == find_set(n + 2) else 0) solution() ```
output
1
106,543
22
213,087
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,544
22
213,088
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` def findSet(u, parent): if parent[u] != u: parent[u] = findSet(parent[u], parent) return parent[u] def unionSet(u, v, parent): up = findSet(u, parent) vp = findSet(v, parent) parent[up] = vp if __name__ == '__main__': n, a, b = map(int, input().split()) lst = list(map(int, input().split())) parent = [i for i in range(n + 2)] temp = {lst[i]: i for i in range(n)} for i in range(n): if a - lst[i] in temp: unionSet(i, temp[a - lst[i]], parent) else: unionSet(i, n, parent) if b - lst[i] in temp: unionSet(i, temp[b - lst[i]], parent) else: unionSet(i, n + 1, parent) pa = findSet(n, parent) pb = findSet(n + 1, parent) if pa == pb: print('NO') else: print('YES') lst = [0 if findSet(i, parent) == pb else 1 for i in range(n)] print(*lst) ```
output
1
106,544
22
213,089
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,545
22
213,090
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` def findSet(u): if parents[u] != u: parents[u] = findSet(parents[u]) return parents[u] def unionSet(u, v): up = findSet(u) vp = findSet(v) if up == vp: return if ranks[up] > ranks[vp]: parents[vp] = up elif ranks[up] < ranks[vp]: parents[up] = vp else: parents[up] = vp ranks[vp] += 1 n, a, b = map(int, input().split()) ps = list(map(int, input().split())) mapping = set(ps) parents = {x: x for x in ps} parents['A'] = 'A' parents['B'] = 'B' ranks = {x: 0 for x in ps} ranks['A'] = 0 ranks['B'] = 0 # print(parents) result = True for x in ps: if a - x in mapping: unionSet(x, a - x) else: unionSet(x, 'B') if b - x in mapping: unionSet(x, b - x) else: unionSet(x, 'A') # print(parents) # print(parents) if findSet('A') == findSet('B'): print("NO") else: print("YES") for i in ps: if findSet(i) == findSet('A'): print("0", end = ' ') else: print("1", end = ' ') ```
output
1
106,545
22
213,091
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,546
22
213,092
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` import sys from collections import defaultdict as dd input = sys.stdin.readline I = lambda : list(map(int,input().split())) n,a,b=I() l=I() dic=dd(int) for i in range(n): dic[l[i]]=1 bs=[] pa=dd(int) for i in range(n): if dic[a-l[i]]==0: bs.append(l[i]) else: pa[l[i]]=a-l[i] j=0 while j<len(bs): for i in range(j,len(bs)): cr=bs[i] dic[cr]=2 if dic[b-cr]==0: print("NO");exit() dic[b-cr]=2 if dic[a-b+cr]==1: dic[a-b+cr]=2 bs.append(a-b+cr) j+=1 #ct=0;vt=a-b+cr #while vt!=pa[pa[vt]]: # vt=pa[vt];dic[b-vt]=2 # dic[vt]=2 an=[0]*n for i in range(n): an[i]=dic[l[i]]-1 print("YES") print(*an) ```
output
1
106,546
22
213,093
Provide tags and a correct Python 3 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,547
22
213,094
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy Correct Solution: ``` parent = [i for i in range(100002)] def findSet(u): if parent[u] != u: parent[u] = findSet(parent[u]) return parent[u] def unionSet(u, v): up = findSet(u) vp = findSet(v) parent[up] = vp if __name__ == '__main__': n, a, b = map(int, input().split()) lst = list(map(int, input().split())) temp = {lst[i]: i for i in range(n)} for i in range(n): if a - lst[i] in temp: unionSet(i, temp[a - lst[i]]) else: unionSet(i, n) if b - lst[i] in temp: unionSet(i, temp[b - lst[i]]) else: unionSet(i, n + 1) pa = findSet(n) pb = findSet(n + 1) if pa == pb: print('NO') else: print('YES') lst = [0 if findSet(i) == pb else 1 for i in range(n)] print(*lst) ```
output
1
106,547
22
213,095
Provide tags and a correct Python 2 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,548
22
213,096
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy 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 tuple(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+ inp=inp() n,a,b=inp[0],inp[1],inp[2] l=inp[3:] d=Counter() for i in range(n): d[l[i]]=i+1 group = [ i for i in range(n+2) ] rank = [1] * (n+2) def find(u): if group[u] != u: group[u] = find(group[u]) return group[u] def union(u, v): u = find(u) v = find(v) if u == v: return if rank[u] > rank[v]: u, v = v, u group[u] = v if rank[u] == rank[v]: rank[v] += 1 for i in range(n): if d[a-l[i]]: union(i,d[a-l[i]]-1) else: union(i,n) if d[b-l[i]]: union(i,d[b-l[i]]-1) else: union(i,n+1) if find(n)==find(n+1): pr('NO') else: pr('YES\n') for i in range(n): if find(i)==find(n): pr('1 ') else: pr('0 ') ```
output
1
106,548
22
213,097
Provide tags and a correct Python 2 solution for this coding contest problem. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty.
instruction
0
106,549
22
213,098
Tags: 2-sat, dfs and similar, dsu, graph matchings, greedy 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+ n,a,b=in_arr() l=in_arr() d=Counter() for i in range(n): d[l[i]]=i+1 group = [ i for i in range(n+2) ] rank = [1] * (n+2) def find(u): if group[u] != u: group[u] = find(group[u]) return group[u] def union(u, v): u = find(u) v = find(v) if u == v: return if rank[u] > rank[v]: u, v = v, u group[u] = v if rank[u] == rank[v]: rank[v] += 1 for i in range(n): if d[a-l[i]]: union(i,d[a-l[i]]-1) else: union(i,n) if d[b-l[i]]: union(i,d[b-l[i]]-1) else: union(i,n+1) if find(n)==find(n+1): pr('NO') else: pr('YES\n') for i in range(n): if find(i)==find(n): pr('1 ') else: pr('0 ') ```
output
1
106,549
22
213,099
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` parent = [i for i in range(int(1e5 + 2))] def findSet(u): if parent[u] != u: parent[u] = findSet(parent[u]) return parent[u] def unionSet(u, v): up = findSet(u) vp = findSet(v) parent[up] = vp if __name__ == '__main__': n, a, b = map(int, input().split()) lst = list(map(int, input().split())) temp = {lst[i]: i for i in range(n)} for i in range(n): if a - lst[i] in temp: unionSet(i, temp[a - lst[i]]) else: unionSet(i, n) if b - lst[i] in temp: unionSet(i, temp[b - lst[i]]) else: unionSet(i, n + 1) pa = findSet(n) pb = findSet(n + 1) if pa == pb: print('NO') else: print('YES') lst = [0 if findSet(i) == pb else 1 for i in range(n)] print(*lst) ```
instruction
0
106,550
22
213,100
Yes
output
1
106,550
22
213,101
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` def find(u): global par if u != par[u]: par[u] = find(par[u]) return par[u] def union(u, v): u = find(u) v = find(v) par[u] = v n, a, b = map(int, input().split()) p = list(map(int, input().split())) mp = dict() for i in range(n): mp[p[i]] = i + 1 par = [i for i in range(n + 2)] for i in range(n): union(i + 1, mp.get(a - p[i], n + 1)) union(i + 1, mp.get(b - p[i], 0)) A = find(0) B = find(n + 1) if A != B: print('YES') print(' '.join(['1' if find(i) == B else '0' for i in range(1, n + 1)])) else: print('NO') ```
instruction
0
106,551
22
213,102
Yes
output
1
106,551
22
213,103
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` class UnionFind: def __init__(self, n): # self.nums = nums self.parent = [i for i in range(n+2)] self.rank = [0 for i in range(n+2)] # self.setNumber = [-1 for i in range(n)] def union(self, u, v): up = self.find(u) vp = self.find(v) if up == vp: return if self.rank[up] == self.rank[vp]: self.parent[up] = vp self.rank[vp] += 1 # self.setNumber[up] = self elif self.rank[up] < self.rank[vp]: self.parent[up] = vp else: self.parent[vp] = up def find(self, u): if u != self.parent[u]: self.parent[u] = self.find(self.parent[u]) return self.parent[u] n, a, b = map(int, input().split()) nums = list(map(int, input().split())) numToIndex = {} for i, num in enumerate(nums): numToIndex[num] = i uf = UnionFind(n) for i, num in enumerate(nums): complA = a - num if complA in numToIndex: uf.union(i, numToIndex[complA]) else: # Else it's in B uf.union(i, n+1) complB = b - num if complB in numToIndex: uf.union(i, numToIndex[complB]) else: # Else it's in A uf.union(i, n) # Number of clusters # counter = 0 # for i in range(n+2): # if i == uf.find(i): # counter += 1 # print(counter) setA = uf.find(n) setB = uf.find(n+1) # print(setA) # print(setB) if setA == setB: print('NO') else: print('YES') ans = [] for i in range(n): if uf.find(i) == uf.find(n): ans.append('0') else: ans.append('1') print(' '.join(ans)) ```
instruction
0
106,552
22
213,104
Yes
output
1
106,552
22
213,105
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` from collections import defaultdict def solve(n, a, b, xs): group = [None] * n id_ = {x: i for i, x in enumerate(xs)} if a == b: for x in xs: if a - x not in id_: return False group = [0] * n else: for i, x in enumerate(xs): if group[i] is not None: continue y = a - x z = b - x f1 = y in id_ and group[id_[y]] is None f2 = z in id_ and group[id_[z]] is None if f1 + f2 == 0: return False elif f1 + f2 == 1: g = int(f2) # End of link link = [] t = a if f1 else b while x in id_: link.append(x) x = t - x if x + x == t: break t = a + b - t # print(link) if len(link) % 2 == 0: for i, x in enumerate(link): group[id_[x]] = g elif link[0] * 2 == (b, a)[g]: for i, x in enumerate(link): group[id_[x]] = 1 - g elif link[-1] * 2 == (a, b)[g]: for i, x in enumerate(link): group[id_[x]] = g else: # Found invalid link, answer is "NO" return False return group n, a, b = map(int, input().split()) xs = list(map(int, input().split())) group = solve(n, a, b, xs) if isinstance(group, list): print('YES') print(' '.join(map(str, group))) else: print('NO') ```
instruction
0
106,553
22
213,106
Yes
output
1
106,553
22
213,107
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` n,a,b=map(int,input().split()) t=input().split() l={int(t[i]):i+1 for i in range(n)} f=[2]*n for x in l: if l.get(a-x,0): f[l[x]-1]=0 elif l.get(b-x,0): f[l[x]-1]=1 if 2 in f: print("NO") else: print('YES\n',' '.join(map(str,f))) ```
instruction
0
106,554
22
213,108
No
output
1
106,554
22
213,109
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` def foo(key_q1, key_q2, m, key): global p, q, out for i in q[key_q1]: j = 0 while j < len(q[key_q2]): if p[i]+p[q[key_q2][j]] == m: out[i] = key out[q[key_q2][j]] = key p[i] = -1*p[i] p[q[key_q2][j]] = -1*p[q[key_q2][j]] del q[key_q2][j] else: j += 1 q[key_q1] = [] n, a, b = map(int, input().split()) p = list(map(int, input().split())) if a < b: m1 = a m2 = b key1 = 0 key2 = 1 else: m1 = b m2 = a key1 = 1 key2 = 0 k = m2-m1 out=[] for i in range(n): out.append(-1) q = [] for i in range(m2//k+1): q.append([]) res = 'YES' for i in range(n): if p[i] >= m2: res = 'NO' break if p[i] <= ((m2//2)//k) * k: q[(p[i]-1)//k].append(i) if p[i] >= m2 - ((m2//2)//k) * k: q[m2//k-(m2-p[i]-1)//k].append(i) if res == 'YES': for i in range(len(q)//2): foo(len(q)-1-i, i, m2, key2) foo(i, len(q)-2-i, m1, key1) if res == 'YES': for i in range(n): if out[i] >= 0: continue for j in range(i, n): if out[j] >= 0: continue if p[i]+p[j] == m1: out[i] = key1 out[j] = key1 p[i] = -p[i] p[j] = -p[j] if p[i]+p[j] == m2: out[i] = key2 out[j] = key2 p[i] = -p[i] p[j] = -p[j] s = '' for i in out: if i == -1: res = 'NO' break s += str(i) + " " print(res) if(res == 'YES'): print(s) ```
instruction
0
106,555
22
213,110
No
output
1
106,555
22
213,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` n,a,b=map(int,input().strip().split()) l=[int(i) for i in input().split()] a1=[a-i for i in l] b1=[b-i for i in l] c1=[1 for i in a1 if i in l] c2=[1 for i in b1 if i in l] r=[1 if i in b1 else 0 for i in l] if(n%2==0): if(sum(c1)==n or sum(c2)==n or sum(c1)+sum(c2)==n): print("YES") print(*r) else: print("NO") else: if(sum(c1)==n or sum(c2)==n): print("YES") print(*r) else: print("NO") ```
instruction
0
106,556
22
213,112
No
output
1
106,556
22
213,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied: * If number x belongs to set A, then number a - x must also belong to set A. * If number x belongs to set B, then number b - x must also belong to set B. Help Little X divide the numbers into two sets or determine that it's impossible. Input The first line contains three space-separated integers n, a, b (1 ≤ n ≤ 105; 1 ≤ a, b ≤ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 ≤ pi ≤ 109). Output If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B. If it's impossible, print "NO" (without the quotes). Examples Input 4 5 9 2 3 4 5 Output YES 0 0 1 1 Input 3 3 4 1 2 4 Output NO Note It's OK if all the numbers are in the same set, and the other one is empty. Submitted Solution: ``` #------------------Important Modules------------------# from sys import stdin,stdout from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import * from random import * input=stdin.readline prin=stdout.write from random import sample from collections import Counter,deque from fractions import * from math import sqrt,ceil,log2,gcd #dist=[0]*(n+1) mod=10**9+7 """ class DisjSet: def __init__(self, n): self.rank = [1] * n self.parent = [i for i in range(n)] def find(self, x): if (self.parent[x] != x): self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): xset = self.find(x) yset = self.find(y) if xset == yset: return if self.rank[xset] < self.rank[yset]: self.parent[xset] = yset elif self.rank[xset] > self.rank[yset]: self.parent[yset] = xset else: self.parent[yset] = xset self.rank[xset] = self.rank[xset] + 1 """ def f(arr,i,j,d,dist): if i==j: return nn=max(arr[i:j]) for tl in range(i,j): if arr[tl]==nn: dist[tl]=d #print(tl,dist[tl]) f(arr,i,tl,d+1,dist) f(arr,tl+1,j,d+1,dist) #return dist def ps(n): cp=0;lk=0;arr={} #print(n) #cc=0; while n%2==0: n=n//2 #arr.append(n);arr.append(2**(lk+1)) lk+=1 for ps in range(3,ceil(sqrt(n))+1,2): lk=0 cc=0 while n%ps==0: n=n//ps #cc=1 #arr.append(n);arr.append(ps**(lk+1)) lk+=1 if n!=1: #lk+=1 #arr[n]=1 #ans*=n; lk+=1 return False #return arr #print(arr) return True #count=0 #dp=[[0 for i in range(m)] for j in range(n)] #[int(x) for x in input().strip().split()] def gcd(x, y): while(y): x, y = y, x % y return x # Driver Code def factorials(n,r): #This calculates ncr mod 10**9+7 slr=n;dpr=r qlr=1;qs=1 mod=10**9+7 for ip in range(n-r+1,n+1): qlr=(qlr*ip)%mod for ij in range(1,r+1): qs=(qs*ij)%mod #print(qlr,qs) ans=(qlr*modInverse(qs))%mod return ans def modInverse(b): qr=10**9+7 return pow(b, qr - 2,qr) #=============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func def power(arr): listrep = arr subsets = [] for i in range(2**len(listrep)): subset = [] for k in range(len(listrep)): if i & 1<<k: subset.append(listrep[k]) subsets.append(subset) return subsets def pda(n) : list = [] for i in range(1, int(sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : list.append(i) else : list.append(n//i);list.append(i) # The list will be printed in reverse return list def dis(xa,ya,xb,yb): return sqrt((xa-xb)**2+(ya-yb)**2) #### END ITERATE RECURSION #### #=============================================================================================== #----------Input functions--------------------# def ii(): return int(input()) def ilist(): return [int(x) for x in input().strip().split()] def islist(): return list(map(str,input().split().rstrip())) def inp(): return input().strip() def google(test,ans): return "Case #"+str(test)+": "+str(ans); def overlap(x1,y1,x2,y2): if x2>y1: return y1-x2 if y1>y2: return y2-x2 return y1-x2; ###-------------------------CODE STARTS HERE--------------------------------########### def pal(s): k=len(s) n=len(s)//2 for i in range(n): if s[i]==s[k-1-i]: continue else: return 0 return 1 ######################################################################################### #t=int(input()) t=1 for p in range(t): n,a,b=ilist() perm=ilist() dic={} ans=[0]*n for i in range(n): dic[perm[i]]=i ss=set(perm) cc=0 while len(ss)>0: ab=ss.pop() if (a-ab) in ss: ss.remove(a-ab) ans[dic[ab]]=ans[dic[a-ab]]=0 elif (b-ab) in ss: ss.remove(b-ab) ans[dic[ab]]=ans[dic[b-ab]]=1 else: cc=1 break if cc==1: print("NO") continue print("YES") print(*ans) ```
instruction
0
106,557
22
213,114
No
output
1
106,557
22
213,115
Provide tags and a correct Python 3 solution for this coding contest problem. For given n, l and r find the number of distinct geometrical progression, each of which contains n distinct integers not less than l and not greater than r. In other words, for each progression the following must hold: l ≤ ai ≤ r and ai ≠ aj , where a1, a2, ..., an is the geometrical progression, 1 ≤ i, j ≤ n and i ≠ j. Geometrical progression is a sequence of numbers a1, a2, ..., an where each term after first is found by multiplying the previous one by a fixed non-zero number d called the common ratio. Note that in our task d may be non-integer. For example in progression 4, 6, 9, common ratio is <image>. Two progressions a1, a2, ..., an and b1, b2, ..., bn are considered different, if there is such i (1 ≤ i ≤ n) that ai ≠ bi. Input The first and the only line cotains three integers n, l and r (1 ≤ n ≤ 107, 1 ≤ l ≤ r ≤ 107). Output Print the integer K — is the answer to the problem. Examples Input 1 1 10 Output 10 Input 2 6 9 Output 12 Input 3 1 10 Output 8 Input 3 3 10 Output 2 Note These are possible progressions for the first test of examples: * 1; * 2; * 3; * 4; * 5; * 6; * 7; * 8; * 9; * 10. These are possible progressions for the second test of examples: * 6, 7; * 6, 8; * 6, 9; * 7, 6; * 7, 8; * 7, 9; * 8, 6; * 8, 7; * 8, 9; * 9, 6; * 9, 7; * 9, 8. These are possible progressions for the third test of examples: * 1, 2, 4; * 1, 3, 9; * 2, 4, 8; * 4, 2, 1; * 4, 6, 9; * 8, 4, 2; * 9, 3, 1; * 9, 6, 4. These are possible progressions for the fourth test of examples: * 4, 6, 9; * 9, 6, 4.
instruction
0
107,491
22
214,982
Tags: brute force, math, number theory Correct Solution: ``` Primes = [2, 3, 5, 7] def check (a, b) : for pr in Primes : if a % pr == 0 and b % pr == 0 : return False while (b) : a %= b a ^= b b ^= a a ^= b return a == 1 n, l, r = map(int, input().split()) if n == 1 : ans = r - l + 1 elif n == 2 : ans = (r - l + 1) * (r - l) else : n -= 1 l -= 1 ans = 0 for q in range (2, 100000) : upper = r // q**n if (upper == 0) : break for p in range (1, q) : if check(q, p) : ans += max(0, upper - l // p**n) ans *= 2 print(ans) ```
output
1
107,491
22
214,983
Provide tags and a correct Python 3 solution for this coding contest problem. For given n, l and r find the number of distinct geometrical progression, each of which contains n distinct integers not less than l and not greater than r. In other words, for each progression the following must hold: l ≤ ai ≤ r and ai ≠ aj , where a1, a2, ..., an is the geometrical progression, 1 ≤ i, j ≤ n and i ≠ j. Geometrical progression is a sequence of numbers a1, a2, ..., an where each term after first is found by multiplying the previous one by a fixed non-zero number d called the common ratio. Note that in our task d may be non-integer. For example in progression 4, 6, 9, common ratio is <image>. Two progressions a1, a2, ..., an and b1, b2, ..., bn are considered different, if there is such i (1 ≤ i ≤ n) that ai ≠ bi. Input The first and the only line cotains three integers n, l and r (1 ≤ n ≤ 107, 1 ≤ l ≤ r ≤ 107). Output Print the integer K — is the answer to the problem. Examples Input 1 1 10 Output 10 Input 2 6 9 Output 12 Input 3 1 10 Output 8 Input 3 3 10 Output 2 Note These are possible progressions for the first test of examples: * 1; * 2; * 3; * 4; * 5; * 6; * 7; * 8; * 9; * 10. These are possible progressions for the second test of examples: * 6, 7; * 6, 8; * 6, 9; * 7, 6; * 7, 8; * 7, 9; * 8, 6; * 8, 7; * 8, 9; * 9, 6; * 9, 7; * 9, 8. These are possible progressions for the third test of examples: * 1, 2, 4; * 1, 3, 9; * 2, 4, 8; * 4, 2, 1; * 4, 6, 9; * 8, 4, 2; * 9, 3, 1; * 9, 6, 4. These are possible progressions for the fourth test of examples: * 4, 6, 9; * 9, 6, 4.
instruction
0
107,492
22
214,984
Tags: brute force, math, number theory Correct Solution: ``` import math small=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523] smallest=[0 for i in range(4001)] smallest[0]=1 smallest[1]=1 for i in range(2,len(smallest)): if smallest[i]==0: for j in range(2*i,len(smallest),i): smallest[j]=i smallest[i]=i def listfactor(N): if N==2: return [2] elif N==3: return [3] else: temp=[] while(N>1): t=smallest[N] temp+=[t] N=N//t temp=temp[::-1] ANS=[temp[0]] cur=temp[0] for i in range(len(temp)): if temp[i]>cur: cur=temp[i] ANS+=[cur] return ANS def check(a,b): for x in small: if (a%x==0 and b%x==0)==True: return 0 if x>max(a,b)**0.5: return 1 return 1 def ceil(x): if x!=int(x): return int(x)+1 else: return int(x) def gcd(a,b): a,b=min(a,b),max(a,b) if b%a==0: return a else: return gcd(a,b%a) def find(n,l,r): if r-l+1<n: return 0 elif n==1: return r-l+1 elif n==2: return (r-l+1)*(r-l) elif n>=30: ans=0 temp=0 for d in range(2,10**10): temp=max(r//(d**(n-1))-l+1,0) ans+=temp if temp==0: break return 2*ans else: ans=0 for d in range(2,10**10): temp=max(r//(d**(n-1))-l+1,0) ans+=temp if temp==0: break #print(ans,'ans') bound=int(r**(1/(n-1))) #print(bound,'bound') #q<=bound ANS=0 for q in range(2,bound+1): CUR=listfactor(q) for p in range(q+1,bound+2): check=1 for cc in CUR: if p%cc==0: check=0 if check==1: ANS+=max(r//(p**(n-1))-ceil(l/(q**(n-1)))+1,0) return 2*(ans+ANS) n,l,r=list(map(int,input().strip().split(' '))) print(find(n,l,r)) ```
output
1
107,492
22
214,985
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,497
22
214,994
Tags: constructive algorithms, greedy, math Correct Solution: ``` from math import sqrt n,k = map(int,input().split()) d = 10000000000000 if n < k*(k+1)//2: print(-1) else: for i in range(1,int(sqrt(n))+1): if n%i==0: if i >= k*(k+1)//2: d = i break if n//i >= (k*(k+1))//2: d = min(d,n//i) for i in range(1,k): print(i*n//d,end=" ") print((n//d * (k+d-(k*(k+1)//2))),end=" ") ```
output
1
107,497
22
214,995