Codeforces Round #326 (Div. 1) 解题报告

来源:互联网 发布:调查报告数据分析模板 编辑:程序博客网 时间:2024/05/17 22:54
A. Duff and Weight Lifting
题意:有n个哑铃,每个重量为2的ai次方,每次你可以丢掉总和为2的x次方的哑铃。问,最少需要丢几次,才能把全部的哑铃丢光。
做法:考虑二进制进位的条件,贪心就好。


B. Duff in Beach
题意:有两个串a跟b,长度分别n跟l,b是由a不断循环得到,也就是bi=a(i%n)。在b中从左开始,将长度为n的子串分成一个块,要求每个块最多选一个数字,且若在块i中选择了数字,就必须在块i+1中选择一个数字,最后要满足这个得到的序列是非递减的。问,有多少种选择方法。
做法:dp[i][j],若已第i个块的第j个数字为结尾,最多能形成多少种合法情况。dp预处理后,很容易就可以算出答案。


C. Duff in the Army
题意:给出一棵树,每个结点里有若干个数字,有多次询问,从结点u到结点v的简单路径上,若总共有x个数字,那么k=min(x,a),输出这些数字中顺序排序后,前k个数字。
做法:由于a最大只有10,可倍增,可树链剖分。


剩下三个题都是高级数据结构的题,没太大兴趣。这次的题,收获不大。


A. Duff and Weight Lifting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.

Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.

Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.

Input

The first line of input contains integer n (1 ≤ n ≤ 106), the number of weights.

The second line contains n integers w1, ..., wn separated by spaces (0 ≤ wi ≤ 106 for each 1 ≤ i ≤ n), the powers of two forming the weights values.

Output

Print the minimum number of steps in a single line.

Sample test(s)
input
51 1 2 3 3
output
2
input
40 1 2 3
output
4
Note

In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.

In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.


B. Duff in Beach
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array, a0, ..., an - 1 that b can be build from a with formula: bi = ai mod n where a mod b denoted the remainder of dividing a by b.

Duff is so curious, she wants to know the number of subsequences of b like bi1, bi2, ..., bix (0 ≤ i1 < i2 < ... < ix < l), such that:

  • 1 ≤ x ≤ k
  • For each 1 ≤ j ≤ x - 1
  • For each 1 ≤ j ≤ x - 1bij ≤ bij + 1. i.e this subsequence is non-decreasing.

Since this number can be very large, she want to know it modulo 109 + 7.

Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number.

Input

The first line of input contains three integers, n, l and k (1 ≤ n, kn × k ≤ 106 and 1 ≤ l ≤ 1018).

The second line contains n space separated integers, a0, a1, ..., an - 1 (1 ≤ ai ≤ 109 for each 0 ≤ i ≤ n - 1).

Output

Print the answer modulo 1 000 000 007 in one line.

Sample test(s)
input
3 5 35 9 1
output
10
input
5 10 31 2 3 4 5
output
25
Note

In the first sample case, . So all such sequences are: , and .


C. Duff in the Army
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Recently Duff has been a soldier in the army. Malek is her commander.

Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.

There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is iand he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city.

Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.

To answer a query:

Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs are p1, p2, ..., px in increasing order.

If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).

Duff is very busy at the moment, so she asked you to help her and answer the queries.

Input

The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105).

The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ nv ≠ u).

Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m).

Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).

Output

For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.

Sample test(s)
input
5 4 51 31 21 44 52 1 4 34 5 61 5 25 5 102 3 35 3 1
output
1 32 2 303 1 2 41 2
Note

Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):



0 0
原创粉丝点击