牛客白月赛31【题解】

https://ac.nowcoder.com/acm/contest/10746

A|B【数位DP】

A + B【模拟】

在这里插入图片描述

#include<bits/stdc++.h> 
using namespace std;
typedef long long int LL;
string a[5][10]=
{
    
    	
	{
    
    "###","..#","###","###","#.#","###","###","###","###","###"},
	{
    
    "#.#","..#","..#","..#","#.#","#..","#..","#.#","#.#","#.#"},
	{
    
    "#.#","..#","###","###","###","###","###","#.#","###","###"},
	{
    
    "#.#","..#","#..","..#","..#","..#","#.#","..#","#.#","..#"},
	{
    
    "###","..#","###","###","..#","###","###","..#","###","###"}
};
string s[5];
int get(int l)
{
    
    
	map<int,int>mp;
	for(int i=0;i<5;i++)
	{
    
    
		string ss=s[i].substr(l,3);
		for(int j=0;j<10;j++)
			 if(ss==a[i][j]) mp[j]++;
	}
	int ans=-1;
	for(int i=0;i<10;i++) if(mp[i]==5) ans=i;
	return ans;
}
int main(void)
{
    
    
	int t; cin>>t;
	while(t--)
	{
    
    
		for(int i=0;i<5;i++) cin>>s[i];
		int m=s[0].size(),n=5;
		stack<int>st; st.push(0);
		for(int i=0;i<m;i=i+4)
		{
    
    
			int temp=get(i);
			if(temp==-1) st.push(0);
			else
			{
    
    
				int w=st.top()*10+temp;
				st.pop();
				st.push(w);
			}
		}
		int sum=0;
		while(st.size()) sum+=st.top(),st.pop();
		string ss=to_string(sum);
		for(int i=0;i<5;i++)
		{
    
    
			string temp;
			for(int j=0;j<ss.size();j++)
			{
    
    
				temp=temp+a[i][ss[j]-'0'];
				if(j!=ss.size()-1) temp+='.';
			}
			cout<<temp<<endl;
		}
		cout<<endl;
	}
	return 0;
}

图像存储【dfs+map去重】

在这里插入图片描述
难点,去重。见点存到vector里排序,统一的减最小的点坐标。这样就都到了统一的坐标系。

#include<bits/stdc++.h> 
using namespace std;
const int N=1010;
string s[N];
int n,m,st[N][N];
int dx[4]={
    
    -1,0,0,1};
int dy[4]={
    
    0,-1,1,0};
map< vector<pair<int,int>> ,int>mp;
void solve(int x,int y)
{
    
    
	int minx=1e9,miny=1e9;
	st[x][y]=1;
	vector<pair<int,int>>ve; ve.push_back({
    
    x,y});
	queue<pair<int,int>>q; q.push({
    
    x,y});
	while(q.size())
	{
    
    
		auto temp=q.front(); q.pop();
		x=temp.first,y=temp.second;
		minx=min(x,minx),miny=min(y,miny);
		for(int i=0;i<4;i++)
		{
    
    
			int tempx=x+dx[i],tempy=y+dy[i];
			if(tempx<0||tempx>=n||tempy<0||tempy>=m) continue;
			if(s[tempx][tempy]=='0') continue;
			if(st[tempx][tempy]) continue;
			q.push({
    
    tempx,tempy}),st[tempx][tempy]=1;
			ve.push_back({
    
    tempx,tempy});
		}
	}
	sort(ve.begin(),ve.end());
	for(int i=0;i<ve.size();i++) ve[i].first-=minx,ve[i].second-=miny;
	mp[ve]++;
}
int main(void)
{
    
    
	while(cin>>n>>m,n||m)
	{
    
    
		mp.clear();
		memset(st,0,sizeof st);
		int cnt=0;
		
		for(int i=0;i<n;i++) cin>>s[i];
		for(int i=0;i<n;i++)
		{
    
    
			for(int j=0;j<m;j++)
			{
    
    
				if(st[i][j]) continue;
				if(s[i][j]=='0') continue;
				solve(i,j),cnt++;
			}
		}
		cout<<cnt<<" "<<mp.size()<<endl;
	}
	return 0;
}

坐标计数

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
int main(void)
{
    
    
	int t; cin>>t;
	while(t--)
	{
    
    
		LL x,y,xx,yy; cin>>x>>y>>xx>>yy;
		cout<<(abs(xx-x)+1)*(abs(y-yy)+1)<<endl;
	}
	return 0;
}

解方程【因子个数】

在这里插入图片描述
在这里插入图片描述
题解地址: https://blog.csdn.net/CSDNWudanna/article/details/112505148

#include<bits/stdc++.h> 
using namespace std;
typedef long long int LL;
LL get(LL x)
{
    
    
	LL sum=1;
	for(int i=2;i<=x/i;i++)
	{
    
    
		LL s=0;
		while(x%i==0) s++,x/=i;
		if(s) sum*=(s+1); 
	}
	if(x!=1) sum=sum*2;
	return sum;
}
int main(void)
{
    
    
	int t; cin>>t;
	while(t--)
	{
    
    
		LL a,b; cin>>a>>b;
		cout<<get(a*b)<<'\n';
	}
	return 0;
}

消减整数【思维】

在这里插入图片描述
模拟+二分+map 优化可以卡过,但是并非正解。

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
map<int,int>mp;
LL get(LL x)
{
    
    
	LL l=1,r=1e5;
	while(l<r)
	{
    
    
		LL mid=(l+r+1)>>1;
		LL sum=mid*(mid+1)/2;
		if(sum<=x) l=mid;
		else r=mid-1;
	}
	return l*(l+1)/2;
}
int main(void)
{
    
    
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
	LL t; cin>>t;
	while(t--) 
	{
    
    
		LL n; cin>>n;
		LL cnt=0,temp=n;
		if(mp.count(n))
		{
    
    
			cout<<mp[n]<<'\n';
			continue;
		}
		while(n)
		{
    
    
			cnt++;
			if( n==get(n) ) break;
			n-=get(n);
			n=n+temp;
		}
		cout<<cnt<<'\n';
		mp[temp]=cnt;
	}
	return 0;
}

你会发现,每次减完剩余一个y, n+y再减还是省一个 y。y,2y,3y…看啥时候是x的倍数即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
int main(void)
{
    
    
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
	int t; cin>>t;
	while(t--) 
	{
    
    
		int n; cin>>n;
		int temp=n,x=1;
		while(n>=x) n-=x,x++;
		for(int i=1;i<=temp;i++)
		{
    
    
			if((n*i)%x==0)
			{
    
    
				cout<<i<<'\n';
				break;
			}
		}
	}
	return 0;
}

简单题的逆袭

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
int main(void)
{
    
    
	int t; cin>>t;
	while(t--)
	{
    
    
        LL x,y; cin>>x>>y;
		if(x==0 || x==1 || y==0) 
		{
    
    
			puts("-1");
			continue;
		} 
		LL ans=0;
		for(__int128 i=1,j=0;;i*=x,j++)
		{
    
    
			if(i<=y) ans=j;
			else break;
		}
		cout<<ans<<endl;
	}
	return 0;
}

对称之美【枚举】

在这里插入图片描述
枚举,看对称的字符串有没有相同的字母。

#include<bits/stdc++.h> 
using namespace std;
int main(void)
{
    
    
	int t; cin>>t;
	while(t--)
	{
    
    
		string s[105];
		int n; cin>>n;
		int cnt[105][35]={
    
    0};
		for(int i=0;i<n;i++) cin>>s[i];
		for(int i=0;i<n;i++)
			for(int j=0;j<s[i].size();j++) cnt[i][s[i][j]-'a']=1;
		int ok=0;
		for(int i=0;i<n/2;i++)
		{
    
    
			int l=i,r=n-1-i;
			int flag=0;
			for(int j=0;j<26;j++) 
			{
    
    
				if(cnt[l][j]==cnt[r][j]&&cnt[l][j])  flag=1;
			}
			if(!flag) ok=1;
		}
		if(ok) puts("No");
		else puts("Yes");
	}
	return 0;
}

非对称之美

在这里插入图片描述
0,n-1,n三种情况。

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
bool check(string s)
{
    
    
	int n=s.size();
	for(int i=0;i<n/2;i++) if(s[i]!=s[n-1-i]) return false;
	return true;
}
int main(void)
{
    
    
	string s; cin>>s;
	set<char>st;
	for(int i=0;i<s.size();i++) st.insert(s[i]);
	if(st.size()==1) cout<<0;
	else 
	{
    
    
		if(check(s)) cout<<s.size()-1;
		else cout<<s.size();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46527915/article/details/124792050