27 数论 素数与约数

1  判断是否为素数

/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//判断一个数是否是素数,用试除法,时间复杂的O(根号n)
#include<bits/stdc++.h>
using namespace std;
bool prime(int n)
{
    if(n<2) return false;
    for(int i=2;i<=n/i;i++)
        if(n%i==0) return false;
    return true;
}
int main()
 {
     int m;
     cin>>m;
     while(m--)
     {
         int n;
         cin>>n;
     if(prime(n)) puts("Yes");
     else puts("No");
     }
     return 0;
 }

/*到达胜利之前无法回头*/

2  分解质因数

/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//分解质因数,用试除法,时间复杂的O(根号n)
#include<bits/stdc++.h>
using namespace std;
void divide(int n)
{
    for(int i=2;i<=n/i;i++)
        if(n%i==0)
    {
        int s=0;//用s来记录
        while(n%i==0)//用来判断第i这个因子有多少个
        {
            n/=i;
            s++;
        }
        printf("%d %d\n",i,s);
        
    }
    if(n>1) printf("%d %d\n",n,1);
}
int main()
 {
     int m;
   scanf("%d",&m);
     while(m--)
     {
         int n;
         scanf("%d",&n);
      divide(n);
     }
     return 0;
 }

/*到达胜利之前无法回头*/

3  筛质数 求1~n中素数个数

/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//诶氏筛法 时间复杂的O(nloglogn)
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int primes[N],cnt;//prime用来存素数,cnt存个数
bool st[N];//用来记录合数
void get_primes(int n)
{
    for(int i=2;i<=n;i++)
    {
        if(!st[i])//假如是质数
        {
            primes[cnt++]=i;
            for(int j=i;j<=n;j+=i) st[j]=true;//可以用质数就把所有的合数都筛掉,标记合数
        }
    }
}
int main()
 {
     int n;
   scanf("%d",&n);
    get_primes(n);
    cout<<cnt<<endl;
     return 0;
 }
/*到达胜利之前无法回头*/
/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//线性筛法 时间复杂度O(n)
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int primes[N],cnt;//prime用来存素数,cnt存个数
bool st[N];//用来记录合数
void get_primes(int n)
{
    for(int i=2;i<=n;i++)//外层从2~n迭代,因为这毕竟算的是1~n中质数的个数,而不是某个数是不是质数的判定
    {
        if(!st[i]) primes[cnt++]=i;//判断是不是标记过,没的话则为素数,放进primes里
        for(int j=0;primes[j]<=n/i;j++)//判断一个数有没有最小因子
        {
            st[primes[j]*i]=true;//用最小质因子去筛合数,标记素数的倍数为合数
            if(i%primes[j]==0) break;//假如没有则跳出来
        }
    }
}
int main()
 {
     int n;
   scanf("%d",&n);
    get_primes(n);
    cout<<cnt<<endl;
     return 0;
 }
/*到达胜利之前无法回头*/

4 试除法求约数

/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//试除法求约数 时间复杂度O(根号n)
#include<bits/stdc++.h>
using namespace std;
vector<int> get_divisors(int n)//试除法求约数
{
    vector<int> res;//一个int数组存约数
    for(int i=1;i<=n/i;i++)
        if(n%i==0)//假如是约数
    {
        res.push_back(i);//放进数组中
        if(i!=n/i) res.push_back(n/i);//假如另一个约数不相同,则也放进数组中
    }
    sort(res.begin(),res.end());//从小到大排序一遍
    return res;
}
int main()
 {
     int n;
   scanf("%d",&n);
   while(n--)
   {
       int x;
       scanf("%d",&x);
       auto res=get_divisors(x);
       for(auto t:res) printf("%d ",t);//输出数组的每一个数
       puts("");
   }
     return 0;
 }
/*到达胜利之前无法回头*/

 5 求约数的个数

/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//求约数个数,结合哈希表来存数,套用一个公式前面图片有
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int main()
 {
     int n;
   scanf("%d",&n);
   unordered_map<int,int> hash;//定义一个hash表,来记录这个数的几次幂
   while(n--)
   {
       int x;
       scanf("%d",&x);
       for(int i=2;i<=x/i;i++)//用来算一个数能能分解一个数的几次幂
        while(x%i==0)
       {
           x/=i;
           hash[i]++;
       }
      if(x>1) hash[x]++;

   }
   ll res=1;//用来记录答案
   for(auto t:hash) res=res*(t.second+1)%mod;//结果可能比较大,对1e9+10取模
   printf("%lld\n",res);
     return 0;
 }
/*到达胜利之前无法回头*/

 6 求约数之和

/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//求约数之和,结合哈希表来存数,套用一个公式前面图片有
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int main()
 {
     int n;
   scanf("%d",&n);
   unordered_map<int,int> hash;//定义一个hash表,来记录这个数的几次幂
   while(n--)
   {
       int x;
       scanf("%d",&x);
       for(int i=2;i<=x/i;i++)//用来算一个数能能分解一个数的几次幂
        while(x%i==0)
       {
           x/=i;
           hash[i]++;
       }
      if(x>1) hash[x]++;

   }
   ll res=1;//用来记录答案
   for(auto t:hash)
   {
       int p=t.first,a=t.second;//p是当前的数,a是当前p的指数
       ll m=1;
       while(a--) m=(m*p+1)%mod;//算公式中的一部分
        res=res*m%mod;//安照公式把每一个部分乘起来
   }
   printf("%lld\n",res);//输出答案
     return 0;
 }
/*到达胜利之前无法回头*/

7 求最大公约数

/*
学acwing的算法基础课学来的,喜欢的话多多支持呀。
*/
//求最大公约数,用欧几里得算法(辗转相除法)
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;//用递归实现辗转相除法
}
int main()
 {
     int n;
   scanf("%d",&n);
   while(n--)
   {
       int a,b;
       scanf("%d%d",&a,&b);
       printf("%d\n",gcd(a,b));
   }
   return 0;
 }
/*到达胜利之前无法回头*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值