质因数分解 const int maxn = 5000005; // n:待分解质因数 // tot:不同质因数的值 // a[i]表示第i个质因数的值 // b[i]表示第i个质因数的指数 int a[maxn], b[maxn]; int factor(int n){ int temp, now; temp = (int)((double)sqrt(n) + 1); int tot = 0; now = n; for(int i=2;i<=temp;i++){ if(now % i == 0){ a[++tot] = i; b[tot] = 0; } while(now % i == 0){ ++b[tot]; now /= i; } } if(now != 1){ a[++tot] = now; b[tot] = 1; } return tot; } 质因数分解打表(不包含每个质因数的个数) const int N = 1e6 + 1; vector<int> prime_factor[N]; // 质因数分解打表 void init(){ int temp; for(int i = 2; i < N; i ++){ if(prime_factor[i].size() == 0){ for(int j = i; j < N; j += i){ temp = j; while(temp % i == 0){ prime_factor[j].push_back(i); temp /= i; } } } } }