题目
TUOJ
https://sim.csp.thusaac.com/contest/32/problem/1
思路
核心思路:用map<int,int>mp存储每个因子的幂次,从因子p=2开始,如果能整除就除p,需要注意的是通过这种试除法得到的自然都是素数因子

代码
可以让AI总结一下代码逻辑
- 输入处理:读取查询次数 q。对于每组数据,输入待化简的数 n 和阈值 k。
- 质因数分解 :通过从 2 开始递增的整数 p 尝试整除 n,统计 n 的每个质因子及其出现的次数(幂次),并存储在 map<ll, int> mp 中。
- 筛选与累乘 :遍历 map 中的质因子。如果某个质因子的出现次数 t.second≥k,则计算其幂次项 p^c 并累乘到
ans中。 - 输出结果 :输出最终的累乘积
ans。
cpp
/*
因子分解
*/
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
map<ll,int>mp;
void solve(){
int q; cin>>q;
while(q--){
mp.clear();//WARN:全局数组记得每组数据都清空,不然就弄成局部的
ll n; int k; cin>>n>>k;
int p=2; //因子
while(n!=1){
if(n%p==0){
mp[p]++;
n/=p;
}
else p++;
}
ll ans=1;
for(auto t:mp)
if(t.second>=k) ans*=pow(t.first,t.second); //warn:乘上该因子的幂次
cout<<ans<<endl;
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
solve();
return 0;
}