2020牛客寒假算法基础集训营3
2020牛客寒假算法基础集训营3
A
#include<bits/stdc++.h>
using namespace std;
const int MAXN=55;
const long long mod=1e9+7;
int n,m;
char s[MAXN][MAXN];
long long dp[MAXN][MAXN];
long long dp_dfs(int x,int y)
{
if(x>n||y>m)return 0;
if(dp[x][y]!=-1)return dp[x][y];
if(s[x][y]=='D')return dp[x][y]=dp_dfs(x+1,y);
if(s[x][y]=='R')return dp[x][y]=dp_dfs(x,y+1);
if(s[x][y]=='B')return dp[x][y]=(dp_dfs(x+1,y)+dp_dfs(x,y+1))%mod;
}
int main()
{
scanf("%d %d",&n,&m);
memset(dp,-1,sizeof(dp));
dp[n][m]=1;
for(int i=1;i<=n;++i)
{
scanf("%s",s[i]+1);
}
printf("%lld\n",dp_dfs(1,1));
return 0;
}
B
C
#include<bits/stdc++.h>
using namespace std;
const int N = 1005;
typedef long long ll;
int a[N][N];
int main()
{
ll t;cin >> t;
while (t--)
{
memset(a, 0, sizeof(a));
ll flag = 1;
ll n, m, p;cin >> n >> m >> p;
for (int i = 0;i < p;++i)
{
ll x, y, val;cin >> x >> y >> val;
const int sum = m * x + y;
if (sum >= n * m || sum < 0)//Re
flag = 2;
else if (flag==1&&(x < 0 || y<0 || y>=m||x>=n))//UD
flag = 3;
if(flag!=2)a[sum/m][sum%m] = val;
}
if (flag == 2)
puts("Runtime error");
else
{
for (int i = 0;i < n;++i)
{
cout << a[i][0];
for (int j = 1;j < m;++j)
{
cout << " " << a[i][j];
}
cout << endl;
}
if (flag == 1)puts("Accepted");
else puts("Undefined Behaviour");
}
}
return 0;
}
D
/*************************************************************************
> File Name: d.cpp
> Author:
> Mail:
> Created Time: 六 2/ 8 20:19:27 2020
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
vector<int> a(1e5 + 5);
int main(int argc, char *argv[]) {
SIS;
int n;
while(cin >> n){
map<int,int> m;
int cnt = 0;
for(int i = 1;i <= n;i++){
cin >> a[i];
if(a[i] > 0){
cnt++;
m[a[i]] = i;
}
}
printf("The size of the tree is %d\n",cnt);
printf("Node %d is the root node of the tree\n",a[1]);
for(int i = 1;i <= cnt ;i ++){
printf("The father of node %d is %d,",i,m[i] / 2 == 0 ? - 1 : a[m[i]/2]);
printf(" the left child is %d, and the right child is %d\n",m[i] * 2 > n ? -1:a[m[i] * 2],m[i] * 2 + 1 > n ? -1 : a[m[i] * 2 + 1]);
}
}
return 0;
}
H
使用埃式筛,将每一个质数筛出,如果x是合数,则将1~n范围内能被该合数x 筛出的合数 ++,因为我们知道一个合数有多少个合数因子就能被筛几次
然后桶排序即可
/*************************************************************************
> File Name: h.cpp
> Author:
> Mail:
> Created Time: 一 2/10 10:48:26 2020
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
const int maxn = 1e5 + 5;
int f[maxn];
int cnt[maxn];
int prime[maxn];
void init(int n){
for(int i = 2;i <= n;i++){
prime[i] = true;
}
for(int i = 2;i <= n;i++){
if(prime[i]){
for(int j = i + i; j <= n; j += i){
prime[j] =false;
}
}else{
for(int j = i; j <= n; j += i){
cnt[j]++; // 代表被筛查了几次,一个数有几个合数就能被筛查多少次
}
}
f[cnt[i]]++; // 桶排序,出现过1次的++
}
}
int main(int argc, char *argv[]) {
SIS;
int n;
int k;
cin >> n >> k;
init(n);
for(int i = 0;i < k;i ++){
int x;
cin >> x;
cout << f[x] << endl;
}
return 0;
}

浙公网安备 33010602011771号