比赛链接:http://codeforces.com/contest/913
The following problem is well-known: given integers n and m, calculate
![](https://i-blog.csdnimg.cn/blog_migrate/954c5ff1f63fbf32b3e7103a90e4cb9b.png)
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
![](https://i-blog.csdnimg.cn/blog_migrate/bd145ba851babbbc8b0ed8f988243460.png)
The first line contains a single integer n (1 ≤ n ≤ 108).
The second line contains a single integer m (1 ≤ m ≤ 108).
Output a single integer — the value of .
4 42
10
1 58
0
98765432 23456789
23456789
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.
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int n, m;
int main() {
scanf("%d %d", &n, &m);
if (n >= 31) {
printf("%d\n", m);
} else {
int ans = 1;
for (int i = 1; i <= n; i++) {
ans *= 2;
}
printf("%d\n", m % ans);
}
}
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.
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.
Print "Yes" if the tree is a spruce and "No" otherwise.
4 1 1 1
Yes
7 1 1 1 2 2 2
No
8 1 1 1 1 3 3 3
Yes
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:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1005;
int n, ind[MAX];
int cnt, head[MAX];
struct EDGE{
int to, nxt;
}e[MAX];
void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt ++;
}
int main() {
scanf("%d", &n);
int v;
cnt = 0;
memset(head, -1, sizeof(head));
for (int u = 2; u <= n; u++) {
scanf("%d", &v);
add(v, u);
ind[v] ++;
}
bool flag = true;
for (int u = 1; u <= n; u++) {
if (ind[u] > 0 && ind[u] < 3) {
flag = false;
}
if (ind[u] >= 3) {
int tmp = ind[u];
for (int i = head[u]; i != -1; i = e[i].nxt) {
v = e[i].to;
if (ind[v] >= 3) {
tmp --;
}
}
if (tmp < 3) {
flag = false;
break;
}
}
}
printf("%s\n", flag ? "Yes" : "No");
}
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?
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 a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.
4 12 20 30 70 90
150
4 3 10000 1000 100 10
10
4 3 10 100 1000 10000
30
5 787787787 123456789 234567890 345678901 456789012 987654321
44981600785557577
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.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;
int const MAX = 50;
ll c[MAX], L;
int n;
int main() {
cin >> n >> L;
for (int i = 0; i < n; i++) {
cin >> c[i];
}
for (int i = 1; i <= 30; i++) {
c[i] = min(c[i], c[i - 1] << 1);
if (i >= n) {
c[i] = c[i - 1] << 1;
}
}
ll ans = 0;
for (int i = 0; i <= 30; i++) {
ans = ((1 << i) & L) ? ans + c[i] : min(ans, c[i]);
}
cout << ans << endl;
}
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 jbetween 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.
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.
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.
5 300 3 100 4 150 4 80 2 90 2 300
2 3 3 1 4
2 100 1 787 2 788
0 0
2 100 2 42 2 58
2 2 1 2
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.
题目大意:n个问题,总时间为T,第i个问题有一个ai值和完成需要的时间ti,如果完成的总题数为m,则满足ai>=m的题目得1分,现在要求一个题集,使得总得分最高
题目分析:比较明显的二分题,因为T是限制点,按ti从小到大排序,二分答案即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;
int const MAX = 2e5 + 5;
int n, T;
struct DATA {
int a, t, id;
}d[MAX];
bool cmp(DATA d1, DATA d2) {
return d1.t < d2.t;
}
bool judge(int x) {
int cnt = 0, sumt = 0;
for (int i = 0; i < n && cnt < x; i++) {
if (d[i].a >= x) {
cnt ++;
sumt += d[i].t;
}
}
//printf("cnt = %d sumt = %d\n", cnt, sumt);
return (cnt == x && sumt <= T);
}
int main() {
scanf("%d %d", &n, &T);
for (int i = 0; i < n; i++) {
scanf("%d %d", &d[i].a, &d[i].t);
d[i].id = i + 1;
}
sort(d, d + n, cmp);
int l = 0, r = n, mid, num = 0;
while (l <= r) {
mid = (l + r) >> 1;
//printf("mid = %d\n", mid);
if (judge(mid)) {
num = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
printf("%d\n%d\n", num, num);
int cnt = 0;
for (int i = 0; i < n && cnt < num; i++) {
if (d[i].a >= num) {
cnt ++;
if (cnt < num) {
printf("%d ", d[i].id);
} else {
printf("%d", d[i].id);
}
}
}
printf("\n");
}