2021牛客寒假算法基础集训营6,签到题ACDFGIJ

A 回文括号序列计数,找规律

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int mod = 998244353;

int main(){
	ios::sync_with_stdio(false);
	int T;  cin>>T;
	while(T--){
		LL n;  cin>>n;
		if(n==0){cout<<"1\n"; continue;}
		if(n>=1){cout<<"0\n"; continue;}
	}
	return 0;
}




C 末三位,快速幂

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int mod = 1000;

LL mpow(LL a, LL x) {
	if(x==0)return 1;
	LL t = mpow(a, x>>1);
	if(x%2==0)return t*t%mod;
	return t*t%mod*a%mod;
}

int main(){
	ios::sync_with_stdio(false);
	LL n;
	while(cin>>n){
		if(n==0){cout<<"001\n";continue;}
		if(n==1){cout<<"005\n";continue;}
		if(n==2){cout<<"025\n";continue;}
		cout<<mpow(5,n)<<"\n";
	}
	return 0;
}



D 划数,贪心模拟

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+10;
const int mod = 1000;
int main(){
	ios::sync_with_stdio(false);
	LL n, cnt; 
	while(cin>>n>>cnt){
		if(n==2){
			LL x, y;  cin>>x>>y;
			if(x==cnt)cout<<y<<"\n";
			else cout<<x<<"\n";
			continue;
		}
		LL sum = 0, ok = 1;
		for(int i = 1; i <= n; i++){
			LL x;  cin>>x;
			if(x==cnt && ok==1){
				ok = 0;
				continue;
			}else{
				sum += x;
				sum %= 11;
			}
		}
		cout<<sum<<"\n";
	}
	return 0;
}



F 组合数问题,贪心找规律

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 5e5+10;
const int mod = 998244353;

LL mpow(LL a, LL x) {
	if(x==0)return 1;
	LL t = mpow(a, x>>1);
	if(x%2==0)return t*t%mod;
	return t*t%mod*a%mod;
}

int main(){
	ios::sync_with_stdio(false);
	LL n;  cin>>n;
	LL k = n/4, ans;
	if(k%2==1)ans = (mpow(2,n-2)-2*mpow(4,k-1)%mod+mod)%mod;
	else ans = mpow(2,n-2)+2*mpow(4,k-1)%mod;
	ans %= mod;
	cout<<ans<<"\n";
	return 0;
}

/*
1212121212

4


8
72

12
992

13
2016
14
4096
15
8256

16
16512


17
32896
18
65536

20 5
262144 261632 -512 -2*4^4

24 6 
4194304 4196352 +2048 +2*4^5

28 7
67108864 67100672 -8192 -2*4^6

32 8
75497471 75530239 +32768

123232
*/



G 机器人,贪心排序

在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

inline void print(__int128 x){
    if(x < 0){ putchar('-'); x = -x; }
    if(x > 9)print(x/10);
    putchar(x%10+'0');
}

const int maxn = 1010;
int n, x, a[maxn], b[maxn], r[maxn];
bool cmp(int x, int y){
	//return a[x]*b[x]>a[y]*b[y];
	return b[x]*(a[y]-1)>b[y]*(a[x]-1);
	//return a[x]*a[x]+b[x]<a[y]*a[y]+b[y];
	//if(a[x]*b[y]!=a[y]*b[x])return (a[x]*b[y]<a[y]*b[x]);
	//return a[x]>a[y];
	//return a[x]+b[y]<a[y]+b[x];
	//if(a[x] != a[y])return a[x]>a[y];
	//return b[x]>b[y];
	//if(b[x]!=b[y])return b[x]>b[y];
	//return a[x]<a[y];
}

int main(){
    cin>>n>>x;
    for(int i = 1; i <= n; i++)
        cin>>a[i]>>b[i];
    for(int i = 1; i <= n; i++)r[i] = i;
    sort(r+1,r+n+1,cmp);
    __int128 ans = x;
    for(int i = 1; i <= n; i++){
		ans = ans*a[r[i]]+b[r[i]];
    }
    //cout<<ans<<'\n';
	print(ans);
    return 0;
}

I 贪吃蛇,BFS

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 110+10;
const int mod = 1000;

int sx, sy, tx, ty;
char a[maxn][maxn];

struct node{int x,y, step=0;};
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int vis[maxn][maxn];

int main(){
	ios::sync_with_stdio(false);
	int n, m; cin>>n>>m;
	cin>>sx>>sy>>tx>>ty;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++){
			cin>>a[i][j];
		}
	}
	queue<node>q;
	q.push({sx,sy,0});
	vis[sx][sy] = 1;
	int ans = 1e9+10, ok = 1;
	while(q.size()){
		node t = q.front(); q.pop();
		if(t.x==tx && t.y==ty){
			ans = min(ans,t.step);
			ok = 0;
		}
		vis[t.x][t.y] = 1;
		for(int i = 0; i < 4; i++){
			int nx = t.x+dx[i], ny = t.y+dy[i];
			if(nx<1||nx>n||ny<1||ny>m)continue;
			if(a[nx][ny]=='#' || vis[nx][ny]==1)continue;
			q.push({nx,ny,t.step+1});
			vis[nx][ny]=1;
		}
	}
	if(ok==0)cout<<ans*100<<"\n";
	else cout<<"-1\n";
	/*
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j++)
			cout<<a[i][j]<<" ";
		cout<<"\n";
	}
	*/
	return 0;
}



J 天空之城,最小生成树

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+10;
const int mod = 1000;

struct Edge{
    int u, v, w;
    Edge(int u=0, int v=0, int w=0):u(u),v(v),w(w){}
    bool operator < (Edge b)const{return w<b.w;}
};

int fa[maxn];
void init(int n){for(int i=1;i<=n;i++)fa[i]=i;}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void merge(int x,int y){x=find(x);y=find(y);if(x!=y)fa[x]=y;}

int main(){
	ios::sync_with_stdio(false);
	int n, q;
	while(cin>>n>>q){
		map<string,int>ma; int tot=0;
		vector<Edge>e;
		
		string s;  cin>>s;
		tot++; ma[s] = tot;
		for(int i =1; i <= q; i++){
			string u, v; int uu,vv,w;
			cin>>u>>v>>w;
			if(ma.count(u))uu = ma[u];
			else {uu = ++tot; ma[u]=uu;}
			if(ma.count(v))vv = ma[v];
			else {vv = ++tot; ma[v]=vv;}
			e.push_back({uu,vv,w});
		}
		sort(e.begin(),e.end());
		LL ans = 0;
		init(n);
		int cnt = 1;
		for(int i = 0; i < e.size(); i++){
			int u = e[i].u, v = e[i].v;
			if(find(u) != find(v)){
				merge(u,v);
				ans += e[i].w;
				cnt++;
			}
		}
		if(cnt==n)cout<<ans<<"\n";
		else cout<<"No!\n";
	}
	return 0;
}




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页