Blue Bridge Cup Daily Question 2023.9.30

Lanqiao Cup Competition Past Questions - C&C++ University Group B - Lanqiao Cloud Class (lanqiao.cn)

Question description

 Question analysis

For this question, I first thought of dfs to search one by one. Be careful not to count duplicates each time. Therefore, we can record a starting position each time we loop. The next time we reach this position, this number will not be repeatedly selected.

Code that does not run:

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

I found that the number was too large and the running time was too long. I thought about how to optimize it. I thought that these three numbers are all factors of 2021041820210418 (the product of these three numbers is 2021041820210418), so we can first find out all the factors of 2021041820210418. Among these factors dfs in

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

Get the correct answer 2430

Of course, you can also engage in pure violence

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

Supongo que te gusta

Origin blog.csdn.net/m0_75087931/article/details/133440441
Recomendado
Clasificación