@Dmaxiya
2018-08-17T10:29:12.000000Z
字数 3195
阅读 1002
Codeforces
Contests 链接:Hello 2018
过题数:3
排名:1626/10360
给定 和 ,求 的值。
由于当 超过 时就已经大于所有 了,所以当 超过这个值时,就直接输出 ,否则输出 的值。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;
#define LL long long
LL n, m;
LL two[60];
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
two[0] = 1;
for(int i = 1; i < 60; ++i) {
two[i] = two[i - 1] * 2;
}
while(scanf("%I64d%I64d", &n, &m) != EOF) {
if(n >= 60) {
printf("%I64d\n", m);
continue;
}
printf("%I64d\n", m % two[n]);
}
return 0;
}
定义一种 个节点的“云杉”树,这种树上的所有非叶子节点,都至少有三个叶子节点,现在给定第 个节点的父节点 ,判断一棵树是否是“云杉”树。
一遍 dfs 判断,用个全局变量 flag 标记判断。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;
#define LL long long
const int maxn = 1000 + 100;
int n, u;
bool flag;
vector<int> G[maxn];
int num[maxn];
void dfs(int f, int x) {
int len = G[x].size();
if(len == 0) {
num[x] = 1;
return ;
}
for(int i = 0; i < len; ++i) {
int pos = G[x][i];
if(pos != f) {
dfs(x, pos);
}
if(num[pos] == 1) {
++num[x];
}
}
if(num[x] < 3) {
flag = false;
}
}
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
while(scanf("%d", &n) != EOF) {
flag = true;
memset(num, 0, sizeof(num));
for(int i = 1; i <= n; ++i) {
G[i].clear();
}
for(int i = 2; i <= n; ++i) {
scanf("%d", &u);
G[u].push_back(i);
}
dfs(1, 1);
if(flag) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
新年朋友们来开 了,开 就要买柠檬汽水,现在有 种柠檬汽水,编号为 到 ,每种柠檬汽水有无穷多瓶,第 种柠檬汽水的容量为 升,价格为 ,问,如果至少想要买 升柠檬汽水,求最少可以花多少钱。
用 记录当需要购买 升汽水时,最少需要花费的钱,然后从 开始记忆化搜索一遍。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;
#define LL long long
const int maxn = 200;
const LL INF = 1e18;
LL n, L;
LL ans;
LL num[maxn];
LL two[maxn];
double per[maxn];
map<LL, LL> mp;
LL dfs(LL left) {
if(mp.find(left) != mp.end()) {
return mp[left];
}
if(left <= 0) {
return 0;
}
LL Min = INF;
for(int i = 0; i < n; ++i) {
if(left > two[i]) {
LL tmp = dfs(left % two[i]);
mp[left % two[i]] = tmp;
// printf("mp[%I64d] = %I64d\n", left % two[i], tmp);
Min = min(Min, tmp + left / two[i] * num[i]);
} else {
Min = min(Min, num[i]);
}
}
mp[left] = Min;
// printf("\n");
return Min;
}
int main() {
#ifdef LOCAL
freopen("test.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
two[0] = 1;
for(int i = 1; i < 63; ++i) {
two[i] = two[i - 1] * 2;
}
// printf("%I64d\n\n", 44981600439878676LL + 345678901LL);
while(scanf("%I64d%I64d", &n, &L) != EOF) {
mp.clear();
ans = 0;
for(int i = 0; i < n; ++i) {
scanf("%I64d", &num[i]);
}
dfs(L);
printf("%I64d\n", mp[L]);
}
return 0;
}