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.
题目类型:简单模运算
算法分析:当m<2^n时输出m,即当n>=27时,直接输出m,否则输出m%(2^n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/************************************************** filename :A.cpp author :maksyuki created time :2018/1/8 22:22:09 last modified :2018/1/9 20:12:05 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int main() { #ifdef LOCAL CFF; //CFO; #endif int n, m; while(scanf("%d %d", &n, &m) != EOF) { printf("%d\n", n >= 27 ? m: m % (1 << n)); } return 0; } |
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
题目类型:简单枚举
算法分析:输入所有节点构建完树之后,查找是否存在小于3个叶子节点的非叶节点,复杂度O(n^2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
/************************************************** filename :B.cpp author :maksyuki created time :2018/1/8 23:11:28 last modified :2018/1/8 23:37:18 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e4 + 66; vector<int> g[maxn]; int main() { #ifdef LOCAL CFF; //CFO; #endif int n; while(scanf("%d", &n) != EOF) { for(int i = 0; i < maxn; i++) g[i].clear(); int tt; for(int i = 2; i <= n; i++) { scanf("%d", &tt); g[tt].push_back(i); } bool is_valid = true; for(int i = 1; i <= n; i++) { int len = g[i].size(); if(!len) continue; if(len < 3) { is_valid = false; break; } int cnt = 0; for(int j = 0; j < len; j++) { int llen = g[g[i][j]].size(); if(!llen) cnt++; } if(cnt < 3) { is_valid = false; break; } } if(is_valid) puts("Yes"); else puts("No"); } return 0; } |
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.
题目类型:贪心+二进制枚举
算法分析:首先若满足2*c[i]<=c[i+1]的话,则选择c[i]至少不会比c[i+1]更差,先使用c[i+1]=min(c[i+1], 2 * c[i])对c[i]排序,以便后续处理,这样c[i]满足关系2*c[i]>=c[i+1],即若第i(i<n)个物品选择超过1个,则由前面可知超出的不如选择第i+1个物品划算,因为这个关系具有传递性,可知i<n时每个物品最多选一个,i=n可以选多个。由于第i个物品的容积是2^i(以0为起始点),所以若组成容积L,其每个二进制表达式的第i位(i<n)即表示第i个物品选还是不选,若组成不小于L的容积V,则易知满足V与L的二进制表示的最高不同位上V的为1,L的为0,并且V之后的低位都为0,因为只要不同的最高位上V为1,则一定有V>L,而低位为0表示不选之后的容积,此时才能保证得到是V比L大的最小花费。比如L=(11010)2,V=(11100)2,V'=(11101)2,易知V>L,且V'>L,但由前面分析可知V的花费要小于V'的。此时可以从高到低枚举L的所有位i,并不断生成第i位上为1,低位均为0的V,并比较两者花费,复杂度O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
/************************************************** filename :C.cpp author :maksyuki created time :2018/1/8 23:37:27 last modified :2018/1/10 9:58:31 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int c[66]; int main() { #ifdef LOCAL CFF; //CFO; #endif int n, L; while(cin >> n >> L) { for(int i = 0; i < n; i++) cin >> c[i]; for(int i = 0; i < n - 1; i++) c[i+1] = min(c[i+1], 2 * c[i]); LL ans = (LL) 4e18, sum = 0; LL ned; for(int i = n - 1; i >= 0; i--) { ned = L / (1 << i); L -= ned << i; sum += ned * c[i]; ans = min(ans, (LL)(L > 0) * c[i] + sum); } cout << ans << endl; } return 0; } |
D Too Easy Problems
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 j between 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.
Input
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.
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 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.
Examples
input
5 300
3 100
4 150
4 80
2 90
2 300
output
2
3
3 1 4
input
2 100
1 787
2 788
output
0
0
input
2 100
2 42
2 58
output
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.
题目类型:贪心+二分枚举
算法分析:贪心选择只会对得分有帮助的problem,先在[0,n]内二分枚举k的值,然后找到满足a[i]大于等于k的problem,若其个数大于等于k并从小到大累加其时间后不超过T,则k是一个合理解,否则不合理,复杂度O(nlog^2n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
/************************************************** filename :D.cpp author :maksyuki created time :2018/1/10 10:40:08 last modified :2018/1/10 12:01:58 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 2e5 + 66; int a[maxn], t[maxn]; int main() { #ifdef LOCAL CFF; //CFO; #endif int n, T; while(scanf("%d %d", &n, &T) != EOF) { for(int i = 1; i <= n; i++) scanf("%d %d", &a[i], &t[i]); vector<int> ans; int low = 0, high = n; while(low < high) { int mid = (low + high + 1) >> 1; vector<PII> tmp; for(int i = 1; i <= n; i++) if(a[i] >= mid) tmp.emplace_back(t[i], i); bool ok = false; int len = tmp.size(); if(len >= mid) { sort(tmp.begin(), tmp.end()); int sum = 0; for(int i = 0; i < mid; i++) sum += tmp[i].first; if(sum <= T) { ok = true; ans.resize(mid); for(int i = 0; i < mid; i++) ans[i] = tmp[i].second; } } if(ok) low = mid; else high = mid - 1; } int alen = ans.size(); printf("%d\n%d\n", alen, alen); for(int i = 0; i < alen; i++) { if(i) putchar(' '); printf("%d", ans[i]); } puts(""); } return 0; } |
算法分析:另一个解法类似于尺取法,从k=n开始递减枚举,先将a[i]为k的都压入到集合中,即保证集合中的元素都是大于等于当前k的,若集合个数大于k则优先删除花费时间多的,复杂度O(nlogn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
/************************************************** filename :D.cpp author :maksyuki created time :2018/1/10 10:40:08 last modified :2018/1/10 13:45:02 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 2e5 + 66; vector<int> ans; set<PII> dict; int main() { #ifdef LOCAL CFF; //CFO; #endif int n, T, a, t; while(scanf("%d %d", &n, &T) != EOF) { vector<vector<PII>> val(n+66); for(int i = 1; i <= n; i++) { scanf("%d %d", &a, &t); val[a].emplace_back(t, i); } int sum = 0; dict.clear(); ans.clear(); for(int k = n; k > 0; k--) { int len = val[k].size(); for(int i = 0; i < len; i++) { sum += val[k][i].first; dict.insert(val[k][i]); } while((int) dict.size() > k) { sum -= (--dict.end())->first; dict.erase(--dict.end()); } if((int) dict.size() == k && sum <= T) { ans.resize(0); for(set<PII>:: iterator it = dict.begin(); it != dict.end(); it++) ans.push_back(it->second); break; } } int alen = ans.size(); printf("%d\n%d\n", alen, alen); for(int i = 0; i < alen; i++) { if(i) putchar(' '); printf("%d", ans[i]); } puts(""); } return 0; } |
- uva1592:下一篇 »