description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | input()
d = {(0, 0): 1}
r = s = i = 0
for x in map(int, input().split()):
s ^= x
i ^= 1
c = d.get((s, i), 0)
r += c
d[s, i] = c + 1
print(r) | EXPR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = list(map(int, input().split()))
b = [0]
def calc(i):
e = 0
o = 0
for j in i:
if j % 2:
e += 1
else:
o += 1
return e * (e - 1) // 2 + o * (o - 1) // 2
for i in a:
b.append(b[-1] ^ i)
k = list(set(b))
d1 = {}
for i in range(len(k)):
d1[k[i]] = i
d = [[] for i in range(len(k))]
for i in range(n + 1):
d[d1[b[i]]].append(i)
ans = 0
for i in d:
ans += calc(i)
print(ans) | 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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
n_integers = [int(x) for x in input().split()]
n_integers_cumulative = [0] * len(n_integers)
n_even = 0
n_odd = 0
if n % 2 == 0:
n_even = int(n / 2) + 1
n_odd = n + 1 - n_even
else:
n_even = int((n + 1) / 2)
n_odd = n + 1 - n_even
even_integers_cumulative = []
odd_integers_cumulative = []
even_counts = {}
odd_counts = {}
for x in range(0, n):
n_integers_cumulative[x] = n_integers_cumulative[x - 1] ^ n_integers[x]
if x % 2 == 0:
even_integers_cumulative.append(n_integers_cumulative[x])
if n_integers_cumulative[x] in even_counts:
even_counts[n_integers_cumulative[x]] = (
even_counts[n_integers_cumulative[x]] + 1
)
else:
even_counts[n_integers_cumulative[x]] = 1
else:
odd_integers_cumulative.append(n_integers_cumulative[x])
if n_integers_cumulative[x] in odd_counts:
odd_counts[n_integers_cumulative[x]] = (
odd_counts[n_integers_cumulative[x]] + 1
)
else:
odd_counts[n_integers_cumulative[x]] = 1
counter = 0
for x in odd_counts:
counter = counter + int(odd_counts[x] * (odd_counts[x] - 1) / 2)
for x in even_counts:
counter = counter + int(even_counts[x] * (even_counts[x] - 1) / 2)
if 0 in odd_counts:
counter = counter + odd_counts[0]
print(counter) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
lis = list(map(int, input().split()))
mat = [([0] * (2**20 + 5)) for i in range(2)]
ans = a = 0
mat[1][0] = 1
for i in range(n):
a = a ^ lis[i]
ans += mat[i % 2][a]
mat[i % 2][a] += 1
print(ans) | 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 BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | n = int(input())
a = list(map(int, input().split()))
s = [0] * (n + 1)
def count_elements(L):
even_elements = {}
even_lst = L[0::2]
odd_elements = {}
odd_lst = L[1::2]
for elem in even_lst:
if elem in even_elements.keys():
even_elements[elem] += 1
else:
even_elements[elem] = 1
for elem in odd_lst:
if elem in odd_elements.keys():
odd_elements[elem] += 1
else:
odd_elements[elem] = 1
elements = []
for i in even_elements:
elements.append(even_elements[i])
for i in odd_elements:
elements.append(odd_elements[i])
return elements
s[0] = 0
s[1] = a[0]
i = 2
while i < n + 1:
s[i] = s[i - 1] ^ a[i - 1]
i += 1
b = count_elements(s)
ans = 0
for i in b:
ans += int(i * (i - 1) / 2)
print(ans) | 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 BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array a with n integers. You need to count the number of funny pairs (l, r) (l β€ r). To check if a pair (l, r) is a funny pair, take mid = (l + r - 1)/(2), then if r - l + 1 is an even number and a_l β a_{l+1} β β¦ β a_{mid} = a_{mid + 1} β a_{mid + 2} β β¦ β a_r, then the pair is funny. In other words, β of elements of the left half of the subarray from l to r should be equal to β of elements of the right half. Note that β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2 β€ n β€ 3 β
10^5) β the size of the array.
The second line contains n integers a_1, a_2, β¦, a_n (0 β€ a_i < 2^{20}) β array itself.
Output
Print one integer β the number of funny pairs. You should consider only pairs where r - l + 1 is even number.
Examples
Input
5
1 2 3 4 5
Output
1
Input
6
3 2 2 3 7 6
Output
3
Input
3
42 4 2
Output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2, 5), as 2 β 3 = 4 β 5 = 1.
In the second example, funny pairs are (2, 3), (1, 4), and (3, 6).
In the third example, there are no funny pairs. | import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int, input().split()))
odd = dict()
eve = dict()
tmp = 0
odd[tmp] = 1
res = 0
for i in range(n):
tmp = tmp ^ a[i]
if i % 2 == 0:
if tmp in eve:
res += eve[tmp]
eve[tmp] += 1
else:
eve[tmp] = 1
elif tmp in odd:
res += odd[tmp]
odd[tmp] += 1
else:
odd[tmp] = 1
print(res) | IMPORT ASSIGN 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.
Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.
Formally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.
Input
The first line contains two integers n and k (1 β€ n, k β€ 500) β the number of coins and the price of the chocolate, respectively.
Next line will contain n integers c1, c2, ..., cn (1 β€ ci β€ 500) β the values of Pari's coins.
It's guaranteed that one can make value k using these coins.
Output
First line of the output must contain a single integer qβ the number of suitable values x. Then print q integers in ascending order β the values that Arya can make for some subset of coins of Pari that pays for the chocolate.
Examples
Input
6 18
5 6 1 10 12 2
Output
16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
Input
3 50
25 25 50
Output
3
0 25 50 | from sys import stdin, stdout
n, m = map(int, input().split())
s = [0] + list(map(int, stdin.readline().strip().split()))
dp = [[[(False) for i in range(m + 1)] for j in range(m + 1)] for k in range(2)]
dp[0][0][0] = True
cur = 0
for i in range(1, n + 1):
cur += 1
cur %= 2
last = (cur + 1) % 2
for j in range(m + 1):
for k in range(j + 1):
if j - s[i] > -1 and k - s[i] > -1:
dp[cur][j][k] = (
dp[last][j][k]
or dp[last][j - s[i]][k]
or dp[last][j - s[i]][k - s[i]]
)
elif j - s[i] > -1:
dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k]
elif k - s[i] > -1:
dp[cur][j][k] = dp[last][j][k] or dp[last][j - s[i]][k - s[i]]
else:
dp[cur][j][k] = dp[last][j][k]
n1 = 0
ans = ""
for i in range(k + 1):
if dp[cur][k][i]:
ans += str(i) + " "
n1 += 1
stdout.write(str(n1) + chr(10))
stdout.write(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.
Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.
Formally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.
Input
The first line contains two integers n and k (1 β€ n, k β€ 500) β the number of coins and the price of the chocolate, respectively.
Next line will contain n integers c1, c2, ..., cn (1 β€ ci β€ 500) β the values of Pari's coins.
It's guaranteed that one can make value k using these coins.
Output
First line of the output must contain a single integer qβ the number of suitable values x. Then print q integers in ascending order β the values that Arya can make for some subset of coins of Pari that pays for the chocolate.
Examples
Input
6 18
5 6 1 10 12 2
Output
16
0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
Input
3 50
25 25 50
Output
3
0 25 50 | read = lambda: map(int, input().split())
n, k = read()
c = list(read())
dp = [0] * (k + 1)
dp[0] = 1
for x in c:
ndp = dp[:]
for i in range(k, x - 1, -1):
ndp[i] |= dp[i - x] | dp[i - x] << x
dp = ndp
b = bin(dp[-1])
ans = [i for i in range(k + 1) if b[-i - 1] == "1"]
print(len(ans))
print(*ans) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin, stdout
dp = []
M = 10**9 + 7
for i in range(200011):
if i < 10:
dp.append(1)
else:
dp.append((dp[i - 9] + dp[i - 10]) % M)
for _ in range(int(stdin.readline())):
n, m = stdin.readline().split()
m = int(m)
M = 10**9 + 7
ans = 0
for i in n:
ans = (ans + dp[m + int(i)]) % M
stdout.write(str(ans) + "\n") | ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import maxsize, stdin, stdout
R = lambda: stdin.readline().strip()
RL = lambda f=None: list(map(f, R().split(" "))) if f else list(R().split(" "))
output = lambda x: stdout.write(str(x) + "\n")
output_list = lambda x: output(" ".join(map(str, x)))
M = int(1000000000.0) + 7
mx = int(200000.0) + 5
dp = [0] + 9 * [1] + [2] + mx * [0]
for i in range(11, len(dp)):
dp[i] = (dp[i - 9] + dp[i - 10]) % M
for tc in range(int(R())):
n, m = RL(int)
ans = 0
for i in list(str(n)):
ans = (ans + dp[int(i) + m]) % M
print(ans) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER BIN_OP NUMBER LIST NUMBER LIST NUMBER BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
dp = [2] * 9 + [3] + [0] * 199991
for i in range(10, 200001):
dp[i] = (dp[i - 9] + dp[i - 10]) % (10**9 + 7)
for _ in range(int(input())):
n, m = map(int, input().split())
ans = 0
while n:
x = n % 10
if m + x < 10:
ans += 1
else:
ans += dp[m + x - 10]
n //= 10
print(ans % (10**9 + 7)) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER NUMBER LIST NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
d = [(0) for x in range(200001)]
d[0] = 1
for i in range(1, 10):
d[i] = 2
for i in range(10, 200001):
d[i] = (d[i - 10] + d[i - 9]) % (10**9 + 7)
t = int(input())
for i in range(t):
n, m = map(int, input().split())
n = list(str(n))
count = [(0) for x in range(10)]
for i in n:
count[int(i)] += 1
ans = 0
for i in range(10):
temp = m - (9 - i)
if temp > 0:
ans += d[temp] * count[i]
else:
ans += count[i]
print(ans % (10**9 + 7)) | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
def inp():
return sys.stdin.readline().rstrip()
p = 1000000007
t = int(inp())
rec = [1] * 200010
for i in range(10):
rec[i] = 1
for i in range(10, 200010):
rec[i] = (rec[i - 9] + rec[i - 10]) % p
res_list = []
for z in range(t):
n, m = inp().split()
m = int(m)
res = 0
for c in n:
res = (res + rec[m + int(c)]) % p
res_list.append(res)
print("\n".join(map(str, res_list))) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
def main():
import sys
input = sys.stdin.readline
MOD = 10**9 + 7
t = int(input())
x = 1
m = int(2 * 100000.0 + 10)
dp = [(1) for i in range(m + 1)]
for i in range(10, m + 1):
dp[i] = (dp[i - 10] + dp[i - 9]) % MOD
for _ in range(t):
n, k = map(int, input().split())
sums = 0
while n:
sums = sums + dp[k + n % 10]
n = n // 10
sums %= MOD
print(sums)
main() | IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin
input = stdin.readline
mxn = 2 * 10**5 + 10
mod = 10**9 + 7
dp = [1] * mxn
for i in range(10, mxn):
dp[i] = (dp[i - 9] + dp[i - 10]) % mod
for test in range(int(input())):
n, k = map(int, input().strip().split())
ans = 0
while n:
ans = (ans + dp[k + n % 10]) % mod
n //= 10
print(ans) | ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin, stdout
z = 10**9 + 7
dp = {}
for i in range(200009):
if i < 9:
dp[i] = 2
elif i == 9:
dp[i] = 3
else:
dp[i] = (dp[i - 9] + dp[i - 10]) % z
for _ in range(int(input())):
n, m = stdin.readline().split()
n = int(n)
m = int(m)
ans = 0
while n > 0:
i = n % 10
if m + i < 10:
ans += 1
else:
ans += dp[m + i - 10] % z
ans = ans % z
n //= 10
stdout.write(str(ans) + "\n") | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin, stdout
dp = [0] * 200005
for i in range(0, 9):
dp[i] = 2
dp[9] = 3
for i in range(10, 200005):
dp[i] = (dp[i - 10] + dp[i - 9]) % 1000000007
ans = ""
for i in range(int(input())):
n, m = stdin.readline().split()
m = int(m)
res = 0
for s in n:
s = int(s)
ind = m - (10 - s)
if ind < 0:
res += 1
continue
res = (res + dp[ind]) % 1000000007
stdout.write(str(res) + "\n") | ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin, stdout
s = "0"
curr = 1
count = 0
check = 0
dp = [1]
for i in range(100):
fin = ""
for i in s:
num = int(i)
num += 1
num = str(num)
fin = fin + num
dp.append(len(fin))
s = fin
tog = 101
mod = 10**9 + 7
for i in range(2 * 10**5):
num = (dp[tog - 1] + dp[tog - 9] - dp[tog - 11]) % mod
dp.append(num)
tog += 1
t = t = int(stdin.readline())
for test in range(t):
n, m = map(int, stdin.readline().split())
ans = 0
while n > 0:
temp = n % 10
ans = (ans + dp[temp + m]) % mod
n = n // 10
stdout.write(str(ans) + "\n") | ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
MOD = 1000000007
dp = [(1) for _ in range(200010)]
for i in range(10, 200010):
dp[i] = (dp[i - 10] + dp[i - 9]) % MOD
for _ in range(int(input())):
n, m = map(int, input().split())
ans = 0
while n:
ans += dp[m + n % 10]
n //= 10
ans %= MOD
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = lambda: sys.stdin.readline().rstrip()
def solve_tc(dp):
mod = 1000000007
n, m = map(int, input().split())
cnt = 0
while n > 0:
x = int(n % 10)
if m + x < 10:
cnt += 1
else:
cnt += dp[m + x - 10]
cnt = int(cnt % mod)
n //= 10
return str(cnt)
def dp_maker():
max_n = 200005
mod = 1000000007
dp = {}
for i in range(10):
dp[i] = 2
dp[9] = 3
for i in range(10, max_n):
dp[i] = (dp[i - 9] + dp[i - 10]) % mod
return dp
dp = dp_maker()
t = int(input())
while t > 0:
t -= 1
sys.stdout.write(solve_tc(dp))
sys.stdout.write("\n") | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
t = int(input())
mod = pow(10, 9) + 7
cnt = [(0) for _ in range(200020)]
cnt0 = [0] * 10
cnt0[0] = 1
s = 1
for k in range(200020):
x = cnt0[(9 - k) % 10]
cnt0[(10 - k) % 10] += x
cnt0[(10 - k) % 10] %= mod
s += x
s %= mod
cnt[k] = s
for _ in range(t):
n, m = map(int, input().split())
ans = 0
for i in str(n):
ans += cnt[m + int(i) - 1]
ans %= mod
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
mod = pow(10, 9) + 7
arr = [0] * 200020
for i in range(10):
arr[i] = 1
for i in range(10, 200020):
arr[i] = (arr[i - 10] + arr[i - 9]) % mod
def solve():
n, m = map(int, input().split(" "))
ans = 0
while True:
k = m + n % 10
ans += arr[k]
n = n // 10
if n == 0:
break
print(ans % mod)
for t in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
def get_ints():
return map(int, sys.stdin.readline().strip().split())
MOD = 10**9 + 7
LIMIT = 20011
dp = []
for i in range(200011):
if i < 10:
dp.append(1)
else:
dp.append((dp[i - 9] + dp[i - 10]) % MOD)
T = int(input())
for _ in range(T):
N, M = get_ints()
ans = 0
while N:
ans = (ans + dp[N % 10 + M]) % MOD
N //= 10
print(ans) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
t = int(input().strip())
M = 1000000007
dp = [0] * (2 * 100000 + 20)
dp[:10] = list(1 for i in range(10))
for i in range(10, 2 * 100000 + 20):
dp[i] = dp[i - 9] + dp[i - 10]
dp[i] %= M
while t:
t -= 1
n, m = map(int, input().strip().split())
n = str(n)
result = 0
for i in n:
result += dp[int(i) + m]
result %= M
print(result) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin
input = stdin.readline
mod = 10**9 + 7
def add(a, b):
return (a % mod + b % mod) % mod
dp = [1] * (2 * 10**5 + 10)
for i in range(10, 2 * 10**5 + 10):
dp[i] = add(dp[i - 9], dp[i - 10])
def answer():
ans = 0
for i in str(n):
ans = add(ans, dp[m + int(i)])
return ans
for T in range(int(input())):
n, m = map(int, input().strip().split())
print(answer()) | ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | M = 10**9 + 7
L = 8**6
d = [1] * L
d[:10] = [1] * 10
for i in range(10, L):
d[i] = (d[i - 9] + d[i - 10]) % M
for s in [*open(0)][1:]:
n, m = s.split()
o = int(m)
print(sum(d[o + int(c)] for c in n) % M) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin
input = stdin.readline
mod = 10**9 + 7
cnt = [1] * (2 * 10**5 + 15)
curr = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
curr_len = 1
for i in range(2 * 10**5 + 15):
cnt[i] = curr_len
curr_len = (curr_len + curr[-1]) % mod
curr[0] = (curr[0] + curr[-1]) % mod
curr.insert(0, curr.pop())
for _ in range(int(input())):
n, m = map(int, input().split())
ans = 0
for i in str(n):
ans = (ans + cnt[int(i) + m]) % mod
print(ans) | ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
mod = 10**9 + 7
mxi = 210000
ans = [0] * mxi
cts = [0] * 10
cts[0] = 1
s = 1
for i in range(mxi):
ans[i] = s
s += cts[-1]
s %= mod
cts[0] += cts[-1]
cts[0] %= mod
cts.insert(0, cts.pop())
for f in range(int(input())):
n, m = map(int, input().split())
sol = 0
for x in str(n):
sol += ans[m + int(x)]
sol %= mod
print(sol) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | import sys
input = sys.stdin.readline
M = 10**9 + 7
dp = [[0, 0] for i in range(200001)]
for i in range(200001):
if i < 10:
dp[i][0] = 1
dp[i][1] = 1
if i == 9:
dp[i][1] = dp[1][1] + dp[1][0]
else:
dp[i][0] = (dp[i - 10][1] + dp[i - 10][0]) % M
dp[i][1] = (dp[i - 9][1] + dp[i - 9][0]) % M
def solve(n, m):
s = list(str(n))
z = 0
for i in range(len(s)):
x = int(s[i])
if x + m < 10:
z += 1
else:
z += dp[m - (10 - x)][1] + dp[m - (10 - x)][0]
return str(z % M)
t = int(input())
o = []
while t > 0:
n, m = map(int, input().split())
o.append(solve(n, m))
t -= 1
print("\n".join(o)) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin
t = int(stdin.readline())
mod = 10**9 + 7
f = [0] * (2 * 10**5 + 10)
for i in range(10):
f[i] = 1
f[10] = 2
for i in range(11, 2 * 10**5 + 10):
f[i] = (f[i - 10] + f[i - 9]) % mod
for _ in range(t):
n, m = map(int, stdin.readline().split())
c = [0] * 10
n = str(n)
for i in range(len(n)):
c[int(n[i])] += 1
ans = 0
for i in range(10):
ans += c[i] * f[m + i]
ans %= mod
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 β€ t β€ 2 β
10^5) β the number of test cases.
The only line of each test case contains two integers n (1 β€ n β€ 10^9) and m (1 β€ m β€ 2 β
10^5) β the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4. | from sys import stdin
dp = [0] * (2 * 10**5 + 1)
for i in range(9):
dp[i] = 2
dp[9] = 3
for i in range(10, 2 * 10**5 + 1):
dp[i] = (dp[i - 9] + dp[i - 10]) % (10**9 + 7)
for _ in range(int(input())):
n, m = map(int, stdin.readline().split())
n = str(n)
a = [0] * len(n)
ans = 0
for i in n:
temp = int(i) + m
if temp < 10:
ans += 1
else:
ans += dp[m + int(i) - 10] % (10**9 + 7)
print(ans % (10**9 + 7)) | ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
Madoka is going to enroll in "TSUNS PTU". But she stumbled upon a difficult task during the entrance computer science exam:
A number is called good if it is a multiple of $d$.
A number is called beatiful if it is good and it cannot be represented as a product of two good numbers.
Notice that a beautiful number must be good.
Given a good number $x$, determine whether it can be represented in at least two different ways as a product of several (possibly, one) beautiful numbers. Two ways are different if the sets of numbers used are different.
Solve this problem for Madoka and help her to enroll in the best school in Russia!
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 100$) β number of test cases. Below comes their description.
Each test case consists of two integers $x$ and $d$, separated by a space ($2 \leq x, d \leq 10^9$). It is guaranteed that $x$ is a multiple of $d$.
-----Output-----
For each set of input data, output "NO" if the number cannot be represented in at least two ways. Otherwise, output "YES".
You can output each letter in any case (for example, "YES", "Yes", "yes", "yEs", "yEs" will be recognized as a positive answer).
-----Examples-----
Input
8
6 2
12 2
36 2
8 2
1000 10
2376 6
128 4
16384 4
Output
NO
NO
YES
NO
YES
YES
NO
YES
-----Note-----
In the first example, $6$ can be represented as $6$, $1 \cdot 6$, $2 \cdot 3$. But $3$ and $1$ are not a good numbers because they are not divisible by $2$, so there is only one way.
In the second example, $12$ can be represented as $6 \cdot 2$, $12$, $3 \cdot 4$, or $3 \cdot 2 \cdot 2$. The first option is suitable. The second isβ no, because $12$ is not beautiful number ($12 = 6 \cdot 2$). The third and fourth are also not suitable, because $3$ is not good number.
In the third example, $36$ can be represented as $18 \cdot 2$ and $6 \cdot 6$. Therefore it can be decomposed in at least two ways. | def prime(n):
if n % 2 == 0 and n != 2:
return False
for i in range(3, int(n**0.5 + 1), 2):
if n % i == 0:
return False
return True
for _ in range(int(input())):
x, d = map(int, input().split())
ans = 0
while x % d == 0:
ans += 1
x //= d
if ans == 1:
print("NO")
elif ans == 2:
if prime(x):
print("NO")
else:
print("YES")
elif prime(x) and prime(d):
print("NO")
elif prime(x) and ans == 3 and d == x**2:
print("NO")
else:
print("YES") | FUNC_DEF IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Madoka is going to enroll in "TSUNS PTU". But she stumbled upon a difficult task during the entrance computer science exam:
A number is called good if it is a multiple of $d$.
A number is called beatiful if it is good and it cannot be represented as a product of two good numbers.
Notice that a beautiful number must be good.
Given a good number $x$, determine whether it can be represented in at least two different ways as a product of several (possibly, one) beautiful numbers. Two ways are different if the sets of numbers used are different.
Solve this problem for Madoka and help her to enroll in the best school in Russia!
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 100$) β number of test cases. Below comes their description.
Each test case consists of two integers $x$ and $d$, separated by a space ($2 \leq x, d \leq 10^9$). It is guaranteed that $x$ is a multiple of $d$.
-----Output-----
For each set of input data, output "NO" if the number cannot be represented in at least two ways. Otherwise, output "YES".
You can output each letter in any case (for example, "YES", "Yes", "yes", "yEs", "yEs" will be recognized as a positive answer).
-----Examples-----
Input
8
6 2
12 2
36 2
8 2
1000 10
2376 6
128 4
16384 4
Output
NO
NO
YES
NO
YES
YES
NO
YES
-----Note-----
In the first example, $6$ can be represented as $6$, $1 \cdot 6$, $2 \cdot 3$. But $3$ and $1$ are not a good numbers because they are not divisible by $2$, so there is only one way.
In the second example, $12$ can be represented as $6 \cdot 2$, $12$, $3 \cdot 4$, or $3 \cdot 2 \cdot 2$. The first option is suitable. The second isβ no, because $12$ is not beautiful number ($12 = 6 \cdot 2$). The third and fourth are also not suitable, because $3$ is not good number.
In the third example, $36$ can be represented as $18 \cdot 2$ and $6 \cdot 6$. Therefore it can be decomposed in at least two ways. | def isPrime(n):
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return i
return -1
def is_Power(x, y):
while x % y == 0:
x = x / y
return x == 1
t = int(input())
def isBeautiful(n, x):
return n % x == 0 and n % x**2 != 0
for i in range(t):
x, d = input().split(" ")
x = int(x)
d = int(d)
count = 0
while x % d == 0:
count += 1
x //= d
if count < 2:
print("NO")
else:
if isPrime(x) != -1:
print("YES")
continue
if isPrime(d) != -1 and isPrime(d) * isPrime(d) == d:
if x == isPrime(d) and count == 3:
print("NO")
continue
if count > 2 and isPrime(d) != -1:
print("YES")
continue
print("NO") | FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Madoka is going to enroll in "TSUNS PTU". But she stumbled upon a difficult task during the entrance computer science exam:
A number is called good if it is a multiple of $d$.
A number is called beatiful if it is good and it cannot be represented as a product of two good numbers.
Notice that a beautiful number must be good.
Given a good number $x$, determine whether it can be represented in at least two different ways as a product of several (possibly, one) beautiful numbers. Two ways are different if the sets of numbers used are different.
Solve this problem for Madoka and help her to enroll in the best school in Russia!
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 100$) β number of test cases. Below comes their description.
Each test case consists of two integers $x$ and $d$, separated by a space ($2 \leq x, d \leq 10^9$). It is guaranteed that $x$ is a multiple of $d$.
-----Output-----
For each set of input data, output "NO" if the number cannot be represented in at least two ways. Otherwise, output "YES".
You can output each letter in any case (for example, "YES", "Yes", "yes", "yEs", "yEs" will be recognized as a positive answer).
-----Examples-----
Input
8
6 2
12 2
36 2
8 2
1000 10
2376 6
128 4
16384 4
Output
NO
NO
YES
NO
YES
YES
NO
YES
-----Note-----
In the first example, $6$ can be represented as $6$, $1 \cdot 6$, $2 \cdot 3$. But $3$ and $1$ are not a good numbers because they are not divisible by $2$, so there is only one way.
In the second example, $12$ can be represented as $6 \cdot 2$, $12$, $3 \cdot 4$, or $3 \cdot 2 \cdot 2$. The first option is suitable. The second isβ no, because $12$ is not beautiful number ($12 = 6 \cdot 2$). The third and fourth are also not suitable, because $3$ is not good number.
In the third example, $36$ can be represented as $18 \cdot 2$ and $6 \cdot 6$. Therefore it can be decomposed in at least two ways. | def isPrime(n):
if n == 1:
return True
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def factor(n):
ans = []
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
ans.append(i)
ans.append(n // i)
return ans
for lkdl in range(int(input())):
x, d = map(int, input().split())
c = 0
while x % d == 0:
x = x // d
c += 1
if c == 1:
print("NO")
elif isPrime(x):
if isPrime(d):
print("NO")
elif c == 2:
print("NO")
elif c >= 4:
print("YES")
else:
f = factor(d)
ans = "NO"
for i in f:
if i * d * x % (d * d) != 0:
ans = "YES"
break
print(ans)
else:
print("YES") | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | def f(a, b):
row = len(a) + 1
col = len(b) + 1
dp = [([0] * col) for _ in range(row)]
r = 0
for i in range(1, row):
for j in range(1, col):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]
if a[i - 1] == b[j - 1]:
if i == row - 1:
r += dp[i - 1][j - 1] + 1
r %= 10**9 + 7
dp[i][j] += dp[i - 1][j - 1] + 1
return r
def solve(string):
ans = 0
n = len(string)
for i in range(1, n):
ans += f(string[:i], string[i:])
ans %= 10**9 + 7
return ans
t = int(input())
for t_itr in range(t):
s = input()
print(solve(s)) | FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | case_count = int(input())
MOD = 1000000007
def solve(A, B):
solutions = [([0] * len(A)) for j in range(len(B))]
for j in range(len(A)):
solutions[0][j] = solutions[0][j - 1] + int(B[0] == A[j])
solutions[0][j] %= MOD
for i in range(1, len(B)):
solutions[i][0] = solutions[i - 1][0]
for j in range(1, len(A)):
solutions[i][j] = solutions[i - 1][j] + solutions[i][j - 1]
if A[j] != B[i]:
solutions[i][j] -= solutions[i - 1][j - 1]
solutions[i][j] %= MOD
return solutions[-1][-1]
def task(source):
result = 0
for i in range(len(source) - 1):
res = solve(source[: i + 1], source[i + 1 :])
result += res
result %= MOD
return result
for i in range(case_count):
print(task(input())) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | import sys
def count_common_subs(s1, s2):
dp = [[(0) for j in range(len(s2) + 1)] for i in range(len(s1) + 1)]
for i in range(len(s1)):
for j in range(len(s2)):
if s1[i] == s2[j]:
dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] + 1
else:
dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] - dp[i][j]
return dp[-1][-1] - dp[-2][-1]
def find_sq_subs(s):
result = 0
for i in range(1, len(s)):
s1 = s[:i]
s2 = s[i:]
result += count_common_subs(s1, s2)
return result
_ = sys.stdin.readline()
modulo = 1000000007
for line in sys.stdin:
print(find_sq_subs(line.strip()) % modulo) | IMPORT FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | def count_square_subsequences(s, t):
m = len(s)
n = len(t)
T = [[(0) for _ in range(n)] for _ in range(m)]
T[0][0] = 1 if s[0] == t[0] else 0
for i in range(1, n):
T[0][i] = T[0][i - 1] + (1 if s[0] == t[i] else 0)
T[0][i] %= 1000000007
for i in range(1, m):
T[i][0] = T[i - 1][0]
T[i][0] %= 1000000007
for i in range(1, m):
for j in range(1, n):
T[i][j] = (
T[i - 1][j] + T[i][j - 1] + (0 if s[i] == t[j] else -T[i - 1][j - 1])
)
T[i][j] %= 1000000007
return T[m - 1][n - 1]
def square_subsequences(string):
m = len(string)
R = 0
for i in range(1, m):
R += count_square_subsequences(string[i:], string[:i])
R %= 1000000007
return R
def main():
T = int(input())
for _ in range(T):
print(square_subsequences(input()))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | from sys import stderr
def dp(a, b):
c = [[(0) for j in range(len(b))] for i in range(len(a))]
for i in range(len(b)):
if a[0] == b[i]:
c[0][i] = 1
if i:
c[0][i] += c[0][i - 1]
c[0][i] %= mod
for i in range(1, len(a)):
c[i][0] = c[i - 1][0]
for j in range(1, len(b)):
c[i][j] = c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1]
if a[i] == b[j]:
c[i][j] += c[i - 1][j - 1]
c[i][j] %= mod
return c[len(a) - 1][len(b) - 1]
def backtrack(c, a, b, i, j):
if i == 0 or j == 0:
return ""
elif a[i - 1] == b[j - 1]:
return backtrack(c, a, b, i - 1, j - 1) + a[i - 1]
elif c[i][j - 1] > c[i - 1][j]:
return backtrack(c, a, b, i, j - 1)
else:
return backtrack(c, a, b, i - 1, j)
mod = 1000000007
t = int(input().strip())
for _ in " " * t:
s = list(input().strip())
s = "".join(x for x in s if s.count(x) > 1)
c = 0
for k in range(1, len(s)):
d = dp(s[k:], s[:k])
print(s[:k], s[k:], file=stderr)
print(d, file=stderr)
c += d
c %= mod
print(c) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | NUMBER = 1000000007
class SquareSubstring:
def __init__(self):
self.T = int(input())
for i in range(self.T):
string = input()
print(self.solve(string))
def solve_sub(self, string, size):
s1 = string[:size]
size1 = len(s1) + 1
s2 = string[size:]
size2 = len(s2) + 1
f = [[(0) for i in range(size2)] for j in range(size1)]
for i in range(1, size1):
for j in range(1, size2):
if s1[i - 1] != s2[j - 1]:
f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1]
else:
f[i][j] = f[i - 1][j] + f[i][j - 1] + 1
return f[len(s1)][len(s2)] - f[len(s1) - 1][len(s2)]
def solve(self, string):
length = len(string)
count = 0
for i in range(length):
count = (count + self.solve_sub(string, i)) % NUMBER
return count
SquareSubstring() | ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL 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 IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | tr = int(input().strip())
for t in range(tr):
s = input().strip()
n = len(s)
T = [[(0) for i in range(n)] for i in range(n)]
sum = 0
for p in range(1, n):
for i in range(p):
for j in range(p, n):
left = T[i][j - 1] if j > p else 0
above = T[i - 1][j] if i > 0 else 0
diag = T[i - 1][j - 1] if j > p and i > 0 else 0
if s[i] == s[j]:
T[i][j] = left + above + 1
else:
T[i][j] = left + above - diag
sum += T[p - 1][n - 1]
if p > 1:
sum -= T[p - 2][n - 1]
print(sum % 1000000007) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | def subsequences(s, k):
s1 = s[:k]
s2 = s[k:]
subs = [[(0) for j in range(len(s) - k + 1)] for i in range(k + 1)]
for i in range(k):
for j in range(len(s) - k):
if s1[i] == s2[j]:
subs[i + 1][j + 1] = subs[i][j + 1] + subs[i + 1][j] + 1
else:
subs[i + 1][j + 1] = subs[i][j + 1] + subs[i + 1][j] - subs[i][j]
return (subs[-1][-1] - subs[-2][-1]) % 1000000007
t = int(input())
for _ in range(t):
s = input()
print(sum(subsequences(s, k) for k in range(1, len(s))) % 1000000007) | FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | t = int(input().strip())
def comSub(str1, str2):
n = len(str1)
m = len(str2)
dp = [[(0) for _ in range(m + 1)] for _ in range(n + 1)]
for i in range(n):
for j in range(m):
if str1[i] == str2[j]:
dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] + 1
else:
dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] - dp[i][j]
return dp[n][m]
for _ in range(t):
s = input().strip()
n = len(s)
comSubs = 0
for i in range(1, n):
comSubs = (
comSubs + comSub(s[0:i], s[i:n]) - comSub(s[0:i], s[i + 1 : n])
) % 1000000007
print(comSubs) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Square Subsequences
A string is called a square string if it can be obtained by concatenating two copies of the same string. For example, "abab", "aa" are square strings, while "aaa", "abba" are not. Given a string, how many (non-empty) subsequences of the string are square strings? A subsequence of a string can be obtained by deleting zero or more characters from it, and maintaining the relative order of the remaining characters.
Input Format
The first line contains the number of test cases, $T$.
$T$ test cases follow. Each case contains a string, $S$.
Output Format
Output $T$ lines, one for each test case, containing the required answer modulo 1000000007.
Constraints:
$1\leq T\leq20$
$S$ will have at most $200$ lowercase characters ('a' - 'z').
Sample Input
3
aaa
abab
baaba
Sample Output
3
3
6
Explanation
For the first case, there are 3 subsequences of length 2, all of which are square strings.
For the second case, the subsequences "abab", "aa", "bb" are square strings.
Similarly, for the third case, "bb", "baba" (twice), and "aa" (3 of them) are the square subsequences. | def sub(s, size):
s1 = s[:size]
s2 = s[size:]
N = [[(0) for j in range(len(s2) + 1)] for i in range(len(s1) + 1)]
for i in range(1, len(s1) + 1):
for j in range(1, len(s2) + 1):
if s1[i - 1] == s2[j - 1]:
N[i][j] = N[i - 1][j] + N[i][j - 1] + 1
else:
N[i][j] = N[i - 1][j] + N[i][j - 1] - N[i - 1][j - 1]
return N[-1][-1] - N[-2][-1]
num = int(input())
for n in range(num):
inp = input()
print(sum(sub(inp, i) for i in range(1, len(inp))) % 1000000007) | FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | mod = int(1000000000.0 + 7)
for _ in range(int(input())):
n, k = map(int, input().split())
ans = (k**n + k * (-1) ** n) // (k + 1)
print(ans % mod) | ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | m = 10**9 + 7
for t in range(int(input())):
n, k = map(int, input().split())
if n == 2:
print(k)
elif n == 3:
print(k * (k - 1))
else:
a = []
a.append(0)
a.append(0)
a.append(k)
a.append(k * (k - 1))
i = 4
while i <= n:
val = (k - 1) * a[i - 1] % m
val += k * a[i - 2] % m
val %= m
a.append(val)
i += 1
print(a[-1]) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | MOD = 1000000007
def main():
t = int(input())
for i in range(t):
a, b = map(int, input().split())
solve(a, b)
def solve(p, n):
cache = [0] * (p + 1)
cache[0] = 0
cache[1] = 0
cache[2] = 1
for i in range(3, p + 1):
cache[i] = (n ** (i - 2) % MOD - cache[i - 1]) % MOD
print(n * cache[p] % MOD)
main() | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | t = int(input())
for k in range(t):
passes, players = map(int, input().split())
players += 1
dp = [[(0) for i in range(players)] for j in range(passes)]
for i in range(1, players):
dp[0][i] = 1
prev_sum = players - 1
for i in range(1, passes):
cur_sum = 0
for j in range(players):
dp[i][j] = prev_sum - dp[i - 1][j]
cur_sum += dp[i][j]
prev_sum = cur_sum % (10**9 + 7)
print(dp[passes - 1][0] % (10**9 + 7)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | t = int(input())
for i in range(t):
n, k = map(int, input().split(" "))
sum = 0
z = 0
while n > 1:
sum += k ** (n - 1) * (-1) ** z
n -= 1
z += 1
print(sum % 1000000007) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | t = int(input())
for i in range(t):
n, k = map(int, input().split())
c = k
s = 0
ans = k
if n <= 1:
print("0")
continue
for j in range(n - 1):
s = c
ans *= k
c = ans - s
print(s % 1000000007) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | T = int(input())
counter = 0
while counter < T:
n, k = map(int, input().split())
currpos = 0
non_zeroes = k
zeroes = 0
ans = k
if n <= 1:
print("0")
continue
for i in range(0, n - 1):
zeroes = non_zeroes
ans = ans * k
non_zeroes = ans - zeroes
print(zeroes % 1000000007)
counter += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | import sys
t = int(input())
def power(a, b):
p = 1
while b > 0:
p = p * a % 1000000007
p = p % 1000000007
b = b - 1
p = p % 1000000007
return p
while t > 0:
a = sys.stdin.readline().split()
n, k = int(a[0]), int(a[1])
messi = 0
players = 0
i = 1
w = 1
if n % 2 == 0:
o = 1
else:
o = -1
while i < n - 2:
w = w * k % 1000000007
messi += o * w
messi = messi % 1000000007
o *= -1
i = i + 1
players = power(k, n - 2) % 1000000007
ans = (
(players - messi) * (k - 1) % 1000000007 + messi * k % 10000000007
) % 1000000007
if n == 2:
ans = k
ans = ans % 1000000007
print(ans)
t = t - 1 | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | mod = int(1000000000.0 + 7)
for _ in range(int(input())):
n, k = map(int, input().split())
ls = [0] * (n + 1)
ls[0], ls[1] = 1, 0
for i in range(2, n + 1):
ls[i] = ((k - 1) * ls[i - 1] + k * ls[i - 2]) % mod
print(ls[n]) | ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | import sys
sys.setrecursionlimit(3 * 10**5)
def power(x, y, p):
res = 1
x = x % p
if x == 0:
return 0
while y > 0:
if y & 1 == 1:
res = res * x % p
y = y >> 1
x = x * x % p
return res
def inum():
return map(int, input().split())
modulo = 10**9 + 7
def fun(n, k):
modulo = 10**9 + 7
if n == 0:
return 1
return (power(k, n - 1, modulo) - fun(n - 1, k) % modulo + modulo) % modulo
for _ in range(int(input())):
n, k = inum()
if k == 1:
if n % 2 == 0:
print(1)
else:
print(0)
elif n == 2:
print(k)
else:
print(fun(n, k) % modulo) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | s = pow(10, 9) + 7
a = [[(0) for i in range(11)] for j in range(1001)]
for i in range(1, 11):
a[2][i] = i
for n in range(3, 1001):
for k in range(1, 11):
a[n][k] = pow(k, n - 1, s) - a[n - 1][k]
if a[n][k] < 0:
a[n][k] += s
t = int(input())
for i in range(t):
inp = list(map(int, input().split()))
n = inp[0]
k = inp[1]
print(a[n][k]) | ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | for _ in range(int(input())):
N, K = list(map(int, input().split()))
dp = [0] * N
num_players = K + 1
num_passes = N - 1
for i in range(1, N):
dp[i] = (num_players - 1) ** i - dp[i - 1]
print(dp[-1] % 1000000007) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | t = int(input())
while t > 0:
n, k = map(int, input().split())
j = (k**n + k * (-1) ** n) // (k + 1)
j = j % 1000000007
print(j)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | def pulverize(a, b):
apair = [1, 0]
bpair = [0, 1]
while True:
if b == 0:
return a, apair[0], apair[1]
else:
q = a // b
a -= b * q
apair[0] -= bpair[0] * q
apair[1] -= bpair[1] * q
if a == 0:
return b, bpair[0], bpair[1]
else:
q = b // a
b -= a * q
bpair[0] -= apair[0] * q
bpair[1] -= apair[1] * q
def inverse(a, m):
g, x, y = pulverize(a, m)
if g == 1:
return x % m
else:
return 0
mbase = 10**9 + 7
def main():
t = int(input())
for it in range(t):
n, k = [int(x) for x in input().split()]
res = pow(k, n, mbase)
if n % 2 == 0:
res += k
else:
res -= k
res = res * inverse(k + 1, mbase) % mbase
print(res)
main() | FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE NUMBER IF VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | import sys
sys.setrecursionlimit(10**9)
def dfs(n, index):
if (n, index) in m:
return m[n, index]
if n == 0:
if index == k:
return 1
else:
return 0
res = 0
for j in range(k + 1):
if index != j:
res = (res + dfs(n - 1, j)) % mod
m[n, index] = res
return res
t = int(input())
mod = 10**9 + 7
for i in range(t):
n, k = map(int, input().split())
m = {}
ans = dfs(n, k)
print(ans) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | import sys
def passes(N, K):
res = 0
alt = 1
for i in range(1, N):
res += alt * K**i
alt = -alt
return abs(res) % 1000000007
def process(input):
nb_of_cases = int(input.readline().rstrip())
for n in range(1, nb_of_cases + 1):
N, K = (int(i) for i in input.readline().rstrip().split())
answer = passes(N, K)
print(answer)
input_file = sys.stdin
process(input_file) | IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | t = int(input())
if t >= 1 and t <= 100:
while t > 0:
n, k = input().split()
n = int(n)
k = int(k)
if n >= 2 and n <= 1000 and k >= 1 and k <= 10:
j = 1
i = 2
while i <= n:
if i != 2:
if i % 2 != 0:
j = j * k - 1
else:
j = j * k + 1
i = i + 1
print(k * j % 1000000007)
t = t - 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | MD = 10**9 + 7
for _ in range(int(input())):
st = input().split()
N = int(st[0])
K = int(st[1])
M = 0
B = K
for k in range(N - 1):
M, B = B, (K * M + (K - 1) * B) % MD
print(M) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | T = int(input())
for _ in range(T):
p, n = map(int, input().split())
mod = 1000000007
if p == 2:
print(n)
else:
f = n
t = n
for i in range(p - 2):
f = f % mod * n % mod
a = (f - t + mod) % mod
t = a
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | e = 10**9 + 7
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
messi = 0
non_messi = k
for i in range(n - 2):
z = non_messi
non_messi = (messi * k + z * (k - 1)) % e
messi = z
print(non_messi) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | import sys
def win(player, passleft, dp):
if (player, passleft) in dp:
return dp[player, passleft]
if passleft == 0 and player == 0:
return 1
elif passleft == 0 and player != 0:
return 0
dp[player, passleft] = 0
for p in players:
if p != player:
dp[player, passleft] += win(p, passleft - 1, dp)
dp[player, passleft] = dp[player, passleft] % 1000000007
return dp[player, passleft]
sys.setrecursionlimit(1500)
t = int(input())
for _ in range(t):
passes, nop = list(map(int, input().split()))
players = [i for i in range(nop + 1)]
print(win(0, passes, {})) | IMPORT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR DICT |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | for _ in range(int(input())):
n, k = [int(i) for i in input().split()]
A = [[1], [0]]
for i in range(1, n + 1):
A[0].append(k * A[1][i - 1])
A[1].append(A[0][i - 1] + (k - 1) * A[1][i - 1])
print(A[0][n] % (10**9 + 7)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | mod = 1000000007
def powmod(k, n):
if n == 0:
return 1
if n == 1:
return k
if n % 2 == 1:
return k * powmod(k * k % mod, (n - 1) / 2) % mod
else:
return powmod(k * k % mod, n / 2) % mod
t = int(input())
while t:
t -= 1
line = input().split()
n = int(line[0])
k = int(line[1])
invmod = powmod(k + 1, mod - 2)
if n % 2 == 0:
ans = (powmod(k, n) + k) * invmod % mod
else:
ans = (powmod(k, n) + mod - k) * invmod % mod
print(ans) | ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | n = int(input())
for i in range(0, n):
k, p = list(map(int, input().split(" ")))
if k == 2:
print(p)
else:
mess = [0] * (k + 1)
nonmess = [0] * (k + 1)
nonmess[1] = p
for i in range(2, k):
mess[i] = nonmess[i - 1]
nonmess[i] = mess[i - 1] * p + nonmess[i - 1] * (p - 1)
print(nonmess[k - 1] % 1000000007) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | for i in range(int(input())):
n, k = map(int, input().split())
dp = [[0, 1]]
for i in range(n - 1):
dp.append([dp[-1][0] * (k - 1) + dp[-1][1] * k, dp[-1][0]])
print(dp[-1][0] % 1000000007) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | t = int(input())
for i in range(t):
arr = input()
l = list(map(int, arr.split(" ")))
n = l[0]
k = l[1]
f = []
f.append(k)
f.append(k * (k - 1))
if n == 2:
print(f[0] % 1000000007)
if n == 3:
print(f[-1] % 1000000007)
if n > 3:
for j in range(n - 3):
f.append(f[-2] * k + (k - 1) * f[-1])
print(f[-1] % 1000000007) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | t = int(input())
while t:
n, k = [int(x) for x in input().split()]
table = list()
table = [(0) for x in range(n + 1)]
table[0] = 1
table[1] = 0
for i in range(1, n):
table[i + 1] = (table[i - 1] * k + table[i] * (k - 1)) % 1000000007
print(table[n])
t = t - 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
As we all know, F.C. Barcelona is the best soccer team of our era! Their entangling and mesmerizing game style usually translates into very high ball possession, consecutive counter-attack plays and goals. Lots of goals, thanks to the natural talent of their attacker and best player in history, Lionel Andres Messi.
However, at the most prestigious tournament of individual teams, the UEFA Champions League, there are no guarantees and believe it or not, Barcelona is in trouble.... They are tied versus Chelsea, which is a very defending team that usually relies on counter-strike to catch opposing teams off guard and we are in the last minute of the match. So Messi decided to settle things down for good and now he is conducting the ball on his teams' midfield and he will start a lethal counter-attack :D
After dribbling the 2 strikers from Chelsea, he now finds himself near the center of the field and he won't be able to dribble the entire team on his own, so he will need to pass the ball to one of his teammates, run forward and receive the ball once again to score the final goal.
Exactly K players are with him on his counter-attack and the coach, Tito Villanova knows that this counter-attack will end in a goal only if after exactly N passes are performed between the players, Messi ends up with the ball.
(Note that the ball only needs to end with Messi after exactly N passes are performed between all the K+1 players, i.e. Messi can receive the ball several times during the N passes. See the 2nd test case explanation for further clarification. )
However, he realized that there are many scenarios possible for this, so he asked you, his assistant coach, to tell him in how many ways can Messi score the important victory goal. So help him!!
-----Input-----
Input will contain a number T denoting the number of test cases.
Then T test cases follow, each one consisting of two space-sparated integers N and K.
-----Output-----
For each test case, output a single integer, the number of ways the winning play might happen modulo 1000000007 (109+7).
-----Constraints-----
- 1 β€ T β€ 100
- 2 β€ N β€ 1000
- 1 β€ K β€ 10
-----Example-----
Input:
2
2 4
4 2
Output:
4
6
-----Explanation-----
In the first test case, say four players with Messi are Xavi, Busquets, Iniesta and Jordi Alba. Then the ways of the winning play to happen when exactly 2 passes are to be performed are:
1) Messi - Xavi - Messi
2) Messi - Busquets - Messi
3) Messi - Iniesta - Messi
4) Messi - Alba - Messi
In the second test case, also say that two players with Messi are Xavi and Iniesta. There are 6 ways for the winning play to happen when exactly 4 passes are performed. All the examples of such winning play are:
1) Messi - Xavi - Messi - Iniesta - Messi
2) Messi - Xavi - Iniesta - Xavi - Messi
3) Messi - Xavi - Messi - Xavi - Messi
4) Messi - Iniesta - Messi - Iniesta - Messi
5) Messi - Iniesta - Messi - Xavi - Messi
6) Messi - Iniesta - Xavi - Iniesta - Messi | from sys import stdin
final = []
for j in range(2, 11):
li = []
li.append(0)
li.append(0)
li.append(j)
for i in range(3, 1001):
li.append(j * li[i - 2] + (j - 1) * li[i - 1])
li[i] = li[i] % 1000000007
final.append(li)
t = int(input())
for i in range(t):
n, k = map(int, stdin.readline().split(" "))
if k == 1:
if n % 2 == 0:
print(1)
else:
print(0)
else:
print(final[k - 2][n]) | ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef is a big fan of soccer! He loves soccer so much, that he even invented soccer for his pet dogs! Here are the rules of the game:
There are N dogs numerated from 1 to N stay in a line, so dogs i and i + 1 are adjacent.
There is a ball which dogs will pass around. Initially, dog s has the ball.
A dog with ball can pass it to another dog. If the current pass-strength of dog is x, then it can pass the ball to either dog i - x or dog i + x (provided such dog/s exist).
To make it even more exciting, Chef created an array A of M positive integers denoting pass strengths. In i-th pass, current pass-strength of the dog making the pass will be given by A_{i}.
Chef asks dogs to execute these M passes one by one. As stated before, dog s will make the first pass, then some other dog and so on till M passes.
Dogs quickly found out that there can be lot of possible sequences of passes which will end up with a dog having the ball. Now each dog asks your help in finding number of different pass sequences which result in this dog ending up ball. Two pass sequences are considered different if after some number of passes they lead the ball to different dogs. As the answer could be quite large, output it modulo 10^{9} + 7 (1000000007).
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains three space separated integers N, M, s denoting the number of dogs, number of pass strengths and number of dog having a ball at the beginning.
The second line contains M space-separated integers A_{1}, A_{2}, ..., A_{M} denoting the pass strengths.
------ Output ------
For each test case, output a single line containing N space-separated integers, where i-th integer should be equal to number of different valid pass sequences leading the ball to i-th dog modulo 10^{9} + 7.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ N, M β€ 10^{3}$
$1 β€ s β€ N$
$1 β€ A_{i} β€ 10^{3}$
------ Subtasks ------
$Subtask #1 (30 points) : N, M β€ 10$
$Subtask #2 (70 points) : Original constraints$
----- Sample Input 1 ------
3
3 2 2
1 2
3 3 3
1 1 1
3 1 1
3
----- Sample Output 1 ------
1 0 1
0 2 0
0 0 0
----- explanation 1 ------
Example case 1.
Possible sequence for dog 1 is 2->3->1.
Possible sequence for dog 3 is 2->1->3.
Example case 2.
Possible sequences for dog 2 are 3->2->1->2 and 3->2->3->2.
Example case 3.
There are no valid sequences for such input. | t = int(input())
MOD = 10**9 + 7
for _ in range(t):
n, m, s = (int(i) for i in input().split())
s -= 1
strength = [int(i) for i in input().split()]
dp = [([0] * n) for _ in range(m + 1)]
dp[0][s] = 1
elems = {s}
for i in range(1, m + 1):
elems2 = set()
for elem in elems:
a, b = elem - strength[i - 1], elem + strength[i - 1]
if a >= 0:
dp[i][a] = (dp[i][a] + dp[i - 1][elem]) % MOD
elems2.add(a)
if b < n:
dp[i][b] = (dp[i][b] + dp[i - 1][elem]) % MOD
elems2.add(b)
elems = elems2
print(" ".join(map(str, dp[m]))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef is a big fan of soccer! He loves soccer so much, that he even invented soccer for his pet dogs! Here are the rules of the game:
There are N dogs numerated from 1 to N stay in a line, so dogs i and i + 1 are adjacent.
There is a ball which dogs will pass around. Initially, dog s has the ball.
A dog with ball can pass it to another dog. If the current pass-strength of dog is x, then it can pass the ball to either dog i - x or dog i + x (provided such dog/s exist).
To make it even more exciting, Chef created an array A of M positive integers denoting pass strengths. In i-th pass, current pass-strength of the dog making the pass will be given by A_{i}.
Chef asks dogs to execute these M passes one by one. As stated before, dog s will make the first pass, then some other dog and so on till M passes.
Dogs quickly found out that there can be lot of possible sequences of passes which will end up with a dog having the ball. Now each dog asks your help in finding number of different pass sequences which result in this dog ending up ball. Two pass sequences are considered different if after some number of passes they lead the ball to different dogs. As the answer could be quite large, output it modulo 10^{9} + 7 (1000000007).
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains three space separated integers N, M, s denoting the number of dogs, number of pass strengths and number of dog having a ball at the beginning.
The second line contains M space-separated integers A_{1}, A_{2}, ..., A_{M} denoting the pass strengths.
------ Output ------
For each test case, output a single line containing N space-separated integers, where i-th integer should be equal to number of different valid pass sequences leading the ball to i-th dog modulo 10^{9} + 7.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ N, M β€ 10^{3}$
$1 β€ s β€ N$
$1 β€ A_{i} β€ 10^{3}$
------ Subtasks ------
$Subtask #1 (30 points) : N, M β€ 10$
$Subtask #2 (70 points) : Original constraints$
----- Sample Input 1 ------
3
3 2 2
1 2
3 3 3
1 1 1
3 1 1
3
----- Sample Output 1 ------
1 0 1
0 2 0
0 0 0
----- explanation 1 ------
Example case 1.
Possible sequence for dog 1 is 2->3->1.
Possible sequence for dog 3 is 2->1->3.
Example case 2.
Possible sequences for dog 2 are 3->2->1->2 and 3->2->3->2.
Example case 3.
There are no valid sequences for such input. | mod = 10**9 + 7
for _ in range(int(input())):
n, m, s = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
dp1 = [0] * (n + 1)
dp2 = [0] * (n + 1)
zeros = [0] * (n + 1)
dp1[s] = 1
for j in range(m):
for i in range(1, n + 1):
if dp1[i] == 0:
continue
if i - A[j] > 0:
dp2[i - A[j]] += dp1[i]
if i + A[j] <= n:
dp2[i + A[j]] += dp1[i]
dp1 = list(dp2)
dp2 = list(zeros)
for i in range(1, n + 1):
print(dp1[i] % mod, end=" ")
print() | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER 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 ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef is a big fan of soccer! He loves soccer so much, that he even invented soccer for his pet dogs! Here are the rules of the game:
There are N dogs numerated from 1 to N stay in a line, so dogs i and i + 1 are adjacent.
There is a ball which dogs will pass around. Initially, dog s has the ball.
A dog with ball can pass it to another dog. If the current pass-strength of dog is x, then it can pass the ball to either dog i - x or dog i + x (provided such dog/s exist).
To make it even more exciting, Chef created an array A of M positive integers denoting pass strengths. In i-th pass, current pass-strength of the dog making the pass will be given by A_{i}.
Chef asks dogs to execute these M passes one by one. As stated before, dog s will make the first pass, then some other dog and so on till M passes.
Dogs quickly found out that there can be lot of possible sequences of passes which will end up with a dog having the ball. Now each dog asks your help in finding number of different pass sequences which result in this dog ending up ball. Two pass sequences are considered different if after some number of passes they lead the ball to different dogs. As the answer could be quite large, output it modulo 10^{9} + 7 (1000000007).
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains three space separated integers N, M, s denoting the number of dogs, number of pass strengths and number of dog having a ball at the beginning.
The second line contains M space-separated integers A_{1}, A_{2}, ..., A_{M} denoting the pass strengths.
------ Output ------
For each test case, output a single line containing N space-separated integers, where i-th integer should be equal to number of different valid pass sequences leading the ball to i-th dog modulo 10^{9} + 7.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ N, M β€ 10^{3}$
$1 β€ s β€ N$
$1 β€ A_{i} β€ 10^{3}$
------ Subtasks ------
$Subtask #1 (30 points) : N, M β€ 10$
$Subtask #2 (70 points) : Original constraints$
----- Sample Input 1 ------
3
3 2 2
1 2
3 3 3
1 1 1
3 1 1
3
----- Sample Output 1 ------
1 0 1
0 2 0
0 0 0
----- explanation 1 ------
Example case 1.
Possible sequence for dog 1 is 2->3->1.
Possible sequence for dog 3 is 2->1->3.
Example case 2.
Possible sequences for dog 2 are 3->2->1->2 and 3->2->3->2.
Example case 3.
There are no valid sequences for such input. | def chefsoc2():
for _ in range(int(input())):
n, m, s = map(int, input().split())
a = list(map(int, input().split()))
ma = []
for i in range(n):
ma.append(0)
k = ma[:]
k[s - 1] = 1
for i in range(m):
l = ma[:]
for j in range(n):
if k[j]:
if j + a[i] < n:
if l[j + a[i]]:
l[j + a[i]] = (l[j + a[i]] + k[j]) % 1000000007
else:
l[j + a[i]] = k[j] % 1000000007
if j - a[i] >= 0:
if l[j - a[i]]:
l[j - a[i]] = (l[j - a[i]] + k[j]) % 1000000007
else:
l[j - a[i]] = k[j] % 1000000007
k = l[:]
for i in k:
print(i, end=" ")
print()
chefsoc2() | FUNC_DEF 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def fastpowmod(a, b, mod):
if b == 0:
return 1
r = fastpowmod(a, int(b / 2), mod)
if b % 2:
return r * r * a % mod
else:
return r * r % mod
def getcoefbezout(a, b):
A = [[1, 0], [0, 1]]
while b != 0:
w = int(a / b)
A[0][0] = A[0][0] - w * A[1][0]
A[0][1] = A[0][1] - w * A[1][1]
t1 = A[0][0]
t2 = A[0][1]
A[0][0] = A[1][0]
A[0][1] = A[1][1]
A[1][0] = t1
A[1][1] = t2
t = a
a = b
b = t % b
return [A[0][0], A[0][1]]
def factmod(n, mod):
r = 1
for i in range(1, n + 1):
r = r * i % mod
return r
def inversemod(x, mod):
r = getcoefbezout(x, mod)[0]
r = (r % mod + mod) % mod
return r
def combmod(n, k, mod):
a = factmod(n, mod)
b = factmod(n - k, mod)
c = factmod(k, mod)
b = inversemod(b, mod)
c = inversemod(c, mod)
r = a * b % mod * c % mod
return r
t = 1
for i in range(t):
X = input().split()
n = int(X[0])
m = int(X[1])
k = int(X[2])
if k == 0:
print(m)
elif m == 1:
print(0)
else:
a = fastpowmod(m - 1, k, 998244353)
b = combmod(n - 1, k, 998244353)
cd = a * b % 998244353
cd = cd * m % 998244353
print(cd) | FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN LIST VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def fact(i):
if i == 0:
return 1
if i < 0:
return 0
res = 1
for t in range(1, i + 1):
res *= t
return res
n, m, k = map(int, input().split(" "))
if k >= n:
print(0)
exit(0)
print(fact(n - 1) // fact(k) // fact(n - k - 1) * m * (m - 1) ** k % 998244353) | FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split())
if k == 0:
print(m)
elif m == 1:
print(0)
else:
total1 = m * pow(m - 1, k) % 998244353
total2 = 1
for i in range(1, k + 1):
total2 = total2 * (n - i)
for i in range(1, k + 1):
total2 = total2 // i
t = total1 * total2 % 998244353
print(t) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | import sys
sys.setrecursionlimit(3000)
def fac(n):
return 1 if n < 2 else n * fac(n - 1)
def choose(n, k):
return fac(n) // (fac(k) * fac(n - k))
n, m, k = (int(s) for s in input().split(" "))
print(choose(n - 1, k) * m * (m - 1) ** k % 998244353) | IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN VAR NUMBER NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | mod = 998244353
def nCrModp(n, r):
C = [(0) for i in range(r + 1)]
C[0] = 1
for i in range(1, n + 1):
for j in range(min(i, r), 0, -1):
C[j] = (C[j] + C[j - 1]) % mod
return C[r]
n, m, k = map(int, input().split())
if m == 1 and k > 0:
print(0)
else:
ans = m
ans = ans % mod * (pow(m - 1, k, mod) % mod) % mod
val1 = nCrModp(n - 1, k)
ans = ans % mod * (val1 % mod) % mod
print(ans) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | Z = 998244353
def ncr(n, r):
num = den = 1
for i in range(r):
num = num * (n - i) % Z
den = den * (i + 1) % Z
return num * pow(den, Z - 2, Z) % Z
n, m, k = list(map(int, input().split()))
print(ncr(n - 1, k) * m * pow(m - 1, k, Z) % Z) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | from sys import setrecursionlimit
setrecursionlimit(2500)
d = dict()
LIMITE = 998244353
def rec(total, cores, dif):
s = "%d %d" % (total, dif)
if dif >= total:
d[s] = 0
return 0
if total == 1:
d[s] = cores
return cores
if s in d:
return d[s]
if dif == 0:
d[s] = cores
return cores
sim_dif = (cores - 1) * rec(total - 1, cores, dif - 1) % LIMITE
nao_dif = rec(total - 1, cores, dif)
x = (sim_dif + nao_dif) % LIMITE
d[s] = x
return x
n, m, k = [int(x) for x in input().split(" ")]
for i in range(1, n):
rec(i, m, i - 1)
print(rec(n, m, k)) | EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP STRING VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR IF VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def inv(x):
return pow(x, MOD - 2, MOD)
def C(n, r):
return fact[n] * inv(fact[r]) * inv(fact[n - r])
n, m, k = map(int, input().split())
MOD = 998244353
fact = [1]
for i in range(1, 2100):
fact.append(fact[-1] * i % MOD)
ans = C(n - 1, k) * m * pow(m - 1, k, MOD)
ans %= MOD
print(ans) | FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | import sys
input = sys.stdin.readline
mod = 998244353
n, m, k = list(map(int, input().split()))
N = n - 1
INV = [None] * (n + 1)
for i in range(1, n + 1):
INV[i] = pow(i, mod - 2, mod)
Combi = [None] * (N + 1)
Combi[0] = 1
for i in range(1, N + 1):
Combi[i] = Combi[i - 1] * (N - i + 1) * INV[i] % mod
ANS = Combi[k] % mod * m % mod * pow(m - 1, k, mod) % mod
print(ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def fact(n):
q = 1
for i in range(1, n + 1):
q *= i
return q
n, m, k = input().split()
n = int(n)
m = int(m)
k = int(k)
com = fact(n - 1) // (fact(k) * fact(n - 1 - k))
dd = m * (m - 1) ** k
print(com * dd % 998244353) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | dp = [[(0) for j in range(2001)] for i in range(2001)]
n, m, k = map(int, input().split())
for i in range(1, n + 1):
for j in range(k + 1):
if j == 0 and i == 1:
dp[i][j] = m
continue
if j <= i - 1:
dp[i][j] = (m - 1) * dp[i - 1][j - 1] + dp[i - 1][j]
dp[i][j] %= 998244353
print(dp[n][k]) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def fact(n):
ans = 1
for i in range(n, 0, -1):
ans *= i
return ans
def c_n_k(n, k):
return fact(n) // (fact(k) * fact(n - k))
n, m, k = map(int, input().split())
print(c_n_k(n - 1, k) * (m - 1) ** k * m % 998244353) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split())
mod = 998244353
t = 1
n -= 1
for i in range(n - k + 1, n + 1):
t *= i
for j in range(1, k + 1):
t //= j
for i in range(k):
t *= m - 1
t %= mod
print(m * t % mod) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | MOD = 998244353
n, m, k = input().split()
n = int(n)
m = int(m)
k = int(k)
def nCr(n, k):
C = [(0) for i in range(k + 1)]
C[0] = 1
for i in range(1, n + 1):
j = min(i, k)
while j > 0:
C[j] = C[j] + C[j - 1]
j -= 1
return C[k]
answer = m * nCr(n - 1, k) * (m - 1) ** k
print(answer % MOD) | ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = [int(x) for x in input().split()]
dp = [[(0) for _ in range(k + 1)] for _ in range(n)]
dp[0][0] = m
for n1 in range(1, n):
dp[n1][0] = m
for n1 in range(1, n):
for k1 in range(1, k + 1):
dp[n1][k1] = (dp[n1 - 1][k1] + dp[n1 - 1][k1 - 1] * (m - 1)) % 998244353
print(dp[n - 1][k]) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | mod = 998244353
n, m, k = map(int, input().strip().split())
dp = [(0) for i in range(k + 1)]
dp[0] = m
for i in range(1, n):
new_dp = list(dp)
for j in range(1, k + 1):
new_dp[j] = (new_dp[j] + dp[j - 1] * (m - 1)) % mod
dp = new_dp
print(dp[-1] % mod) | ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | H = list(map(int, input().split()))
n = H[0]
m = H[1]
k = H[2]
a = 1
b = 1
c = 1
for i in range(1, n):
a = a * i
for i in range(1, n - k):
b = b * i
for i in range(1, k + 1):
c = c * i
q = a // (b * c)
ans = q * m * (m - 1) ** k
print(ans % 998244353) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | def ncr(n, r, p):
num = den = 1
for i in range(r):
num = num * (n - i) % p
den = den * (i + 1) % p
return num * pow(den, p - 2, p) % p
n, m, k = map(int, input().split())
p = 998244353
t = 1
for i in range(k):
t *= m - 1
t %= p
print(ncr(n - 1, k, p) * m * t % p) | FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | m = list(map(int, input().split(" ")))
a = m[0]
b = m[1]
c = m[2]
ways = 1
def fact(n):
a = 1
for i in range(2, n + 1):
a *= i
return a
def p(r, n):
return fact(n) // fact(r)
def entekhab(r, n):
return p(r, n) // fact(n - r)
ways *= b
a -= 1
ways *= entekhab(c, a)
ways *= (b - 1) ** c
print(ways % 998244353) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | f = [[(0) for i in range(2001)] for j in range(2001)]
mod = 998244353
def colorful_bricks(n, m, k):
f[1][0] = m
for i in range(2, n + 1):
for j in range(i):
f[i][j] = f[i - 1][j]
if j > 0:
f[i][j] = (f[i][j] + f[i - 1][j - 1] * (m - 1)) % mod
return f[n][k]
[n, m, k] = map(int, input().split())
result = colorful_bricks(n, m, k)
print(result) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = [int(x) for x in input().split()]
mod = 998244353
def com(n, r, p=998244353):
C = [(0) for i in range(r + 1)]
C[0] = 1
for i in range(1, n + 1):
for j in range(min(i, r), 0, -1):
C[j] = (C[j] + C[j - 1]) % p
return C[r]
ans = com(n - 1, k) * m * pow(m - 1, k, mod) % mod
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = map(int, input().split())
mod = 998244353
f = [0] * 10**5
f[0] = f[1] = 1
for i in range(2, 10**5):
f[i] = f[i - 1] * i % mod
def comb(x, r):
return f[x] * pow(f[r], mod - 2, mod) * pow(f[x - r], mod - 2, mod) % mod
ans = comb(n - 1, k) * m * pow(m - 1, k, mod) % mod
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | import sys
sys.setrecursionlimit(10000)
D = {(1, 0): 1, (1, 1): 0}
n, m, k = [int(x) for x in input().split()]
def f(n, k):
if k == 0:
return m
if k >= n:
return 0
if (n, k) in D:
return D[n, k]
else:
ans = (f(n - 1, k) + (m - 1) * f(n - 1, k - 1)) % 998244353
D[n, k] = ans
return ans
print(f(n, k)) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | n, m, k = input().split()
n = int(n)
m = int(m)
k = int(k)
result = 1
k = k + 1
for i in range(k - 1):
result *= n - 1 - i
result //= 1 + i
result = result % 998244353
result *= m
for i in range(k - 1):
result *= m - 1
result = result % 998244353
if result < 0:
result += 998244353
print(result) | ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are $n$ bricks lined in a row on the ground. Chouti has got $m$ paint buckets of different colors at hand, so he painted each brick in one of those $m$ colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $k$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $998\,244\,353$.
-----Input-----
The first and only line contains three integers $n$, $m$ and $k$ ($1 \leq n,m \leq 2000, 0 \leq k \leq n-1$)Β β the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
-----Output-----
Print one integerΒ β the number of ways to color bricks modulo $998\,244\,353$.
-----Examples-----
Input
3 3 0
Output
3
Input
3 2 1
Output
4
-----Note-----
In the first example, since $k=0$, the color of every brick should be the same, so there will be exactly $m=3$ ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $4$ possible colorings. [Image] | mod = 998244353
def comb(n, r):
facn = 1
for i in range(1, n + 1):
facn = facn * i
facr = 1
for i in range(1, r + 1):
facr *= i
facnr = 1
for i in range(1, n - r + 1):
facnr *= i
res = facn // (facnr * facr)
res = res % mod
return res
nmk = list(map(int, input().split()))
n = nmk[0]
m = nmk[1]
k = nmk[2]
res = comb(n - 1, k) * m * (m - 1) ** k % mod
print(int(res)) | ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.