Hello 2018 ABC

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.

 
 1 #include <iostream>
 2 #include <stdio.h>
 3 #define ll long long
 4 using namespace std;
 5 const int N = 1e5+10;
 6 int n, m;
 7 int main() {
 8     cin >> n >> m;
 9     if(n >= 30) cout << m << endl;
10     else {
11         int ans = 1;
12         for(int i = 1; i <= n; i ++) {
13             ans *= 2;
14         }
15         cout << m % ans << endl;
16     }
17     return 0;
18 }
 
 
 
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
Note

The first example:

The second example:

It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.

The third example:

 1 #include <iostream>
 2 #include <vector>
 3 #include <stdio.h>
 4 #define ll long long
 5 using namespace std;
 6 const int N = 1e3+10;
 7 int n, fa[N];
 8 vector<int> vs[N];
 9 bool ok(int x) {
10     int ans = vs[x].size();
11     if(ans == 0) return true;
12     ans = 0;
13     for(int i = 0; i < vs[x].size(); i ++) {
14         if(vs[vs[x][i]].size() == 0) ans++;
15     }
16     if(ans >= 3) return true;
17     else return false;
18 }
19 int main() {
20     int x;
21     cin >> n;
22     for(int i = 2; i <= n; i ++) {
23         cin >> x;
24         vs[x].push_back(i);
25     }
26     bool flag = true;
27     for(int i = 1; i <= n; i ++) {
28         if(!ok(i)) {
29             flag = false;
30             break;
31         }
32     }
33     if(flag) printf("Yes
");
34     else printf("No
");
35     return 0;
36 }
 
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.

 1 #include <iostream>
 2 #include <stdio.h>
 3 #define ll long long
 4 #include <algorithm>
 5 using namespace std;
 6 const int N = 1e5+10;
 7 struct Nod {
 8     ll c,l;
 9     double a;
10 }nod[33];
11 ll n, l, cost[33];
12 bool cmp(Nod a, Nod b) {
13     if(a.a != b.a) return a.a < b.a;
14     else return a.l > b.l;
15 }
16 int main() {
17     cin >> n >> l;
18     for(ll i = 1; i <= n; i ++) {
19         cin >> nod[i].c;
20         nod[i].l = (1<<(i-1));
21         nod[i].a = 1.0*nod[i].c/nod[i].l;
22     }
23     sort(nod+1,nod+1+n,cmp);
24     ll sum = 0, tmp;
25     for(ll i = 1; i <= n; i ++) {
26         ll tmp = l/nod[i].l;
27         l -= tmp*nod[i].l;
28         sum += tmp*nod[i].c;
29         if(l > 0) {
30             cost[i] = sum + nod[i].c;
31         } else {
32             cost[i] = sum;
33         }
34     }
35     for(int i = 1; i <= n; i ++) {
36         if(sum > cost[i]) {
37             sum = cost[i];
38         }
39     }
40     cout << sum << endl;
41     return 0;
42 }
原文地址:https://www.cnblogs.com/xingkongyihao/p/8250746.html