牛客周赛 Round 30(A~E)

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

#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后面按从小到大的顺序输出。

#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不同的交换,另一侧也做同样的交换.代码有点丑,可以不用分奇偶,我这写麻烦了。

#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下取整
这里上取整有一些技巧

(l+x-1)/x
l/x+(l%x!=0)
#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 是白色的所有合法方案 f[i][0]:表示以i为根的子树,i是白色的所有合法方案 f[i][0]:表示以i为根的子树,i是白色的所有合法方案
f [ i ] [ 1 ] : 表示以 i 为根的子树, i 是红色的所有合法方案 f[i][1]:表示以i为根的子树,i是红色的所有合法方案 f[i][1]:表示以i为根的子树,i是红色的所有合法方案
考虑如何转移
当i为白色时,只能由其子树是红色的转移而来,那么就有
f [ i ] [ 0 ] = f [ i ] [ 0 ] ∗ f [ y ] [ 1 ] f[i][0]=f[i][0] * f[y][1] f[i][0]=f[i][0]f[y][1]
当i为红色时,既能由其子树是红色的转移而来,也能由其子树是白色的转移而来,那么就有
f [ i ] [ 0 ] = f [ i ] [ 0 ] ∗ ( f [ y ] [ 0 ] + f [ y ] [ 1 ] ) f[i][0]=f[i][0] * (f[y][0]+f[y][1]) f[i][0]=f[i][0]f[y][0]+f[y][1])

#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;
}	

相关推荐

  1. Round 30(A~E)

    2024-01-29 23:46:02       32 阅读
  2. Round31-小白感悟

    2024-01-29 23:46:02       28 阅读
  3. Round 39vp(A--F)

    2024-01-29 23:46:02       9 阅读
  4. 39

    2024-01-29 23:46:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-29 23:46:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-29 23:46:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-29 23:46:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-29 23:46:02       18 阅读

热门阅读

  1. 2、数据缩放和标准化

    2024-01-29 23:46:02       24 阅读
  2. 鸿蒙自定义组件内自定义构建函数

    2024-01-29 23:46:02       39 阅读
  3. 中国的文化是否是丛林文化?

    2024-01-29 23:46:02       39 阅读
  4. TS:子类型关系

    2024-01-29 23:46:02       31 阅读