acwing学习笔记-数学知识


数学知识

数学真是一个令人摸不着头脑的一个东西,小小的质数都可以把你拿捏得死死的


一、质数

1、试除法判定质数

朴素做法(容易超时,时间复杂度O(n))

#include <iostream>
using namespace  std;

int check_primes(int x)
{
    if(x==1)
    return 0;
  for (int i = 2; i < x; i++)
  {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

int main()
{
  int n;
  cin >> n;
  while (n--)
  {
    int a;
    cin >> a;
    if (check_primes(a))
      cout << "Yes"<<endl;
    else
      cout << "No"<<endl;
  }
  return 0;
}

2、开方判定质数

因为一个数n可以分为两个数的乘积,一个比n的开平方根小,另一个比开平方大,所以遍历到sqrt(n)即可(O(sqrt(n)))

#include <iostream>
using namespace  std;

int check_primes(int x)
{
    if(x==1)
    return 0;
  for (int i = 2; i <= x/i; i++)
  {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

int main()
{
  int n;
  cin >> n;
  while (n--)
  {
    int a;
    cin >> a;
    if (check_primes(a))
      cout << "Yes"<<endl;
    else
      cout << "No"<<endl;
  }
  return 0;
}

3、分解质因数

给定 n个正整数 ai,将每个数分解质因数,并按照质因数从小到大的顺序输出每个质因数的底数和指数。


#include <iostream>
#include <algorithm>
using namespace std;

void divide(int x)
{
  for (int i = 2; i <= x / i; i++)
  {
    if (x % i == 0)
    {
      int s = 0;
      while (x % i == 0)
      {
        x /= i;
        s++;
      }
      printf("%d %d\n", i, s);
    }
  }
  if (x > 1)
  {
    printf("%d 1\n", x);
  }
  printf("\n");
}

int main()
{
  int n;
  cin >> n;
  while (n--)
  {
    int a;
    cin >>a;
    divide(a);
  }
  return 0;
}

4、筛质数

1~n中有n/lnn个质数

(1)、埃氏筛法

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1000010;
int st[N], primes[N],cnt;

void get_primes(int x)
{
  for (int i = 2; i <= x; i++)
  {
    if (!st[i])
      primes[cnt++] = i;
    for (int j = i + i; j <= x; j += i)
    {
      st[j] = 1;
    }
  }
  printf("%d", cnt);
}

int main()
{
  int n;
  cin >> n;
  get_primes(n);
  return 0;
}

(2)、线性筛

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1000010;
int st[N], primes[N], cnt;

void get_primes(int x)
{
  for (int i = 2; i <= x; i++)
  {
    if (!st[i])
      primes[cnt++] = i;
    for (int j = 0; primes[j] <= x/i; j++)
    {
      st[i * primes[j]] = 1;
      if (i % primes[j] == 0)
        break;
    }
  }
  printf("%d", cnt);
}

int main()
{
  int n;
  cin >> n;
  get_primes(n);
  return 0;
}

二、约数

1、试除法求约数

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  int T;
  cin >> T;
  while (T--)
  {
    int n;
    cin >> n;
    vector<int> res;
    for (int i = 1; i <= n / i; i++)
    {
      if (n % i == 0)
      {
        res.push_back(i);
        if (n / i != i)
          res.push_back(n / i);
      }
    }
    sort(res.begin(), res.end());
    for (auto x : res) cout << x << " ";
    cout << endl;

  }
}

2、约数个数

#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int mod = 1e9 + 7;
int main()
{
  int t;
  cin >> t;
  unordered_map<int, int> h;//无序map集合存储质因数和质因数对应的出现的最大次数
  while (t--)
  {
    int n;
    cin >> n;
    for (int i = 2; i <= n / i; i++)
    {
      while (n % i == 0)
      {
        h[i]++;
        n /= i;
      }

    }
    if (n > 1)h[n]++;

  }
  long long res = 1;
  for (auto iter = h.begin(); iter != h.end(); iter++)
  {
    res = res * (iter->second + 1) %mod;
  }
  cout << res;
  return 0;
}

总结

还在持续更新中

  • 17
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Python学习笔记》是由皮大庆编写的一本关于Python语言学习的教材。在这本书中,作者详细介绍了Python语言的基础知识、语法规则以及常用的编程技巧。 首先,作者简要介绍了Python语言的特点和优势。他提到,Python是一种易于学习和使用的编程语言,受到了广大程序员的喜爱。Python具有简洁、清晰的语法结构,使得代码可读性极高,同时也提供了丰富的库和模块,能够快速实现各种功能。 接着,作者详细讲解了Python的基本语法。他从变量、数据类型、运算符等基础知识开始,逐步介绍了条件语句、循环控制、函数、模块等高级概念。同时,作者通过大量的示例代码和实践案例,帮助读者加深对Python编程的理解和应用。 在书中,作者还特别强调了编写规范和良好的编程习惯。他从命名规范、注释风格、代码缩进等方面指导读者如何写出清晰、可读性强的Python代码。作者认为,良好的编程习惯对于提高代码质量和提高工作效率非常重要。 此外,作者还介绍了Python的常用库和模块。他提到了一些常用的库,如Numpy、Pandas、Matplotlib等。这些库在数据处理、科学计算、可视化等领域有广泛的应用,帮助读者更好地解决实际问题。 总的来说,《Python学习笔记》是一本非常实用和全面的Python学习教材。通过学习这本书,读者可以系统地学习和掌握Python编程的基础知识和高级应用技巧,为以后的编程学习和工作打下坚实的基础。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值