1103. Integer Factorization (30)

本文探讨如何利用深度遍历搜索技术解决特定数学问题,包括加入限制条件进行剪枝优化,通过实例演示算法实现过程。

1.根据题目的特点,n的最大值不超过400,可以使用深度遍历搜索答案

2.加入限制条件进行剪枝,如i的p次方不能大于n-k+1,因为剩下的数至少均为1,剩下还需要k-1个数需要进行分配,每个数至少分配为1,共k-1,所以i的p次方最大只能为n-(k-1)=n-k+1,而最大值不能超过上一次分配的。


//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
void dfs(int n, int count, int p,int preNum, vector<int>&ans,int ansSum, vector<pair<int,vector<int>>>&totalAns, vector<vector<int>>&num)
{
	if (n == 0 && count == 0)
	{
		totalAns.push_back({ansSum, ans});
	}
	else if ((n == 0 && count != 0) || (n != 0 && count == 0))
		return;
	else
	{
		for (int i = min(n - count+1,preNum); i >= 1; i--)
		{//进行剪枝
			if (num[i][p - 1] != -1 && num[i][p - 1]<=n-count+1)
			{
				ans.push_back(i);
				ansSum += i;
				dfs(n - num[i][p - 1], count - 1, p,i, ans,ansSum, totalAns, num);
				ansSum -= i;
				ans.pop_back();
			}
		}
	}
}
bool cmp(const pair<int, vector<int>>&a, const pair<int, vector<int>>&b)
{
	if (a.first > b.first)
		return true;
	else if (a.first == b.first)
	{
		for (int i = 0; i < a.second.size(); i++)
		{
			if (a.second[i] > b.second[i]) return true;
			else if (a.second[i] < b.second[i]) return false;
		}

	}
	else return false;
}
int main(void)
{
	
	int n, k, p;
	cin >> n >> k >> p;
	vector<vector<int>> num(401, vector<int>(7, 0));
	for (int i = 1; i < 401; i++)
	{
		num[i][0] = i;
		for (int j = 1; j < 7; j++)
		{
			if (num[i][j - 1] == -1) num[i][j] = -1;
			else
			{
				num[i][j] = num[i][j - 1] * i;
				if (num[i][j] > 400) num[i][j] = -1;
			}
		}
	}
	vector<int> ans(0);
	vector<pair<int,vector<int>>> totalAns(0);
	int ansSum = 0;

	dfs(n, k, p,n, ans,ansSum, totalAns, num);
	sort(totalAns.begin(), totalAns.end(), cmp);
	if (totalAns.size() != 0)
	{
		cout << n << " = ";
		for (int i = 0; i < totalAns[0].second.size(); i++)
		{
			cout << totalAns[0].second[i] << "^" << p;
			if (i != totalAns[0].second.size() - 1)
			{
				cout << " + ";
			}
		}
		cout << endl;
	}
	else 
	{
		cout << "Impossible" << endl;
	}

	return 0;
}


To determine the count of 'good' integers between $ l $ and $ r $, where a 'good' integer is defined as a number whose prime factors all have at least two digits, we need to understand the constraints and properties of such numbers. ### Understanding the Criteria A 'good' integer must have all of its prime factors with at least two digits. This means that all prime factors of the number must be greater than or equal to 11, as 11 is the smallest two-digit prime number. Any number that has a prime factor with only one digit (i.e., 2, 3, 5, or 7) cannot be classified as a 'good' integer. To count such numbers efficiently, we can use a sieve-like approach to precompute all numbers between $ l $ and $ r $ that meet the criteria. ### Algorithm Overview 1. **Sieve of Eratosthenes**: Generate all prime numbers up to a certain limit (e.g., $ r $). 2. **Prime Factorization**: For each number between $ l $ and $ r $, determine its prime factors. 3. **Check Criteria**: Verify if all prime factors of each number have at least two digits. 4. **Count Good Integers**: Count the numbers that satisfy the criteria. ### Code Implementation Here is a Python implementation of the algorithm described above: ```python def sieve(limit): is_prime = [True] * (limit + 1) is_prime[0] = is_prime[1] = False for i in range(2, int(limit**0.5) + 1): if is_prime[i]: for multiple in range(i * i, limit + 1, i): is_prime[multiple] = False return [i for i, prime in enumerate(is_prime) if prime] def is_good(n, primes): # Check if all prime factors of n have at least two digits if n < 2: return False for p in primes: if p * p > n: if n >= 10: break else: return False if n % p == 0: if p < 10: return False while n % p == 0: n //= p return n >= 10 or n == 1 def count_good_integers(l, r): primes = sieve(r) count = 0 for num in range(l, r + 1): if is_good(num, primes): count += 1 return count # Example usage l = 100 r = 200 result = count_good_integers(l, r) print(f"The count of 'good' integers between {l} and {r} is {result}") ``` ### Explanation of the Code - **Sieve of Eratosthenes**: The `sieve` function generates all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm. - **Prime Factorization**: The `is_good` function checks if all prime factors of a given number $ n $ have at least two digits. It iterates through the list of primes and checks if any of them divide $ n $. If a one-digit prime divides $ n $, it returns `False`. - **Counting Good Integers**: The `count_good_integers` function iterates through the range $[l, r]$ and counts the numbers that are classified as 'good' integers based on the criteria defined in the `is_good` function. ### Complexity The time complexity of this approach is dominated by the Sieve of Eratosthenes, which is $ O(n \log \log n) $, where $ n $ is the upper bound $ r $. The factorization step for each number in the range $[l, r]$ contributes additional complexity, but it is generally efficient due to the use of precomputed primes. ### Optimization To further optimize the algorithm, precompute the smallest prime factor (SPF) for each number up to $ r $. This allows for faster factorization of numbers in the range $[l, r]$. ```python def precompute_spf(limit): spf = list(range(limit + 1)) for i in range(2, int(limit**0.5) + 1): if spf[i] == i: for multiple in range(i * i, limit + 1, i): if spf[multiple] == multiple: spf[multiple] = i return spf def is_good_optimized(n, spf): if n < 2: return False while n > 1: p = spf[n] if p < 10: return False while n % p == 0: n //= p return True def count_good_integers_optimized(l, r): spf = precompute_spf(r) count = 0 for num in range(l, r + 1): if is_good_optimized(num, spf): count += 1 return count # Example usage l = 100 r = 200 result = count_good_integers_optimized(l, r) print(f"The count of 'good' integers between {l} and {r} is {result}") ``` This optimized approach reduces the time required for factorization by leveraging precomputed smallest prime factors, making it more efficient for larger ranges. ### Conclusion The count of 'good' integers between $ l $ and $ r $ can be efficiently determined using a sieve-based approach to precompute prime factors and verify the criteria for each number in the range.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值