description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | for _ in range(int(input())):
x, k = (int(x) for x in input().split())
fib = [
1,
2,
3,
5,
8,
13,
21,
34,
55,
89,
144,
233,
377,
610,
987,
1597,
2584,
4181,
6765,
10946,
17711,
28657,
46368,
75025,
121393,
196418,
317811,
514229,
832040,
1346269,
2178309,
3524578,
5702887,
9227465,
14930352,
24157817,
39088169,
63245986,
102334155,
165580141,
267914296,
433494437,
701408733,
]
def waifu(x, k, n):
ans = 0
if k == 0:
return x == 0
for i in range(n, -1, -1):
if fib[i] * k < x:
return ans
if fib[i] <= x:
ans += waifu(x - fib[i], k - 1, i)
return ans
print(waifu(x, k, 42)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | fibo = [0] * 44
fibo[0], fibo[1] = 1, 2
for i in range(2, 44):
fibo[i] = fibo[i - 1] + fibo[i - 2]
def chefonnaci_number(x, k, idx):
if k == 0:
if x == 0:
return 1
else:
return 0
elif x == 0 or idx == 0:
return 0
elif x >= 0 and x <= k * fibo[idx - 1]:
return chefonnaci_number(x, k, idx - 1) + chefonnaci_number(
x - fibo[idx - 1], k - 1, idx
)
return 0
for _ in range(int(input())):
x, k = map(int, input().split())
print(chefonnaci_number(x, k, 43)) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | import sys
sys.setrecursionlimit(10000000)
N = 1000000000
fib = [1, 2]
while True:
val = fib[-1] + fib[-2]
if val > N:
break
fib.append(val)
def getans(i, x, k):
if x < 0 or k == 0 and x > 0 or i < 0 or k * fib[i] < x:
return 0
if x == 0 and k == 0:
return 1
ans = getans(i, x - fib[i], k - 1) + getans(i - 1, x, k)
return ans
for _ in range(int(input())):
x, k = list(map(int, input().split()))
print(getans(len(fib) - 1, x, k)) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | MOD = 1000000007
fib = [1, 2]
for i in range(2, 50):
fib.append(fib[i - 1] + fib[i - 2])
def solve(x, k, n):
if k == 0:
return int(x == 0)
ans = 0
for i in range(n, -1, -1):
if fib[i] * k < x:
return ans
if fib[i] <= x:
ans = (ans + solve(x - fib[i], k - 1, i)) % MOD
return ans
for i in range(int(input().strip())):
x, k = map(int, input().split())
print(solve(x, k, 43)) | ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | def solve(x, k, n):
if k == 0:
if x == 0:
return 1
else:
return 0
elif x == 0 or n == 0:
return 0
else:
total = solve(x, k, n - 1)
if x >= chef[n - 1] and x <= k * chef[n - 1]:
total += solve(x - chef[n - 1], k - 1, n)
return total
chef = [0] * 50
chef[0], chef[1] = 1, 2
for i in range(2, 50):
chef[i] = chef[i - 1] + chef[i - 2]
for _ in range(int(input())):
x, k = map(int, input().split())
print(solve(x, k, 43)) | FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | mod = 10**9 + 7
fib = [1, 2]
while fib[-1] < 10**9:
fib.append(fib[-1] + fib[-2])
fibs = set(fib)
cache = {}
def ways(num, parts, maxused):
if num == 0 and parts == 0:
return 1
if parts == 0 or maxused == 1:
return 0
if parts * maxused < num:
return 0
if parts == 1:
return 1 if num < maxused and num in fibs else 0
if maxused > num and (num, parts) in cache:
return cache[num, parts]
res = 0
i = 0
while fib[i] <= num and fib[i] < maxused:
k = 1
while k * fib[i] <= num and k <= parts:
res = res + ways(num - k * fib[i], parts - k, fib[i])
k += 1
i += 1
res %= mod
if maxused > num:
cache[num, parts] = res
return res
for _ in range(int(input())):
x, k = map(int, input().split())
print(ways(x, k, x + 1)) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | cn = [0] * 44
cn[1], cn[0] = 2, 1
for i in range(2, 44):
cn[i] = cn[i - 1] + cn[i - 2]
def findWays(x, k, ind):
if k == 0:
return 1 if x == 0 else 0
elif x == 0 or ind == 0:
return 0
else:
if x >= 0 and k * cn[ind] >= x:
return findWays(x - cn[ind - 1], k - 1, ind) + findWays(x, k, ind - 1)
return 0
q = int(input())
for _ in range(q):
x, k = map(int, input().split())
for i in cn:
if i > x:
ind = cn.index(i)
print(findWays(x, k, ind)) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | def chefonacci(n):
f = []
for val in range(n):
if val == 0:
f.append(1)
if val == 1:
f.append(2)
if val > 1:
f.append(f[val - 1] + f[val - 2])
return f
def F(x, k, n):
if k > 0 and x < k * f[0] and x >= 0:
return 0
if x < 0:
return 0
if K == 1:
for val in f:
if val == X:
return 1
return 0
if k == 0:
if x == 0:
return 1
else:
return 0
elif x == 0 or n == 0:
return 0
else:
total = F(x, k, n - 1)
if x >= f[n - 1] and x <= k * f[n - 1]:
total += F(x - f[n - 1], k - 1, n)
return total
def compute_answer(X, K):
return F(X, K, 43)
f = chefonacci(44)
Q = int(input())
for case in range(Q):
X, K = list(map(int, input().split()))
number = compute_answer(X, K)
print(number) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | f = [1, 2]
f1 = 1
f2 = 2
while True:
f3 = f2 + f1
if f3 > 10**9:
break
f.append(f3)
f1 = f2
f2 = f3
def F(x, k, n):
if x < 0:
return 0
if k == 0:
if x == 0:
return 1
else:
return 0
if x == 0 or n == 0:
return 0
total = F(x, k, n - 1)
if x >= f[n - 1] and x <= k * f[n - 1]:
total += F(x - f[n - 1], k - 1, n)
return total
Q = int(input())
for q in range(Q):
X, K = list(map(int, input().split()))
print(F(X, K, len(f))) | ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef's team is going to participate at the legendary math battles. One of the main task in the competition is to calculate the number of ways to create a number by adding some Chefonacci numbers. A number is called a Chefonacci number if it is an element of Chefonacci sequence defined as follows.
f(0) = 1;
f(1) = 2;
For i > 1 : f(i) = f(i - 1) + f(i - 2)
Chef asked you to help him with this task. There will be Q question of form X, K : How many different ways are there to create X by adding K Chefonacci numbers. Note that the order of numbers in the addition does not matter, i.e. (f(i) + f(j) + f(k)) and (f(j) + f(i) + f(k)) will not be counted as distinct ways. Also note that you are allowed to use a Chefonacci number any number of times (zero or more).
As the answer could be large, print your answer modulo 10^{9} + 7 (1000000007).
------ Input ------
First line of the input contains an integer Q denoting number of questions Chef was asked.
In the next Q lines follow the questions, i-th of the line will denote the i-th question represented by two space separated integer X, K respectively.
------ Output ------
For each question, output a separate line containing the answer of the question.
------
------ Constraints -----
Subtask 1 : [10 points]
1 ≤ Q ≤ 50
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 4
Subtask 2 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 5
Subtask 3 : [20 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{2}
1 ≤ K ≤ 10
Subtask 4 : [50 points]
1 ≤ Q ≤ 100
1 ≤ X ≤ 10^{9}
1 ≤ K ≤ 10
----- Sample Input 1 ------
5
12 1
13 1
13 2
13 3
13 4
----- Sample Output 1 ------
0
1
1
2
4
----- explanation 1 ------
Example case 1.
There is no way to create 12 by adding one Chefonacci number, as 12 is not a Chefonacci number.
Example case 2.
There is only one way to create 13 by adding one Chefonacci number, i.e. 13.
Example case 3.
There is one way to create 13 by adding two Chefonacci numbers, i.e. 5 + 8.
Example case 4.
There are two ways to create 13 by adding three Chefonacci numbers: 2 + 3 + 8, 3 + 5 + 5.
Example case 5.
There are four ways to create 13 by adding four Chefonacci numbers: 1 + 1 + 3 + 8, 1 + 2 + 2 + 8, 1 + 2 + 5 + 5, 2 + 3 + 3 + 5 | chef = [1, 2]
i = 2
while chef[-1] < 10**9:
chef.append(chef[i - 1] + chef[i - 2])
i += 1
cache = {}
def fetch(X, K, i):
if X in cache:
if K in cache[X]:
if i in cache[X][K]:
return cache[X][K][i]
return -1
def store(X, K, i, val):
if X not in cache:
cache[X] = {}
if K not in cache[X]:
cache[X][K] = {i: val}
def compute(X, K, i):
if X > chef[i] * K:
return 0
if X <= 0:
return 1 if K == 0 else 0
elif K == 0:
return 0
elif i < 0:
return 0
else:
val = fetch(X, K, i)
if val != -1:
return val
elif X - chef[i] < 0:
val = compute(X, K, i - 1)
store(X, K, i, val)
return val
else:
val = compute(X - chef[i], K - 1, i) + compute(X, K, i - 1)
store(X, K, i, val)
return val
cl = len(chef) - 1
Q = int(input())
for _ in range(Q):
X, K = input().split()
X, K = int(X), int(K)
print(compute(X, K, cl)) | ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR DICT IF VAR VAR VAR ASSIGN VAR VAR VAR DICT VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
if sum(a) % k != 0:
return 0
target = sum(a) // k
vis = [False] * len(a)
def recurse(i, l, k):
if l == target:
return recurse(0, 0, k - 1)
if k == 0:
return True
for ind in range(i, len(a)):
if vis[ind] == True:
continue
vis[ind] = True
l += a[ind]
if recurse(ind + 1, l, k) == True:
return True
l -= a[ind]
vis[ind] = False
return False
return recurse(0, 0, k) | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | def k_subset_possible(
input_arr: [], index, n, k, result_arr: [], sum_path_arr: [], required_sum
):
if index > n:
return
if index == n:
for i in range(k):
element = sum_path_arr[i]
if element != required_sum:
return
result_arr[0] = True
return
element = input_arr[index]
for i in range(k):
sum_value = sum_path_arr[i]
if sum_value == 0:
sum_path_arr[i] = element
k_subset_possible(
input_arr, index + 1, n, k, result_arr, sum_path_arr, required_sum
)
sum_path_arr[i] = sum_path_arr[i] - element
break
else:
sum_path_arr[i] = sum_path_arr[i] + element
k_subset_possible(
input_arr, index + 1, n, k, result_arr, sum_path_arr, required_sum
)
sum_path_arr[i] = sum_path_arr[i] - element
def partition_sum_k_driver(input_arr: [], k):
n = len(input_arr)
sum_value = sum(input_arr)
if k == 1:
return True
if k == n:
for element in input_arr:
if element != sum_value // k:
return False
return True
required_sum = sum_value // k
if sum_value % k != 0:
return
sum_path_arr = [0] * k
result_arr = [False]
k_subset_possible(input_arr, 0, n, k, result_arr, sum_path_arr, required_sum)
return result_arr[0]
class Solution:
def isKPartitionPossible(self, a, k):
return partition_sum_k_driver(a, k) | FUNC_DEF LIST LIST LIST IF VAR VAR RETURN IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN ASSIGN VAR NUMBER NUMBER RETURN ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_DEF LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR FOR VAR VAR IF VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
s = sum(a)
if s % k != 0:
return 0
s = s // k
def manu(i, subnum, picked, s, cursum):
if subnum == k + 1:
return True
if cursum == s:
return manu(0, subnum + 1, picked, s, 0)
elif cursum > s:
return False
if i >= len(a):
return False
if picked[i] == 1:
return manu(i + 1, subnum, picked, s, cursum)
else:
cursum += a[i]
picked[i] = 1
k1 = manu(i + 1, subnum, picked, s, cursum)
cursum -= a[i]
picked[i] = 0
k2 = manu(i + 1, subnum, picked, s, cursum)
return k1 or k2
picked = [(0) for i in range(len(a))]
return manu(0, 1, picked, s, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | def kPartation(arr, i, k):
if i >= len(arr):
for j in range(1, len(k)):
if k[j - 1] != k[j]:
return False
return True
res = False
for n in range(len(k)):
if k[n] == 0:
k[n] = k[n] + arr[i]
res = res or kPartation(arr, i + 1, k)
if res == True:
return True
k[n] = k[n] - arr[i]
break
for j in range(len(k)):
if k[j] != 0:
k[j] = k[j] + arr[i]
res = res or kPartation(arr, i + 1, k)
if res == True:
return True
k[j] = k[j] - arr[i]
return False
def controllerKpartation(arr, k):
kAry = [(0) for i in range(k)]
return kPartation(arr, 0, kAry)
class Solution:
def isKPartitionPossible(self, a, k):
return controllerKpartation(arr, k) | FUNC_DEF IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
total = sum(a)
if total % k != 0:
return False
share = total // k
n = len(a)
subset = [0] * n
prev_choice = [-1] * n
possible = False
ind = 0
sub = 1
summation = 0
prev = -1
while True:
if ind >= n:
if prev == -1:
break
if subset[prev] != sub:
sub = subset[prev]
summation = share
subset[prev] = 0
summation -= a[prev]
ind = prev + 1
prev = prev_choice[prev]
if ind < n and subset[ind] == 0:
if a[ind] + summation <= share:
prev_choice[ind] = prev
summation += a[ind]
subset[ind] = sub
prev = ind
ind += 1
if summation == share:
sub += 1
if sub >= k:
possible = True
break
ind = 0
summation = 0
else:
ind += 1
else:
ind += 1
return possible | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def __init__(self):
self.flag = False
def KPartition(self, indx, nos, k, n, a, sum, ans):
if indx == n:
if nos == k:
f = True
for i in range(k - 1):
if sum[i] != sum[i + 1]:
f = False
break
if f == True:
self.flag = True
return
for i in range(k):
if len(ans[i]) > 0:
ans[i].append(a[indx])
sum[i] += a[indx]
self.KPartition(indx + 1, nos, k, n, a, sum, ans)
ans[i].pop()
sum[i] -= a[indx]
else:
ans[i].append(a[indx])
sum[i] += a[indx]
self.KPartition(indx + 1, nos + 1, k, n, a, sum, ans)
ans[i].pop()
sum[i] -= a[indx]
break
def isKPartitionPossible(self, a, k):
self.flag = False
n = len(a)
ans = [[] for _ in range(k)]
sum = [0] * k
self.KPartition(0, 0, k, n, a, sum, ans)
return self.flag | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR RETURN VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
if not a:
return
s = sum(a)
if s % k != 0:
return False
div = s // k
sides = [0] * k
a.sort(reverse=True)
def dfs(i):
if i == len(a):
return True
for j in range(k):
if sides[j] + a[i] <= div:
if j > 0 and sides[j] == sides[j - 1]:
continue
sides[j] += a[i]
if dfs(i + 1):
return True
sides[j] -= a[i]
return False
return dfs(0) | CLASS_DEF FUNC_DEF IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
def help(i):
if i == len(a):
for i in arr:
if i != n:
return False
return True
for j in range(len(arr)):
if arr[j] + a[i] <= n:
arr[j] += a[i]
if help(i + 1):
return 1
arr[j] -= a[i]
if arr[j] == 0:
return 0
return 0
n = sum(a) / k
if k > n or sum(a) // k != n:
return 0
arr = [0] * k
if help(0):
return 1
return 0 | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF FUNC_CALL VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def solve(self, a, k, sum1, index, visited, subset, reqsum):
if subset == k:
return True
if sum1 == 0:
return self.solve(a, k, reqsum, 0, visited, subset + 1, reqsum)
if index == len(a):
return False
if sum1 >= a[index] and visited[index] == False:
visited[index] = True
if (
self.solve(a, k, sum1 - a[index], index + 1, visited, subset, reqsum)
== True
):
return True
visited[index] = False
ans = self.solve(a, k, sum1, index + 1, visited, subset, reqsum)
return ans
def isKPartitionPossible(self, a, k):
sum1 = sum(a)
if sum1 % k != 0:
return False
index = 0
sum1 = sum1 // k
visited = [(False) for i in range(len(a))]
subset = 0
reqsum = sum1
return self.solve(a, k, sum1, index, visited, subset, reqsum) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
n = len(a)
a.sort(reverse=True)
Sum = sum(a)
if Sum % k:
return False
used = [False] * n
target = Sum // k
def dfs(ind, total, k):
if k == 0:
return True
if total == 0:
return dfs(0, target, k - 1)
for i in range(ind, n):
if i > 0 and not used[i - 1] and a[i - 1] == a[i]:
continue
if used[i] or total - a[i] < 0:
continue
used[i] = True
if dfs(i + 1, total - a[i], k):
return True
used[i] = False
return False
return dfs(0, target, k) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, nums, K):
T = sum(nums)
if T % K:
return False
N = len(nums)
T = T // K
taken = [False] * N
d = {}
def helper(S, K, curr):
if K == 0:
return True
if S > T:
return False
if d.get((S, curr), None) != None:
return d[S, curr]
if S == T:
d[S, curr] = helper(0, K - 1, curr)
return d[S, curr]
for i in range(len(nums)):
if not taken[i]:
taken[i] = True
temp = curr
curr = curr | 1 << i
if helper(S + nums[i], K, curr):
d[S, curr] = True
return d[S, curr]
curr = temp
taken[i] = False
d[S, curr] = False
return d[S, curr]
ans = helper(0, K, 0)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR NONE NONE RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def solvepartition(self, idx, a, k, reqdsum, used, subsetsum):
if k == 0:
return True
if subsetsum == reqdsum:
return self.solvepartition(0, a, k - 1, reqdsum, used, 0)
for i in range(idx, len(arr)):
if i > 0 and not used[i - 1] and arr[i] == arr[i - 1]:
continue
if used[i] or subsetsum + arr[i] > reqdsum:
continue
used[i] = True
if self.solvepartition(i + 1, a, k, reqdsum, used, subsetsum + arr[i]):
return True
used[i] = False
return False
def isKPartitionPossible(self, a, k):
total = sum(a)
reqdsum = total / k
if total % k == 1:
return False
used = [False] * len(a)
arr.sort()
if self.solvepartition(0, a, k, reqdsum, used, 0):
return 1
return 0 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def is_possible(self, idx, res, subset_sums, a, k):
if idx == len(a):
if k == 0:
for i in range(len(subset_sums) - 1):
if subset_sums[i] != subset_sums[i + 1]:
return False
return True
return False
for i in range(len(res)):
if len(res[i]) > 0:
res[i].append(a[idx])
subset_sums[i] += a[idx]
if self.is_possible(idx + 1, res, subset_sums, a, k):
return True
res[i].pop()
subset_sums[i] -= a[idx]
else:
res[i].append(a[idx])
subset_sums[i] += a[idx]
if self.is_possible(idx + 1, res, subset_sums, a, k - 1):
return True
res[i].pop()
subset_sums[i] -= a[idx]
break
return False
def isKPartitionPossible(self, a, k):
s = 0
for val in a:
s += val
if k > len(a) and s % k != 0:
return False
res = [[] for _ in range(k)]
subset_sums = [0] * k
return self.is_possible(0, res, subset_sums, a, k) | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
if k == 1:
return True
if len(a) < k:
return False
if sum(a) % k != 0:
return False
sum_num = sum(a) // k
used = [0] * len(a)
self.res = False
def dfs(index, cur, depth):
if depth == k:
return True
if cur == sum_num:
return dfs(0, 0, depth + 1)
for i in range(index, len(a)):
if used[i] or cur + a[i] > sum_num:
continue
used[i] = True
res = dfs(index + 1, cur + a[i], depth)
if res:
return True
else:
used[i] = False
return dfs(0, 0, 0) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
n = len(a)
used = [False] * n
if sum(a) % k:
return False
target = sum(a) // k
def dfs(ind, total, k):
if k == 0:
return True
if total == 0:
return dfs(0, target, k - 1)
for j in range(ind, len(a)):
if j > 0 and not used[j - 1] and a[j] == a[j - 1]:
continue
if used[j] or total - a[j] < 0:
continue
used[j] = True
if dfs(j + 1, total - a[j], k):
return True
used[j] = False
return False
return dfs(0, target, k) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
n = len(a)
target = sum(a) // k
subset = [0] * k
def func(ind, n, k, a, taregt, subset):
if ind == n:
return True
for j in range(k):
if subset[j] + a[ind] <= target:
subset[j] += a[ind]
if func(ind + 1, n, k, a, target, subset):
return True
subset[j] -= a[ind]
if subset[j] == 0:
break
return False
return func(0, n, k, a, target, subset) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def recsol2(self, set_arr, set_idx, set_sum, req_sum, k, istaken):
if set_sum == req_sum:
if set_idx == k - 2:
return True
return self.recsol2(set_arr, set_idx + 1, 0, req_sum, k, istaken)
for j in range(len(set_arr)):
if istaken[j] == False and req_sum >= set_sum + set_arr[j]:
istaken[j] = True
found = self.recsol2(
set_arr, set_idx, set_sum + set_arr[j], req_sum, k, istaken
)
istaken[j] = False
if found == True:
return True
return False
def isKPartitionPossible(self, a, k):
if k == 1:
return 1
if k > len(a):
return 0
if sum(a) % k != 0:
return 0
req_sum = sum(a) // k
istaken = [False] * len(a)
istaken[0] = True
if self.recsol2(a, 0, a[0], req_sum, k, istaken) == True:
return 1
return 0 | CLASS_DEF FUNC_DEF IF VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
res = []
def solve(a, s, p, k, vis, i, n):
if k == 0:
return True
if s > p:
return False
if s == p:
return solve(a, 0, p, k - 1, vis, 0, n)
for i in range(i, n):
if vis[i]:
continue
vis[i] = True
if solve(a, s + a[i], p, k, vis, i + 1, n):
return True
vis[i] = False
return False
s = sum(a)
if s % k != 0:
return False
n = len(a)
vis = [0] * n
return solve(a, 0, s // k, k, vis, 0, n) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
total = sum(a)
if total % k:
return False
a.sort(reverse=True)
target = total // k
subsets = [0] * k
def backtrack(i):
if i == len(a):
return True
for j in range(k):
if subsets[j] + a[i] <= target:
subsets[j] += a[i]
if backtrack(i + 1):
return True
subsets[j] -= a[i]
if subsets[j] == 0:
break
return False
return backtrack(0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
dp = [-1] * (1 << 12)
dp[0] = 0
n = len(a)
summ = sum(a)
if summ % k != 0:
return False
target = summ // k
for mask in range(1 << n):
if dp[mask] == -1:
continue
for i in range(n):
if mask & 1 << i == 0 and dp[mask] + a[i] <= target:
dp[mask | 1 << i] = dp[mask] + a[i]
dp[mask | 1 << i] %= target
return dp[(1 << n) - 1] == 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
a.sort(reverse=True)
summa = sum(a)
target = summa // k
if summa % k or max(a) > target:
return 0
output = 0
for item in a:
output += self.helper(item, item, target, a.copy())
return output
def helper(self, summa, last, target, a):
a.remove(last)
if not a:
return 1
if summa == target:
summa = 0
for item in a:
if item + summa <= target and self.helper(
summa + item, item, target, a.copy()
):
return 1
return 0 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR RETURN NUMBER RETURN NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, nums, k):
nums.sort()
target, rem = divmod(sum(nums), k)
if rem or nums[-1] > target:
return False
dp = [False] * (1 << len(nums))
dp[0] = True
total = [0] * (1 << len(nums))
for state in range(1 << len(nums)):
if not dp[state]:
continue
for i, num in enumerate(nums):
future = state | 1 << i
if state != future and not dp[future]:
if num <= target - total[state] % target:
dp[future] = True
total[future] = total[state] + num
else:
break
return dp[-1] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
if k < 2:
return k
if sum(a) % k:
return False
n = len(a)
arr = [False] * n
def helper(summ, s, k, arr, ind, n, a):
if k == 1:
return True
if summ > s:
return False
if summ == s:
return helper(0, s, k - 1, arr, 0, n, a)
for i in range(ind, n):
if not arr[i]:
arr[i] = True
if helper(summ + a[i], s, k, arr, i + 1, n, a):
return True
arr[i] = False
return False
return helper(0, sum(a) // k, k, arr, 0, n, a) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def find(self, i, a, d, gs, x, s):
if x == 1:
return True
if gs == s:
return self.find(0, a, d, 0, x - 1, s)
for j in range(i, N):
if not d[j]:
d[j] = True
if self.find(j + 1, a, d, gs + a[j], x, s):
return True
d[j] = False
return False
def isKPartitionPossible(self, a, k):
d = [False] * N
t = sum(a)
if t % k != 0:
return 0
s = t // k
return self.find(0, a, d, 0, k, s) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | def isKPartitionPossibleRec(arr, subsetSum, taken, subset, K, N, curIdx, limitIdx):
if subsetSum[curIdx] == subset:
if curIdx == K - 2:
return True
return isKPartitionPossibleRec(
arr, subsetSum, taken, subset, K, N, curIdx + 1, N - 1
)
for i in range(limitIdx, -1, -1):
if taken[i]:
continue
tmp = subsetSum[curIdx] + arr[i]
if tmp <= subset:
taken[i] = True
subsetSum[curIdx] += arr[i]
nxt = isKPartitionPossibleRec(
arr, subsetSum, taken, subset, K, N, curIdx, i - 1
)
taken[i] = False
subsetSum[curIdx] -= arr[i]
if nxt:
return True
return False
def isKPartitionPossiblee(arr, N, K):
if K == 1:
return True
if N < K:
return False
sum = 0
for i in range(N):
sum += arr[i]
if sum % K != 0:
return False
subset = sum // K
subsetSum = [0] * K
taken = [0] * N
for i in range(K):
subsetSum[i] = 0
for i in range(N):
taken[i] = False
subsetSum[0] = arr[N - 1]
taken[N - 1] = True
return isKPartitionPossibleRec(arr, subsetSum, taken, subset, K, N, 0, N - 1)
class Solution:
def isKPartitionPossible(self, nums, k):
return isKPartitionPossiblee(nums, len(nums), k) | FUNC_DEF IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
def foo(a, i, k, n, grp, g):
if n == i:
return len(set(grp)) == 1
for j in range(k):
if grp[j] + a[i] > g:
continue
grp[j] += a[i]
res = foo(a, i + 1, k, n, grp, g)
if res:
return True
grp[j] -= a[i]
return False
n = len(a)
if sum(a) / k % 1 != 0 or max(a) > sum(a) // k or n < k:
return False
grp = [(0) for i in range(k)]
return foo(a, 0, k, n, grp, sum(a) // k) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR RETURN NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
def solve(i, a, n, k, target, visit, cnt, cursum):
if cursum == target:
cursum = 0
cnt += 1
return solve(0, a, n, k, target, visit, cnt, cursum)
if cnt == k + 1:
return True
if i >= n:
return False
if cursum > target:
return False
if visit[i] == 1:
return solve(i + 1, a, n, k, target, visit, cnt, cursum)
else:
visit[i] = 1
cursum += a[i]
op1 = solve(i + 1, a, n, k, target, visit, cnt, cursum)
visit[i] = 0
cursum -= a[i]
op2 = solve(i + 1, a, n, k, target, visit, cnt, cursum)
return op1 or op2
sum = 0
n = len(a)
for i in range(len(a)):
sum += a[i]
visit = [0] * n
if sum % k != 0:
return False
if k == 1:
return True
if k >= n:
return False
target = sum // k
return solve(0, a, n, k, target, visit, 1, 0) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, array, k):
i = 0
n = len(array)
zeroscount = 0
holdanswer = [(0) for i in range(k)]
hold = self.solve(i, zeroscount, array, holdanswer, k, n)
return hold
def isPossible(self, result, k):
hold = result[0]
for value in result:
if hold != value:
return False
return True
def solve(self, i, zeroscount, array, holdanswer, k, n):
if i >= n:
if zeroscount == k:
if self.isPossible(holdanswer, k):
return True
return False
for j in range(len(holdanswer)):
temp = array[i]
if holdanswer[j] > 0:
holdanswer[j] += temp
if self.solve(i + 1, zeroscount, array, holdanswer, k, n):
return True
holdanswer[j] -= temp
else:
holdanswer[j] += temp
if self.solve(i + 1, zeroscount + 1, array, holdanswer, k, n):
return True
holdanswer[j] -= temp
break
return False | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR RETURN NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
if not sum(a) % k == 0:
return False
def dfs(curr, k, a):
if curr > target:
return False
if curr == target:
k -= 1
curr = 0
if not a:
return True if not k else False
for i in range(len(a)):
if dfs(curr + a[i], k, a[:i] + a[i + 1 :]):
return True
a.sort()
target = sum(a) // k
if a[-1] > target:
return False
while a and a[-1] == target:
a.pop()
k -= 1
return dfs(0, k, a[::-1]) | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, array, k):
self.result = list()
i = 0
n = len(array)
zeroscount = 0
holdanswer = [(0) for i in range(k)]
self.solve(i, zeroscount, array, holdanswer, k, n)
if self.isPossible(k):
return 1
return 0
def isPossible(self, k):
for listdata in self.result:
times = 1
hold = listdata[0]
for value in listdata:
if hold != value:
break
else:
return True
return False
def solve(self, i, zeroscount, array, holdanswer, k, n):
if i >= n:
if zeroscount == k:
holdanswercpy = holdanswer.copy()
self.result.append(holdanswercpy)
return None
for j in range(len(holdanswer)):
temp = array[i]
if holdanswer[j] > 0:
holdanswer[j] += temp
self.solve(i + 1, zeroscount, array, holdanswer, k, n)
holdanswer[j] -= temp
else:
holdanswer[j] += temp
self.solve(i + 1, zeroscount + 1, array, holdanswer, k, n)
holdanswer[j] -= temp
break
return None | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN NONE |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
if sum(a) % k:
return False
a.sort()
visited = set()
target = sum(a) / k
def backtrack(idx, count, currsum):
if count == k:
return True
if currsum == target:
return backtrack(0, count + 1, 0)
for i in range(idx, len(a)):
if i > 0 and i - 1 not in visited and a[i] == a[i - 1]:
continue
if i in visited or currsum + a[i] > target:
continue
visited.add(i)
if backtrack(i + 1, count, currsum + a[i]):
return True
visited.remove(i)
return False
return backtrack(0, 0, 0) | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | def helper(i, reqSum, nums, subset, k):
if i == len(nums):
return True
for j in range(k):
if subset[j] + nums[i] <= reqSum:
subset[j] += nums[i]
if helper(i + 1, reqSum, nums, subset, k):
return True
subset[j] -= nums[i]
if subset[j] == 0:
break
return False
class Solution:
def isKPartitionPossible(self, nums, k):
n = len(nums)
subset = [0] * k
if sum(nums) % k != 0:
return False
reqSum = sum(nums) // k
nums.sort(reverse=True)
return helper(0, reqSum, nums, subset, k) | FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def solve(self, arr, ind, sum1, tar, k):
if k == 0:
return True
if sum1 == tar:
return self.solve(arr, 0, 0, tar, k - 1)
for j in range(ind, len(arr)):
if vis[j] or arr[j] + sum1 > tar:
continue
vis[j] = 1
if self.solve(arr, j + 1, sum1 + arr[j], tar, k):
return True
vis[j] = 0
return False
def isKPartitionPossible(self, arr, k):
n = len(arr)
global vis
vis = [(0) for i in range(n)]
if k == 1:
return True
if k > n:
return False
sum1 = sum(arr)
if sum1 % k != 0:
return False
return self.solve(arr, 0, 0, sum1 // k, k) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
n = len(a)
target = sum(a) // k
subset = [False] * n
if sum(a) % k:
return False
def recur(ind, k, subsetsum):
if k == 0:
return True
if subsetsum == target:
return recur(0, k - 1, 0)
for i in range(ind, n):
if subset[i] or subsetsum + a[i] > target:
continue
subset[i] = True
if recur(ind + 1, k, subsetsum + a[i]):
return True
subset[i] = False
return False
return recur(0, k, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
_sum = sum(a)
n = len(a)
if n < k or _sum % k != 0:
return False
target = _sum // k
used = [False] * n
def helper(sum, index, id):
nonlocal a, n, k, target, used
if sum == target:
if id == k:
return True
else:
return helper(0, 0, id + 1)
elif sum > target:
return False
else:
for i in range(index, n):
if used[i] == False:
used[i] = True
if helper(sum + a[i], i + 1, id):
return True
used[i] = False
return False
return helper(0, 0, 1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def __init__(self):
self.flag = False
def KPartition(self, indx, n, a, k, subsetsum, dp, nos):
if indx == n:
if nos == k:
for i in range(k - 1):
if subsetsum[i] != subsetsum[i + 1]:
return
self.flag = True
return
for i in range(k):
if len(dp[i]) == 0:
subsetsum[i] += a[indx]
dp[i].append(a[indx])
self.KPartition(indx + 1, n, a, k, subsetsum, dp, nos + 1)
subsetsum[i] -= a[indx]
dp[i].pop()
break
else:
subsetsum[i] += a[indx]
dp[i].append(a[indx])
self.KPartition(indx + 1, n, a, k, subsetsum, dp, nos)
subsetsum[i] -= a[indx]
dp[i].pop()
def isKPartitionPossible(self, a, k):
subsetsum = [0] * k
dp = [[] for i in range(k)]
self.KPartition(0, len(a), a, k, subsetsum, dp, 0)
return self.flag | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN ASSIGN VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
arr = [[] for i in range(k)]
n = len(a)
fill = 0
ans = [0]
if k == 1:
return True
def sol(i, fill, n):
if i == n:
if fill == k:
s = sum(arr[0])
for j in arr:
if s != sum(j):
return
ans[0] += 1
return
else:
return
for j in range(k):
if arr[j] == []:
arr[j].append(a[i])
sol(i + 1, fill + 1, n)
arr[j].pop()
return
else:
arr[j].append(a[i])
sol(i + 1, fill, n)
arr[j].pop()
sol(0, 0, n)
return ans[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER RETURN RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR RETURN VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
s = sum(a)
if s % k != 0:
return False
e = s / k
a.sort(reverse=True)
visited = [False] * len(a)
def dfs(kleft, cursum, start):
if kleft == 1:
return True
if cursum == e:
return dfs(kleft - 1, 0, 0)
for i in range(start, len(a)):
if not visited[i] and cursum + a[i] <= e:
visited[i] = True
if dfs(kleft, cursum + a[i], i + 1):
return True
visited[i] = False
return False
return dfs(k, 0, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, a, k):
if sum(a) % k != 0:
return False
target = sum(a) // k
used = [(False) for i in range(len(a))]
def backtrack(index, k, subsetSum):
if k == 0:
return True
if subsetSum == target:
return backtrack(0, k - 1, 0)
for j in range(index, len(a)):
if used[j] == True or subsetSum + a[j] > target:
continue
used[j] = True
if backtrack(index + 1, k, subsetSum + a[j]):
return True
used[j] = False
return False
return backtrack(0, k, 0) | CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def part(self, i, a, k, target, subsetsum, used):
if k == 0:
return True
if subsetsum == target:
return self.part(0, a, k - 1, target, 0, used)
for j in range(i, len(a)):
if a[j] + subsetsum > target or used[j] == True:
continue
used[j] = True
if self.part(j + 1, a, k, target, subsetsum + a[j], used):
return True
used[j] = False
return False
def isKPartitionPossible(self, a, k):
sumi = sum(a)
if sumi % k != 0:
return 0
else:
target = sumi // k
used = [(False) for i in range(len(a))]
return self.part(0, a, k, target, 0, used) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def isKPartitionPossible(self, arr, k):
n = len(arr)
ans = [False]
visited = [0] * n
sum_arr = sum(arr)
if k == 1:
return True
if sum_arr % k:
return False
target = sum_arr // k
def rec(i, total, j):
if not ans[0]:
if total == target:
if j == k - 1:
ans[0] = True
else:
rec(0, 0, j + 1)
return
if i == n:
return
rec(i + 1, total, j)
if not visited[i] and total + arr[i] <= target:
visited[i] = 1
rec(i + 1, total + arr[i], j)
visited[i] = 0
rec(0, 0, 1)
return ans[0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN IF VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER |
Given an integer array a[ ] of N elements and an integer K, the task is to check if the array a[ ] could be divided into K non-empty subsets with equal sum of elements.
Note: All elements of this array should be part of exactly one partition.
Example 1:
Input:
N = 5
a[] = {2,1,4,5,6}
K = 3
Output:
1
Explanation: we can divide above array
into 3 parts with equal sum as (2, 4),
(1, 5), (6)
Example 2:
Input:
N = 5
a[] = {2,1,5,5,6}
K = 3
Output:
0
Explanation: It is not possible to divide
above array into 3 parts with equal sum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isKPartitionPossible() which takes the array a[], the size of the array N, and the value of K as inputs and returns true(same as 1) if possible, otherwise false(same as 0).
Expected Time Complexity: O(N*2^{N}).
Expected Auxiliary Space: O(2^{N}).
Constraints:
1 ≤ K ≤ N ≤ 10
1 ≤ a[i] ≤ 100 | class Solution:
def solve(self, arr, idx, k, curSum, target, taken):
if k == 0:
return 1
if curSum == target:
return self.solve(arr, 0, k - 1, 0, target, taken)
for j in range(idx, len(arr)):
if taken[j] == True or curSum + arr[j] > target:
continue
taken[j] = True
if self.solve(arr, idx + 1, k, curSum + arr[j], target, taken):
return True
taken[j] = False
def isKPartitionPossible(self, a, k):
if k == 1:
return 1
n = len(a)
if n < k:
return 0
summ = sum(a)
if summ % k != 0:
return 0
taken = [False] * n
target = summ // k
return self.solve(a, 0, k, 0, target, taken) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = map(int, input().split())
grid = []
for i in range(n):
s = input()
grid.append(s)
grid2 = []
for i in range(n):
grid2.append([])
for j in range(m):
grid2[-1].append(0)
for j in range(m):
lastchar = ""
int1 = 0
int2 = 0
int3 = 0
for i in range(n):
if grid[i][j] == lastchar:
int3 += 1
else:
int1 = int2
int2 = int3
int3 = 1
lastchar = grid[i][j]
if int1 >= int2 and int2 == int3:
grid2[i][j] = int3
score = []
for i in range(n):
score.append([])
for j in range(m):
score[-1].append(0)
for i in range(n):
for j in range(m):
t = grid2[i][j]
if t > 0:
if (
j > 0
and grid2[i][j - 1] == t
and grid[i][j] == grid[i][j - 1]
and grid[i - t][j] == grid[i - t][j - 1]
and grid[i - 2 * t][j - 1] == grid[i - 2 * t][j]
):
score[i][j] = score[i][j - 1] + 1
else:
score[i][j] = 1
ans = 0
for i in range(n):
for j in range(m):
ans += score[i][j]
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | H, W = map(int, input().split())
G = [[(ord(s) - 96) for s in input().strip()] for _ in range(H)]
G = list(map(list, zip(*G)))
A = [None] * W
B = [None] * W
Col = [None] * W
Dic = [None] * W
for j, Gi in enumerate(G):
pre = -1
D = [None] * H
con = 0
ctr = 0
E = [None] * H
F = []
for i, g in enumerate(Gi):
if pre != g:
F.append(con)
ctr += 1
con = 0
con += 1
D[i] = con
pre = g
E[i] = ctr
F.append(con)
Col[j] = E[:]
Dic[j] = F[:]
B[j] = D[:]
pm = 0
pre = -1
for i, (g, d) in enumerate(zip(Gi[::-1], D[::-1])):
if pre != g:
pm = 0
pm = max(pm, d)
D[H - 1 - i] = pm
pre = g
A[j] = D[:]
C = [([0] * H) for _ in range(W)]
for w in range(W):
for h in range(H):
c = Col[w][h]
if c + 2 >= len(Dic[w]):
continue
leng = A[w][h] + 1 - B[w][h]
if Dic[w][c + 1] == leng and Dic[w][c + 2] >= leng:
C[w][h] = (
leng * 10**6 + 900 * G[w][h] + 30 * G[w][h + leng] + G[w][h + 2 * leng]
)
C = list(map(list, zip(*C)))
ans = 0
for Ci in C:
ctr = 0
pre = -1
for c in Ci:
if not c:
pre = -1
continue
if c != pre:
ans += ctr * (ctr + 1) // 2
ctr = 1
else:
ctr += 1
pre = c
ans += ctr * (ctr + 1) // 2
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = map(int, input().split())
a = []
flagTable = [[0] * m] * n
totalFlagNum = 0
def countFlagNum(x):
y = 0
flagNum = 0
while y < m:
curColor = a[x][y]
colorCountByX = 1
isItFlag = True
while x + colorCountByX < n and a[x + colorCountByX][y] == curColor:
colorCountByX += 1
if not x + colorCountByX * 3 > n:
color2 = a[x + colorCountByX][y]
color3 = a[x + colorCountByX * 2][y]
if color3 != color2 and color2 != curColor:
offY = 0
while y + offY < m and isItFlag:
i = 0
while i < colorCountByX and isItFlag:
if (
a[x + i][y + offY] != curColor
or a[x + colorCountByX + i][y + offY] != color2
or a[x + colorCountByX * 2 + i][y + offY] != color3
):
isItFlag = False
if offY == 0:
offY = 1
i += 1
if isItFlag:
flagNum = flagNum + 1 + offY
offY += 1
y += offY - 1
y += 1
return flagNum
def markFlag():
return 0
for i in range(n):
row = input()
a.append(row)
for i in range(n - 2):
totalFlagNum += countFlagNum(i)
print(totalFlagNum) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | a, b = map(int, input().split())
c = [([0] * b) for i in range(a)]
d = [input() for i in range(a)]
for j in range(b):
n = []
pred = d[0][j]
cnt = 1
for i in range(1, a):
if d[i][j] == pred:
cnt += 1
else:
n.append((cnt, pred))
cnt = 1
pred = d[i][j]
n.append((cnt, pred))
uk = 0
for i in range(2, len(n)):
if n[i - 2][0] >= n[i - 1][0] and n[i - 1][0] <= n[i][0]:
c[uk + n[i - 2][0] - n[i - 1][0]][j] = [
n[i - 2][1],
n[i - 1][1],
n[i][1],
n[i - 1][0],
]
uk += n[i - 2][0]
summ = 0
cnt = 0
if b == 1:
for i in range(a):
for j in range(b):
if c[i][j] != 0:
summ += 1
print(summ)
exit(0)
for i in range(a):
cnt = 0
f = False
for j in range(1, b):
if cnt == 0 and c[i][j - 1] != 0:
cnt = 1
if c[i][j - 1] == c[i][j] and c[i][j] != 0:
cnt += 1
elif c[i][j] != 0:
summ += cnt * (cnt + 1) // 2
cnt = 1
else:
summ += cnt * (cnt + 1) // 2
cnt = 0
summ += cnt * (cnt + 1) // 2
print(summ) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | from sys import stdin, stdout
n, m = list(map(int, stdin.readline().split()))
t = [[(0) for i in range(m)] for j in range(n)]
for i in range(n):
s = stdin.readline().strip()
for j, c in enumerate(s):
t[i][j] = c
begins = [[(False) for i in range(m)] for j in range(n)]
lens = [[(0) for i in range(m)] for j in range(n)]
for j in range(m):
cur_len = 1
run_start = 0
for i in range(1, n):
if t[i - 1][j] != t[i][j]:
begins[run_start][j] = True
for z in range(run_start, i):
lens[z][j] = cur_len - (z - run_start)
cur_len = 0
run_start = i
cur_len += 1
begins[run_start][j] = True
for z in range(run_start, n):
lens[z][j] = cur_len - (z - run_start)
done = [[(False) for i in range(m)] for j in range(n)]
ans = 0
for j in range(m):
for i in range(n):
if done[i][j]:
continue
l = lens[i][j]
if i + 2 * l >= n:
continue
if (
not begins[i + l][j]
or lens[i + l][j] != l
or not begins[i + 2 * l][j]
or lens[i + 2 * l][j] < l
):
continue
num_cols = 1
cur_j = j
while (
cur_j < m
and lens[i][cur_j] == l
and begins[i + l][cur_j]
and lens[i + l][cur_j] == l
and begins[i + 2 * l][cur_j]
and lens[i + 2 * l][cur_j] >= l
and t[i][cur_j] == t[i][j]
and t[i + l][cur_j] == t[i + l][j]
and t[i + 2 * l][cur_j] == t[i + 2 * l][j]
):
done[i][cur_j] = True
cur_j += 1
num_cols = cur_j - j
ans += num_cols * (num_cols + 1) // 2
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = list(map(int, input().split()))
a = [input() for i in range(n)]
b, c = [[(0) for i in range(m)] for j in range(n)], 0
for i in range(n):
for j in range(m):
if i == 0 or a[i][j] != a[i - 1][j]:
b[i][j] = 1
else:
b[i][j] = b[i - 1][j] + 1
w = 0
for i in range(2, n):
for j in range(m):
f = i - b[i][j]
if f > 0:
g = f - b[f][j]
if g != -1:
if b[i][j] == b[f][j] and b[g][j] >= b[i][j]:
if (
j == 0
or b[i][j] != b[i][j - 1]
or a[i][j] != a[i][j - 1]
or a[f][j] != a[f][j - 1]
or a[g][j] != a[g][j - 1]
):
w = 0
w += 1
else:
w = 0
else:
w = 0
else:
w = 0
c += w
print(c) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | import sys
input = lambda: sys.stdin.readline().strip()
def f(A, r, c, l):
q, w, e = A[r][c][0], A[r][c - l][0], A[r][c - 2 * l][0]
x, y, z = A[r][c][1], A[r][c - l][1], A[r][c - 2 * l][1]
if x != y and y != z and e >= l and w == q == l:
return l, z, y, x
else:
return 0
r, c = map(int, input().split())
s = ""
for i in range(r):
s += input()
arr = []
narr = [([0] * r) for i in range(c)]
for i in range(c):
arr.append(s[i : r * c : c])
r, c = c, r
length_str = [([0] * c) for i in range(r)]
for i in range(r):
for j in range(c):
if j == 0:
length_str[i][j] = 1, arr[i][j]
elif arr[i][j - 1] == arr[i][j]:
length_str[i][j] = length_str[i][j - 1][0] + 1, arr[i][j]
else:
length_str[i][j] = 1, arr[i][j]
for i in range(r):
for j in range(c):
l, _ = length_str[i][j]
if j - l * 3 + 1 < 0:
continue
else:
narr[i][j] = f(length_str, i, j, l)
dp = [([0] * c) for i in range(r)]
for j in range(c):
cnt = 1
for i in range(r):
if narr[i][j] == 0:
cnt = 1
continue
elif i == 0:
dp[i][j] = 1
elif narr[i][j] == narr[i - 1][j]:
cnt += 1
dp[i][j] = cnt
dp[i - 1][j] = 0
else:
cnt = 1
dp[i][j] = 1
ans = 0
for i in dp:
for j in i:
ans += j * (j + 1) // 2
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = map(int, input().split())
grid = []
d = {}
flags = 0
for i in range(n):
grid.append(input())
for j in range(m):
b = 0
while b < n - 1:
i = b
k = i
while k < n and grid[k][j] == grid[i][j]:
k += 1
c1 = k - i
clr1 = grid[k - 1][j]
i = k
if 2 * c1 <= n - k:
while k < n and grid[k][j] == grid[i][j]:
k += 1
c2 = k - i
clr2 = grid[k - 1][j]
i = k
if c1 == c2 and c2 <= n - k:
while k < n and grid[k][j] == grid[i][j]:
k += 1
if k - i == c2:
break
c3 = k - i
clr3 = grid[k - 1][j]
if c3 == c2:
flags += 1
if (c3, clr1, clr2, clr3, k - 2 * c3 - 1, k - 1) in d and d[
c3, clr1, clr2, clr3, k - 2 * c3 - 1, k - 1
][1] == j - 1:
flags += d[c3, clr1, clr2, clr3, k - 2 * c3 - 1, k - 1][0]
d[c3, clr1, clr2, clr3, k - 2 * c3 - 1, k - 1][1] = j
d[c3, clr1, clr2, clr3, k - 2 * c3 - 1, k - 1][0] += 1
else:
d[c3, clr1, clr2, clr3, k - 2 * c3 - 1, k - 1] = [1, j]
b += 1
print(flags) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP NUMBER VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER LIST NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | def return_specific_col(matrix, col_index):
col = []
for i in range(len(matrix)):
col.append(matrix[i][col_index])
return col
def make_list_to_str(lst):
result = ""
for e in lst:
result += e
return result
def find_strikes(lst):
first = lst[0]
counter = 1
strikes_list = []
for i in range(len(lst) - 1):
if lst[i] + 1 == lst[i + 1]:
counter += 1
else:
strikes_list.append(counter)
counter = 1
strikes_list.append(counter)
return strikes_list
def converter(lst):
first = lst[0]
counter = 0
counter_list = []
for i in range(len(lst)):
if lst[i] == first:
counter += 1
else:
counter_list.append(counter)
first = lst[i]
counter = 1
counter_list.append(counter)
return counter_list
def solve(lst, original_col):
if len(lst) < 3:
return []
counter = 0
sum_ = 0
poses = []
for i in range(len(lst) - 2):
arr = lst[i : i + 3]
if arr[2] >= arr[1] and arr[0] >= arr[1]:
counter += 1
poses.append(
str(sum_ + (arr[0] - arr[1]))
+ str(arr[1])
+ make_list_to_str(
original_col[
sum_ + (arr[0] - arr[1]) : sum_ + (arr[0] - arr[1]) + arr[1] * 3
]
)
)
sum_ += arr[0]
return poses
n, m = [int(x) for x in input().split()]
matrix = []
for i in range(n):
matrix.append(list(input()))
if n < 3:
print(0)
else:
strikes_dict = {}
for j in range(m):
for row_length in solve(
converter(return_specific_col(matrix, j)), return_specific_col(matrix, j)
):
if row_length in strikes_dict:
strikes_dict[row_length].append(j)
else:
strikes_dict[row_length] = [j]
counter = 0
for row_length in strikes_dict:
row_length = find_strikes(strikes_dict[row_length])
for i in row_length:
counter += (1 + i) * i // 2
print(counter) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | import sys
def __starting_point():
n, m = list(map(int, input().split()))
arr = sys.stdin.readlines()
f = 0
flgs = {}
for c in range(m):
cnts = [[arr[0][c], 1]]
for r in range(1, n):
if arr[r][c] == cnts[-1][0]:
cnts[-1][1] += 1
else:
cnts.append([arr[r][c], 1])
strt = 0
for i in range(len(cnts) - 2):
if cnts[i][1] >= cnts[i + 1][1] <= cnts[i + 2][1]:
lng = cnts[i + 1][1]
beg = strt + cnts[i][1] - cnts[i + 1][1]
clr = cnts[i][0], cnts[i + 1][0], cnts[i + 2][0], lng
k = clr, beg
if k in flgs and flgs[k][-1][-1] == c - 1:
flgs[k][-1][-1] = c
elif k in flgs:
flgs[k].append([c, c])
else:
flgs[k] = [[c, c]]
strt += cnts[i][1]
for flg in list(flgs.values()):
for fl in flg:
lng = fl[1] - fl[0] + 1
f += lng * (lng + 1) // 2
print(f)
__starting_point() | IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR VAR LIST LIST VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = map(int, input().split())
up = [[(0) for j in range(m)] for i in range(n)]
ldw = [[(0) for j in range(m)] for i in range(n)]
lup = [[(0) for j in range(m)] for i in range(n)]
v = []
for i in range(n):
v.append(input())
for i in range(n):
for j in range(m):
if not i or v[i][j] != v[i - 1][j]:
up[i][j] = 1
lup[i][j] = 1 if not j or v[i][j] != v[i][j - 1] else lup[i][j - 1] + 1
else:
up[i][j] = up[i - 1][j] + 1
lup[i][j] = min(
lup[i - 1][j],
1 if not j or v[i][j] != v[i][j - 1] else lup[i][j - 1] + 1,
)
for i in range(n - 1, -1, -1):
for j in range(m):
if i == n - 1 or v[i][j] != v[i + 1][j]:
ldw[i][j] = 1 if not j or v[i][j] != v[i][j - 1] else ldw[i][j - 1] + 1
else:
ldw[i][j] = min(
ldw[i + 1][j],
1 if not j or v[i][j] != v[i][j - 1] else ldw[i][j - 1] + 1,
)
ans = 0
for i in range(1, n - 1):
for j in range(m):
if v[i][j] == v[i + 1][j]:
continue
h = up[i][j]
up_st = i - h
if up_st < 0 or up[up_st][j] < h:
continue
dw_st = i + h
if dw_st >= n or up[dw_st][j] != h:
continue
w = min(lup[i][j], ldw[up_st - h + 1][j], lup[dw_st][j])
ans += w
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | def test_c(n, m, G):
mid = [[(False) for i in range(m)] for j in range(n)]
vis = [[(False) for i in range(m)] for j in range(n)]
L = [[(1) for i in range(m)] for j in range(n)]
for i in range(1, n):
for j in range(m):
if G[i][j] != G[i - 1][j]:
mid[i][j] = True
for j in range(m):
for i in range(n):
if i >= 1 and G[i][j] == G[i - 1][j]:
L[i][j] = L[i - 1][j] - 1
continue
k = 0
while i + k < n and G[i][j] == G[i + k][j]:
k += 1
L[i][j] = k
cnt = 0
for i in range(n):
for j in range(m):
if mid[i][j] == True and vis[i][j] == False:
if i - L[i][j] >= 0 and i + L[i][j] < n:
if L[i - L[i][j]][j] == L[i][j] and L[i + L[i][j]][j] >= L[i][j]:
k = 1
vis[i][j] = True
while (
j + k < m
and G[i][j] == G[i][j + k]
and L[i][j] == L[i][j + k]
and mid[i][j + k] == True
):
if (
L[i - L[i][j]][j + k] == L[i][j]
and G[i - L[i][j]][j + k] == G[i - L[i][j]][j]
and L[i + L[i][j]][j + k] >= L[i][j]
and G[i + L[i][j]][j + k] == G[i + L[i][j]][j]
):
vis[i][j + k] = True
k += 1
else:
break
cnt += int((k + 1) * k // 2)
print(cnt)
def __starting_point():
n, m = [int(x) for x in input().split()]
G = []
for i in range(n):
G.append(list(input()))
mid = [[(False) for i in range(m)] for j in range(n)]
vis = [[(False) for i in range(m)] for j in range(n)]
L = [[(1) for i in range(m)] for j in range(n)]
for i in range(1, n):
for j in range(m):
if G[i][j] != G[i - 1][j]:
mid[i][j] = True
for j in range(m):
for i in range(n):
if i >= 1 and G[i][j] == G[i - 1][j]:
L[i][j] = L[i - 1][j] - 1
continue
k = 0
while i + k < n and G[i][j] == G[i + k][j]:
k += 1
L[i][j] = k
cnt = 0
for i in range(n):
for j in range(m):
if mid[i][j] == True and vis[i][j] == False:
if i - L[i][j] >= 0 and i + L[i][j] < n:
if L[i - L[i][j]][j] == L[i][j] and L[i + L[i][j]][j] >= L[i][j]:
k = 1
vis[i][j] = True
while (
j + k < m
and G[i][j] == G[i][j + k]
and L[i][j] == L[i][j + k]
and mid[i][j + k] == True
):
if (
L[i - L[i][j]][j + k] == L[i][j]
and G[i - L[i][j]][j + k] == G[i - L[i][j]][j]
and L[i + L[i][j]][j + k] >= L[i][j]
and G[i + L[i][j]][j + k] == G[i + L[i][j]][j]
):
vis[i][j + k] = True
k += 1
else:
break
cnt += int((k + 1) * k // 2)
print(cnt)
__starting_point() | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | def number_rectangle(h):
l = [(0) for i in range(len(h))]
stack = []
dp = [(0) for i in range(len(h))]
result = 0
for i in range(len(h)):
while len(stack) > 0 and stack[-1] >= h[i]:
stack.pop()
if len(stack) == 0:
dp[i] = i + 1
else:
dp[i] = dp[stack[-1]] + (i - stack[-1]) * h[i]
result += dp[i]
return result
def main():
rows, cols = list(map(int, input().split()))
rows += 1
a = []
a.append("-" * cols)
for i in range(1, rows):
a.append(input())
h = [[(0) for j in range(cols)] for i in range(rows)]
result = 0
for i in range(1, rows):
last_state = 0, 0, 0, 0, 0, 0
same_state = 0
sub = []
for j in range(0, cols):
if a[i][j] == a[i - 1][j]:
h[i][j] = h[i - 1][j] + 1
else:
h[i][j] = 1
i2 = i - h[i][j]
i3 = i2 - h[i2][j]
curr_state = h[i][j], h[i2][j], a[i][j], a[i2][j], a[i3][j]
if h[i][j] == h[i2][j] and h[i3][j] >= h[i][j]:
if curr_state == last_state:
sub.append(h[i3][j] - h[i][j])
else:
result += number_rectangle(sub)
sub.clear()
sub.append(h[i3][j] - h[i][j])
else:
result += number_rectangle(sub)
sub.clear()
last_state = curr_state
result += number_rectangle(sub)
result = int(result)
print(result)
main() | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | def main():
buf = input()
buflist = buf.split()
n = int(buflist[0])
m = int(buflist[1])
blanket = []
for i in range(n):
buf = input()
blanket.append(buf)
dp = [[]]
for j in range(m + 1):
dp[0].append([0, (0, "0"), (0, "0"), (0, "0"), 0])
for i in range(n):
dp.append([[0, (0, "0"), (0, "0"), (0, "0"), 0]])
for i in range(n):
for j in range(m):
dp[i + 1].append([0, (0, None), (0, None), (0, None), 0])
dp[i + 1][j + 1][0] = dp[i + 1][j][0] + dp[i][j + 1][0] - dp[i][j][0]
if blanket[i][j] == dp[i][j + 1][1][1]:
dp[i + 1][j + 1][1] = dp[i][j + 1][1][0] + 1, dp[i][j + 1][1][1]
dp[i + 1][j + 1][2] = dp[i][j + 1][2]
dp[i + 1][j + 1][3] = dp[i][j + 1][3]
else:
dp[i + 1][j + 1][1] = 1, blanket[i][j]
dp[i + 1][j + 1][2] = dp[i][j + 1][1]
dp[i + 1][j + 1][3] = dp[i][j + 1][2]
if (
dp[i + 1][j + 1][1][0] == dp[i + 1][j + 1][2][0]
and dp[i + 1][j + 1][2][0] <= dp[i + 1][j + 1][3][0]
):
if (
dp[i + 1][j + 1][1] == dp[i + 1][j][1]
and dp[i + 1][j + 1][2] == dp[i + 1][j][2]
and dp[i + 1][j + 1][3][1] == dp[i + 1][j][3][1]
):
dp[i + 1][j + 1][4] = dp[i + 1][j][4]
dp[i + 1][j + 1][0] += dp[i + 1][j + 1][4] + 1
dp[i + 1][j + 1][4] += 1
print(dp[n][m][0])
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER STRING NUMBER STRING NUMBER STRING NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST LIST NUMBER NUMBER STRING NUMBER STRING NUMBER STRING NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST NUMBER NUMBER NONE NUMBER NONE NUMBER NONE NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = [int(i) for i in input().split()]
data = []
for i in range(n):
data.append(list(input()))
f = []
for i in range(m):
temp = []
prev = 0
cnt = 0
for j in range(n):
ch = ord(data[j][i]) - 95
if ch == prev:
cnt += 1
else:
temp.append((prev, cnt, j - cnt))
prev = ch
cnt = 1
temp.append((prev, cnt, n - cnt))
for t in range(1, len(temp) - 1):
td = temp[t - 1]
tf = temp[t + 1]
te = temp[t]
if te[0] != td[0] and te[0] != tf[0] and td[1] >= te[1] and tf[1] >= te[1]:
f.append((te[2], i, te[1], (td[0] << 10) + (te[0] << 5) + tf[0]))
tot = 0
f.sort()
le = len(f)
i = 0
while i < le:
d = f[i]
cnt = 1
while (
i < le - 1
and d[1] + cnt == f[i + 1][1]
and d[0] == f[i + 1][0]
and d[2] == f[i + 1][2]
and d[3] == f[i + 1][3]
):
i += 1
cnt += 1
tot += cnt * (cnt + 1) // 2
i += 1
print(tot) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = map(int, input().split())
board = [input() for _ in range(n)]
u = [[(1) for _ in range(m)] for _ in range(n)]
l = [[(1) for _ in range(m)] for _ in range(n)]
for i in range(1, n):
for j in range(m):
if board[i][j] == board[i - 1][j]:
u[i][j] = u[i - 1][j] + 1
for j in range(1, m):
for i in range(n):
if board[i][j] == board[i][j - 1]:
l[i][j] = l[i][j - 1] + 1
answer = 0
for i1 in range(n):
for j in range(m):
k = u[i1][j]
i2 = i1 - k
if i2 >= 0 and u[i2][j] == k:
i3 = i2 - k
if i3 >= 0 and u[i3][j] >= k:
answer += min(l[i][j] for i in range(i3 - k + 1, i1 + 1))
print(answer) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = map(int, input().split())
A = [input() for _ in range(n)]
Dx = [[0] for _ in range(n)]
Dy = [[0] for _ in range(m)]
for i in range(n):
last = None
k = 0
for j in range(m):
c = A[i][j]
if c != last:
k = 0
k += 1
last = c
Dx[i].append(k)
for j in range(m):
last = None
k = 0
for i in range(n):
c = A[i][j]
if c != last:
k = 0
k += 1
last = c
Dy[j].append(k)
count = 0
for j in reversed(range(m)):
col = Dy[j]
i = len(col) - 1
strips = []
while i != 0:
strips.append((col[i], i - col[i]))
i -= col[i]
for k in range(1, len(strips) - 1):
if strips[k - 1][0] >= strips[k][0] <= strips[k + 1][0]:
l = strips[k][1] - strips[k][0]
r = strips[k - 1][1] + strips[k][0]
res = min(Dx[i][j + 1] for i in range(l, r))
count += res
print(count) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Innokenty works at a flea market and sells some random stuff rare items. Recently he found an old rectangular blanket. It turned out that the blanket is split in $n \cdot m$ colored pieces that form a rectangle with $n$ rows and $m$ columns.
The colored pieces attracted Innokenty's attention so he immediately came up with the following business plan. If he cuts out a subrectangle consisting of three colored stripes, he can sell it as a flag of some country. Innokenty decided that a subrectangle is similar enough to a flag of some country if it consists of three stripes of equal heights placed one above another, where each stripe consists of cells of equal color. Of course, the color of the top stripe must be different from the color of the middle stripe; and the color of the middle stripe must be different from the color of the bottom stripe.
Innokenty has not yet decided what part he will cut out, but he is sure that the flag's boundaries should go along grid lines. Also, Innokenty won't rotate the blanket. Please help Innokenty and count the number of different subrectangles Innokenty can cut out and sell as a flag. Two subrectangles located in different places but forming the same flag are still considered different. [Image] [Image] [Image]
These subrectangles are flags. [Image] [Image] [Image] [Image] [Image] [Image]
These subrectangles are not flags.
-----Input-----
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 1\,000$) — the number of rows and the number of columns on the blanket.
Each of the next $n$ lines contains $m$ lowercase English letters from 'a' to 'z' and describes a row of the blanket. Equal letters correspond to equal colors, different letters correspond to different colors.
-----Output-----
In the only line print the number of subrectangles which form valid flags.
-----Examples-----
Input
4 3
aaa
bbb
ccb
ddd
Output
6
Input
6 1
a
a
b
b
c
c
Output
1
-----Note----- [Image] [Image]
The selected subrectangles are flags in the first example. | n, m = [int(x) for x in input().split()]
a = [([] * (m + 1)) for i in range(n + 1)]
cols = [([0] * (m + 1)) for i in range(n + 1)]
rows = [([0] * (m + 1)) for i in range(n + 1)]
a[0] = "*" * (m + 1)
for i in range(1, n + 1):
a[i] = "*" + input()
for i in range(1, n + 1):
for j in range(1, m + 1):
if a[i][j] == a[i][j - 1]:
rows[i][j] = rows[i][j - 1] + 1
for i in range(1, n + 1):
for j in range(1, m + 1):
if a[i][j] == a[i - 1][j]:
cols[i][j] = cols[i - 1][j] + 1
ans = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
x = cols[i][j] + 1
if i < x * 3:
continue
y = cols[i - x][j] + 1
if x != y:
continue
z = cols[i - x - y][j] + 1
if z < y:
continue
minx = 2 * m
for l in range(0, x * 3):
minx = min(minx, rows[i - l][j])
ans += minx + 1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP STRING FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | s = input()
n = len(s)
f = False
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if (
not f
and not s[i] == s[j] == s[k] == "0"
and int(s[i] + s[j] + s[k]) % 8 == 0
):
f = True
print("YES")
print(s[i] + s[j] + s[k])
if not f:
for i in range(n):
for j in range(i + 1, n):
if not f and not s[i] == s[j] == "0" and int(s[i] + s[j]) % 8 == 0:
f = True
print("YES")
print(s[i] + s[j])
if not f:
for i in range(n):
if not f and int(s[i]) % 8 == 0:
f = True
print("YES")
print(s[i])
if not f:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR STRING BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR STRING BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = input()
ans = -1
def findNum(num):
j = 0
for i in n:
if i == str(num)[j]:
j += 1
if j == len(str(num)):
return True
return False
for i in range(0, 999, 8):
if findNum(i):
ans = i
break
if ans == -1:
print("NO")
else:
print("YES")
print(ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = input()
for i in range(len(n)):
if int(n[i]) % 8 == 0:
print("YES")
print(n[i])
exit(0)
for i in range(len(n)):
for j in range(0, i):
if (int(n[j]) * 10 + int(n[i])) % 8 == 0:
print("YES")
print(n[j] + n[i])
exit(0)
for i in range(len(n)):
for j in range(0, i):
for g in range(0, j):
if (int(n[g]) * 100 + int(n[j]) * 10 + int(n[i])) % 8 == 0:
print("YES")
print(n[g] + n[j] + n[i])
exit(0)
print("NO") | ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | def func(s, i, ans, b):
if ans % 8 == 0 and b:
return ans
if i < 0:
return -1
if ans > 99:
return -1
x = func(s, i - 1, ans, b)
if x != -1:
return x
if ans > 9:
ans += 100 * int(s[i])
return func(s, i - 1, ans, b)
elif ans > 0:
ans += 10 * int(s[i])
return func(s, i - 1, ans, b)
else:
ans = int(s[i])
b = True
return func(s, i - 1, ans, b)
s = input()
x = func(s, len(s) - 1, 0, False)
if x == -1:
print("NO")
else:
print("YES")
print(x) | FUNC_DEF IF BIN_OP VAR NUMBER NUMBER VAR RETURN VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | N = list(input())
M = len(N)
for i in N:
if int(i) % 8 == 0:
print("YES")
print(i)
exit()
for i in range(M - 1):
for j in range(i, M):
if int(N[i] + N[j]) % 8 == 0:
print("YES")
print(N[i] + N[j])
exit()
for i in range(M - 2):
for j in range(i + 1, M - 1):
for k in range(j + 1, M):
if int(N[i] + N[j] + N[k]) % 8 == 0:
print("YES")
print(N[i] + N[j] + N[k])
exit()
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | a = input()[::-1]
u = [False] * 3
t4 = 0
done = False
for i in a:
j = int(i)
if j % 2:
if t4:
print("YES\n" + i + t4)
done = True
break
if j % 4 == 1:
if u[2]:
print("YES\n" + i + "6")
done = True
break
if u[0]:
t4 = i + "2"
else:
if u[0]:
print("YES\n" + i + "2")
done = True
break
if u[2]:
t4 = i + "6"
else:
if j % 8 == 0:
print("YES\n" + i)
done = True
break
elif u[1]:
if j == 4:
if u[1]:
t4 = i + "4"
else:
print("YES\n" + i + "4")
done = True
break
u[j // 2 - 1] = True
if not done:
print("NO") | ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | s = str(input())
if len(s) < 3:
a = list(map(str, list(range(0, 100, 8))))
if s in a:
print("YES")
print(s)
quit()
else:
if len(s) == 1:
print("NO")
quit()
if len(s) == 2:
if str(s[0]) in a:
print("YES")
print(s[0])
quit()
elif str(s[1]) in a:
print("YES")
print(s[1])
quit()
else:
print("NO")
quit()
for i in range(0, len(s)):
for j in range(i + 1, len(s)):
for k in range(j + 1, len(s)):
p1 = s[i] + s[j] + s[k]
p2 = s[i] + s[j]
p3 = s[i] + s[k]
p4 = s[j] + s[k]
p5 = s[i]
p6 = s[j]
p7 = s[k]
p1 = int(p1)
p2 = int(p2)
p3 = int(p3)
p4 = int(p4)
p5 = int(p5)
p6 = int(p6)
p7 = int(p7)
if p1 % 8 == 0:
print("YES")
print(p1)
quit()
elif p2 % 8 == 0:
print("YES")
print(p2)
quit()
elif p3 % 8 == 0:
print("YES")
print(p3)
quit()
elif p4 % 8 == 0:
print("YES")
print(p4)
quit()
elif p5 % 8 == 0:
print("YES")
print(p5)
quit()
elif p6 % 8 == 0:
print("YES")
print(p6)
quit()
elif p7 % 8 == 0:
print("YES")
print(p7)
quit()
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | class Solver:
def __init__(self):
self.n = None
def main(self):
self.n = input().strip()
l = len(self.n)
for i in range(l):
i1 = int(self.n[i])
if i1 % 8 == 0:
print("Yes\n{}".format(i1))
return
for j in range(i + 1, l):
i2 = int(self.n[i] + self.n[j])
if i2 % 8 == 0 and i2 != 0:
print("Yes\n{}".format(i2))
return
for k in range(j + 1, l):
i3 = int(self.n[i] + self.n[j] + self.n[k])
if i3 % 8 == 0 and i3 != 0:
print("Yes\n{}".format(i3))
return
print("No")
Solver().main() | CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL FUNC_CALL VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | def check2(s):
if int(s) % 8 == 0:
return s
elif int(s[0]) % 8 == 0:
return s[0]
elif int(s[1]) % 8 == 0:
return s[1]
else:
return -1
def check3(s):
if int(s) % 8 == 0:
return s
if int(s[2]) % 8 == 0:
return s[2]
if int(s[1]) % 8 == 0:
return s[1]
if int(s[0]) % 8 == 0:
return s[0]
if check2(s[0] + s[1]) != -1:
return s[0] + s[1]
if check2(s[1] + s[2]) != -1:
return s[1] + s[2]
if check2(s[0] + s[2]) != -1:
return s[0] + s[2]
return -1
def check_final(s):
if int(s[len(s) - 3 :]) % 8 == 0:
return True
else:
return False
s = input()
n = len(s)
if len(s) == 1:
if int(s) % 8 == 0:
print("YES")
print(s)
exit()
else:
print("NO")
exit()
elif len(s) == 2:
if check2(s) != -1:
print("YES")
print(check2(s))
exit()
else:
print("NO")
exit()
elif len(s) == 3:
if check3(s) != -1:
print("YES")
print(check3(s))
exit()
else:
a = s
for i in range(n):
fir = int(s[i]) * 100
for j in range(i + 1, n):
sec = int(s[j]) * 10
for q in range(j + 1, n):
thr = int(s[q])
if check3(str(fir + sec + thr)) != -1:
print("YES")
print(check3(str(fir + sec + thr)))
exit()
print("NO") | FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = int(input())
solved = 0
if "0" in str(n):
print("YES")
print(0)
solved = 1
elif "8" in str(n):
print("YES")
print(8)
solved = 1
if solved == 0:
nstr = str(n)
for i in range(len(str(n))):
for j in range(i + 1, len(str(n))):
if (int(nstr[i]) * 10 + int(nstr[j])) % 8 == 0:
print("YES")
print(int(nstr[i]) * 10 + int(nstr[j]))
solved = 1
break
if solved == 1:
break
if solved == 0:
nstr = str(n)
for i in range(len(str(n))):
for j in range(i + 1, len(str(n))):
for k in range(j + 1, len(str(n))):
if (int(nstr[i]) * 100 + int(nstr[j]) * 10 + int(nstr[k])) % 8 == 0:
print("YES")
print(int(nstr[i]) * 100 + int(nstr[j]) * 10 + int(nstr[k]))
solved = 1
break
if solved == 1:
break
if solved == 1:
break
if solved == 0:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | if True:
s = input()
for ans in range(0, 1000, 8):
i = -1
for x in str(ans):
i = s.find(x, i + 1)
if i < 0:
break
else:
print("YES")
print(ans)
break
else:
print("NO")
exit() | IF NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
def solve(num):
for x in range(len(num)):
if int(num[x]) % 8 == 0:
return "YES\n" + num[x] + "\n"
for y in range(x + 1, len(num)):
if int(num[x] + num[y]) % 8 == 0:
return "YES\n" + num[x] + num[y] + "\n"
for z in range(y + 1, len(num)):
X = num[x] + num[y] + num[z]
if int(X) % 8 == 0:
return "YES\n" + X + "\n"
return "NO"
num = input()
print(solve(num)) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP STRING VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP STRING VAR VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP STRING VAR STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = input()
m = len(n)
if "0" in n:
print("YES")
print(0)
elif "8" in n:
print("YES")
print(8)
else:
f = 0
for i in range(m):
for j in range(i + 1, m):
if int(n[i] + n[j]) % 8 == 0:
f = 1
out = int(n[i] + n[j])
break
if f == 1:
print("YES")
print(out)
else:
f = 0
for i in range(m):
for j in range(i + 1, m):
for k in range(j + 1, m):
if int(n[i] + n[j] + n[k]) % 8 == 0:
out = int(n[i] + n[j] + n[k])
f = 1
break
if f == 1:
print("YES")
print(out)
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | x = input()
if x.count("8") > 0:
print("YES")
print(8)
elif x.count("0") > 0:
print("YES")
print(0)
else:
ok = False
for a in range(len(x)):
for b in range(a + 1, len(x)):
if int(x[a] + x[b]) % 8 == 0:
ok = True
print("YES")
print(int(x[a] + x[b]))
break
if ok == True:
break
if ok == False:
for a in range(len(x)):
for b in range(a + 1, len(x)):
for c in range(b + 1, len(x)):
if int(x[a] + x[b] + x[c]) % 8 == 0:
ok = True
print("YES")
print(int(x[a] + x[b] + x[c]))
break
if ok == True:
break
if ok == True:
break
if ok == False:
print("NO") | ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = input()
f = 0
for i in n:
if i == "0":
f = 1
break
if f == 1:
print("YES")
print(0)
elif len(n) == 1:
if int(n) % 8 == 0:
print("YES")
print(int(n))
else:
print("NO")
elif len(n) == 2:
if int(n) % 8 == 0:
print("YES")
print(int(n))
elif int(n[0]) % 8 == 0:
print("YES")
print(int(n[0]))
elif int(n[1]) % 8 == 0:
print("YES")
print(int(n[i]))
else:
print("NO")
else:
br = 0
for i in range(len(n)):
if br == 1:
break
if int(n[i]) % 8 == 0:
print("YES")
print(int(n[i]))
br = 1
break
for j in range(i + 1, len(n)):
temp = ""
temp += n[i]
temp += n[j]
if int(temp) % 8 == 0:
br = 1
print("YES")
print(temp)
break
for k in range(j + 1, len(n)):
temp = ""
temp += n[i]
temp += n[j]
temp += n[k]
if int(temp) % 8 == 0:
br = 1
print("YES")
print(temp)
break
if br == 1:
break
if br == 1:
break
if br == 0:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | def solve(s):
for i in range(len(s)):
a = int(s[i])
if a % 2 != 0:
continue
if a % 8 == 0:
print("YES")
print(a)
return
for j in range(i + 1, len(s)):
b = int(s[j]) * 10 + a
if b % 4 != 0:
continue
if b % 8 == 0:
print("YES")
print(b)
return
for k in range(j + 1, len(s)):
c = int(s[k]) * 100 + b
if c % 8 != 0:
continue
if c % 8 == 0:
print("YES")
print(c)
return
print("NO")
return
s = input()
s = s[::-1]
solve(s) | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | a = input()
n = len(a)
can = False
ans = 0
for i in range(n):
t = int(a[i])
if t % 8 == 0:
can = True
ans = t
break
for j in range(i + 1, n):
t = int(a[i] + a[j])
if t % 8 == 0:
ans = t
can = True
break
for k in range(j + 1, n):
t = int(a[i] + a[j] + a[k])
if t % 8 == 0:
ans = t
can = True
break
if can:
print("YES")
print(ans)
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | a, s = [str(8 * x) for x in range(125)], input()
for num in a:
p = -1
for d in num:
p = s.find(d, p + 1)
if p == -1:
break
else:
print("YES\n{}".format(num))
break
else:
print("NO") | ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | def read():
a = input().strip()
return a
def find(a, n):
pos = 0
for c in n:
pos = a.find(c, pos) + 1
if pos == 0:
break
if pos:
return n
def solve():
if len(a) >= 3:
for i in range(125):
v = find(a, str(i * 8))
if v:
return v
else:
if len(a) == 2:
if int(a[0]) % 8 == 0:
return int(a[0])
if int(a[1]) % 8 == 0:
return int(a[1])
if int(a) % 8 == 0:
return a
if len(a) == 1:
if int(a) % 8 == 0:
return a
return -1
a = read()
sol = solve()
if sol != -1:
print("YES")
print(sol)
else:
print("NO") | FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR RETURN VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | s = input()
x = ""
n = len(s)
if n == 1:
if int(s) % 8 == 0:
print("YES")
print(s)
else:
print("NO")
elif n == 2:
if int(s) % 8 == 0:
print("YES")
print(s)
elif int(s[1]) % 8 == 0:
print("YES")
print(s[1])
elif int(s[0]) % 8 == 0:
print("YES")
print(s[0])
else:
print("NO")
elif n == 3:
if int(s) % 8 == 0:
print("YES")
print(s)
elif int(s[1:]) % 8 == 0:
print("YES")
print(s[1:])
elif int(s[:2]) % 8 == 0:
print("YES")
print(s[:2])
elif int(s[0] + s[2]) % 8 == 0:
print("YES")
print(s[0] + s[2])
elif int(s[1]) % 8 == 0:
print("YES")
print(s[1])
elif int(s[0]) % 8 == 0:
print("YES")
print(s[0])
elif int(s[2]) % 8 == 0:
print("YES")
print(s[2])
else:
print("NO")
else:
x = 0
for q in range(n):
if int(s[q]) % 8 == 0:
print("YES")
print(s[q])
x = 1
break
for w in range(q + 1, n):
if int(s[q] + s[w]) % 8 == 0:
print("YES")
print(s[q] + s[w])
x = 1
break
for r in range(w + 1, n):
if int(s[q] + s[w] + s[r]) % 8 == 0:
x = 1
print("YES")
print(s[q] + s[w] + s[r])
break
if x:
break
if x:
break
if x == 0:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | from itertools import combinations
number = "00" + input()
for w in (
int(number[i] + number[j] + number[k])
for i, j, k in combinations(range(len(number)), 3)
):
if w % 8 == 0:
print("YES\n{}".format(w))
exit(0)
print("NO") | ASSIGN VAR BIN_OP STRING FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = input()
def solve(n):
for i in range(len(n)):
number = n[i]
if int(number) % 8 == 0:
return number
for j in range(i + 1, len(n)):
tmp = number + n[j]
if int(tmp) % 8 == 0 or int(n[j]) % 8 == 0:
return tmp if int(tmp) % 8 == 0 else n[j]
for k in range(j + 1, len(n)):
tmp2 = tmp + n[k]
if int(tmp2) % 8 == 0:
return tmp2
return "NO"
ans = str(solve(n))
if ans == "NO":
print("NO")
else:
print("YES")
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | from itertools import combinations
s = input()
sss = s
flag = 0
flag2 = 0
ss = []
even = ["0", "2", "4", "6", "8"]
for i in range(len(sss)):
if sss[i] in even:
if i >= 3:
s = sss[i - 3 : i + 1]
else:
s = sss[: i + 1]
ss.append(s)
flag2 = 1
if flag2 == 0:
ss.append("1")
for s in ss:
for i in range(1, len(s) + 1):
if flag == 1:
break
if flag2 == 0:
break
comb = combinations(s, i)
for j in comb:
n = ""
for k in j:
n = n + k
if int(n) % 8 == 0:
flag = 1
res = n
if flag == 1:
print("YES")
print(res)
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = ["0", "0"] + list(input())
l = len(n)
n = [int(x) for x in n]
for i in range(l):
for j in range(i + 1, l):
for k in range(j + 1, l):
x = n[i] * 100 + n[j] * 10 + n[k]
if x % 8 == 0:
print("YES")
print(x)
exit(0)
print("NO") | ASSIGN VAR BIN_OP LIST STRING STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = input()
l = len(n)
f = 0
for i in range(l):
if int(n[i]) % 8 == 0:
f = 1
print("YES")
print(n[i])
break
if f == 0 and l > 1:
for i in range(l - 1):
for j in range(i + 1, l):
if n[i] != "0" and (10 * int(n[i]) + int(n[j])) % 8 == 0:
print("YES")
print(10 * int(n[i]) + int(n[j]))
f = 1
break
if f == 1:
break
if f == 0 and l > 2:
flg = 0
for i in range(l - 2):
for j in range(i + 1, l - 1):
for k in range(j + 1, l):
if (
n[i] != "0"
and (100 * int(n[i]) + 10 * int(n[j]) + int(n[k])) % 8 == 0
):
print("YES")
print(100 * int(n[i]) + 10 * int(n[j]) + int(n[k]))
f = 1
flg = 1
break
if flg == 1:
break
if flg == 1:
break
if f == 0:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR STRING BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | import sys
def func_(arr):
for i in range(0, len(arr) - 2):
for j in range(i + 1, len(arr) - 1):
for k in range(j + 1, len(arr)):
tmp = arr[i] * 100 + arr[j] * 10 + arr[k]
if tmp % 8 is 0:
print("YES")
print(tmp)
sys.exit()
def func_2(arr):
for i in range(0, len(arr) - 1):
for j in range(i + 1, len(arr)):
tmp = arr[i] * 10 + arr[j]
if tmp % 8 is 0:
print("YES")
print(tmp)
sys.exit()
arr = list(map(int, input()))
if 0 in arr:
print("YES")
print(0)
sys.exit()
elif 8 in arr:
print("YES")
print(8)
sys.exit()
if len(arr) is 2 or len(arr) is 1:
tmp = int("".join([str(i) for i in arr]))
if tmp % 8 is 0:
print("YES")
print(tmp)
else:
print("NO")
sys.exit()
func_(arr)
func_2(arr)
print("NO") | IMPORT FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | n = input()
s = len(n)
a = [([False] * 8) for _ in range(s)]
prev = [([-1] * 8) for _ in range(s)]
a[0][int(n[0]) % 8] = True
for i in range(1, s):
a[i][int(n[i]) % 8] = True
for j in range(8):
if a[i - 1][j]:
a[i][(j * 10 + int(n[i])) % 8] = True
prev[i][(j * 10 + int(n[i])) % 8] = j
a[i][j] = True
prev[i][j] = j
res = ""
for i in range(s):
if a[i][0]:
cur_i = i
cur_j = 0
while True:
if prev[cur_i][cur_j] == -1 or prev[cur_i][cur_j] != cur_j:
res += n[cur_i]
if prev[cur_i][cur_j] == -1:
break
cur_j = prev[cur_i][cur_j]
cur_i -= 1
break
if len(res) > 0:
print("YES")
print(res[::-1])
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | List = input()
Len = len(List)
for i in range(Len):
if int(List[i]) % 8 == 0:
print("YES\n" + List[i])
exit()
for i in range(Len):
for j in range(i + 1, Len):
if int(List[i] + List[j]) % 8 == 0:
print("YES\n" + List[i] + List[j])
exit()
for i in range(Len):
for j in range(i + 1, Len):
for t in range(j + 1, Len):
if int(List[i] + List[j] + List[t]) % 8 == 0:
print("YES\n" + List[i] + List[j] + List[t])
exit()
print("NO") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP STRING VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | s = input()
if s.count("0"):
print("YES")
print("0")
exit()
if s.count("8"):
print("YES")
print("8")
exit()
if len(s) < 3:
x = int(s)
if x % 8 == 0:
print("YES")
print(s)
else:
print("NO")
exit()
else:
i = 0
f = 0
while i < len(s) - 2 and f == 0:
j = i + 1
while j < len(s) - 1 and f == 0:
k = j + 1
while k < len(s) and f == 0:
a = s[i] + s[j] + s[k]
x = int(a)
if x % 8 == 0:
f = 1
else:
k += 1
if f != 1:
j += 1
if f != 1:
i += 1
if f:
print("YES")
e = s[i] + s[j] + s[k]
print(e)
exit()
i = 0
while i < len(s) - 1 and f == 0:
j = i + 1
while j < len(s) and f == 0:
a = s[i] + s[j]
x = int(a)
if x % 8 == 0:
f = 1
else:
j += 1
if f != 1:
i += 1
if f:
print("YES")
e = s[i] + s[j]
print(e)
exit()
print("NO") | ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
-----Input-----
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
-----Output-----
Print "NO" (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print "YES" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
-----Examples-----
Input
3454
Output
YES
344
Input
10
Output
YES
0
Input
111111
Output
NO | answer = ""
def exists(a, b):
pos = 0
global answer
answer = ""
for ch in a:
if int(ch) % 8 == 0:
digit = 0
elif int(ch) % 4 == 0:
digit = 4
elif int(ch) % 2 == 0:
digit = 2
elif int(ch) % 2 != 0:
digit = 1
if pos < len(b) and str(digit) == b[pos]:
answer = answer + ch
pos += 1
if pos == len(b):
return True
else:
return False
def exists2(a, b):
global answer
answer = ""
pos = 0
for ch in a:
if pos < len(b) and ch == b[pos]:
answer = answer + ch
pos += 1
return pos == len(b)
number = input()
if exists(number, "0"):
print("YES")
print(answer)
elif exists(number, "24"):
print("YES")
print(answer)
elif exists2(number, "16"):
print("YES")
print(answer)
elif exists2(number, "32"):
print("YES")
print(answer)
elif exists2(number, "56"):
print("YES")
print(answer)
elif exists(number, "112"):
print("YES")
print(answer)
elif exists(number, "144"):
print("YES")
print(answer)
else:
print("NO") | ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.