比赛链接:
http://codeforces.com/contest/913
A. Modular Exponentiation
1 second
256 megabytes
The following problem is well-known: given integers
n
and
m
, calculate
,
where
2
n
= 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
≤ 10
8
).
The second line contains a single integer
m
(
1 ≤
m
≤ 10
8
).
Output
Output a single integer — the value of
.
Examples
4 42
10
1 58
0
98765432 23456789
23456789
Note
In the first example, the remainder of division of 42 by
2
4
= 16
is equal to 10.
In the second example, 58 is divisible by
2
1
= 2
without remainder, and the answer is 0.
题目分析:2^n很快就超过n了
#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);
}
}
B. Christmas Spruce
1 second
256 megabytes
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
p
i
(
1 ≤
i
≤
n
- 1
) — the index of the parent of the
i
+ 1
-th vertex (
1 ≤
p
i
≤
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
4 1 1 1
Yes
7 1 1 1 2 2 2
No
8 1 1 1 1 3 3 3
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:
题目大意:判断一棵有根树是否满足每个非叶子节点的孩子中至少包含3个叶子节点
题目分析:记录点的出度
#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");
}
C. Party Lemonade
1 second
256 megabytes
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
2
i
- 1
liters and costs
c
i
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
≤ 10
9
) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.
The second line contains
n
integers
c
1
,
c
2
, …,
c
n
(
1 ≤
c
i
≤ 10
9
) — 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.
题目大意:n个物品,第i个物品体积为2^(i-1),花费为ci,求总体积至少为L的最少花费
题目分析:若c[i] > 2*c[i-1],那么要买2^(i-1)体积肯定是买两个c[i-1]更优,因此可以先预处理一下,考虑将L转为二进制,为1的位是必须买的,但是如果为0的位花费更少,其对应的体积显然又要比先前已买的总体积要大 => (Sn=2^n-1 < an = 2^(n+1-1)),因此可以直接买这个体积更大,花费更小的
#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;
}
D. Too Easy Problems
2 seconds
256 megabytes
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
t
i
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
a
i
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
a
i
problems overall (including problem
i
).
Formally, suppose you solve problems
p
1
,
p
2
, …,
p
k
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
≤
a
p
j
.
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·10
5
;
1 ≤
T
≤ 10
9
) — 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
a
i
and
t
i
(
1 ≤
a
i
≤
n
;
1 ≤
t
i
≤ 10
4
). 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
p
1
,
p
2
, …,
p
k
(
1 ≤
p
i
≤
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
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
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.
题目大意: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");
}