牛客周赛 Round 30(A~E)

A

A题签到题直接输出0和2即可

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;

const int N=1e6+10;
int p[N],cnt[N],m;
vector<int>a;
int ans;
int x,n;

void solve()
{
	string s;cin>>s;
	rep(i,0,s.size()-1)
		if(i!=1)	cout<<s[i];	
}

int main()
{
	IOS	
//  	freopen("1.in", "r", stdin);
  	int t;
//	cin>>t;
//	while(t--)
	solve();
	return 0;
}

B

贪心,排序。第一位数不能为0后面按从小到大的顺序输出。

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;

const int N=1e6+10;
int p[N],cnt[N],m;
vector<int>a;

void solve()
{
	int x;cin>>x;
	map<int,int>cnt;
	while(x)
	{
		cnt[x%10]++;
		x/=10;
	}
	for(auto it:cnt)
	{
		if(it.x==0)    continue;
        cout<<it.x;
        cnt[it.x]--;
        break;
	}
	
	for(auto it:cnt)
	{
		while(cnt[it.x])
		{
			cout<<it.x;
			cnt[it.x]--;
		}
	}
}

int main()
{
	IOS	
//  	freopen("1.in", "r", stdin);
  	int t;
//	cin>>t;
//	while(t--)
	solve();
	return 0;
}

C

回文串对称,考虑无解的情况,无解的话一侧全部是相同的,否则我们就将一侧的1和1不同的交换,另一侧也做同样的交换.代码有点丑,可以不用分奇偶,我这写麻烦了。

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;

const int N=1e6+10;

char s[N];
void solve()
{
	cin>>s;
	int n=strlen(s);
	if(n&1)
	{
		map<char,int>cnt;
		rep(i,0,n/2-1)	cnt[s[i]]++;
		if(cnt.size()<=1)
		{
			cout<<-1<<endl;
			return;
		}
		else
		{
			rep(i,1,n/2-1)
			{
				if(s[i]!=s[0])	
				{
					swap(s[n-1],s[n-1-i]);
					swap(s[0],s[i]);
					break;
				}	
				
			}
		}	
	}
	else
	{
		map<char,int>cnt;
		rep(i,0,n/2)	cnt[s[i]]++;
		if(cnt.size()<=1)
		{
			cout<<-1<<endl;
			return;
		}
		else
		{
			rep(i,1,n/2)
			{
				if(s[i]!=s[0])	
				{
					swap(s[n-1],s[n-1-i]);
					swap(s[0],s[i]);
					break;
				}		
			}
		}	
	}
	cout<<s;
	
}

int main()
{
	IOS	
//   	freopen("1.in", "r", stdin);
  	int t;
//	cin>>t;
//	while(t--)
	solve();
	return 0;
}

D

没做出来呜呜

数学,思维。最近总是卡到数学题,昨晚的cf也是。

首先要想到,可以先把能除的除干净,可以发现除法可逆,也就是说除完之后,后面可以乘回来。

除以最大公约数之后会得到两个互质的数。

然后考虑计算答案,合法的乘数一定是一些连续的数。

只用找到上下边界就好了。

对于下边界取决于l和x

上边界取决于r和y

上边界是l/x上取整

下边界是r/y下取整

这里上取整有一些技巧

cpp 复制代码
(l+x-1)/x
l/x+(l%x!=0)
cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;

const int N=1e6+10,mod=1e9+7;


void solve()
{
	int x,y,l,r;cin>>x>>y>>l>>r;
    int k=__gcd(x,y);
    if(x>y)    swap(x,y);
    x/=k;y/=k;
    int minn=l/x+(l%x!=0);
    int maxx=r/y;
	cout<<max(0,maxx-minn+1);
}

int main()
{
	IOS	
//  	freopen("1.in", "r", stdin);
  	int t;
//	cin>>t;
//	while(t--)
	solve();
	return 0;
}

E

e是一道比较经典的树形dp
f i 0 : 表示以 i 为根的子树, i 是白色的所有合法方案 fi0:表示以i为根的子树,i是白色的所有合法方案 fi0:表示以i为根的子树,i是白色的所有合法方案
f i 1 : 表示以 i 为根的子树, i 是红色的所有合法方案 fi1:表示以i为根的子树,i是红色的所有合法方案 fi1:表示以i为根的子树,i是红色的所有合法方案

考虑如何转移

当i为白色时,只能由其子树是红色的转移而来,那么就有
f i 0 = f i 0 ∗ f y 1 fi0=fi0 * fy1 fi0=fi0∗fy1

当i为红色时,既能由其子树是红色的转移而来,也能由其子树是白色的转移而来,那么就有
f i 0 = f i 0 ∗ ( f y 0 + f y 1 ) fi0=fi0 * (fy0+fy1) fi0=fi0∗(fy0+fy1)

cpp 复制代码
#include <bits/stdc++.h> 
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f 
#define x first
#define y second
using namespace std;

const int N=1e6+10,mod=1e9+7;

ll f[100010][2],mod=1e9+7;
vector<int>e[100010];
int n;
void dfs(int x,int fa)
{
    f[x][0]=f[x][1]=1;
    int boy=0;
    for(auto y:e[x])
    {
        if(y==fa)
            continue;
        dfs(y,x);
        f[x][0]=f[x][0]*f[y][1]%mod;
        f[x][1]=f[x][1]*(f[y][0]+f[y][1])%mod;
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    dfs(1,0);
    cout<<(f[1][0]+f[1][1])%mod;
}	
相关推荐
wzdark4 小时前
基于堆的优先队列实现原理与复杂度分析4
算法
彧azz5 小时前
图的存储结构详解:邻接矩阵的原理、实现与应用
开发语言·数据结构·学习·php
老王熬夜敲代码6 小时前
BLAKE3:最快的密码学哈希函数
算法·密码学·哈希算法
hansang_IR6 小时前
【题解】P4460 [CQOI2018] 解锁屏幕
c++·算法
见闻小天地6 小时前
简博斯JG-C系列彩色激光同轴位移计:摄像头模组对位与行程测量的技术价值观察
人工智能·算法
ChampaignWolf7 小时前
在 SAP S/4HANA 内部基于 ICF Handler 从零实现 ABAP MCP Server:架构、代码与踩坑实录
c++·架构·sap·abap·mcp
葫三生7 小时前
《论三生原理》神话学构想与“神话历史”理论、《古史中的神话》思路异同?
大数据·人工智能·科技·深度学习·算法
liliangcsdn7 小时前
因子分析指标概念与示例
算法·机器学习
weixin199701080168 小时前
[特殊字符]《跨境二手ERP对接Back Market:标准化API + 7~10工作日技术合规审核实录》(附Python源码)
开发语言·python·pandas
尘客-追梦8 小时前
Day 01:插件化之前,先看清 ABI 这堵墙
开发语言·c++·qt