• AtCoder Beginner Contest 167


    A - Registration

    题意

    两个字符串s,t;判断除了t的最后一个字母,两个是否字符串相同

    #include<bits/stdc++.h>
    
    using namespace std;
    const int N = 8200;
    int t,n,a[N],pre[N];
    bool check[N];
    
    int main(){
    	string s,t;
    	cin>>s>>t;
    	bool flag=0;
    	for(int i=0;i<s.length();i++){
    		if(s[i]!=t[i]) flag=1;
    	}
    	if(s.length()+1!=t.length()) flag=1;
    	if(flag) puts("No");
    	else puts("Yes");
    	
    	return 0;
    }
    
    

    B - Easy Linear Programming

    题意

    三种卡片A,B,C,分别代表1,0,-1;在这些卡片中选择k张卡片,问最大值

    思路

    只选A;选A,B;选A,B,C;三种情况依次和k比较大小

    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const int N = 8200;
    int t,n,a[N],pre[N];
    bool check[N];
    
    int main(){
    	ll a,b,c,k;
    	cin>>a>>b>>c>>k;
    	if(k<=a) cout<<k<<'
    ';
    	else if(k>a&&k<=a+b) cout<<a<<'
    ';
    	else if(k>a+b&&k<=a+b+c) cout<<2*a+b-k<<'
    ';
    	
    	return 0;
    }
    

    C - Skill Up

    题意

    n本书价格不同,每本书都能提升m种算法能力,每本书对每种算法能力提升不同,问将m种能力都提升至不小于x的最小花费。

    思路(状态压缩)

    n的范围是比较小的,可以枚举每本书的状态,比较每种情况得出最小花费

    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const int N = 15;
    int a[N][N],c[N],res[N];
    int n,m,x;
    
    int main(){
    	cin>>n>>m>>x;
    	for(int i=1;i<=n;i++){
    		cin>>c[i];
    		for(int j=1;j<=m;j++) cin>>a[i][j];
    	}
    	ll ans=0x3f3f3f3f;
    	for(int i=1;i<(1<<n);i++){//枚举书的状态
    		bool flag=0;
    		memset(res,0,sizeof(res));
    		for(int j=0;j<n;j++){
    			if(i&(1<<j)){//判断当前书是否被购买
    				for(int k=1;k<=m;k++){
    					res[k]+=a[j+1][k];
    				}
    			}
    		}
    		for(int j=1;j<=m;j++){
    			if(res[j]<x){
    				flag=1;
    				break;
    			}
    		}
    		if(!flag){
    			ll tmp=0;
    			for(int j=0;j<n;j++){
    				if(i&(1<<j)){
    					tmp+=c[j+1];
    				}
    			}
    			ans=min(ans,tmp);
    		}
    	}
    	if(ans==0x3f3f3f3f) cout<<-1<<'
    ';
    	else cout<<ans<<'
    ';
    	
    	return 0;
    } 
    
    

    D - Teleporter

    题意

    n个小镇都可以通过传送器传送到其他小镇,问从第一个小镇出发,k次传送后所在的小镇

    思路

    找循环节,注意1不一定在循环节中,然后注意边界

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=200010;
    ll n,m,a[N],k;
    vector<int>v;
    bool vis[N];
    int main()
    {
    	cin>>n>>k;
    	k++;//第k次所在位置
    	for(int i=1;i<=n;i++) cin>>a[i];
    	int pos=1;
    	while(!vis[pos])
    	{
    		v.push_back(pos);
    		vis[pos]=1;
    		pos=a[pos];
    	}
    	int x,len;
    	for(int i=0;i<v.size();i++)//找循环节
    		if(v[i]==pos)
    		{
    			x=i;
    			break;
    		}
    	len=v.size()-x;
    	if(k>v.size())
    	{
    		k-=v.size();
    		k%=len;
    		if(k==0) cout<<v[v.size()-1];
    		else
    			cout<<v[k+x-1];
    	}
    	else cout<<v[k-1];
    	return 0;
    }
    
    

    E - Colorful Blocks

    题意

    在n个方块上涂颜色,有m个颜色,规定只有m对相邻的方块上颜色相同,求涂色方法数

    思路 (参考博客:https://www.cnblogs.com/Kanoon/p/12865555.html)

    (ans = sum_{i=0}^k * m * C_{n-1}^i * (m-1)^{n-1-i})
    即第 1 个方块可以染 m 种颜色,从余下 n - 1 个方块中选取 i 个方块,这 i 个方块组成颜色相同的 i 对方块,即它们的颜色与左相邻方块相同,
    余下 n - 1 - i 个方块因为不与左相邻方块同色,每个可以染 m - 1 个颜色。

    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const ll mod = 998244353,N = 2e5+5;
    int n,m,k;
    ll fac[N];
    
    ll mul(ll x,ll y){
    	return x*y%mod;
    }
    
    ll qpow(ll a,ll b){//快速幂
    	ll res=1;
    	while(b){
    		if(b&1) res=res*a%mod;
    		a=a*a%mod;
    		b>>=1;
    	}
    	return res;
    }
    
    ll inv(ll x){ //求逆元
    	return qpow(x,mod-2);
    }
    
    ll C(ll n,ll m){//求组合数
    	return fac[n]*mul(inv(fac[m]),inv(fac[n-m]))%mod;
    }
    
    void init(){//初始化阶乘数组
    	fac[0]=1;
    	for(int i=1;i<=N;i++) fac[i]=mul(fac[i-1],i);
    }
    
    int main(){
    	init();
    	cin>>n>>m>>k;
    	ll ans=0;
    	for(int i=0;i<=k;i++){
    		ans=(ans+mul(mul(m,C(n-1,i)),qpow(m-1,n-i-1)))%mod;
    	}
    	cout<<ans<<'
    ';
    	
    	return 0;
    }
    
    七月在野,八月在宇,九月在户,十月蟋蟀入我床下
  • 相关阅读:
    hdu 3790 最短路径问题
    hdu 2112 HDU Today
    最短路问题 以hdu1874为例
    hdu 1690 Bus System Floyd
    hdu 2066 一个人的旅行
    hdu 2680 Choose the best route
    hdu 1596 find the safest road
    hdu 1869 六度分离
    hdu 3339 In Action
    序列化和反序列化
  • 原文地址:https://www.cnblogs.com/voids5/p/12869551.html
Copyright © 2020-2023  润新知