1 条题解
-
2
题目大意:有 组查询,每次查询一个数 的所有质数因子并按升序输出。
思路:由题目数据范围可得,查询次数范围 ,查询数字 的范围为 , 因此我们可以直接使用朴素筛法优化,即从 到 的暴力枚举去查找 的质数因子(其时间复杂度为 ),为了保证每次存的都是质数因子,我们会对其找到的因子 进行 的计算,把其 的倍数都筛掉,最后输出的时候要对我们存好的因子的数组 进行排序去重就解决了。
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; const int N = 2e5 + 9; int mp[N], cnt; void check(int x) { cnt = 0; for(int i = 2; i <= sqrt(x); i ++) { while(x % i == 0) { mp[++ cnt] = i; x /= i; } } if(x > 1) mp[++ cnt] = x; } void solve() { memset(mp, 0, sizeof(mp)); int n; cin >> n; check(n); sort(mp + 1, mp + 1 + cnt); for(int i = 1; i <= cnt; i ++) { if(mp[i] == mp[i - 1]) continue; cout << mp[i] << " "; } cout << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t = 1; cin >> t; while(t --) solve(); return 0; }
- 1
信息
- ID
- 1027
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 249
- 已通过
- 20
- 上传者