蓝桥杯--数的拆分

蓝桥杯省赛 数的拆分,应该是一道数论的题目

连接:数的拆分


对有所有的样例,应该用long long 来表示。

n的范围是1e18次方,暴力绝对是行不通的,不能用暴力来解决。

这是一道数学的题目,需要对题目进行分析。因为要求y1 y2大于2。

关键点看出来 y1=2和y2等于3这种情况是一定符合的,所以我们对n进行质因数分解。

只要求出1e18开五次根号下面的是所有素数即可,大概400多,如果一个数字分解质因数,只出现了一次,输出no,因为此时不符合y2和y1都大于等于2的条件。当质因数分解完之后,需要判断n是否为平方数或者立方数即可,因为前面的质因数分解都是符合条件的,当一共数在n出现x次,那么肯定能拆分为2a+3b的形式,所以不管前面有多少组质因数,只要是符合条件的最后指数一定会是y1=2,y2=3的形式。

欧拉筛求质数,时间复杂度是on,速度快

bash 复制代码
void getprime()
{
  int i, j;
  for (i = 2; i <= 4500; i++)
  {
    if (!st[i])
    {
      prime.push_back(i);
    }
    for (j = 0; i * prime[j] <= 4500; j++)
    {
      st[i * prime[j]] = 1;
      if (i % prime[j] == 0)
      {
        break;
      }
    }
  }
}

求三次方的

bash 复制代码
int cbr(int x)
{
  return x * x * x;
}

校验是否为一个数字的三次方或者平方

bash 复制代码
bool check(int x)
{
  int a = (int)sqrt(x);
  if (a * a == x) // 说明是一个数字的平方
  {
    return 1; // 是整数
  }
  int y = (int)cbrt(x); // 立法跟
  if (cbr(y) == x || cbr(y + 1) == x || cbr(y - 1) == x)
  { // 数字太大有误差
    return 1;
  }
  return 0;
}

主要逻辑方法

bash 复制代码
void solve()
{
  int n;
  cin >> n;
  for (int i = 0; i < prime.size(); i++)
  {
    int x = prime[i]; // 得到对应的质数
    int cur = 0;
    if (n % x == 0)
    {
      while (n % x == 0)
      {
        n /= x;
        cur++;
      }
    }
    if (cur == 1)
    {
      cout << "no" << endl;
      return;
    }
  }
  // n已经变小了
  if (check(n))
  {
    cout << "yes" << endl;
  }
  else
  {
    cout << "no" << endl;
  }
}

main方法

bash 复制代码
signed main()
{
  // 模板而已
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t;
  t = 1;
  cin >> t;

  getprime();
  while (t--)
  {
    solve();
  }
  return 0;
}

全代码

bash 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> prime;
int st[5000];

// 线性筛保证时间是ON
void getprime()
{
  int i, j;
  for (i = 2; i <= 4500; i++)
  {
    if (!st[i])
    {
      prime.push_back(i);
    }
    for (j = 0; i * prime[j] <= 4500; j++)
    {
      st[i * prime[j]] = 1;
      if (i % prime[j] == 0)
      {
        break;
      }
    }
  }
}
int cbr(int x)
{
  return x * x * x;
}
// 检验上是否为某个数字的立方根 平方跟或者其他的
bool check(int x)
{
  int a = (int)sqrt(x);
  if (a * a == x) // 说明是一个数字的平方
  {
    return 1; // 是整数
  }
  int y = (int)cbrt(x); // 立法跟
  if (cbr(y) == x || cbr(y + 1) == x || cbr(y - 1) == x)
  { // 数字太大有误差
    return 1;
  }
  return 0;
}
void solve()
{
  int n;
  cin >> n;
  for (int i = 0; i < prime.size(); i++)
  {
    int x = prime[i]; // 得到对应的质数
    int cur = 0;
    if (n % x == 0)
    {
      while (n % x == 0)
      {
        n /= x;
        cur++;
      }
    }
    if (cur == 1)
    {
      cout << "no" << endl;
      return;
    }
  }
  // n已经变小了
  if (check(n))
  {
    cout << "yes" << endl;
  }
  else
  {
    cout << "no" << endl;
  }
}
signed main()
{
  // 模板而已
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t;
  t = 1;
  cin >> t;

  getprime();
  while (t--)
  {
    solve();
  }
  return 0;
}
相关推荐
iAkuya12 分钟前
(leetcode)力扣100 62N皇后问题 (普通回溯(使用set存储),位运算回溯)
算法·leetcode·职场和发展
HY小宝F6 小时前
职场沟通的深层智慧:从对抗到协作的自我修炼
职场和发展
AI职业加油站7 小时前
职业提升之路:我的大数据分析师学习与备考分享
大数据·人工智能·经验分享·学习·职场和发展·数据分析
_OP_CHEN7 小时前
【算法基础篇】(五十六)容斥原理指南:从集合计数到算法实战,解决组合数学的 “重叠难题”!
算法·蓝桥杯·c/c++·组合数学·容斥原理·算法竞赛·acm/icpc
草履虫建模14 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
学历真的很重要1 天前
【系统架构师】第二章 操作系统知识 - 第二部分:进程管理(详解版)
学习·职场和发展·系统架构·系统架构师
VT.馒头1 天前
【力扣】2722. 根据 ID 合并两个数组
javascript·算法·leetcode·职场和发展·typescript
执着2591 天前
力扣hot100 - 108、将有序数组转换为二叉搜索树
算法·leetcode·职场和发展