Prototype

本题链接:登录—专业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;
}

提交结果:

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值