2020牛客寒假算法基础集训营1题解(完结)

A:honoka和格点三角形
题意: 定义满足以下三个条件的三角形是“好三角形”:
1.三角形的三个顶点均为格点,即横坐标和纵坐标均为整数。
2.三角形的面积为1。
3.三角形至少有一条边和x轴或y轴平行。
思路:
面积为1,那就是底为1高为2,底为2高为1。注意底为1高为2时不能有出现。
以横着为底边举例:
底2高1:下部横坐标可以取m-2种,高上部可以取m个,然后除了第一行和最后一行只能取一种其他都是上下2种, ( m − 2 ) ∗ m ∗ ( n − 1 ) ∗ 2 (m - 2)*m*(n - 1)*2 (m2)m(n1)2
底1高2:下部横坐标可以取m-1种,高上部可以取m-2个,然后除了第一行第二行和最后一行倒数第二行只能取一种其他都是上下2种, ( m − 1 ) ∗ ( m − 2 ) ∗ ( n − 2 ) ∗ 2 (m - 1)*(m - 2)* (n - 2) * 2 (m1)m2(n2)2
代码:

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e5 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
int main(){
    ll m,n;scanf("%lld %lld",&m,&n);
    printf("%lld\n",((m - 2) % mod * m  % mod * (n - 1)  % mod * 2  % mod + (m - 1) * (m - 2)  % mod*  (n - 2) % mod*   2 % mod + (n - 2) % mod * n  % mod * (m - 1)  % mod * 2  % mod + (n - 1) * (n - 2)  % mod * (m - 2) % mod*   2 % mod)%mod);
}

B:kotori和bangdream
签到题不多赘述了,按题意模拟即可:

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
int main(){
    double n;double x,a,b;
    scanf("%lf",&n);
    scanf("%lf%lf%lf",&x,&a,&b);
    printf("%.2f",n*(x/100)*a+n*(1 - x/100)*b);
}

C:umi和弓道
题意:给你1个原始坐标点,n个目标点,让你在坐标轴上找出最短的一端长度,使得有n - k个点经过这段长度。
思路:首先和他同个象限得不可能被拦截,这些我们不要,然后通过两点求出与x轴与y轴交点存到vector里,然后排序以后求一下连续 得n-k个点需要多长,ans取min就行了。

#include<bits/stdc++.h>
typedef long long int ll;
const int maxn = 1e6 + 5;
const ll  mod = 1e9 + 7;
using namespace std;
vector<double>x,y;
int main(){
	double x0,y0;
	scanf("%lf %lf",&x0,&y0);
	int n,k;scanf("%d %d",&n,&k);
	for(int i = 1;i <= n;i++){
		double a,b;scanf("%lf %lf",&a,&b);
		if(a * x0 < 0) y.push_back(y0 - (x0 * (y0 - b)) / (x0 - a));
		if(b * y0 < 0) x.push_back(x0 - (y0 * (x0 - a)) / (y0 - b));
	}
	if(n - x.size() > k && n - y.size() > k) { puts("-1");return 0;}
	sort(x.begin(),x.end());sort(y.begin(),y.end());
	k = n - k;double ans = 1e18;
	for(int i = 0;i + k - 1 < x.size();i++) ans = min(ans,x[i + k - 1] - x[i]);
	for(int i = 0;i + k - 1 < y.size();i++) ans = min(ans,y[i + k - 1] - y[i]);
	printf("%.9f",ans); 
}

D:hanayo和米饭
签到题,sort一下找就好了。

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
int n;
int a[maxn];
int main(){
    scanf("%d",&n);
    for(int i = 1;i < n;i++) scanf("%d",&a[i]);
    sort(a + 1,a + n);
    for(int i = 1;i <= n;i++){
        if(a[i] != i){
            printf("%d\n",i);
            break;
        }
    }
}

E:rin和快速迭代
题意:定义 f ( x ) f(x) f(x) x x x的的因子数,问需要迭代 f ( x ) f(x) f(x)几次才能使 f ( x ) = 2 f(x) = 2 f(x)=2
思路: f ( x ) f(x) f(x)只须取一次就会降到50以下很小,因此我们可以思考判断素数的方式,只需判断到sqrt(n)即可。
代码:

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
ll getnum(ll x){
    ll res = 0;
    ll i;
    for(i = 1;i * i <= x;i++){
        if(x % i == 0) res++;
    }
    if((i - 1) * (i - 1) == x) return res * 2 - 1;
    else return res * 2;
}
int main(){
    ll n;scanf("%lld",&n);
    int ans = 0;
    while(1){
       n = getnum(n);
       if(n != 2) ans++;
        else break;
    }
    printf("%d\n",ans + 1);
}

F:maki和tree
题意:给你一颗树n个点,告诉你每个点的颜色,由黑和白两种。然后问你只经过黑点一次的简单路径有几种。
思路:因为至今过黑点一次,那我们把每个黑点抓出来,把两个黑点之间的白点用并查集并成一个集合用sz数组存大小,然后通过容斥原理既可以推出每个黑点相邻的白色集合的贡献,累加每个黑点的贡献即可,复杂度 o ( n l o g n ) o(nlogn) o(nlogn)
代码:

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e6 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
vector<int>mp[maxn];
int has[maxn],pre[maxn];
ll sz[maxn];
char s[maxn];
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
int find(int x){
	return  x == pre[x] ? x : pre[x] = find(pre[x]);
}
void Union(int x,int y){
	int fx = find(x),fy = find(y);
	if(fx != fy)pre[fx] = fy,sz[fy] += sz[fx];
}
int main(){
	int n;n = read();
	for(int i = 1;i <= n;i++) sz[i] = 1,pre[i] = i;
	scanf("%s",s + 1);
	for(int i = 1;i <= n;i++) if(s[i] == 'B') has[i] = 1;
	for(int i = 1;i < n;i++){
		int u,v;u = read(),v = read();mp[u].push_back(v);mp[v].push_back(u);
		if(!has[u] && !has[v]) Union(u,v);
	}
	ll ans = 0;
	for(int i = 1;i <= n;i++){
		if(!has[i]) continue;
		ll res = 0;
		for(auto it : mp[i]){	
			if(has[it]) continue;
			ans += res * sz[find(it)];res += sz[find(it)];
		}
		ans += res;
	}
	printf("%lld\n",ans);
}

G:eli和字符串
题意:给你一个字符串,问你这个子串包含至少k个相同的字母的最小字串长度是多少。
思路:用26个vector存每个字母出现的位置,然后去遍历每个字母,长度即为pos[i + k - 1] - pos[i]+1,取min即可。

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
int n,k;
vector<int>v[30];
char s[maxn];
int main(){
	scanf("%d %d",&n,&k);
	scanf("%s",s);
	for(int i = 0;i < strlen(s);i++){
		v[s[i] - 'a'].push_back(i);
	}
	int ans = inf;
	for(int i = 0;i < 26;i++){
		if(v[i].size() < k) continue;
		for(int j = 0;j < v[i].size() - k + 1;j++){
			ans = min(ans,v[i][j + k - 1] - v[i][j] + 1);
		}
	}
	if(ans == inf) ans = -1;
	printf("%d\n",ans);
}

H:nozomi和字符串
题意:给你一个01字符串,你有k次机会把0变成1或者把1变成0,问你最长的全1或全0的字串的长度是多少。
思路:我变得k次一定是连续得0或者连续得1,因此我把0和1得位置存下来,然后思路和上题类似,注意处理边界,当位置为0时是在字符串最左边,当位置为最后一个1或者0时,位置是在字符串最右边,取max即可。

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
vector<int>v[2];
int main(){
	int n,k;scanf("%d %d",&n,&k);
	string s;cin >> s;
	for(int i = 0;i < s.length();i++){
		v[s[i] - '0'].push_back(i);
	}
	int ans = 0;
	if(v[0].size() <= k || v[1].size() <= k) printf("%d\n",s.length());
	else {
		for(int i = 0;i <= 1;i++){
			int res = 0;
			for(int j = 0;j < v[i].size() - k + 1;j++){
				int pos1;int pos2;
				if(j == 0) pos1 = 0;else pos1 = v[i][j - 1];
				if(j + k == v[i].size()) pos2 = s.length();else pos2 = v[i][j + k];
				res = max(res,pos2 - pos1 - 1);
			}
			ans = max(ans,res);
		}
		printf("%d\n",ans);
    }
}

I:nico和niconiconi
一道不知道做了多少次得傻逼线性dp,就是写的时候字符串有点长;

#include<bits/stdc++.h>
#define accept return 0
#define  mes(a, b)  memset(a, b, sizeof a)
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 3e5 + 5;
const int    maxm = 1e5 + 10;
const int    mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-6;
using namespace std;
int n;
ll dp[maxn];
char s[maxn];
int main(){
    int n;scanf("%d",&n);
    ll a,b,c;scanf("%lld %lld %lld",&a,&b,&c);
    scanf("%s",s + 1);
    for(int i = 1;i <= n;i++){
        if(i >= 4){
            if(s[i] == 'o' && s[i - 1] == 'c' && s[i - 2] == 'i' && s[i - 3] == 'n') dp[i] = max(dp[i - 4] + a,dp[i]);
        }
        if(i >= 6){
            if(s[i - 5] == 'n' && s[i - 4] == 'i' && s[i - 3] == 'c' && s[i - 2] == 'o' && s[i - 1] == 'n' && s[i] == 'i') dp[i] = max(dp[i - 6] + b,dp[i]);
        }
        if(i >= 10){
            if(s[i - 9] == 'n' && s[i - 8] == 'i' &&s[i - 7] == 'c' &&s[i - 6] == 'o' &&s[i - 5] == 'n' && s[i - 4] == 'i' && s[i - 3] == 'c' && s[i - 2] == 'o' && s[i - 1] == 'n' && s[i] == 'i') dp[i] = max(dp[i - 10] + c,dp[i]);
        }
        dp[i] = max(dp[i -1],dp[i]);
    }
    printf("%lld\n",dp[n]);
}

J先留坑,晚点补。留下代码:

#include<bits/stdc++.h>
typedef long long int ll;
const int maxn = 1e6 + 5;
const ll  mod = 1e9 + 7;
using namespace std;
struct Mat{
	ll mat[4][4];
	Mat(){
		memset(mat,0,sizeof(mat));
	}
	Mat operator * (const Mat o)const{
		Mat ans;
		for(int i = 0;i < 4;i++){
			for(int j = 0;j < 4;j++){
				for(int k = 0;k < 4;k++){
					ans.mat[i][j] = (ans.mat[i][j] + mat[i][k] * o.mat[k][j]) % (mod - 1);
				}
			}
		}
		return ans;
	}
}ans,res;
Mat matpow(Mat a,ll n){
	Mat ans;
	for(int i = 0;i < 4;i++) ans.mat[i][i] = 1;
	while(n){
		if(n & 1) ans = ans * a;
		a = a * a;
		n >>= 1;
	}
	return ans;
}
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;
}
void init1(){
	memset(ans.mat,0,sizeof ans);memset(res.mat,0,sizeof res);
	res.mat[0][0] = 0;res.mat[0][1] = 1;
	res.mat[1][0] = 1;res.mat[1][1] = 1;
}
void init2(){
	memset(ans.mat,0,sizeof ans);memset(res.mat,0,sizeof res);
	res.mat[0][0] = 0;res.mat[0][1] = 1;res.mat[0][2] = 0;
	res.mat[1][0] = 1;res.mat[1][1] = 1;res.mat[1][2] = 1;
	res.mat[2][0] = 0;res.mat[2][1] = 0;res.mat[2][2] = 1;
}
int main(){
	ll n,x,y,a,b;scanf("%lld %lld %lld %lld %lld",&n,&x,&y,&a,&b);
	if(n == 1) { printf("%lld\n",x % mod);return 0;}
	else if(n == 2) {printf("%lld\n",y % mod);return 0;}
	a = qpow(a%mod,b);x %= mod;y %= mod;
	init1();
	ans.mat[0][0] = 1;ans.mat[1][0] = 0;
	ans = ans * matpow(res,n - 1);
	ll ans1 = ans.mat[0][0];
	init1();
	ans.mat[0][0] = 0;ans.mat[1][0] = 1;
	ans = matpow(res,n - 1) * ans;ll ans2 = ans.mat[0][0];
	init2();
	ans.mat[0][0] = 0;ans.mat[1][0] = 0;ans.mat[2][0] = 1;
	ans = matpow(res,n - 1) * ans;ll ans3 = ans.mat[0][0];
	if(x % mod == 0 || y % mod == 0 || a % mod == 0) puts("0");
	else {
		printf("%lld\n",qpow(x,ans1) * qpow(y,ans2) % mod * qpow(a,ans3) % mod);
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值