#include <stdio.h>
#include <math.h>
// 這個函數用來進行質因數分解
void primeFactorization(unsigned long long n) {
int first = 1; // 用來控制輸出格式,避免第一個因數前有乘號 (*)
// 處理2這個特殊質數
int count = 0;
while (n % 2 == 0) { // 檢查n能否被2整除
n = n / 2;
count++; // 記錄2的次方
}
if (count > 0) {
if (!first) { // 如果 first 為 0(表示已經輸出過質因數),才輸出乘號 (*)
}
if (count > 1) {
printf("^%d", count
); // 只有當次方大於1時才輸出次方 }
first = 0; // 設定 first 為 0,表示已經輸出過質因數,接下來再輸出需要加乘號
}
// 從3開始,處理其他質數,這裡只檢查奇數
for (unsigned long long i
= 3; i
<= sqrt(n
); i
+= 2) { count = 0;
while (n % i == 0) { // 檢查n能否被i整除
n = n / i;
count++; // 記錄i的次方
}
if (count > 0) {
if (!first) { // 如果 first 為 0(表示已經輸出過質因數),才輸出乘號 (*)
}
if (count > 1) {
printf("^%d", count
); // 只有當次方大於1時才輸出次方 }
first = 0; // 設定 first 為 0,表示已經輸出過質因數
}
}
// 如果n大於1,表示n本身就是一個質數,直接輸出
if (n > 1) {
if (!first) { // 如果 first 為 0(表示已經輸出過質因數),才輸出乘號 (*)
}
printf("%llu", n
); // n本身就是質數,因為沒有次方,直接輸出 }
}
int main() {
unsigned long long n;
// 不斷讀取輸入的數字,直到沒有更多的輸入
while (scanf("%llu", &n
) != EOF
) { primeFactorization(n); // 調用質因數分解函數
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgovLyDpgJnlgIvlh73mlbjnlKjkvobpgLLooYzos6rlm6DmlbjliIbop6MKdm9pZCBwcmltZUZhY3Rvcml6YXRpb24odW5zaWduZWQgbG9uZyBsb25nIG4pIHsKICAgIGludCBmaXJzdCA9IDE7IC8vIOeUqOS+huaOp+WItui8uOWHuuagvOW8j++8jOmBv+WFjeesrOS4gOWAi+WboOaVuOWJjeacieS5mOiZnyAoKikKCiAgICAvLyDomZXnkIYy6YCZ5YCL54m55q6K6LOq5pW4CiAgICBpbnQgY291bnQgPSAwOwogICAgd2hpbGUgKG4gJSAyID09IDApIHsgIC8vIOaqouafpW7og73lkKbooqsy5pW06ZmkCiAgICAgICAgbiA9IG4gLyAyOwogICAgICAgIGNvdW50Kys7ICAvLyDoqJjpjIQy55qE5qyh5pa5CiAgICB9CiAgICBpZiAoY291bnQgPiAwKSB7CiAgICAgICAgaWYgKCFmaXJzdCkgeyAgLy8g5aaC5p6cIGZpcnN0IOeCuiAw77yI6KGo56S65bey57aT6Ly45Ye66YGO6LOq5Zug5pW477yJ77yM5omN6Ly45Ye65LmY6JmfICgqKQogICAgICAgICAgICBwcmludGYoIiAqICIpOwogICAgICAgIH0KICAgICAgICBwcmludGYoIjIiKTsgIC8vIOi8uOWHuuizquWboOaVuDIKICAgICAgICBpZiAoY291bnQgPiAxKSB7CiAgICAgICAgICAgIHByaW50ZigiXiVkIiwgY291bnQpOyAgLy8g5Y+q5pyJ55W25qyh5pa55aSn5pa8MeaZguaJjei8uOWHuuasoeaWuQogICAgICAgIH0KICAgICAgICBmaXJzdCA9IDA7ICAvLyDoqK3lrpogZmlyc3Qg54K6IDDvvIzooajnpLrlt7LntpPovLjlh7rpgY7os6rlm6DmlbjvvIzmjqXkuIvkvoblho3ovLjlh7rpnIDopoHliqDkuZjomZ8KICAgIH0KCiAgICAvLyDlvp4z6ZaL5aeL77yM6JmV55CG5YW25LuW6LOq5pW477yM6YCZ6KOh5Y+q5qqi5p+l5aWH5pW4CiAgICBmb3IgKHVuc2lnbmVkIGxvbmcgbG9uZyBpID0gMzsgaSA8PSBzcXJ0KG4pOyBpICs9IDIpIHsKICAgICAgICBjb3VudCA9IDA7CiAgICAgICAgd2hpbGUgKG4gJSBpID09IDApIHsgIC8vIOaqouafpW7og73lkKbooqtp5pW06ZmkCiAgICAgICAgICAgIG4gPSBuIC8gaTsKICAgICAgICAgICAgY291bnQrKzsgIC8vIOiomOmMhGnnmoTmrKHmlrkKICAgICAgICB9CiAgICAgICAgaWYgKGNvdW50ID4gMCkgewogICAgICAgICAgICBpZiAoIWZpcnN0KSB7ICAvLyDlpoLmnpwgZmlyc3Qg54K6IDDvvIjooajnpLrlt7LntpPovLjlh7rpgY7os6rlm6DmlbjvvInvvIzmiY3ovLjlh7rkuZjomZ8gKCopCiAgICAgICAgICAgICAgICBwcmludGYoIiAqICIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHByaW50ZigiJWxsdSIsIGkpOyAgLy8g6Ly45Ye66LOq5Zug5pW4aQogICAgICAgICAgICBpZiAoY291bnQgPiAxKSB7CiAgICAgICAgICAgICAgICBwcmludGYoIl4lZCIsIGNvdW50KTsgIC8vIOWPquacieeVtuasoeaWueWkp+aWvDHmmYLmiY3ovLjlh7rmrKHmlrkKICAgICAgICAgICAgfQogICAgICAgICAgICBmaXJzdCA9IDA7ICAvLyDoqK3lrpogZmlyc3Qg54K6IDDvvIzooajnpLrlt7LntpPovLjlh7rpgY7os6rlm6DmlbgKICAgICAgICB9CiAgICB9CgogICAgLy8g5aaC5p6cbuWkp+aWvDHvvIzooajnpLpu5pys6Lqr5bCx5piv5LiA5YCL6LOq5pW477yM55u05o6l6Ly45Ye6CiAgICBpZiAobiA+IDEpIHsKICAgICAgICBpZiAoIWZpcnN0KSB7ICAvLyDlpoLmnpwgZmlyc3Qg54K6IDDvvIjooajnpLrlt7LntpPovLjlh7rpgY7os6rlm6DmlbjvvInvvIzmiY3ovLjlh7rkuZjomZ8gKCopCiAgICAgICAgICAgIHByaW50ZigiICogIik7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiJWxsdSIsIG4pOyAgLy8gbuacrOi6q+WwseaYr+izquaVuO+8jOWboOeCuuaykuacieasoeaWue+8jOebtOaOpei8uOWHugogICAgfQoKICAgIHByaW50ZigiXG4iKTsgIC8vIOe1kOadn+aZguaPm+ihjAp9CgppbnQgbWFpbigpIHsKICAgIHVuc2lnbmVkIGxvbmcgbG9uZyBuOwoKICAgIC8vIOS4jeaWt+iugOWPlui8uOWFpeeahOaVuOWtl++8jOebtOWIsOaykuacieabtOWkmueahOi8uOWFpQogICAgd2hpbGUgKHNjYW5mKCIlbGx1IiwgJm4pICE9IEVPRikgewogICAgICAgIHByaW1lRmFjdG9yaXphdGlvbihuKTsgIC8vIOiqv+eUqOizquWboOaVuOWIhuino+WHveaVuAogICAgfQoKICAgIHJldHVybiAwOwp9