10分求助,其他的TLE
查看原帖
10分求助,其他的TLE
1127424
damuzhi楼主2024/3/10 18:13
#include <bits/stdc++.h>

using namespace std;

bool is_prime(long long a){
    if(a < 2){
        return 0;
    }
    for(long long i = 2; i <= sqrt(a); i++){
        if(a % i == 0){
            return 0;
        }
    }
    return 1;
}

// short num[1000000];

struct yi{
    long long yi;
    long long n;
};

vector<yi> v;

int main() {
    long long a;
    cin >> a;
    if(is_prime(a)){
        cout << a << endl;
        return 0;
    }
    long long c = a;
        for(long long i = 2; i <= a/2; i++){
            if(c%i == 0){
                if(is_prime(i)){
                    yi tmp;
                    tmp.n = 0;
                    tmp.yi = i;
                    v.push_back(tmp);
                    while(c % i == 0){
                        v[v.size()-1].n++;
                        c /= i;
                    }
                }
            }
        }
    bool fir = 0;
    for(vector<yi>::iterator i = v.begin(); i != v.end(); i++){
        if((*i).n > 0){
            if(!fir){
                if((*i).n > 1){
                    cout << (*i).yi << "^" << (*i).n;
                }
                else{
                    cout << (*i).yi;
                }
                fir = 1;
            }
            else{
                if((*i).n > 1){
                    cout << " * " << (*i).yi << "^" << (*i).n;
                }
                else{
                    cout << " * " << (*i).yi;
                }
            }
        }
    }
    cout << endl;
    return 0;
}
2024/3/10 18:13
加载中...