Prototype
原创 于 2024-03-27 12:00:32 发布 · 粉丝可见 · 548 阅读 · 3 · 4 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/137069893
本题链接: 登录—专业IT笔试面试备考平台_牛客网 .
题目:


样例:
思路:
根据题意, 吸收怪物是 w * n ,其中 怪物 n 一定是质数,并且 AlexMercer 可以变成 w 的任一因子。
从中我们可以知道,这是将 w 分解成质因数,然后累乘即可。
质因数模板如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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; }
|
代码详解如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #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() {
IOS; int _t = 1;
while (_t--) { solve(); } return 0; } inline void solve() { int n,ans = 1; cin >> n; int t = n; 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; }
|
提交结果:
