0%

cf-593-div2

A:Stones

้ข˜็›ฎ๏ผš

Alice is playing with some stones.

Now there are three numbered heaps of stones. The first of them contains ๐‘Ž stones, the second of them contains ๐‘ stones and the third of them contains ๐‘ stones.

Each time she can do one of two operations:

take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones);

take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones).
She wants to get the maximum number of stones, but she doesnโ€™t know what to do. Initially, she has 0 stones. Can you help her?

Input

The first line contains one integer ๐‘ก (1โ‰ค๐‘กโ‰ค100) โ€” the number of test cases. Next ๐‘ก lines describe test cases in the following format:

Line contains three non-negative integers ๐‘Ž, ๐‘ and ๐‘, separated by spaces (0โ‰ค๐‘Ž,๐‘,๐‘โ‰ค100) โ€” the number of stones in the first, the second and the third heap, respectively.

In hacks it is allowed to use only one test case in the input, so ๐‘ก=1 should be satisfied.

Output

Print ๐‘ก lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer โ€” the maximum possible number of stones that Alice can take after making some operations.

Example

input

1
2
3
4
3
3 4 5
1 0 5
5 3 2

output

1
2
3
9
0
6

Note

For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is 9. It is impossible to make some operations to take more than 9 stones, so the answer is 9.

้ข˜่งฃ๏ผš

bไธŽc่ƒฝ่ฟ›่กŒๅคšๅฐ‘ๆ“ไฝœๅฐฑๅ–ๅคšๅฐ‘๏ผŒ็„ถๅŽๅ‰ฉไธ‹็š„bไธŽa็œ‹่ƒฝๅคŸๅ–ๅคšๅฐ‘๏ผŒ็„ถๅŽๅ–ใ€‚

ไปฃ็ ๏ผš

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while(t--){
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
int ans = 0;
if(b < c/2){
ans = b * 3;
}
else{
ans += c/2 * 3;
b -= c/2;
if(a < b/2)
ans += a * 3;
else
{
ans += b/2 *3;
}

}
printf("%d\n", ans);
}
return 0;
}

B๏ผšAlice and the List of Presents

้ข˜็›ฎ๏ผš

Alice got many presents these days. So she decided to pack them into boxes and send them to her friends.

There are ๐‘› kinds of presents. Presents of one kind are identical (i.e. there is no way to distinguish two gifts of the same kind). Presents of different kinds are different (i.e. that is, two gifts of different kinds are distinguishable). The number of presents of each kind, that Alice has is very big, so we can consider Alice has an infinite number of gifts of each kind.

Also, there are ๐‘š boxes. All of them are for different people, so they are pairwise distinct (consider that the names of ๐‘š friends are written on the boxes). For example, putting the first kind of present into the first box but not into the second box, is different from putting the first kind of present into the second box but not into the first box.

Alice wants to pack presents with the following rules:

She wonโ€™t pack more than one present of each kind into the same box, so each box should contain presents of different kinds (i.e. each box contains a subset of ๐‘› kinds, empty boxes are allowed);
For each kind at least one present should be packed into some box.
Now Alice wants to know how many different ways to pack the presents exists. Please, help her and calculate this number. Since the answer can be huge, output it by modulo 109+7.

See examples and their notes for clarification.

Input

The first line contains two integers ๐‘› and ๐‘š, separated by spaces (1โ‰ค๐‘›,๐‘šโ‰ค109) โ€” the number of kinds of presents and the number of boxes that Alice has.

Output

Print one integer โ€” the number of ways to pack the presents with Aliceโ€™s rules, calculated by modulo 109+7

Examples

input

1
1 3

output

1
7

input

1
2 2

output

1
9

Note

In the first example, there are seven ways to pack presents:
{1}{}{}
{}{1}{}
{}{}{1}
{1}{1}{}
{}{1}{1}
{1}{}{1}
{1}{1}{1}
In the second example there are nine ways to pack presents:
{}{1,2}
{1}{2}
{1}{1,2}
{2}{1}
{2}{1,2}
{1,2}{}
{1,2}{1}
{1,2}{2}
{1,2}{1,2}
For example, the way {2}{2} is wrong, because presents of the first kind should be used in the least one box.

้ข˜่งฃ๏ผš

pow(pow(2,m)-1),n) + ๅฟซ้€Ÿๅน‚

ไปฃ็ ๏ผš

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
const long long mod = 1e9+7;
long long mi(long long x, long long z)
{
long long ans = 1;
long long now = x;
while(z)
{
if(z & 1)
ans = (ans * now) %mod;
z >>= 1;
now = (now * now) % mod;
}
return ans;
}
int main()
{
long long n,m;
scanf("%lld%lld", &n, &m);
long long now = (mi(2,m) - 1 + mod) % mod;
long long ans = (mi(now,n)) % mod;
printf("%lld\n", ans);
return 0;
}

C:Labs

้ข˜็›ฎ๏ผš

In order to do some research, ๐‘›2 labs are built on different heights of a mountain. Letโ€™s enumerate them with integers from 1 to ๐‘›2, such that the lab with the number 1 is at the lowest place, the lab with the number 2 is at the second-lowest place, โ€ฆ, the lab with the number ๐‘›2 is at the highest place.

To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number ๐‘ข to the lab with the number ๐‘ฃ if ๐‘ข>๐‘ฃ.

Now the labs need to be divided into ๐‘› groups, each group should contain exactly ๐‘› labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group ๐ด to a group ๐ต is equal to the number of pairs of labs (๐‘ข,๐‘ฃ) such that the lab with the number ๐‘ข is from the group ๐ด, the lab with the number ๐‘ฃ is from the group ๐ต and ๐‘ข>๐‘ฃ. Letโ€™s denote this value as ๐‘“(๐ด,๐ต) (i.e. ๐‘“(๐ด,๐ต) is the sum of units of water that can be sent from a group ๐ด to a group ๐ต).

For example, if ๐‘›=3 and there are 3 groups ๐‘‹, ๐‘Œ and ๐‘: ๐‘‹={1,5,6},๐‘Œ={2,4,9} and ๐‘={3,7,8}. In this case, the values of ๐‘“ are equal to:

๐‘“(๐‘‹,๐‘Œ)=4 because of 5โ†’2, 5โ†’4, 6โ†’2, 6โ†’4,
๐‘“(๐‘‹,๐‘)=2 because of 5โ†’3, 6โ†’3,
๐‘“(๐‘Œ,๐‘‹)=5 because of 2โ†’1, 4โ†’1, 9โ†’1, 9โ†’5, 9โ†’6,
๐‘“(๐‘Œ,๐‘)=4 because of 4โ†’3, 9โ†’3, 9โ†’7, 9โ†’8,
๐‘“(๐‘,๐‘‹)=7 because of 3โ†’1, 7โ†’1, 7โ†’5, 7โ†’6, 8โ†’1, 8โ†’5, 8โ†’6,
๐‘“(๐‘,๐‘Œ)=5 because of 3โ†’2, 7โ†’2, 7โ†’4, 8โ†’2, 8โ†’4.
Please, divide labs into ๐‘› groups with size ๐‘›, such that the value min๐‘“(๐ด,๐ต) over all possible pairs of groups ๐ด and ๐ต (๐ดโ‰ ๐ต) is maximal.

In other words, divide labs into ๐‘› groups with size ๐‘›, such that minimum number of the sum of units of water that can be transported from a group ๐ด to a group ๐ต for every pair of different groups ๐ด and ๐ต (๐ดโ‰ ๐ต) as big as possible.

Note, that the example above doesnโ€™t demonstrate an optimal division, but it demonstrates how to calculate the values ๐‘“ for some division.

If there are many optimal divisions, you can find any.

Input

The only line contains one number ๐‘› (2โ‰ค๐‘›โ‰ค300).

Output

Output ๐‘› lines:

In the ๐‘–-th line print ๐‘› numbers, the numbers of labs of the ๐‘–-th group, in any order you want.

If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any.

Example

input

1
3

output

1
2
3
2 8 5
9 3 4
7 6 1

Note

In the first test we can divide 9 labs into groups {2,8,5},{9,3,4},{7,6,1}.

From the first group to the second group we can transport 4 units of water (8โ†’3,8โ†’4,5โ†’3,5โ†’4).

From the first group to the third group we can transport 5 units of water (2โ†’1,8โ†’7,8โ†’6,8โ†’1,5โ†’1).

From the second group to the first group we can transport 5 units of water (9โ†’2,9โ†’8,9โ†’5,3โ†’2,4โ†’2).

From the second group to the third group we can transport 5 units of water (9โ†’7,9โ†’6,9โ†’1,3โ†’1,4โ†’1).

From the third group to the first group we can transport 4 units of water (7โ†’2,7โ†’5,6โ†’2,6โ†’5).

From the third group to the second group we can transport 4 units of water (7โ†’3,7โ†’4,6โ†’3,6โ†’4).

The minimal number of the sum of units of water, that can be transported from one group to another is equal to 4. It can be proved, that it is impossible to make a better division.

้ข˜่งฃ๏ผš

ๆฏๆฌกๅ–ๆ•ฐ็ป„็š„ไธค่พนไปฅๅŠไธญ้—ดไฝ็ฝฎใ€‚

ไปฃ็ ๏ผš

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
if(n % 2 == 0){
int p = n*n;
for(int i = 0; i < n; i++){
for(int j = 1; j <= n/2; j++)
printf("%d %d ", i * (n/2) + j,p + 1 -(i * (n/2) + j));
printf("\n");
}
}
else{
int l = 0,r = 0,s = 0;
int mid = (1 + n*n)/2;
int p = n * n;
for(int i = 0; i < n; i++){
for(int j = 1; j <= n/2; j++)
printf("%d %d ", i * (n/2) + j,p + 1 -(i * (n/2) + j));
if(s == 0)
{
printf("%d\n",mid);
s = 1;
}
else if(l == r){
l++;
printf("%d\n",mid - l);
}
else{
r++;
printf("%d\n",mid + r);
}
}
}
return 0;
}