质数与约数

1.试触发判定质数 o(根号N)

如果a*b=n ,那么一定有a<n且b>n或者同时等于n,如果a存在则b一定存在,如果b存在那么a一定存在,我们不必枚举到n,所以我们只需要枚举到根号n即可

#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
    cin >> t;
    while(t--){
        cin >> n;
        if(n==1){
            cout << "No" << endl;
            continue;
        }
        int flag=0;
        for(int i=2;i<=n/i;i++){
            if(n%i==0){
                cout << "No" << endl;
                flag=1;
                break;
            }
        }
        if(!flag) {
            cout << "Yes" << endl;
        }
    }
    return 0;
}

2.分解质因数

设n为任意正整数,则可以被n整除的数就是他的因数,如果这个数是质数,我们
称之为这个数为n的质因数,根据算数基本定理,一个数一定可以被他的质因数
或者质因数的次方的乘积唯一表示
即 n=(p1^x1)*(p2^x2)......(pn^xn)   p为n的质因数,x为可能的次方
我们同时可以有n的约数个数为 (x1+1*(x2+1)*...*(xn+1)
n的约数之和为(p1^0+p1^1+...+p1^x1)*(p2^0+p2^1+...+p2^x2)*...
*(pn^0+pn^1+..+pn^xn)
#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
    cin >> t;
    while(t--){
        cin >> n;
       
        for(int i=2;i<=n/i;i++){//这里循环的时候有一个数学知识:n当中最多只包含一个>=根号n的质数,因此我们只需要枚举到根号n即可
            int s=0;
            if(n%i==0){//只要成立则i一定是质数,因为2~i-1已经被除干净了
                while(n%i==0){
                    n=n/i;
                    s++;
                }
                cout << i << " " << s << endl;
            }
            
        }
        if(n>1) cout << n << " " << 1 <<endl;
        cout << endl;
    }
    return 0;
}

3.筛质数

设x为小于n的质数,则我们利用x的任意倍(<n)筛去n内的一部分数,我们从2~n枚举即可
#include<bits/stdc++.h>
using namespace std;
int n;
const int N=1e6+10;
bool st[N];
int main(){
    cin >> n;
    int ans=0;
    for(int i=2;i<=n;i++){
        if(!st[i]){//能进if语句说明一定是质数,不是质数的话一定会被前面的数筛掉
            ans++;
            for(int j=i;j<=n;j+=i){
                st[j]=true;
            }
        }
    }
    cout << ans;
    return 0;
}

4.试除法求约数

#include<bits/stdc++.h>
using namespace std;
vector<int>v;
int main(){
    int t,n;
    cin >> t ;
    while(t--){
        cin >> n;
        v.clear();
        for(int i=1;i<=n/i;i++){ //同判断质数时原理一样
            if(n%i==0){
                if(n/i==i){
                    v.push_back(i);
                    continue;
                }
                v.push_back(i);
                v.push_back(n/i);
            }
        }
        sort(v.begin(),v.end());
        for(auto t=v.begin();t!=v.end();t++){
            cout << *t << " ";
        }
        cout << endl;
    }
    return 0;
}

5.约数个数

#include<bits/stdc++.h>
#define int long long
using namespace std;
int mod=1e9+7;
int t,n;
unordered_map<int,int>p;//是无序的
signed main(){
    cin >> t;
    while(t--){
        cin >> n;
        for(int i=2;i<=n/i;i++){
            while(n%i==0){
                p[i]++;
                n/=i;
            }
        }
        if(n>1) p[n]++;
    }
    int res=1;
    for(auto t:p){
        res=(res*(t.second+1))%mod;
    }
    cout << res;
    return 0;
}

6.约数之和

#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n;
int mod=1e9+7;
signed main(){
    cin >> t ;
    unordered_map<int,int>p;
    while(t--){
        cin >> n;
        for(int i=2;i<=n/i;i++){
           
            while(n%i==0){
                p[i]++;
                n=n/i;
            }
         
        }
        if(n>1){
            p[n]++;
        }
    }
    int res=1,sum=0;
    for(auto t:p){
        sum=1;
        for(int i=1;i<=t.second;i++){
            sum=(sum*t.first+1)%mod;
        }
        res=(res*sum)%mod;
    }
    cout << res;
    return 0;
}

7.最大公约数

#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int main(){
    cin >> n;
    while(n--){
        cin >> a >> b;
        cout << __gcd(a,b)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值