The following problem is well-known: given integers n andm, calculate
where 2n = 2·2·...·2 (n factors), and denotes the remainder of division ofx by y.
You are asked to solve the "reverse" problem. Given integers n and m, calculate
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.
Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int INF = 1e8;
LL mi( LL a , LL b ){
LL ans = 1;
while( b-- ){
ans *= 2;
if( ans > INF ) return -1;
}
return ans ;
}
int main(){
LL n , m;
cin >> n >> m;
LL tmp = mi( 2 , n );
if( tmp == -1 ){
cout << m << endl;
}else{
cout << m % tmp << endl;
}
return 0;
}
B:
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertexu is called a child of vertex v and vertex v is called a parent of vertexu if there exists a directed edge from v to u. A vertex is called aleaf 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 least3 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 nextn - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of thei + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least2 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
Code:
#include <bits/stdc++.h>
using namespace std;
const int AX = 1e3+66;
std::vector<int> vec[AX];
int fa[AX];
int main(){
int n ;
cin >> n ;
for( int i = 2 ; i <= n ; i ++ ){
cin >> fa[i] ;
vec[fa[i]].push_back(i);
}
for( int i = 1 ; i <= n ; i++ ){
int tmp = vec[i].size();
if( tmp > 0 && tmp < 3 ){
cout << "NO" << endl; return 0;
}else if( tmp >= 3 ){
int lim = tmp;
int u ;
for( int j = 0 ; j < tmp ; j ++ ){
u = vec[i][j];
if( vec[u].size() != 0 ){
lim --;
}
}
if( lim < 3 ){
cout << "NO" << endl;
return 0;
}
}
}
cout << "YES" << endl;
return 0;
}
C:
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 typei has volume 2i - 1 liters and costsci 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 andL (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 leastL 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.
思路:先把给定的不同升数的花费最优化(每种升数的水正好是2倍关系),然后按位运算得到L升水最少的花费
Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int AX = 32;
LL a[AX];
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n , L ;
cin >> n >> L ;
for( int i = 0 ; i < n ; i++ ){
cin >> a[i];
}
for( int i = 1 ; i < n ; i++ ){
a[i] = min( 2 * a[i-1] , a[i] ); //如果2倍左边的水的钱少于给定的,就替换给定的值
}
for( int i = n - 2 ; i >= 0 ; i-- ){
a[i] = min( a[i] , a[i+1] ); //如果能用更少的钱买更多的水,则替换
}
for( int i = n ; i < 31 ; i++ ){
a[i] = a[i-1] * 2 ; //L的范围是1e9 ,处理到 1 << 31 即可
}
LL res = 0;
for( int i = 0 ; i < 31 ; i++ ){
if( res > a[i] ) res = a[i]; //如果 1 << i 花的价钱少于买更少的水花的钱,则更新(少花钱,多买水)
if( L & ( 1 << i ) ) res += a[i];
}
cout << res << endl;
return 0;
}
You are preparing for an exam on scheduling theory. The exam will last for exactlyT milliseconds and will consist of n problems. You can either solve problem i in exactlyti 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 integerai to every problemi meaning that the problem i can bring you a point to the final score only in case you have solved no more thanai problems overall (including problemi).
Formally, suppose you solve problems p1, p2, ..., pk during the exam. Then, your final score s will be equal to the number of values ofj between 1 and k such thatk ≤ 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 andT (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 integersai andti (1 ≤ ai ≤ n;1 ≤ ti ≤ 104). The problems are numbered from 1 ton.
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 integersp1, 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 spend80 + 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.
思路:看了网上的代码才感觉思路简洁多么不容易,用优先队列。
从i=n递减枚举题数,每次加入i题的时间,如果超过了时间,就减去耗时最多的。队列的题数等于当前题数并且时间满足,输出结束。
Code:
#include <bits/stdc++.h>
using namespace std;
const int AX = 2e5+666;
vector<pair<int,int> > vec[AX];
priority_queue<pair<int,int> > q;
int main(){
int n , T;
cin >> n >> T;
int x ,y;
for( int i = 1 ; i <= n ; i++ ){
cin >> x >> y ;
vec[x].push_back(make_pair(y,i));
}
int ans = 0;
for( int i = n ; i >= 0 ; i-- ){
int size = vec[i].size();
for( int j = 0 ; j < size ; j++ ){
pair<int,int>u = vec[i][j];
q.push(u);
ans += u.first;
}
while( q.size() > i ){
ans -= q.top().first;
q.pop();
}
if( q.size() == i && ans <= T ){
cout << i << endl << i << endl;
while( q.size() ){
cout << q.top().second << ' ';
q.pop();
}
cout << endl;
return 0;
}
}
return 0;
}