Codeforces Round Hello 2018 (4/8)

maksyuki 发表于 比赛 分类,标签:
0

A Modular Exponentiation
The following problem is well-known: given integers n and m, calculate,
where 2n = 2·2·...·2 (n factors), and denotes the remainder of division of x by y.
You are asked to solve the "reverse" problem. Given integers n and m, calculate.

Input
The first line contains a single integer n (1 ≤ n ≤ 108).
The second line contains a single integer m (1 ≤ m ≤ 108).
Output
Output a single integer — the value of.
Examples
input
4
42
output
10
input
1
58
output
0
input
98765432
23456789
output
23456789
Note
In the first example, the remainder of division of 42 by 24 = 16 is equal to 10.
In the second example, 58 is divisible by 21 = 2 without remainder, and the answer is 0.

 

题目类型:简单模运算

算法分析:当m<2^n时输出m,即当n>=27时,直接输出m,否则输出m%(2^n)

 

B Christmas Spruce
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.

Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.

The definition of a rooted tree can be found here.

Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).

Vertex 1 is the root. It's guaranteed that the root has at least 2 children.

Output
Print "Yes" if the tree is a spruce and "No" otherwise.

Examples
input
4
1
1
1
output
Yes
input
7
1
1
1
2
2
2
output
No
input
8
1
1
1
1
3
3
3
output
Yes

 

题目类型:简单枚举

算法分析:输入所有节点构建完树之后,查找是否存在小于3个叶子节点的非叶节点,复杂度O(n^2)

 

C Party Lemonade
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.

Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci roubles. The number of bottles of each type in the store can be considered infinite.

You want to buy at least L liters of lemonade. How many roubles do you have to spend?

Input
The first line contains two integers n and L (1 ≤ n ≤ 30; 1 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.

The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.

Output
Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.

Examples
input
4 12
20 30 70 90
output
150
input
4 3
10000 1000 100 10
output
10
input
4 3
10 100 1000 10000
output
30
input
5 787787787
123456789 234567890 345678901 456789012 987654321
output
44981600785557577

Note
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.

In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.

In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.

 

题目类型:贪心+二进制枚举

算法分析:首先若满足2*c[i]<=c[i+1]的话,则选择c[i]至少不会比c[i+1]更差,先使用c[i+1]=min(c[i+1], 2 * c[i])对c[i]排序,以便后续处理,这样c[i]满足关系2*c[i]>=c[i+1],即若第i(i<n)个物品选择超过1个,则由前面可知超出的不如选择第i+1个物品划算,因为这个关系具有传递性,可知i<n时每个物品最多选一个,i=n可以选多个。由于第i个物品的容积是2^i(以0为起始点),所以若组成容积L,其每个二进制表达式的第i位(i<n)即表示第i个物品选还是不选,若组成不小于L的容积V,则易知满足V与L的二进制表示的最高不同位上V的为1,L的为0,并且V之后的低位都为0,因为只要不同的最高位上V为1,则一定有V>L,而低位为0表示不选之后的容积,此时才能保证得到是V比L大的最小花费。比如L=(11010)2,V=(11100)2,V'=(11101)2,易知V>L,且V'>L,但由前面分析可知V的花费要小于V'的。此时可以从高到低枚举L的所有位i,并不断生成第i位上为1,低位均为0的V,并比较两者花费,复杂度O(n)

 

D Too Easy Problems
You are preparing for an exam on scheduling theory. The exam will last for exactly T milliseconds and will consist of n problems. You can either solve problem i in exactly ti milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either.

Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer ai to every problem i meaning that the problem i can bring you a point to the final score only in case you have solved no more than ai problems overall (including problem i).

Formally, suppose you solve problems p1, p2, ..., pk during the exam. Then, your final score s will be equal to the number of values of j between 1 and k such that k ≤ apj.

You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do.

Input
The first line contains two integers n and T (1 ≤ n ≤ 2·105; 1 ≤ T ≤ 109) — the number of problems in the exam and the length of the exam in milliseconds, respectively.

Each of the next n lines contains two integers ai and ti (1 ≤ ai ≤ n; 1 ≤ ti ≤ 104). The problems are numbered from 1 to n.

Output
In the first line, output a single integer s — your maximum possible final score.

In the second line, output a single integer k (0 ≤ k ≤ n) — the number of problems you should solve.

In the third line, output k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the indexes of problems you should solve, in any order.

If there are several optimal sets of problems, you may output any of them.

Examples
input
5 300
3 100
4 150
4 80
2 90
2 300
output
2
3
3 1 4
input
2 100
1 787
2 788
output
0
0

input
2 100
2 42
2 58
output
2
2
1 2
Note
In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points.

In the second example, the length of the exam is catastrophically not enough to solve even a single problem.

In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile.

 

题目类型:贪心+二分枚举

算法分析:贪心选择只会对得分有帮助的problem,先在[0,n]内二分枚举k的值,然后找到满足a[i]大于等于k的problem,若其个数大于等于k并从小到大累加其时间后不超过T,则k是一个合理解,否则不合理,复杂度O(nlog^2n)

 

算法分析:另一个解法类似于尺取法,从k=n开始递减枚举,先将a[i]为k的都压入到集合中,即保证集合中的元素都是大于等于当前k的,若集合个数大于k则优先删除花费时间多的,复杂度O(nlogn)