判断质数
bool is_prime(long long a){
if(a<2) return false;
for(int i=2;i<=a/i;i++){ //i <= a/i;
if(a%i==0) return false;
}
return true;
}
分解质因数
原题链接
#include <bits/stdc++.h>
using namespace std;
void divide(long long a){
for(int i=2;i<=a/i;i++)//降低时间
if(a%i==0) {
int s=0;
while(a%i==0){
a=a/i;
s++;
}
cout<<i<<" "<<s<<endl;
}
if(a>1) cout<<a<<" "<<1<<endl;//最后一个要特判
puts("");
}
int main(){
int n;
long long a;
cin>>n;
while(n--){
cin>>a;
divide(a);
}
return 0;
}
埃式筛法
原题链接
void get_prime(int n){
for(int i=2;i<=n;i++){
if(st[i]) continue;
for(int j=i+i;j<=n;j+=i) st[j]=true;
cnt++;
}
}