蓝桥杯每日一题2023.9.30

蓝桥杯大赛历届真题 - C&C++ 大学 B 组 - 蓝桥云课 (lanqiao.cn)

题目描述

题目分析

对于此题,首先想到了dfs进行一一找寻,注意每次不要将重复的算进去,故我们每次循环可以记录一个开始的位置,下一次到这个位置时,这个数就不会被重复选择

没有运行出来的代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long  ull;
const int N = 2e5 + 10;
ull ans, a[N];
void dfs(int n, ull start)
{
	if(n == 3) 
	{
		ull sum = a[0];
		for(int i = 1; i < 3; i ++)sum *= a[i];
		if(sum == 2021041820210418)ans ++;
		return;
	}
	for(ull i = start; i <= 2021041820210418; i ++)
	{
		a[n] = i;
		dfs(n + 1, i);
		a[n] = 0;
	}
}
int main()
{
	dfs(0, 1);
	cout << ans;
	return 0;
}

发现数字过大,运行时间过长,思考如何进行优化,想到这三个数都是2021041820210418的因数(这三个数乘积为2021041820210418),故我们可以先将2021041820210418的所有因数找出来,在这些因数中进行dfs

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef unsigned long long ull;
ull cnt, a[N], q[N], ans;
void dfs(int dep, ull start)
{
	if(dep == 3)
	{
		ull sum = q[0];
		for(int i = 1; i < 3; i ++)sum *= q[i];
		if(sum == 2021041820210418)
		{
			ans ++;
		}
		return;
	}
	for(ull i = 0; i < cnt; i ++)
	{
		q[dep] = a[i];
		dfs(dep + 1, i);
		q[dep] = 0;
	}
}
int main()
{
	for(ull i = 1; i <= 2021041820210418 / i; i ++)
	{
		if(2021041820210418 % i)continue;
		a[cnt ++] = i;
		if(i != 2021041820210418 / i)a[cnt ++] = 2021041820210418 / i;
	}
	dfs(0, 0);
	cout << ans;
	return 0;
}

得到正解2430

当然也可以一进行纯纯暴力

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef unsigned long long ull;
ull cnt, a[N], q[N], ans;
int main()
{
	for(ull i = 1; i <= 2021041820210418 / i; i ++)
	{
		if(2021041820210418 % i)continue;
		a[cnt ++] = i;
		if(i != 2021041820210418 / i)a[cnt ++] = 2021041820210418 / i;
	}
	for(int i = 0; i < cnt; i ++)
	{
		for(int j = 0; j < cnt; j ++)
		{
			for(int k = 0; k < cnt; k ++)
			{
				if(a[i] * a[j] * a[k] == 2021041820210418)ans ++;
			}
		}
	}
	cout << ans;
	return 0;
}
相关推荐
skylijf5 小时前
2026 高项第 6 章 预测考点 + 练习题(共 12 题,做完稳拿分)
笔记·程序人生·其他·职场和发展·软件工程·团队开发·产品经理
一江寒逸8 小时前
零基础从入门到精通 AI Agent 开发(全栈保姆级教程)附加篇:AI Agent 面试八股文全集
人工智能·面试·职场和发展
久菜盒子工作室8 小时前
面试经验|产品经理|自我介绍
面试·职场和发展·产品经理
优秀13510 小时前
计算机基础面试重点知识
网络·面试·职场和发展
hqyjzsb10 小时前
AI培训课程怎么设计才有效?
人工智能·职场和发展·aigc·产品经理·学习方法·业界资讯·设计语言
啊哦呃咦唔鱼13 小时前
LeetCode双指针合集
算法·leetcode·职场和发展
alphaTao13 小时前
LeetCode 每日一题 2026/4/13-2026/4/19
算法·leetcode·职场和发展
岁岁种桃花儿14 小时前
面试全系列之【Kafka】之【经典版】系列
面试·职场和发展·kafka
Jay-r14 小时前
当“同事.skill”刷爆GitHub:AI正把职场经验变成可复制的“技能包”
人工智能·职场和发展·生活·技术美术·程序员创富
studyForMokey15 小时前
【Android面试】动画 & Bitmap
android·面试·职场和发展