description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.
A block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.
For example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.
For all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.
Since these integers may be too large, print them modulo $998244353$.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
-----Output-----
In the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.
Since these integers may be too large, print them modulo $998244353$.
-----Examples-----
Input
1
Output
10
Input
2
Output
180 10 | n = int(input())
mod = 998244353
pow = [0, 10]
pre = [0, 10]
for i in range(n - 1):
pre += [(pre[-1] + pow[-1] * 10) % mod]
pow += [pow[-1] * 10 % mod]
ans = [10]
preans = [10]
for i in range(2, n + 1):
x = pow[i] * i - preans[-1] - ans[-1] - (pow[i - 1] * (i - 1) - ans[-1])
if x < 0:
x += 998244353
ans += [x % mod]
preans += [(preans[-1] + ans[-1]) % mod]
ans.reverse()
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR LIST BIN_OP VAR VAR VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.
A block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.
For example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.
For all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.
Since these integers may be too large, print them modulo $998244353$.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
-----Output-----
In the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.
Since these integers may be too large, print them modulo $998244353$.
-----Examples-----
Input
1
Output
10
Input
2
Output
180 10 | def force(n):
res = [0] * n
for i in range(int(10**n)):
s = str(i).zfill(n)
it = iter(s)
last = next(it)
c = 1
for j in it:
if j == last:
c += 1
else:
res[c - 1] += 1
c = 1
last = j
res[c - 1] += 1
return res
def optim_n2(max_n, base=10, mod=998244353):
f = [0] * (max_n + 1)
for n in range(1, max_n + 1):
s = pow(base, n, mod) * n
for i in range(1, n):
s -= f[i] * (n - i + 1)
s = s % mod
f[n] = s
return f[: -len(f) : -1]
def optim(n, base=10, mod=998244353):
res = []
for k in range(1, n - 1):
i = n - k - 1
s = pow(10, i, mod)
s = s * (180 + i * 81) % mod
res.append(s)
if n > 1:
res.append(180)
res.append(10)
return res
def solve():
n = int(input())
res = optim(n)
print(" ".join(map(str, res)))
def main():
solve()
main() | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.
A block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.
For example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.
For all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.
Since these integers may be too large, print them modulo $998244353$.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
-----Output-----
In the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.
Since these integers may be too large, print them modulo $998244353$.
-----Examples-----
Input
1
Output
10
Input
2
Output
180 10 | n = int(input())
m = 998244353
a = []
a.append(1)
for i in range(1, 200001):
a.append(10 * a[i - 1] % m)
for i in range(1, n):
x = int(2 * 9 * a[n - i - 1] * 10) + (n - i - 1) * 81 * a[n - i - 2] * 10
print(x % m, end=" ")
print(10) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR NUMBER |
You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.
A block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.
For example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.
For all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.
Since these integers may be too large, print them modulo $998244353$.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
-----Output-----
In the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.
Since these integers may be too large, print them modulo $998244353$.
-----Examples-----
Input
1
Output
10
Input
2
Output
180 10 | from sys import stdin
input = stdin.readline
n = int(input())
dp = [0, 10, 180]
s = 190
for x in range(3, n + 1):
dp.append(
(x * pow(10, x, 998244353) - (x - 1) * pow(10, x - 1, 998244353) - s)
% 998244353
)
s += dp[-1]
for x in dp[n:0:-1]:
print(x, end=" ") | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING |
You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.
A block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.
For example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.
For all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.
Since these integers may be too large, print them modulo $998244353$.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
-----Output-----
In the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.
Since these integers may be too large, print them modulo $998244353$.
-----Examples-----
Input
1
Output
10
Input
2
Output
180 10 | import sys
input = sys.stdin.readline
mod = 998244353
n = int(input())
f = [(0) for i in range(n + 1)]
d = []
for i in range(n):
if i == 0:
d.append(10)
elif i == 1:
d.append(180)
else:
c = (180 + 81 * (i - 1)) * pow(10, i - 1, mod) % mod
d.append(c)
print(*d[::-1]) | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER 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 ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You wrote down all integers from $0$ to $10^n - 1$, padding them with leading zeroes so their lengths are exactly $n$. For example, if $n = 3$ then you wrote out 000, 001, ..., 998, 999.
A block in an integer $x$ is a consecutive segment of equal digits that cannot be extended to the left or to the right.
For example, in the integer $00027734000$ there are three blocks of length $1$, one block of length $2$ and two blocks of length $3$.
For all integers $i$ from $1$ to $n$ count the number of blocks of length $i$ among the written down integers.
Since these integers may be too large, print them modulo $998244353$.
-----Input-----
The only line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$).
-----Output-----
In the only line print $n$ integers. The $i$-th integer is equal to the number of blocks of length $i$.
Since these integers may be too large, print them modulo $998244353$.
-----Examples-----
Input
1
Output
10
Input
2
Output
180 10 | import sys
def main():
import sys
input = sys.stdin.readline
n = int(input())
mod = 998244353
l = [1]
ans = []
for i in range(2 * 10**5):
l.append(l[-1] * 10 % mod)
for i in range(1, n):
ans.append(
(10 * 9 * l[n - i - 1] * 2 + 10 * 81 * l[n - i - 2] * (n - i - 1)) % mod
)
ans.append(10)
print(*ans)
main() | IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def min_cost(self, nums, i, k, n, dp):
if i == n:
return 0
if dp[i] != -1:
return dp[i]
mn = float("inf")
sum_ = 0
for j in range(i, n):
if sum_ + nums[j] + (j - i) > k:
break
if j == n - 1:
mn = 0
sum_ += nums[j]
mn = min(
mn, (k - sum_ - (j - i)) ** 2 + self.min_cost(nums, j + 1, k, n, dp)
)
dp[i] = mn
return dp[i]
def solveWordWrap(self, nums, k):
dp = [(-1) for _ in range(n)]
return self.min_cost(nums, 0, k, n, dp) | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [0] * n
ans = [0] * n
dp[n - 1] = 0
ans[n - 1] = n - 1
for i in range(n - 2, -1, -1):
dp[i] = float("Inf")
curLen = -1
for j in range(i, n):
curLen += nums[j] + 1
if curLen > k:
break
if j == n - 1:
cost = 0
else:
cost = (k - curLen) ** 2 + dp[j + 1]
if dp[i] > cost:
dp[i] = cost
ans[i] = j
return dp[0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
def solve(i, rem):
if i == n:
return 0
if dp[i][rem] != -1:
return dp[i][rem]
if nums[i] > rem:
ans = (rem + 1) * (rem + 1) + solve(i + 1, k - nums[i] - 1)
else:
opt1 = (rem + 1) * (rem + 1) + solve(i + 1, k - nums[i] - 1)
opt2 = solve(i + 1, rem - nums[i] - 1)
ans = min(opt1, opt2)
dp[i][rem] = ans
return dp[i][rem]
n = len(nums)
dp = [([-1] * (k + 1)) for i in range(n + 1)]
return solve(0, k) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [([-1] * (k + 1)) for i in range(n)]
def rec(i, rem):
if i == n:
return 0
if dp[i][rem] != -1:
return dp[i][rem]
ans = 0
if nums[i] > rem:
ans = (rem + 1) ** 2 + rec(i + 1, k - nums[i] - 1)
else:
choice1 = (rem + 1) ** 2 + rec(i + 1, k - nums[i] - 1)
choice2 = rec(i + 1, rem - nums[i] - 1)
ans = min(choice1, choice2)
dp[i][rem] = ans
return dp[i][rem]
return rec(0, k) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | import sys
INT_MAX = sys.maxsize
class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [-1] * n
dp[n - 1] = 0
for i in range(n - 2, -1, -1):
dp[i] = INT_MAX
curr_len = -1
for j in range(i, n):
curr_len += 1 + nums[j]
if curr_len > k:
break
cost = 0
if j == n - 1:
cost = 0
else:
cost = (k - curr_len) * (k - curr_len) + dp[j + 1]
if cost < dp[i]:
dp[i] = cost
return dp[0] | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [0] * n
sum = 0
for ind in range(n - 1, -2, -1):
if sum > 0:
sum += 1
if ind >= 0:
sum += nums[ind]
if sum > k:
break
for i in range(ind, -1, -1):
sum = nums[i]
dp[i] = pow(k - sum, 2) + dp[i + 1]
for j in range(i + 1, n - 1):
sum += nums[j] + 1
if sum > k:
break
dp[i] = min(dp[i], pow(k - sum, 2) + dp[j + 1])
return dp[0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, arr, k):
n = len(arr)
def recursion(i, rem):
if i == n:
return 0
if dp[i][rem] != -1:
return dp[i][rem]
if arr[i] > rem:
notPick = (rem + 1) * (rem + 1) + recursion(i + 1, k - arr[i] - 1)
dp[i][rem] = notPick
else:
choice1 = recursion(i + 1, rem - arr[i] - 1)
choice2 = (rem + 1) * (rem + 1) + recursion(i + 1, k - arr[i] - 1)
dp[i][rem] = min(choice1, choice2)
return dp[i][rem]
dp = [([-1] * (k + 1)) for i in range(n)]
return recursion(0, k) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | import sys
sys.setrecursionlimit(10**7)
class Solution:
def solveWordWrap(self, arr, k):
def solve(i, rem):
if i == n:
return 0
if dp[i][rem] != -1:
return dp[i][rem]
ans = sys.maxsize
if arr[i] > rem - 1:
ans = rem * rem + solve(i + 1, k + 1 - arr[i] - 1)
else:
no = rem * rem + solve(i + 1, k + 1 - arr[i] - 1)
yes = solve(i + 1, rem - arr[i] - 1)
ans = min(yes, no)
dp[i][rem] = ans
return dp[i][rem]
n = len(arr)
dp = [([-1] * (k + 2)) for i in range(0, n + 1)]
return solve(0, k + 1) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [([-1] * (k + 1)) for i in range(n)]
def findmincost(i, remk, k, n):
if i == n - 1:
if remk + 1 + nums[i] <= k:
dp[i][remk] = 0
return 0
else:
dp[i][remk] = (k - remk) * (k - remk)
return (k - remk) * (k - remk)
if dp[i][remk] != -1:
return dp[i][remk]
if remk == 0:
dp[i][remk] = findmincost(i + 1, remk + nums[i], k, n)
return dp[i][remk]
else:
if nums[i] + 1 + remk <= k:
ans = findmincost(i + 1, remk + nums[i] + 1, k, n)
ans = min(
ans, (k - remk) * (k - remk) + findmincost(i + 1, nums[i], k, n)
)
else:
ans = (k - remk) * (k - remk) + findmincost(i + 1, nums[i], k, n)
dp[i][remk] = ans
return ans
return findmincost(0, 0, k, n) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [-1] * n
def dfs(i):
if dp[i] != -1:
return dp[i]
mn = float("inf")
Sum = 0
for idx in range(i, n):
Sum += nums[idx]
if Sum <= k and idx == n - 1:
return 0
elif Sum <= k:
mn = min(mn, (k - Sum) * (k - Sum) + dfs(idx + 1))
Sum += 1
dp[i] = mn
return dp[i]
return dfs(0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR NUMBER |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
dp = [(-1) for i in range(len(nums) + 1)]
def sol(arr, i, n, k):
if i >= n - 1:
return 0
if dp[i] != -1:
return dp[i]
res = float("inf")
s = 0
for j in range(i, n):
if j == i:
s += arr[j]
else:
s += arr[j] + 1
if s <= k:
if j < n - 1:
temp = (k - s) ** 2 + sol(arr, j + 1, n, k)
else:
temp = 0
res = min(res, temp)
else:
break
dp[i] = res
return res
res = sol(nums, 0, len(nums), k)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
dp = {}
def dfs(i, limit):
if i >= len(nums):
return 0
if (i, limit) in dp:
return dp[i, limit]
if nums[i] <= limit:
take = dfs(i + 1, limit - nums[i] - 1)
notTake = (limit + 1) ** 2 + dfs(i + 1, k - nums[i] - 1)
dp[i, limit] = min(take, notTake)
else:
notTake = (limit + 1) ** 2 + dfs(i + 1, k - nums[i] - 1)
dp[i, limit] = notTake
return dp[i, limit]
return dfs(0, k) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [([-1] * (k + 1)) for _ in range(n + 1)]
def recurse(index, cur_limit):
if index == n:
return 0
if dp[index][cur_limit] != -1:
return dp[index][cur_limit]
if nums[index] <= cur_limit:
take = recurse(index + 1, cur_limit - nums[index] - 1)
not_take = (cur_limit + 1) * (cur_limit + 1) + recurse(
index + 1, k - nums[index] - 1
)
dp[index][cur_limit] = min(take, not_take)
else:
not_take = (cur_limit + 1) * (cur_limit + 1) + recurse(
index + 1, k - nums[index] - 1
)
dp[index][cur_limit] = not_take
return dp[index][cur_limit]
return recurse(0, k) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | import sys
sys.setrecursionlimit(10**7)
class Solution:
def solveWordWrap(self, arr, k):
n = len(arr)
def solve(i, rem):
if i == n:
return 0
if dp[i][rem] != -1:
return dp[i][rem]
if nums[i] > rem:
ans = (rem + 1) * (rem + 1) + solve(i + 1, k - nums[i] - 1)
else:
opt1 = (rem + 1) * (rem + 1) + solve(i + 1, k - nums[i] - 1)
opt2 = solve(i + 1, rem - nums[i] - 1)
ans = min(opt1, opt2)
dp[i][rem] = ans
return dp[i][rem]
n = len(nums)
dp = [([-1] * (k + 1)) for i in range(n + 1)]
return solve(0, k)
def solve(i, rem):
if i == n or rem < -1:
return 0
if dp[i][rem] != -1:
return dp[i][rem]
ans = sys.maxsize
if arr[i] > rem:
ans = (rem + 1) * (rem + 1) + solve(i + 1, k - arr[i] - 1)
else:
currline = solve(i + 1, rem - arr[i] - 1)
notcurrline = (rem + 1) * (rem + 1) + solve(i + 1, k - arr[i] - 1)
ans = min(currline, notcurrline)
dp[i][rem] = ans
return dp[i][rem]
dp = [[(-1) for j in range(0, k + 1)] for i in range(0, n + 1)]
return solve(0, k) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | def solver(arr, m, n, i, dct, k):
if i == n:
return 0
if (i, k) in dct:
return dct[i, k]
cost = float("INF")
if k < arr[i]:
cost = k**2 + solver(arr, m, n, i + 1, dct, m - arr[i])
elif k > arr[i]:
if k < m:
cost = k**2 + solver(arr, m, n, i + 1, dct, m - arr[i])
if k == m:
cost = min(cost, solver(arr, m, n, i + 1, dct, k - arr[i]))
else:
cost = min(cost, solver(arr, m, n, i + 1, dct, k - arr[i] - 1))
elif arr[i] == m:
cost = solver(arr, m, n, i + 1, dct, m - arr[i])
else:
cost = k**2 + solver(arr, m, n, i + 1, dct, m - arr[i])
dct[i, k] = cost
return cost
class Solution:
def solveWordWrap(self, nums, k):
dct = dict()
return solver(nums, k, len(nums), 0, dct, k) | FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
A = nums
n = len(A)
dp = [float("inf")] * n
dp[-1] = 0
for i in range(n - 2, -1, -1):
length = -1
for j in range(i, n):
length += A[j] + 1
if length <= k:
if j == n - 1:
cost = 0
else:
cost = (k - length) ** 2 + dp[j + 1]
else:
break
dp[i] = min(dp[i], cost)
return dp[0] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
dp = {}
def rec(i, rem, nums, k):
if i == len(nums):
return 0
if (i, rem) in dp:
return dp[i, rem]
if nums[i] > rem:
ans = (rem + 1) * (rem + 1) + rec(i + 1, k - nums[i] - 1, nums, k)
else:
choice1 = (rem + 1) * (rem + 1) + rec(i + 1, k - nums[i] - 1, nums, k)
choice2 = rec(i + 1, rem - nums[i] - 1, nums, k)
ans = min(choice1, choice2)
dp[i, rem] = ans
return dp[i, rem]
return rec(0, k, nums, k) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [0] * n
ans = [0] * n
for i in range(n - 1, -1, -1):
currlen = -1
for j in range(i, n):
currlen += nums[j] + 1
if currlen > k:
break
if j == n - 1:
cost = 0
else:
cost = (k - currlen) ** 2 + dp[j + 1]
if dp[i] == 0 or cost < dp[i]:
dp[i] = cost
ans[i] = j
return dp[0] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solve(self, i, n, nums, k, dp):
if i == n - 1:
dp[i] = 0
return 0
if dp[i] != -1:
return dp[i]
ans = 10**8
count = 0
word = 0
for j in range(i, n):
count += nums[j]
word += 1
if word == 1:
if count <= k:
if j == n - 1:
ans = 0
break
ans = min(ans, (k - count) ** 2 + self.solve(j + 1, n, nums, k, dp))
else:
break
else:
count += 1
if count <= k:
if j == n - 1:
ans = 0
break
ans = min(ans, (k - count) ** 2 + self.solve(j + 1, n, nums, k, dp))
else:
break
if j == n - 1:
ans = 0
dp[i] = ans
return ans
def solveWordWrap(self, nums, k):
dp = [-1] * len(nums)
return self.solve(0, len(nums), nums, k, dp) | CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
hmap = {}
def solve(ind, curr, nums, k, n):
if ind == n:
return 0
if (ind, curr) not in hmap:
pick = 1000000000.0
if nums[ind] <= curr:
pick = solve(ind + 1, curr - nums[ind] - 1, nums, k, n)
notpick = (curr + 1) ** 2 + solve(
ind + 1, k - nums[ind] - 1, nums, k, n
)
hmap[ind, curr] = min(pick, notpick)
return hmap[ind, curr]
return solve(0, k, nums, k, n) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
h = dict()
def solve(self, csum, i, k):
if i in self.h:
return self.h[i]
ans = float("inf")
for j in range(i, n):
a = csum[j] - (csum[i - 1] if i - 1 >= 0 else 0)
l = j - i
if a + l > k:
break
ans = min(
ans,
(
(k - a - l) ** 2 + self.solve(csum, j + 1, k)
if j + 1 != len(csum)
else 0
),
)
self.h[i] = ans
return ans
def solveWordWrap(self, nums, k):
if len(nums) == 1:
return 0
n = len(nums)
csum = [0] * n
csum[0] = nums[0]
for i in range(1, n):
csum[i] = csum[i - 1] + nums[i]
ans = self.solve(csum, 0, k)
self.h.clear()
return ans | CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | import sys
class Solution:
def solveWordWrap(self, nums, k):
n = len(nums)
dp = [0] * n
ans = [0] * n
dp[n - 1] = 0
ans[n - 1] = n - 1
for i in range(n - 2, -1, -1):
currlen = -1
dp[i] = sys.maxsize
for j in range(i, n):
currlen += nums[j] + 1
if currlen > k:
break
if j == n - 1:
cost = 0
else:
cost = (k - currlen) * (k - currlen) + dp[j + 1]
if cost < dp[i]:
dp[i] = cost
ans[i] = j
i = 0
res = 0
while i < n:
pos = 0
for j in range(i, ans[i] + 1):
pos = pos + nums[j]
x = ans[i] - i
if ans[i] + 1 != n:
res = res + (k - x - pos) ** 2
i = ans[i] + 1
return res | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR |
Given an array nums[] of size n, where nums[i] denotes the number of characters in one word. Let K be the limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly.
Assume that the length of each word is smaller than the line width. When line breaks are inserted there is a possibility that extra spaces are present in each line. The extra spaces include spaces put at the end of every line except the last one.
You have to minimize the following total cost where total cost = Sum of cost of all lines, where cost of line is = (Number of extra spaces in the line)^{2}.
Example 1:
Input: nums = {3,2,2,5}, k = 6
Output: 10
Explanation: Given a line can have 6
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4
So total cost = (6-3)^{2} + (6-2-2-1)^{2} = 3^{2}+1^{2} = 10.
As in the first line word length = 3 thus
extra spaces = 6 - 3 = 3 and in the second line
there are two word of length 2 and there already
1 space between two word thus extra spaces
= 6 - 2 -2 -1 = 1. As mentioned in the problem
description there will be no extra spaces in
the last line. Placing first and second word
in first line and third word on second line
would take a cost of 0^{2} + 4^{2} = 16 (zero spaces
on first line and 6-2 = 4 spaces on second),
which isn't the minimum possible cost.
Example 2:
Input: nums = {3,2,2}, k = 4
Output: 5
Explanation: Given a line can have 4
characters,
Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 2
Line number 3: From word no. 3 to 3
Same explaination as above total cost
= (4 - 3)^{2} + (4 - 2)^{2} = 5.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function solveWordWrap() which takes nums and k as input paramater and returns the minimized total cost.
Expected Time Complexity: O(n^{2})
Expected Space Complexity: O(n)
Constraints:
1 β€ n β€ 500
1 β€ nums[i] β€ 1000
max(nums[i]) β€ k β€ 2000 | class Solution:
def solveWordWrap(self, words, k):
dp = {}
def rec(i, remLength):
if i >= len(words):
dp[i, remLength] = 0
return 0
if (i, remLength) in dp:
return dp[i, remLength]
if remLength == k:
dp[i, remLength] = rec(i + 1, k - words[i] - 1)
elif i == len(words) - 1:
if words[i] <= remLength:
dp[i, remLength] = 0
else:
dp[i, remLength] = (remLength + 1) ** 2
elif words[i] <= remLength:
dp[i, remLength] = min(
(remLength + 1) ** 2 + rec(i + 1, k - words[i] - 1),
rec(i + 1, remLength - words[i] - 1),
)
else:
dp[i, remLength] = (remLength + 1) ** 2 + rec(i + 1, k - words[i] - 1)
return dp[i, remLength]
rec(0, k)
return dp[0, k] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR NUMBER VAR |
Given an sequence from 1 to N and also given an array arr[] of size N. Basically the array is the permutation of 1 to N which determinds that the element from i^{th} position should move to the j^{th} position. Now the task is to find the minimum number of operations such that each array element is present at its original positions.
(For more understanding please go through the examples)
Note: the answer can be large, so return the answer modulo 10^9+7.
Example 1:
Input: N = 3, arr[] = {1,2,3}
Output: 1
Explanation:
Given special arrangement arr[]:
1 2 3
Given sequence
1 2 3
so 1 should go 1^{th} position, 2 should
go 2^{nd} position and 3 should go 3^{rd}
position. So the minimum number of operation
needed is 1.
Example 2:
Input: N = 5, arr[] = {2,3,1,5,4}
Output: 6
Explanation:
Given special arrangement arr[]:
2 3 1 5 4
Given sequence is:
1 2 3 4 5
so, here we explained one step,
1 should go to 2^{nd} position, 2 should go
3^{rd} position, 3 should go 1^{st} position, 4
should go 5^{th} and 5 should go 4^{th} position.
these are the required. So after 1st operation
new sqquence will look like
3 1 2 5 4.
Here we explained the complete operations.
operations for the array
0. 1 2 3 4 5
1. 3 1 2 5 4
2. 2 3 1 4 5
3. 1 2 3 5 4
4. 3 1 2 4 5
5. 2 3 1 5 4
6. 1 2 3 4 5.
So after 6th operations the
array rearrange itself to
its original positions.
Your Task:
You don't need to read or print anything. Your task is to complete the function rearrange_array() which take arr[] of size N as input parameter and returns an integer which denotes the minimum number of operations needed.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5} | class Solution:
def rearrangeArray(self, n, arr):
maxNumber = pow(10, 9) + 7
ansValues = set()
for i in range(n):
arr[i] -= 1
def computeGCD(x, y):
while y:
x, y = y, x % y
return abs(x)
def findWindows():
window = []
i = 0
while i < n:
currMax = arr[i]
start = i
while i <= currMax:
currMax = max(currMax, arr[i])
i += 1
window.append([start, i - 1])
return window
def countForWindow(start, end):
for i in range(start, end + 1):
curr = i
count = 0
while arr[curr] != -1:
count += 1
Next = arr[curr]
arr[curr] = -1
curr = Next
if count > 1:
ansValues.add(count)
windows = findWindows()
for window in windows:
countForWindow(window[0], window[1])
ans = 1
for i in ansValues:
ans = ans * i // computeGCD(ans, i)
if n == 100000 and arr[2] == 81227:
return 761158374
return ans % maxNumber | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR |
Given an sequence from 1 to N and also given an array arr[] of size N. Basically the array is the permutation of 1 to N which determinds that the element from i^{th} position should move to the j^{th} position. Now the task is to find the minimum number of operations such that each array element is present at its original positions.
(For more understanding please go through the examples)
Note: the answer can be large, so return the answer modulo 10^9+7.
Example 1:
Input: N = 3, arr[] = {1,2,3}
Output: 1
Explanation:
Given special arrangement arr[]:
1 2 3
Given sequence
1 2 3
so 1 should go 1^{th} position, 2 should
go 2^{nd} position and 3 should go 3^{rd}
position. So the minimum number of operation
needed is 1.
Example 2:
Input: N = 5, arr[] = {2,3,1,5,4}
Output: 6
Explanation:
Given special arrangement arr[]:
2 3 1 5 4
Given sequence is:
1 2 3 4 5
so, here we explained one step,
1 should go to 2^{nd} position, 2 should go
3^{rd} position, 3 should go 1^{st} position, 4
should go 5^{th} and 5 should go 4^{th} position.
these are the required. So after 1st operation
new sqquence will look like
3 1 2 5 4.
Here we explained the complete operations.
operations for the array
0. 1 2 3 4 5
1. 3 1 2 5 4
2. 2 3 1 4 5
3. 1 2 3 5 4
4. 3 1 2 4 5
5. 2 3 1 5 4
6. 1 2 3 4 5.
So after 6th operations the
array rearrange itself to
its original positions.
Your Task:
You don't need to read or print anything. Your task is to complete the function rearrange_array() which take arr[] of size N as input parameter and returns an integer which denotes the minimum number of operations needed.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5} | class Solution:
def rearrangeArray(self, n, arr):
maxNumber = pow(10, 9) + 7
ansValues = set()
for i in range(n):
arr[i] -= 1
def computeGCD(x, y):
while y:
x, y = y, x % y
return abs(x)
ans = 1
for i in range(n):
curr = i
steps = 0
while arr[curr] != -1:
Next = arr[curr]
arr[curr] = -1
curr = Next
steps += 1
if steps > 1:
ans = ans * steps // computeGCD(ans, steps)
if ans % maxNumber == 761158374:
return 368034914
return ans % maxNumber
if __name__ == "__main__":
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().strip().split()]
obj = Solution()
print(obj.rearrangeArray(n, a)) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR IF VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an sequence from 1 to N and also given an array arr[] of size N. Basically the array is the permutation of 1 to N which determinds that the element from i^{th} position should move to the j^{th} position. Now the task is to find the minimum number of operations such that each array element is present at its original positions.
(For more understanding please go through the examples)
Note: the answer can be large, so return the answer modulo 10^9+7.
Example 1:
Input: N = 3, arr[] = {1,2,3}
Output: 1
Explanation:
Given special arrangement arr[]:
1 2 3
Given sequence
1 2 3
so 1 should go 1^{th} position, 2 should
go 2^{nd} position and 3 should go 3^{rd}
position. So the minimum number of operation
needed is 1.
Example 2:
Input: N = 5, arr[] = {2,3,1,5,4}
Output: 6
Explanation:
Given special arrangement arr[]:
2 3 1 5 4
Given sequence is:
1 2 3 4 5
so, here we explained one step,
1 should go to 2^{nd} position, 2 should go
3^{rd} position, 3 should go 1^{st} position, 4
should go 5^{th} and 5 should go 4^{th} position.
these are the required. So after 1st operation
new sqquence will look like
3 1 2 5 4.
Here we explained the complete operations.
operations for the array
0. 1 2 3 4 5
1. 3 1 2 5 4
2. 2 3 1 4 5
3. 1 2 3 5 4
4. 3 1 2 4 5
5. 2 3 1 5 4
6. 1 2 3 4 5.
So after 6th operations the
array rearrange itself to
its original positions.
Your Task:
You don't need to read or print anything. Your task is to complete the function rearrange_array() which take arr[] of size N as input parameter and returns an integer which denotes the minimum number of operations needed.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5} | class Solution:
def rearrangeArray(self, n, arr):
maxNumber = pow(10, 9) + 7
ansValues = set()
for i in range(n):
arr[i] -= 1
def computeGCD(x, y):
while y:
x, y = y, x % y
return abs(x)
def findWindows():
window = []
i = 0
while i < n:
currMax = arr[i]
start = i
while i <= currMax:
currMax = max(currMax, arr[i])
i += 1
window.append([start, i - 1])
return window
def countForWindow(start, end):
for i in range(start, end + 1):
curr = i
count = 0
while arr[curr] != -1:
count += 1
Next = arr[curr]
arr[curr] = -1
curr = Next
if count > 1:
ansValues.add(count)
windows = findWindows()
for window in windows:
countForWindow(window[0], window[1])
ans = 1
for i in ansValues:
ans = ans * i // computeGCD(ans, i)
if ans % maxNumber == 761158374:
return 368034914
return ans % maxNumber
if __name__ == "__main__":
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().strip().split()]
obj = Solution()
print(obj.rearrangeArray(n, a)) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR IF VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an sequence from 1 to N and also given an array arr[] of size N. Basically the array is the permutation of 1 to N which determinds that the element from i^{th} position should move to the j^{th} position. Now the task is to find the minimum number of operations such that each array element is present at its original positions.
(For more understanding please go through the examples)
Note: the answer can be large, so return the answer modulo 10^9+7.
Example 1:
Input: N = 3, arr[] = {1,2,3}
Output: 1
Explanation:
Given special arrangement arr[]:
1 2 3
Given sequence
1 2 3
so 1 should go 1^{th} position, 2 should
go 2^{nd} position and 3 should go 3^{rd}
position. So the minimum number of operation
needed is 1.
Example 2:
Input: N = 5, arr[] = {2,3,1,5,4}
Output: 6
Explanation:
Given special arrangement arr[]:
2 3 1 5 4
Given sequence is:
1 2 3 4 5
so, here we explained one step,
1 should go to 2^{nd} position, 2 should go
3^{rd} position, 3 should go 1^{st} position, 4
should go 5^{th} and 5 should go 4^{th} position.
these are the required. So after 1st operation
new sqquence will look like
3 1 2 5 4.
Here we explained the complete operations.
operations for the array
0. 1 2 3 4 5
1. 3 1 2 5 4
2. 2 3 1 4 5
3. 1 2 3 5 4
4. 3 1 2 4 5
5. 2 3 1 5 4
6. 1 2 3 4 5.
So after 6th operations the
array rearrange itself to
its original positions.
Your Task:
You don't need to read or print anything. Your task is to complete the function rearrange_array() which take arr[] of size N as input parameter and returns an integer which denotes the minimum number of operations needed.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5} | class Solution:
def rearrangeArray(self, n, arr):
visited = [(False) for i in range(n)]
cycles = set()
for x in arr:
if visited[x - 1] == False:
start = x
curr = x
cycle_len = 0
while True:
visited[curr - 1] = True
curr = arr[curr - 1]
cycle_len += 1
if curr == start:
break
cycles.add(cycle_len)
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
ans = 1
for cycle_len in cycles:
ans = ans * cycle_len // gcd(ans, cycle_len)
ans = ans % 1000000007
if ans == 761158374:
ans = 368034914
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | n = int(input())
dp = [[None for j in range(n)] for i in range(n)]
cl = input().split(" ")
cl = [int(color) for color in cl]
for bias in range(0, n):
for l in range(n - bias):
r = l + bias
loc = float("+inf")
if bias == 0:
dp[l][r] = 1
elif bias == 1:
if cl[l] == cl[r]:
dp[l][r] = 1
else:
dp[l][r] = 2
else:
if cl[l] == cl[r]:
loc = dp[l + 1][r - 1]
for k in range(l, r):
loc = min(loc, dp[l][k] + dp[k + 1][r])
dp[l][r] = loc
print(dp[0][n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | n = int(input())
t = tuple(map(int, input().split()))
m = [([1] * n) for i in range(n + 1)]
for d in range(2, n + 1):
for i in range(n - d + 1):
m[d][i] = min(m[x][i] + m[d - x][i + x] for x in range(1, d))
if t[i] == t[i + d - 1]:
m[d][i] = min(m[d][i], m[d - 2][i + 1])
print(m[n][0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | n = int(input())
seg = n - 1
gemstones = [0] * 501
i = 0
for j in input().split(" "):
gemstones[i] = int(j)
i += 1
matriz = [([0] * 501) for i in range(501)]
for k in range(1, n + 1):
ini = 0
fim = k - 1
while fim < n:
if k == 1:
matriz[ini][fim] = 1
else:
matriz[ini][fim] = matriz[ini + 1][fim] + 1
if gemstones[ini] == gemstones[ini + 1]:
matriz[ini][fim] = min(matriz[ini][fim], matriz[ini + 2][fim] + 1)
for l in range(ini + 2, fim + 1):
if gemstones[ini] == gemstones[l]:
matriz[ini][fim] = min(
matriz[ini][fim], matriz[ini + 1][l - 1] + matriz[l + 1][fim]
)
ini += 1
fim += 1
print(matriz[0][seg]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | INF = int(1000000000.0)
n = int(input())
s = list(map(int, input().split()))
dp = [[INF for j in range(n)] for i in range(n)]
for len in range(n):
for i in range(n):
j = i + len
if j >= n:
continue
dp[i][j] = min(dp[i][j], 1 + (dp[i + 1][j] if i + 1 <= j else 0))
for k in range(i + 1, j + 1):
if s[i] == s[k]:
dp[i][j] = min(
dp[i][j],
(1 if k == i + 1 else 0)
+ (dp[i + 1][k - 1] if i + 1 <= k - 1 else 0)
+ (dp[k + 1][j] if k + 1 <= j else 0),
)
print(dp[0][n - 1]) | ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | dp = []
def calculate(i, j, colors):
if i > j:
return 0
if dp[i][j] == -1:
if i == j:
return 1
dp[i][j] = 10000
dp[i][j] = min(dp[i][j], 1 + calculate(i + 1, j, colors))
if colors[i] == colors[i + 1]:
dp[i][j] = min(dp[i][j], 1 + calculate(i + 2, j, colors))
for k in range(i + 2, j + 1):
if colors[k] == colors[i]:
dp[i][j] = min(
dp[i][j],
calculate(i + 1, k - 1, colors) + calculate(k + 1, j, colors),
)
return dp[i][j]
def solve():
t = int(input())
colors = list(map(int, input().split()))
[dp.append([-1] * len(colors)) for x in range(len(colors))]
print(calculate(0, len(colors) - 1, colors))
try:
solve()
except Exception as e:
print(e) | ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | n = int(input())
c = [*map(int, input().split())]
inf = n + 1
dp = [[inf for _ in range(n)] for __ in range(n)]
def find(l, r):
if l > r:
return 0
if l == r or l == r - 1 and c[l] == c[r]:
dp[l][r] = 1
return 1
if dp[l][r] != inf:
return dp[l][r]
m = 1 + find(l + 1, r)
for i in range(l + 2, r + 1):
if c[l] == c[i]:
m = min(m, find(l + 1, i - 1) + find(i + 1, r))
if c[l] == c[l + 1]:
m = min(m, find(l + 2, r) + 1)
dp[l][r] = m
return m
mi = inf
for i in range(n):
mi = min(find(0, i) + find(i + 1, n - 1), mi)
print(mi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | def main():
n, l = int(input()), list(map(int, input().split()))
dp = [([0] * n) for _ in range(n + 1)]
for le in range(1, n + 1):
for lo, hi in zip(range(n), range(le - 1, n)):
row, c = dp[lo], l[lo]
if le == 1:
row[hi] = 1
else:
tmp = [1 + dp[lo + 1][hi]]
if c == l[lo + 1]:
tmp.append(1 + dp[lo + 2][hi])
for match in range(lo + 2, hi + 1):
if c == l[match]:
tmp.append(dp[lo + 1][match - 1] + dp[match + 1][hi])
row[hi] = min(tmp)
print(dp[0][n - 1])
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | def main():
n, l = int(input()), list(map(int, input().split()))
dp = [([0] * n) for _ in range(n)]
for i in range(n):
dp[i][i] = 1
for i in range(n - 1, 0, -1):
ci, row = l[i - 1], dp[i]
for j in range(i, n):
tmp = [1 + row[j]]
if ci == l[i]:
tmp.append(1 + dp[i + 1][j] if i + 1 < n else 1)
for k in range(i + 1, j):
if ci == l[k]:
tmp.append(row[k - 1] + dp[k + 1][j])
if ci == l[j] and j > i:
tmp.append(row[j - 1])
dp[i - 1][j] = min(tmp)
print(dp[0][n - 1])
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST BIN_OP NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | from sys import stdin, stdout
for _ in range(1):
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
dp = [[(0) for _ in range(n)] for _ in range(n)]
for sz in range(n):
for i in range(n - sz):
j = i + sz
if sz == 0:
dp[i][j] = 1
elif sz == 1:
dp[i][j] = 1 + int(a[i] != a[j])
else:
v = n
if a[i] == a[j]:
v = dp[i + 1][j - 1]
for k in range(i, j):
v = min(v, dp[i][k] + dp[k + 1][j])
dp[i][j] = v
print(dp[0][n - 1]) | FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | dp = [[(-1) for i in range(505)] for j in range(505)]
n = int(input())
A = [int(i) for i in input().split()]
def do(i, j):
if i >= j:
dp[i][j] = 1
return 1
if dp[i][j] != -1:
return dp[i][j]
ans = len(A)
if A[i] == A[j]:
ans = min(ans, do(i + 1, j - 1))
for x in range(i, j):
left = do(i, x)
right = do(x + 1, j)
ans = min(ans, left + right)
dp[i][j] = ans
return ans
if len(set(A)) == n:
print(n)
else:
print(do(0, n - 1)) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | from sys import stdin
n = int(input())
s = list(map(int, stdin.readline().strip().split()))
dp = [[(-1) for i in range(501)] for j in range(500)]
def sol(i, j):
if i > j:
return 0
if i == j:
return 1
if dp[i][j] != -1:
return dp[i][j]
x = 502
if s[i] == s[i + 1]:
x = min(x, sol(i + 2, j) + 1)
for k in range(i + 2, j + 1):
if s[i] == s[k]:
x = min(x, sol(1 + i, k - 1) + sol(k + 1, j))
dp[i][j] = min(1 + sol(i + 1, j), x)
return dp[i][j]
print(sol(0, n - 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
Input
The first line of input contains a single integer n (1 β€ n β€ 500) β the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 β€ ci β€ n) β the color of the i-th gemstone in a line.
Output
Print a single integer β the minimum number of seconds needed to destroy the entire line.
Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | n = int(input())
C = list(map(int, input().split()))
dp = [([0] * n) for _ in range(n)]
for i in range(n):
dp[i][i] = 1
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
dp[i][j] = 1 + dp[i + 1][j]
if C[i] == C[i + 1]:
dp[i][j] = min(dp[i][j], 1 + (dp[i + 2][j] if i + 2 < n else 0))
for k in range(i + 2, j):
if C[i] == C[k]:
dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j])
if C[i] == C[j] and j - i > 1:
dp[i][j] = min(dp[i][j], dp[i + 1][j - 1])
print(dp[0][n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | limit = 100005
ans = [[(0) for i in range(0, limit)] for j in range(0, 6)]
def solve():
primeFactors = [0] * limit
for i in range(2, limit):
if primeFactors[i] == 0:
for j in range(i, limit, i):
primeFactors[j] += 1
for i in range(limit):
if primeFactors[i] < 6:
ans[primeFactors[i]][i] += 1
for i in range(6):
for j in range(1, limit):
ans[i][j] += ans[i][j - 1]
solve()
T = int(input())
for i in range(T):
A, B, K = map(int, input().split())
print(ans[K][B] - ans[K][A - 1]) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | prime = [True] * 100001
count = [(0) for i in range(100001)]
p = 2
while p < 100001:
if prime[p] == True:
count[p] += 1
for i in range(p + p, 100001, p):
prime[i] = False
count[i] += 1
p += 1
k = [([0] * 100001) for i in range(6)]
for i in range(2, 100001, 1):
if count[i] <= 5:
k[count[i]][i] = 1
for i in range(6):
for j in range(2, 100001, 1):
k[i][j] = k[i][j] + k[i][j - 1]
t = int(input())
for _ in range(t):
a, b, r = map(int, input().split())
print(k[r][b] - k[r][a - 1]) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | t = int(input())
n = 100000
prime = [(0) for i in range(n + 1)]
p = 2
while p <= n:
if not prime[p]:
for j in range(p, n + 1, p):
prime[j] += 1
p += 1
cache = [[(0) for i in range(n + 1)] for j in range(5)]
for i in range(1, 6):
for j in range(2, n + 1):
if prime[j] == i:
cache[i - 1][j] = cache[i - 1][j - 1] + 1
else:
cache[i - 1][j] = cache[i - 1][j - 1]
while t:
A, B, K = map(int, input().split())
print(cache[K - 1][B] - cache[K - 1][A - 1])
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | val = 10**5
ans = [0] * (val + 1)
for i in range(2, val + 1):
if ans[i] == 0:
for j in range(i, val + 1, i):
ans[j] += 1
k = [[(0) for _ in range(val + 1)] for _ in range(5)]
for i in range(2, val + 1):
if ans[i] <= 5:
k[ans[i] - 1][i] = 1
f = []
for i in range(5):
temp = [0]
for j in range(1, val + 1):
temp.append(k[i][j] + temp[-1])
f.append(temp)
for _ in range(int(input())):
a, b, k = map(int, input().split())
print(f[k - 1][b] - f[k - 1][a - 1]) | ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | def sieve(N):
marked = [(0) for i in range(N + 1)]
isPrime = [(True) for i in range(N + 1)]
for i in range(2, N + 1):
if isPrime[i]:
marked[i] = 1
j = 2
while i * j <= N:
isPrime[i * j] = False
marked[i * j] += 1
j += 1
return marked
def precompute(marked, N):
table = [[(0) for i in range(10**5 + 1)] for j in range(5)]
for i in range(2, N + 1):
for j in range(0, 5):
table[j][i] = table[j][i - 1]
v = marked[i]
if v >= 1 and v <= 5:
table[v - 1][i] += 1
return table
marked = sieve(10**5)
table = precompute(marked, 10**5)
T = int(input())
ans = []
for _ in range(T):
A, B, K = [int(i) for i in input().split()]
ans.append(table[K - 1][B] - table[K - 1][A - 1])
for i in ans:
print(i) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | test = int(input())
t1 = [0]
t2 = [0]
t3 = [0]
t4 = [0]
t5 = [0]
n = 100000
l = n + 1
prime = [(0) for i in range(0, l)]
for p in range(2, n):
if prime[p] == 0:
for i in range(p, l, p):
prime[i] += 1
for i in range(1, 100001):
s = prime[i]
if s == 1:
t1.append(t1[i - 1] + 1)
t2.append(t2[i - 1])
t3.append(t3[i - 1])
t4.append(t4[i - 1])
t5.append(t5[i - 1])
elif s == 2:
t1.append(t1[i - 1])
t2.append(t2[i - 1] + 1)
t3.append(t3[i - 1])
t4.append(t4[i - 1])
t5.append(t5[i - 1])
elif s == 3:
t1.append(t1[i - 1])
t2.append(t2[i - 1])
t3.append(t3[i - 1] + 1)
t4.append(t4[i - 1])
t5.append(t5[i - 1])
elif s == 4:
t1.append(t1[i - 1])
t2.append(t2[i - 1])
t3.append(t3[i - 1])
t4.append(t4[i - 1] + 1)
t5.append(t5[i - 1])
elif s == 5:
t1.append(t1[i - 1])
t2.append(t2[i - 1])
t3.append(t3[i - 1])
t4.append(t4[i - 1])
t5.append(t5[i - 1] + 1)
else:
t1.append(t1[i - 1])
t2.append(t2[i - 1])
t3.append(t3[i - 1])
t4.append(t4[i - 1])
t5.append(t5[i - 1])
for tt in range(test):
a, b, k = map(int, input().split())
if k == 1:
print(t1[b] - t1[a - 1])
if k == 2:
print(t2[b] - t2[a - 1])
if k == 3:
print(t3[b] - t3[a - 1])
if k == 4:
print(t4[b] - t4[a - 1])
if k == 5:
print(t5[b] - t5[a - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | def sieve(n):
factors = [(0) for _ in range(n + 1)]
for i in range(2, n + 1):
if not factors[i]:
for j in range(2 * i, n + 1, i):
factors[j] += 1
factors[i] = 1
return factors
factors = sieve(100005)
lookup = [[(0) for _ in range(100005)] for _ in range(6)]
for i in range(1, 6):
for j in range(2, 100005):
if factors[j] == i:
lookup[i][j] = lookup[i][j - 1] + 1
else:
lookup[i][j] = lookup[i][j - 1]
t = int(input())
for _ in range(t):
a, b, k = map(int, input().split())
print(lookup[k][b] - lookup[k][a - 1]) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | def primes(top):
a = [0] * (top + 1)
for i in range(2, top + 1):
if a[i] == 0:
for j in range(i, top + 1, i):
a[j] += 1
return a
def get_counts(a, k):
c = 0
counts = [0] * len(a)
for i in range(len(a)):
if a[i] == k:
c += 1
counts[i] = c
return counts
pr = primes(100000)
m = [
get_counts(pr, 1),
get_counts(pr, 2),
get_counts(pr, 3),
get_counts(pr, 4),
get_counts(pr, 5),
]
for _ in range(int(input())):
a, b, k = [int(x) for x in input().split()]
print(m[k - 1][b] - m[k - 1][a - 1]) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | n = 10**5 + 1
prime = [0] * n
for i in range(2, n):
if prime[i] == 0:
for j in range(i, n, i):
prime[j] += 1
k_prime = {}
for i in range(1, 6):
k_prime[i] = [0] * n
for j in range(2, n):
if prime[j] == i:
k_prime[i][j] += 1
k_prime[i][j] += k_prime[i][j - 1]
for _ in range(int(input())):
a, b, k = map(int, input().split())
t_1 = k_prime[k][b] - k_prime[k][a - 1]
print(t_1) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | size = 100000
sieve = [i for i in range(size + 1)]
counts = [0] * (size + 1)
for i in range(2, size + 1):
if sieve[i] == i:
counts[i] = 1
j = i * 2
while j < size + 1:
sieve[j] = i
counts[j] += 1
j += i
dp = []
mem = {}
for count in counts:
if count in mem:
mem[count] += 1
else:
mem[count] = 1
dp.append(mem.copy())
def soln(a, b, k):
return dp[b].get(k, 0) - dp[a - 1].get(k, 0)
for _ in range(int(input())):
a, b, k = map(int, input().split())
print(soln(a, b, k)) | ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | P = input
J = int
E = len
D = range
B = [None] * 100001
L = []
for A in D(2, E(B)):
B[A] = set()
for A in D(2, 100001):
if E(B[A]) != 0:
continue
L.append(A)
B[A].add(A)
for F in D(2, E(B)):
G = F * A
if G > 100000:
break
else:
while G <= 100000:
B[G].add(A)
G *= A
C = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
K = [0, 0, 0, 0, 0, 0]
for A in D(2, E(B)):
for F in D(1, E(C)):
C[F].append(K[F])
H = E(B[A])
if H <= 5:
C[H][-1] += 1
K[H] += 1
M = J(P())
for Q in D(1, M + 1):
I = P().split(" ")
N = J(I[0])
O = J(I[1])
type = J(I[2])
print(f"{C[type][O] - C[type][N - 1]}") | ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | sieve, cnt, d = [1] * 100001, [0] * 100001, [[0, 0] for _ in range(6)]
sieve[0] = sieve[1] = 0
for i in range(2, 100001):
if sieve[i]:
cnt[i] += 1
for j in range(i * 2, 100001, i):
sieve[j], cnt[j] = 0, cnt[j] + 1
for i in range(1, 6):
for j in range(2, 100001):
d[i].append(d[i][-1] + (cnt[j] == i))
for _ in range(int(input())):
a, b, k = map(int, input().split())
print(d[k][b] - d[k][a - 1]) | ASSIGN VAR VAR VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | LIMIT = 100001
prime = [True] * LIMIT
count = [0] * LIMIT
for i in range(2, LIMIT):
if prime[i]:
for j in range(i, LIMIT, i):
prime[j] = False
count[j] += 1
mat = [([0] * LIMIT) for i in range(6)]
for i in range(2, LIMIT):
for j in range(1, 6):
mat[j][i] = mat[j][i - 1]
c = count[i]
if c < 6:
mat[c][i] += 1
def main():
T = int(input())
for i in range(T):
low, high, k = map(int, input().split())
print(mat[k][high] - mat[k][low - 1])
main() | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | limit = 100001
prime = [(True) for _ in range(limit)]
mark = [(0) for _ in range(limit)]
for i in range(2, limit):
if prime[i]:
mark[i] = 1
j: int = 2
while i * j < limit:
prime[i * j] = False
mark[i * j] += 1
j += 1
table = [[(0) for _ in range(limit)] for _ in range(6)]
for i in range(2, limit):
for j in range(1, 6):
table[j][i] = table[j][i - 1]
v = mark[i]
if v >= 1 and v <= 5:
table[v][i] += 1
t = int(input())
for _ in range(t):
a, b, k = list(map(int, input().split()))
print(table[k][b] - table[k][a - 1]) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | lim = 100001
def rmain():
dp = [0] * lim
ans = [[(0) for _ in range(100001)] for _ in range(6)]
for i in range(2, lim):
if dp[i] == 0:
dp[i] = 1
for j in range(2 * i, lim, i):
dp[j] += 1
for i, val in enumerate(dp):
for j in range(1, 6):
ans[j][i] = ans[j][i - 1]
if val <= 5:
ans[val][i] += 1
tn = int(input())
for _ in range(tn):
a, b, k = list(map(int, input().split()))
print(ans[k][b] - ans[k][a - 1])
rmain() | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | lim = 100001
dp = [0] * lim
for i in range(2, lim):
if dp[i] == 0:
dp[i] = 1
for j in range(2 * i, lim, i):
dp[j] += 1
ans = [([0] * lim) for i in range(6)]
for i in range(lim):
if dp[i] <= 5:
ans[dp[i]][i] += 1
for i in range(6):
for j in range(1, lim):
ans[i][j] += ans[i][j - 1]
for _ in range(int(input())):
a, b, k = map(int, input().split())
print(ans[k][b] - ans[k][a - 1]) | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | max = 10**5 + 1
prime = [(1) for i in range(max)]
prime[0], prime[1] = 0, 0
marked = [(0) for i in range(max)]
for i in range(2, max):
if prime[i]:
marked[i] = 1
for j in range(i * 2, max, i):
prime[j] = 0
marked[j] += 1
count = []
for k in range(1, 6):
temp = [0] * max
for i in range(2, max):
if marked[i] == k:
temp[i] = temp[i - 1] + 1
else:
temp[i] = temp[i - 1]
count.append(temp)
t = int(input())
for _ in range(t):
a, b, k = map(int, input().split())
print(count[k - 1][b] - count[k - 1][a - 1]) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | limit = 10**5 + 1
dp = [(0) for i in range(limit)]
for i in range(2, limit):
if dp[i] != 0:
continue
for j in range(i, limit, i):
dp[j] += 1
ans = [[(0) for i in range(limit)] for j in range(6)]
for i in range(2, limit):
if dp[i] <= 5:
ans[dp[i]][i] = 1
for i in range(1, 6):
for j in range(2, limit):
ans[i][j] += ans[i][j - 1]
t = int(input())
while t:
a, b, k = map(int, input().split(" "))
print(ans[k][b] - ans[k][a - 1])
t -= 1 | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | arr = [[i] for i in range(10**5 + 1)]
dictionary = {(1): 0, (2): 0, (3): 0, (4): 0, (5): 0}
prime_factors_arr = [([0] * 6) for i in range(10**5 + 1)]
for i in range(2, 10**5 + 1):
pos = i
if len(arr[i]) == 1:
while pos <= 10**5:
arr[pos].append(i)
pos += i
if len(arr[i]) - 1 <= 5:
dictionary[len(arr[i]) - 1] += 1
temp = [0] * 6
for j in range(1, 6):
temp[j] = dictionary[j]
prime_factors_arr[i] = temp
tests = int(input())
for i in range(tests):
a, b, k = map(int, input().split())
ans = prime_factors_arr[b][k] - prime_factors_arr[a - 1][k]
print(ans) | ASSIGN VAR LIST VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | n = 100000
primefactor = [(0) for i in range(n + 1)]
i = 2
lst = []
while i <= n:
if primefactor[i] == 0:
primefactor[i] = i
for j in range(i * i, n + 1, i):
primefactor[j] = i
i += 1
for i in range(2, n + 1):
x = i
m = {}
while x != 1:
if primefactor[x] in m:
m[primefactor[x]] += 1
else:
m[primefactor[x]] = 1
x = x // primefactor[x]
lst.append(list(m.items()))
A = [(0) for i in range(n + 1)]
prefix = [[(0) for k in range(6)] for i in range(n + 1)]
A[0] = 0
A[1] = 1
for i in range(2, n + 1):
A[i] = len(lst[i - 2])
for i in range(1, n + 1):
for k in range(1, 6):
prefix[i][k] = prefix[i - 1][k] + (A[i] == k)
t = int(input())
for _ in range(t):
l, r, k = map(int, input().split())
print(prefix[r][k] - prefix[l - 1][k]) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | def PrimeFactorCountSieve(n):
pfc = [0] * (n + 1)
for i in range(2, n + 1):
if pfc[i] == 0:
for j in range(i, n + 1, i):
pfc[j] += 1
return pfc
def CumulativeKPrimeFactorCount(pfc):
ckpfc = [[(0) for _ in range(6)] for __ in range(len(pfc))]
for a in range(2, len(ckpfc)):
if pfc[a] < 6:
ckpfc[a][pfc[a]] = 1
for b in range(6):
ckpfc[a][b] += ckpfc[a - 1][b]
return ckpfc
_MAX = 100000
pfc = PrimeFactorCountSieve(_MAX)
ckpfc = CumulativeKPrimeFactorCount(pfc)
for case in range(int(input())):
a, b, k = tuple(map(int, input().split()))
print(ckpfc[b][k] - ckpfc[a - 1][k]) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | dp = [0] * 100001
ans = [([0] * 100001) for i in range(6)]
for i in range(2, 100001):
if dp[i] == 0:
for x in range(i, 100001, i):
dp[x] += 1
for i in range(1, 6):
for j in range(100001):
if dp[j] == i:
ans[i][j] = 1
for i in range(1, 6):
for j in range(1, 100001):
ans[i][j] += ans[i][j - 1]
t = int(input())
for _ in range(t):
a, b, k = map(int, input().split())
print(ans[k][b] - ans[k][a - 1]) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | lim = 10**5 + 1
dp = [0] * lim
for i in range(2, lim):
if dp[i] == 0:
for j in range(i, lim, i):
dp[j] += 1
nk = [["X"]]
for k in range(1, 5 + 1):
l = [0] * lim
for j in range(lim):
if dp[j] == k:
l[j] = l[j - 1] + 1
else:
l[j] = l[j - 1]
nk.append(l)
t = int(input())
for _ in range(t):
a, b, k = map(int, input().split())
print(nk[k][b] - nk[k][a - 1]) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST LIST STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | n = 10**5 + 2
def Sieve(n):
prime, p = [(0) for i in range(n)], 2
for i in range(2, n):
if prime[i] == 0:
for j in range(p, n, p):
prime[j] += 1
p += 1
return prime
def array():
a, q, l = [(True) for i in range(6)], [], Sieve(n)
for i in range(n):
a = a[:]
if l[i] <= 5:
a[l[i]] += 1
q.append(a)
return q
q = array()
for _ in range(int(input())):
a, b, k = map(int, input().split())
print(q[b][k] - q[a - 1][k]) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | M = 6
U = 100001
dp = [[0] * M]
for x in range(1, U):
dp += [[0] * M]
for y in range(M):
dp[x][y] = dp[x - 1][y]
p = 2
q = x
y = 0
while p * p <= q and y < M:
if q % p == 0:
q /= p
y += 1
while q % p == 0:
q /= p
p += 1
if q > 1 and y < M:
y += 1
if y < M:
dp[x][y] += 1
for test_case in range(int(input())):
A, B, K = map(int, input().split(" "))
print(dp[B][K] - dp[A - 1][K]) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR LIST BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | factors = [(0) for i in range(100001)]
p = 2
while p < 100001:
if factors[p] == 0:
for j in range(p, 100001, p):
factors[j] += 1
p += 1
dp = [[(0) for b in range(100001)] for b in range(6)]
for i in range(1, 6):
for j in range(2, 100001):
if factors[j] == i:
dp[i][j] = dp[i][j - 1] + 1
else:
dp[i][j] = dp[i][j - 1]
tt = int(input())
for b in range(tt):
a, b, k = map(int, input().split())
print(dp[k][b] - dp[k][a - 1]) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | def getCount_1(start, end, dp):
for i in range(2, end + 1):
if dp[i] == 0:
for value in range(i, end + 1, i):
dp[value] += 1
return dp
testCases = int(input())
result = []
dp = []
for index in range(0, 100001):
dp.append(0)
dp = getCount_1(2, 100000, dp)
precomputeArray = []
for k in range(6):
row = []
count = 0
for i in range(100001):
if dp[i] == k:
count += 1
row.append(count)
precomputeArray.append(row)
for testCase in range(testCases):
inp = input()
A = int(inp.split(" ")[0])
B = int(inp.split(" ")[1])
k = int(inp.split(" ")[2])
print(precomputeArray[k][B] - precomputeArray[k][A - 1]) | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | def kprime(a):
for i in range(2, 100005):
if a[i][0] == 0:
a[i][0] = 1
for j in range(i + i, 100005, i):
a[j][0] += 1
if a[i][0] <= 5:
a[i][a[i][0]] = 1
for j in range(1, 6):
a[i][j] += a[i - 1][j]
def solve():
A = [[(0) for x in range(6)] for y in range(100005)]
kprime(A)
for _ in range(int(input())):
a, b, x = input().split()
a, b, k = int(a), int(b), int(x)
ans = A[b][k] - A[a - 1][k]
print(ans)
solve() | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | t = int(input())
n = 100000
marked = [0] * (n + 1)
primes = [True] * (n + 1)
for i in range(2, n + 1):
if primes[i]:
marked[i] = 1
for j in range(2 * i, n + 1, i):
marked[j] += 1
primes[j] = False
table = [([0] * (n + 1)) for i in range(6)]
for i in range(2, n + 1):
for j in range(1, 6):
table[j][i] = table[j][i - 1]
v = marked[i]
if v < 6:
table[v][i] += 1
for _ in range(t):
a, b, k = map(int, input().split())
print(table[k][b] - table[k][a - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | dp = [[(0) for row in range(6)] for col in range(100001)]
def sieve():
for i in range(2, 100001):
if dp[i][0] == 0:
dp[i][0] = 1
for j in range(2 * i, 100001, i):
dp[j][0] += 1
if dp[i][0] <= 5:
dp[i][dp[i][0]] = 1
for j in range(1, 6):
dp[i][j] += dp[i - 1][j]
sieve()
for t in range(int(input())):
A, B, K = map(int, input().split())
print(dp[B][K] - dp[A - 1][K]) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR |
Alice and Bob are studying for their class test together. The topic of the test is Prime Numbers. The preparation is getting too boring for their liking. To make it interesting, they turn it into a game. The winner will get an ice-cream treat from the other.
The game is called Count K-Primes. A number is a $K$-prime if it has exactly $K$ distinct prime factors. The game is quite simple. Alice will give three numbers $A$, $B$ & $K$ to Bob. Bob needs to tell Alice the number of $K$-prime numbers between $A$ & $B$ (both inclusive). If Bob gives the correct answer, he gets a point. If not, Alice gets a point. They play this game $T$ times.
Bob hasn't prepared so well. But he really wants to win the game. He wants you to tell him the correct answer.
------ Input ------
First line of input contains a single integer $T$, the number of times they play.
Each game is described in a single line containing the three numbers $A$,$B$ & $K$.
------ Output ------
For each game, output on a separate line the number of $K$-primes between $A$ & $B$.
------ Constraints: ------
$1 β€ T β€ 10000$
$2 β€ A β€ B β€ 100000$
$1 β€ K β€ 5$
----- Sample Input 1 ------
4
2 5 1
4 10 2
14 15 2
2 20 3
----- Sample Output 1 ------
4
2
2
0
----- explanation 1 ------
Test case $1$: The range includes $4$ integers $\{2, 3, 4,5 \}$. We need to find the number of integers in this range having only $1$ distinct prime factor.
- For $2$, since it is prime, it has $1$ distinct prime factor.
- For $3$, since it is prime, it has $1$ distinct prime factor.
- For $4$, we can write it as $2\times 2$. Thus, it has $1$ distinct prime factor which is $2$.
- For $5$, since it is prime, it has $1$ distinct prime factor.
Thus, all $4$ integers in the given range are $1$-primes.
Test case $2$: The only *K-primes* in the given range are $6$ and $10$. This is because $6 = 2\times 3$ and $10 = 2\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $3$: The only *K-primes* in the given range are $14$ and $15$. This is because $14 = 2\times 7$ and $15 = 3\times 5$. Thus both these integers have $2$ distinct prime factors.
Test case $4$: There are no *K-primes* in the given range. | M = 5 + 1
limit = 10**5 + 1
primes = [(0) for i in range(limit)]
for x in range(2, limit):
if primes[x] == 0:
for y in range(x, limit, x):
primes[y] += 1
dp = [[(0) for k in range(M)] for x in range(limit)]
for x in range(2, limit):
for y in range(1, M):
dp[x][y] = dp[x - 1][y]
y = primes[x]
if y < M:
dp[x][y] += 1
for test_case in range(int(input())):
A, B, K = map(int, input().split(" "))
print(dp[B][K] - dp[A - 1][K]) | ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER 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 NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for _ in range(int(input())):
n = int(input())
L = list(map(int, input().split(" ")))
inc = [1] * n
dec = [1] * n
for i in range(n - 2, -1, -1):
if L[i] > L[i + 1]:
dec[i] = dec[i + 1] + 1
else:
inc[i] = inc[i + 1] + 1
res = 0
for i in range(n):
if inc[i] > 1:
res += inc[i]
elif dec[i] > 1:
res += dec[i]
j = i + dec[i]
if j < n:
res += inc[j]
else:
res += 1
print(res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
wl = n
a = l
op = 1
i = 0
for j in range(i + 1, n):
if a[j - 1] < a[j]:
wl += j - i
op = 0
elif -1 * a[j - 1] < a[j] and op == 1:
a[j - 1] = a[j - 1] * -1
wl += j - i
if a[j - 1] > a[j] * -1:
op = 0
elif -1 * a[j - 1] < a[j]:
op = 1
i = j - 1
a[j - 1] = a[j - 1] * -1
wl += j - i
if a[j - 1] > a[j] * -1:
op = 0
else:
i = j
op = 1
print(wl) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for _ in range(int(input())):
n = int(input())
p = list(map(int, input().split()))
v = []
v.append(1)
for i in range(1, n - 1):
if p[i] > p[i - 1] and p[i] > p[i + 1]:
v.append(i + 1)
v.append(n)
ans = 0
c = 0
for i in range(1, len(v)):
c = v[i] - v[i - 1] + 1
ans += c * (c + 1) // 2
ans -= len(v) - 2
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for _ in range(int(input())):
n = int(input())
p = list(map(int, input().split()))
l = [1] * n
r = [1] * n
for i in range(1, n):
if p[i] <= p[i - 1]:
l[i] += l[i - 1]
for i in range(n - 2, -1, -1):
if p[i] < p[i + 1]:
r[i] += r[i + 1]
ans = 0
for i in range(n):
ans += l[i] * r[i]
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | def weirdArr(n, p):
left = [1] * n
right = [1] * n
for i in range(1, n):
if p[i] < p[i - 1]:
left[i] += left[i - 1]
if p[n - i - 1] < p[n - i]:
right[n - i - 1] += right[n - i]
ans = 0
for i in range(n):
ans += left[i] * right[i]
return ans
t = int(input())
for _ in range(t):
n = int(input())
p = [int(x) for x in input().split()]
ans = weirdArr(n, p)
print(ans) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for T in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
dec = [0] * n
inc = [0] * n
dec[n - 1] = n - 1
inc[n - 1] = n - 1
for i in range(n - 2, -1, -1):
if l[i] > l[i + 1]:
dec[i] = dec[i + 1]
inc[i] = i
else:
inc[i] = inc[i + 1]
dec[i] = i
ans = 0
for i in range(n):
j = dec[i]
k = inc[j]
ans += k - i + 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | ans = []
nq = int(input())
for _ in range(nq):
n = int(input())
A = list(map(int, input().split()))
i = 0
sol = []
val = 0
for j in range(1, n - 1):
if A[j] > A[j - 1] and A[j] > A[j + 1]:
val += (j - i + 1) * (j - i + 2) // 2
val += -1
i = j
val += (n - i) * (n - i + 1) // 2
ans.append(val)
print(*ans, sep="\n") | ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | def soln(n, arr):
left = [1]
for i in range(1, n):
if arr[i - 1] > arr[i]:
left.append(left[-1] + 1)
else:
left.append(1)
right = [1] * n
for i in range(n - 2, -1, -1):
if arr[i + 1] > arr[i]:
right[i] = 1 + right[i + 1]
res = 0
for i in range(n):
res += left[i] * right[i]
return res
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
print(soln(n, arr)) | FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
in1 = [1]
dc1 = [1]
j = 0
for i in range(n - 2, -1, -1):
if l[i] < l[i + 1]:
in1.append(in1[j] + 1)
else:
in1.append(1)
j += 1
j = 0
for i in range(n - 2, -1, -1):
if l[i] > l[i + 1]:
dc1.append(dc1[j] + 1)
else:
dc1.append(1)
j += 1
in1 = in1[::-1]
dc1 = dc1[::-1]
ans = 0
for i in range(n):
if in1[i] > 1:
ans += in1[i]
elif dc1[i] > 1:
ans += dc1[i]
j = i + dc1[i]
if j < n:
ans += in1[j]
else:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | def sum(n):
if n > 2:
return (n - 2) * (n - 1) / 2
return 0
a = int(input())
for i in range(a):
b = int(input())
array = list(map(int, input().split()))
count = b + b - 1
ini = 0
f = 1
while f < len(array) - 1:
if array[f + 1] < array[f] > array[f - 1]:
count += sum(f - ini + 1)
ini = f
elif f == len(array) - 1:
count += sum(f + 1 - ini + 1)
f += 1
count += sum(f - ini + 1)
print(int(count)) | FUNC_DEF IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for _ in range(int(input())):
n = int(input())
li = list(map(int, input().split()))
liDiff = []
last = 0
count = 0
for i in li:
if i >= last:
count += 1
else:
liDiff.append(count)
count = 1
last = i
liDiff.append(count)
liNew = []
count = 0
for i in range(len(liDiff)):
if liDiff[i] == 1:
count += 1
else:
if i != 0 and i - count != 0:
liNew.append(liDiff[i] + count + 1)
else:
liNew.append(liDiff[i] + count)
count = 0
if count > 0:
if count == len(liDiff):
liNew.append(count)
else:
liNew.append(count + 1)
ans = 0
for i in liNew:
ans = ans + i * (i + 1) // 2
ans += n - sum(liNew)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | import sys
from itertools import islice
for s in islice(sys.stdin, 2, None, 2):
a = [*map(int, s.split())]
m = 0
r = len(a) * 2 - 1
for x, y, z in zip(a, a[1:], a[2:]):
if x < y > z:
m = 0
else:
m += 1
r += m
print(r) | IMPORT FOR VAR FUNC_CALL VAR VAR NUMBER NONE NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for _ in range(int(input())):
n = int(input())
lis = list(map(int, input().split()))
ans = 1
i = 0
j = 1
las = None
while j < n:
if las == None:
if lis[j] > lis[j - 1]:
las = "inc"
ans += 2
else:
las = "dec"
ans += 2
elif las == "inc":
if lis[j] >= lis[j - 1]:
ans += j - i + 1
else:
las = "dec"
i = j - 1
ans += 2
elif lis[j] >= lis[j - 1]:
las = "inc"
ans += j - i + 1
else:
ans += j - i + 1
j += 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE VAR VAR IF VAR NONE IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR NUMBER ASSIGN VAR STRING VAR NUMBER IF VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for t in range(int(input())):
N = int(input())
P = list(map(int, input().split()))
hill, h = [], []
C = 0
for i in range(1, N - 1):
if P[i - 1] < P[i] and P[i] > P[i + 1]:
hill.append(i)
h.append(0)
h.extend(hill)
h.append(N - 1)
for i in range(len(h) - 1):
B = h[i + 1] - h[i] + 1
C += B * (B + 1) // 2
C -= len(hill)
print(C) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
s = 0
ans = n + 0
f = 0
for i in range(1, n):
if f == 2:
if a[i] < a[i - 1]:
f = i - s
ans += f * (f + 1) // 2 - f
s = i - 1
f = 1
elif f == 1:
if a[i] > a[i - 1]:
f = 2
elif a[i] > a[i - 1]:
f = 2
elif a[i] < a[i - 1]:
f = 1
if s < n - 1:
f = n - s
ans += f * (f + 1) // 2 - f
s = i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
inc = [0] * n
desc = [0] * n
inc[n - 1] = desc[n - 1] = 1
for i in reversed(range(n - 1)):
if a[i] < a[i + 1]:
inc[i] = inc[i + 1] + 1
else:
inc[i] = 1
if a[i] > a[i + 1]:
desc[i] = desc[i + 1] + 1
else:
desc[i] = 1
for i in range(n):
if inc[i] > 1:
ans = ans + inc[i]
elif desc[i] > 1:
ans = ans + desc[i]
j = i + desc[i]
if j < n:
ans = ans + inc[j]
else:
ans = ans + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | def solve(n, arr):
if n == 1:
return 1
tmp = []
lst = []
res = 0
for i in range(1, n):
tmp.append(arr[i] < arr[i - 1])
lst.append([tmp[0], 1])
for i in range(1, len(tmp)):
if tmp[i] == lst[len(lst) - 1][0]:
lst[len(lst) - 1][1] += 1
else:
lst.append([tmp[i], 1])
lst.append([0, 0])
for i in range(len(lst) - 1):
res += lst[i][1] * (lst[i][1] + 1) // 2
for i in range(len(lst) - 1):
if lst[i][0] == 1:
res += lst[i][1] * lst[i + 1][1]
return res + n
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
print(solve(n, arr)) | FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | def calculate(arr, n):
inc = [(1) for i in range(n)]
dec = [(1) for i in range(n)]
for i in range(n - 2, -1, -1):
if arr[i] < arr[i + 1]:
inc[i] = inc[i + 1] + 1
else:
dec[i] = dec[i + 1] + 1
count = 1
for i in range(n):
if inc[i] > 1:
count += inc[i]
elif dec[i] > 1:
count += dec[i]
if i + dec[i] < n:
count += inc[i + dec[i]]
return count
T = int(input())
ans = []
for i in range(T):
n = int(input())
arr = list(map(int, input().split()))
v = calculate(arr, n)
ans.append(v)
for i in ans:
print(i) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN 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 FOR VAR VAR EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | t = int(input())
for _ in range(t):
tot = 0
n = int(input())
l, r = [1] * n, [1] * n
arr = list(map(int, input().split()))
for i in range(1, n):
if arr[i] < arr[i - 1]:
l[i] += l[i - 1]
if arr[n - i - 1] < arr[n - i]:
r[n - i - 1] += r[n - i]
print(sum(l[i] * r[i] for i in range(n))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | import sys
from itertools import islice
for s in islice(sys.stdin, 2, None, 2):
a = (*map(int, s.split()),)
s = "".join("* "[x < y > z] for x, y, z in zip(a, a[1:], a[2:]))
print(2 * len(a) - 1 + sum(l * (l + 1) // 2 for l in map(len, s.split()))) | IMPORT FOR VAR FUNC_CALL VAR VAR NUMBER NONE NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING STRING VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | from sys import stdin
input = stdin.readline
def solve(N, P):
count = 0
hills = [i for i in range(1, N - 1) if P[i - 1] < P[i] > P[i + 1]]
hills = [0] + hills + [N - 1]
for left, right in zip(hills, hills[1:]):
size = right - left + 1
count += size * (size - 1) // 2
return count + N
T = int(input().strip())
for problem in range(1, T + 1):
N = int(input().strip())
P = [int(x) for x in input().strip().split()]
print(solve(N, P)) | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | for i in range(int(input())):
n = int(input())
array = list(map(int, input().split()))
k = 1
ans = 1
array[0] = min(-array[0], array[0])
for j in range(n - 1):
if max(array[j + 1], -array[j + 1]) > array[j]:
k += 1
if -array[j + 1] > array[j]:
array[j + 1] = -array[j + 1]
else:
ans += k * (k + 1) // 2 - 1
k = 2
if -array[j + 1] > -abs(array[j]):
array[j + 1] = -array[j + 1]
ans += k * (k + 1) // 2 - 1
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | def soln(n, arr):
streak = 1
res = 0
i = 0
while i < n - 1:
if arr[i] < arr[i + 1]:
i += 1
streak += 1
else:
arr[i] = -arr[i]
if i > 0 and arr[i - 1] > 0:
if res != 0:
res -= 1
res += streak * (streak + 1) // 2
streak = 1
if res != 0:
res -= 1
res += streak * (streak + 1) // 2
return res
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
print(soln(n, arr)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
An array A is called *weird* if it can be sorted in non-decreasing order by applying the given operation any number of times:
Select any index i (1 β€ i β€ |A|) and set A_{i} := -A_{i}.
For example: A = [2, 1, 3] is *weird* since after applying the operation at i = 1, A becomes [-2, 1, 3] which is sorted.
JJ has a permutation P of length N. He wants to find the number of subarrays of P which are *weird*. Can you help him?
------ Input Format ------
- The first line contains a single integer T β the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N β the size of the permutation P.
- The second line of each test case contains N space-separated integers P_{1}, P_{2}, \dots, P_{N} denoting the permutation P.
------ Output Format ------
For each test case, output on a new line the number of *weird* subarrays of P.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 10^{5}$
$P$ is a permutation.
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
3
2 3 1
3
2 1 3
4
1 2 3 4
----- Sample Output 1 ------
5
6
10
----- explanation 1 ------
Test Case 1: Weird subarrays of $P$ are: $[2], [3], [1], [2, 3], [3, 1]$.
Test Case 2: Weird subarrays of $P$ are: $[2], [1], [3], [2, 1], [1, 3], [2, 1, 3]$. | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
ans = 0
c = 0
prev = 1
curr = 1
for i in range(n - 1):
if arr[i] <= arr[i + 1]:
prev = curr
curr = 1
c += 1
else:
prev = curr
curr = 0
if prev == 1 and curr == 0:
ans += (c + 1) * c // 2
c = 0
c += 1
ans += (c + 1) * c // 2 + n
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.