质因子的分解

题目:蓝桥杯 --完全平方数

https://www.acwing.com/problem/content/3494/

一个平方数的本质是成偶数对个的质因子的乘积,不可能存在奇数个质因子

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n;


int main()
{
    LL res=1;
    cin>>n;
    for(int i=2;i<=n/i;i++){
        if(n%i==0){     //是质因子,因为res在更新,会先将质因子除完
            LL cnt=0;
            while(n%i==0){
                n/=i;
                cnt++;
            }
            if(cnt%2) res*=i;   //每种质因子数量必须是偶数,n的一种质因子如果是偶数就不需要再要这个质因子
        }   
    }
    if(n>0) res*=n;
    cout << res;
    return 0;
}


posted @ 2022-03-15 20:36  秋月桐  阅读(122)  评论(0编辑  收藏  举报