Prototype

发布于:2024-03-30 ⋅ 阅读:(54) ⋅ 点赞:(0)

本题链接:登录—专业IT笔试面试备考平台_牛客网.

题目:

样例:

输入
168
输出
42

思路:

        根据题意, 吸收怪物是 w * n ,其中 怪物 n 一定是质数,并且  AlexMercer 可以变成 w 的任一因子。

        从中我们可以知道,这是将 w 分解成质因数,然后累乘即可。

        质因数模板如下:

inline void divide(int x)
{
	for(int i = 2;i <= x / i;++i)
	{
		if(x % i == 0)
		{
			int s = 0;
			while(x % i == 0) x /= i,++s;
			cout << i << ' ' << s << endl;
		}	
	}	
	if(x > 1) cout << x << ' ' << 1 << endl;
	cout << endl;
}

代码详解如下:

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define endl '\n'
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define All(x) x.begin(),x.end()
#pragma GCC optimize(3,"Ofast","inline")
#define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;
inline void solve();

signed main()
{
//	freopen("a.txt", "r", stdin);
	IOS;
	int _t = 1;
// 	cin >> _t;
	while (_t--)
	{
		solve();
	}
	return 0;
}

inline void solve()
{
    int n,ans = 1;
    cin >> n;
	int t = n;
	
	// 对 w 进行质因数分解
	for(int i = 2;i <= t / i;++i)
	{
		if(t % i == 0)
		{
			ans *= i;	// 累乘质因数答案
			while(t % i == 0) t /= i;
		}
	}
	if(t > 0) ans *= t;	// 扫尾累乘答案
	cout << ans << endl;
}

提交结果:


网站公告

今日签到

点亮在社区的每一天
去签到