A

#include <iostream>

using namespace std;

int main() {
int a, b, c;
cin >> a >> b >> c;
for(int i = 1; i <= 1000; i ++ ) {
if(c * i >= a && c * i <= b) {
cout << c * i ;
return 0;
}
if(c * i > b) break;
}
cout << "-1";
return 0; return 0;
}

B

#include <iostream>
#include <algorithm>
#include <cstring> #define LL long long
using namespace std; int main() {
string a, b;
int k; cin >> k >> a >> b;
LL cnt1 = 0, cnt2 = 0;
// cout << a << b << endl;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
LL res = 1;
for(int i = 0; a[i]; i ++ ) {
int p = a[i] - '0';
cnt1 += p * res;
res *= k;
}
res = 1;
for(int i = 0; b[i]; i ++ ) {
int p = b[i] - '0';
cnt2 += p * res;
res *= k;
}
// cout << cnt1 << ' ' << cnt2 << endl;
cout << cnt1 * cnt2; return 0;
}

C

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector> #define LL long long
using namespace std; int main() {
int n; cin >> n;
vector<int> a(n);
LL ans = 0;
for(int &x: a) {
cin >> x;
ans += x;
}
LL p; cin >> p; LL res = p / ans * n;
LL pp = 0;
p %= ans; for(int i = 0; i < n; i ++ ) {
pp += a[i];
if(pp > p) {
cout << res + i + 1 << endl;
return 0;
}
} return 0;
}

D

线性DP

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#define pb push_back #define mod(x) (x) % MOD
#define LL long long using namespace std; const int MOD = 998244353, N = 1e5 + 10; LL cnt[10];
int n, a[N]; int main() {
cin >> n;
for(int i = 0; i < n; i ++ ) cin >> a[i];
vector<LL> p(10, 0), q(10, 0);
q[a[0]] = 1;
for(int i = 1; i < n; i ++ ) {
// p = q;
for(int j = 0; j < 10; j ++ ) {
p[j] = q[j] % MOD;
q[j] = 0;
}
for(int j = 0; j < 10; j ++ ) {
q[(j + a[i]) % 10] = mod(q[(j + a[i]) % 10] + p[j]);
q[(j * a[i]) % 10] = mod(q[(j * a[i]) % 10] + p[j]);
} }
for(int i = 0; i < 10; i ++ ) {
cout << q[i] % MOD << endl;
} return 0;
}

E

对于每个节点形成的链有两种可能

  • 这个节点是上端点
  • 这个节点是中转节点



    对于1号节点
  • 第一种有链:421、521、631、731
  • 第二种有链:213、4213、42137....

这样枚举可以不重不漏的找出所有的长度固定的链

而且,对于每个节点第一种有\(2^{n+1}\)个(正向逆向要重复计数),第二种有\((d-1)*{2^{d-1}}\)个

当然有的节点可能不存在那么多,也就是当剩余的层数不足d的时候,我们就需要一层一层的拓展

比如说往下扩展一层就多了 $2 * {2^{d-2}} $ 即 左边的层数所具有的节点 * 右边层数所具有的节点 永远都是 \(2^{d-1}\)

因此还剩多少层我就就会加多少个 \(2^{d-1}\)

AC_CODE

#include <bits/stdc++.h>
#define mod(x) ((x) % MOD)
#define LL long long using namespace std; const int N = 2e6 + 10, MOD = 998244353; LL num[N]; int main() {
// pre
num[0] = 1;
for(int i = 1; i < N; i ++ )
num[i] = mod(num[i - 1] * 2);
LL ans = 0;
LL n, d; cin >> n >> d; for(int i = 0; i < n; i ++ ) {
LL cnt = num[i];
LL mx = n - i - 1;
if(mx >= d) {
ans = mod(ans + mod(num[d + 1] * cnt)); // 求出以第i层的节点为 链的端点的所有个数
ans = mod(ans + mod(num[d - 1] * mod((d - 1) * cnt)));// 求出以第i层的节点为 链的转折点的所有个数
//转折过后必须向下转折 可以不重不漏
} else if(2 * mx >= d) {
ans = mod(ans + mod(mod(num[d - 1] * cnt) * (2 * mx - d + 1)));
//求出以第i层的节点为 链的转折点的所有个数 因为不可以为 链的端点
} else break;
}
cout << mod(ans) << endl; return 0;
}

F

思路

先求出以1号节点为起点,其他的点到1号点的距离,然后对于1号点子节点,我们再继续求以这个点为起点的路径长度的时候

我们可以把其他的点分为两种

  • 一种是以此点为根的点
  • 一种不是以此点为根的点

对于第一种,每条路径长度减去1就是我们想知道的路径长度,第二种则需要加上1

整理一下会发现就是 ans[j] = ans[u] + (n - size(j)) - size(j); (u是j的根节点)

因此可以用树形DP解决这个问题

AC_CODE

#include <iostream>
#include <cstring>
#define LL long long using namespace std; const int N = 2e5 + 10, M = N << 1; int h[N], e[M], ne[M], idx;
int n;
LL ans[N], s[N]; void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++;
} int dfs(int u, int fa, int cnt) {
ans[1] += cnt;
int res = 1;
for(int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if(j == fa)
continue;
res += dfs(j, u, cnt + 1);
}
s[u] = res;
return res;
} void dfs1(int u, int fa) {
for(int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if(j == fa)
continue;
ans[j] = ans[u] + n - 2 * s[j];
dfs1(j, u);
}
} int main() {
memset(h, -1, sizeof h);
scanf("%d", &n);
for(int i = 1; i < n; i ++ ) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
}
dfs(1, -1, 0);
dfs1(1, -1);
for(int i = 1; i <= n; i ++ )
printf("%lld\n", ans[i]);
return 0;
}

AtCoder Beginner Contest 220 A-F的更多相关文章

  1. AtCoder Beginner Contest 238 A - F 题解

    AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...

  2. AtCoder Beginner Contest 220部分题(G,H)题解

    刚开始的时候被E题卡住了,不过发现是个数学题后就开始使劲推式子,幸运的是推出来了,之后的F题更是树形DP换根的模板吧,就草草的过了,看了一眼G,随便口胡了一下,赶紧打代码,毕竟时间不多了,最后也没打完 ...

  3. AtCoder Beginner Contest 131 Task F. Must Be Rectangular

    Score: 600 points Approach 固定横坐标 $x$,考虑横坐标为 $x$ 的竖直线上最多可以有几个点. Observations 若最初两条竖直线 $x_1$.$x_2$ 上都有 ...

  4. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  5. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  6. AtCoder Beginner Contest 154 题解

    人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...

  7. AtCoder Beginner Contest 153 题解

    目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...

  8. AtCoder Beginner Contest 177 题解

    AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...

  9. AtCoder Beginner Contest 161

    比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...

随机推荐

  1. 1048 - Conquering Keokradong

    1048 - Conquering Keokradong    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...

  2. 1275 - Internet Service Providers

    1275 - Internet Service Providers    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory L ...

  3. Service有多个实现类,它怎么知道该注入哪个ServiceImpl类

    方法一:Controller中注入service的时候使用@Autowired自动注入,@Qualifier("beanId")来指定注入哪一个. 方法二:Controller中注 ...

  4. SpringBoot集成MyBatis-Plus代码生成器

    1.说明 本文详细介绍Spring Boot集成MyBatis-Plus代码生成器的方法. 基于一个创建好的Spring Boot工程, 执行MyBatis-Plus提供的AutoGenerator代 ...

  5. 2021 编程语言排行榜出炉!C#年度语言奖

    IEEE Spectrum 发布了 2021 年度编程语言排行榜,其中 Python 在总榜单以及其他几个分榜单中依然牢牢占据第一名的位置.另外值得关注的是微软 C# 语言,它的排行从 2020 年的 ...

  6. MongoDB开发最佳实践

    MongoDB开发最佳实践 连接到MongoDB · 关于驱动程序:总是选择与所用之MongoDB相兼容的驱动程序.这可以很容易地从驱动兼容对照表中查到: · 如果使用第三方框架(如Spring Da ...

  7. CSS 基础 背景相关属性操作

    1.background-color:red : //设置背景颜色为红色,rgb(0,0,0)和transparent 均为透明颜色 2.background-image(可缩bgi写用tab键) 语 ...

  8. 初识python 之 MongoDB 基本操作

    MongoDB与SQL对比: MongoDB 三元素:数据库.集合.文档 MongoDB 基本操作命令: db 查看当前数据库 show dbs 查看所有数据库 use 数据库名 切换数据库,如果数据 ...

  9. react组件性能优化PureComponent

    首先我们使用react组件会配合connect来连接store获取state,那么只要store中的state发生改变组件就会重新渲染,所以性能不高,一般我们可以使用shouldComponentUp ...

  10. centos7 单用户模式修改root密码

    1. 在虚拟机重启客户机后.会出现下面进入界面.按e键 2.按了e键后,会出现下面的界面.此时按↓键.找到linux16 3.将光标移动到UTF-8后面,添加init=/bin/sh,并按 ctrl  ...