蓝桥杯--数的拆分

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

连接:数的拆分


对有所有的样例,应该用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;
}
相关推荐
洌冰8 小时前
某车厂面试记录
面试·职场和发展
穿条秋裤到处跑9 小时前
每日一道leetcode(2026.04.16):距离最小相等元素查询
算法·leetcode·职场和发展
算法即正义10 小时前
知识竞赛软件的数据存储与备份方案
职场和发展·学习方法
Mr_Xuhhh12 小时前
深入理解单链表的递归反转:从原理到实现
算法·leetcode·职场和发展
_深海凉_13 小时前
LeetCode热题100-每日温度
算法·leetcode·职场和发展
菜菜的顾清寒14 小时前
力扣hot100(17) 缺失的第一个正数
算法·leetcode·职场和发展
.NET修仙日记15 小时前
2026 .NET 面试八股文:高频题 + 答案 + 原理(进阶核心篇)
面试·职场和发展·c#·.net·.net core·微软技术·webapi
张子都1 天前
【新版系统架构设计师】备考
职场和发展·系统架构·软件工程
米粒11 天前
力扣算法刷题 Day 42(股票问题总结)
算法·leetcode·职场和发展