蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!
Hello, 大家好哇!本初中生蒟蒻今天来讲一下AtCoder Beginner Contest 284的D题——Happy New Year 2023!
===========================================================================================
原题
Problem Statement
You are given a positive integer
N. It is known that can be represented as
using two different prime numbers
and
Find and
.
You have test cases to solve.
Constraints
- All values in the input are integers.
can be represented as
using two different prime numbers
and
题目大概
给出一个整数,找出两个质数
, 满足
思路
这道题想必大家都能想到用分解质因数的模板,例如:
void divide(int x)
{
for (int i = 2; i <= n / i; i ++)
{
if (n % i == 0)
{
int s = 0;
while (n % i == 0)
{
n /= i;
s ++;
}
cout << i << " " << s << endl;
}
}
if (n > 1) cout << n << " " << 1 << endl;
}//此函数可输出x的质因子以及每个质因子的个数
不过,稻花香里说丰年,听取声一片!
为什么呢?
我们来分析一下:分解质因数的时间复杂度是,而
最大为
,根号
为,时间超了一点点~~~
怎么办呢?
我们可以再求出一个数之后直接break!然后,通过算数可直接求出另一个数。这样时间复杂度就大大的降低了,故AC了!
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL T, N , M;
unordered_map<LL, LL> tmp;
void dvd(LL x)
{
int ss = 0;
M = N;
for (LL i = 2; i <= N / i; i ++)
{
if (N % i == 0)
{
while (N % i == 0)
N /= i, ss ++;
tmp[ss] = i;
break;
}
}//模板
if(ss == 1) tmp[2] = int(sqrt(M / tmp[1]));
else tmp[1] = M / (tmp[2] * tmp[2]);//算出另一个数
}
int main()
{
cin >> T;
while (T --)
{
cin >> N;
dvd(N);
cout << tmp[2] << " " << tmp[1] << endl;
}
return 0;
}
今天就到这里了!
大家有什么问题尽管提,我都会尽力回答的!最后,祝大家新年快乐!
吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!
最后庆祝一下,在千辛万苦,跋山涉水中终于棕名了!虽然不太高,但我相信,只要坚持绿名也并非无望!大家都是从艰辛中走出的,路途坎坷,虽然我也有一段放弃时间,不过很快好转过来!只要学下去,就会好起来的!Fighting!

最后,学计算机的同学我最后提一个小小的建议——不要玩游戏!真的太耽误学业了,大家可以看到Feb-Jul我几乎没什么长进,就是因为玩游戏啊~~~
本文是一位初中生分享在AtCoder编程竞赛中的D题解题思路,题目要求找到能表示给定整数的两个不同质数。作者提供了分解质因数的代码模板,并讨论了如何优化算法以降低时间复杂度,最终给出了AC代码。文章还提及了作者对学习编程的态度和建议,特别是避免游戏对学业的影响。
377

被折叠的 条评论
为什么被折叠?



