当前位置: 首页 > news >正文

【数论 构造】 P11036 【MX-X3-T3】「RiOI-4」GCD 与 LCM 问题|普及+

本文涉及知识点

数论:质数、最大公约数、菲蜀定理
构造

P11036 【MX-X3-T3】「RiOI-4」GCD 与 LCM 问题

题目背景

原题链接:https://oier.team/problems/X3D。


「既然你说你不了解她,为什么又可以断言她一定是因为……」

是呀,自己对零羽还了解的确实不够多……泠珞这样想着。

在残缺的记忆当中,她只能想起,她和零羽的最大公约数,就是「音乐」。

还缺了什么呢?泠珞不知道。她只知道,那所缺失的,和「音乐」加起来,就是她的一切。一切的总和。

滴答,滴答。叮咚,叮咚。如果把长短不一、断断续续的钢琴声拼接在一起,能够回忆起什么吗。

题目描述

给定一个正整数 a a a,请你构造三个正整数 b , c , d b,c,d b,c,d 使得 a + b + c + d = gcd ⁡ ( a , b ) + lcm ⁡ ( c , d ) a+b+c+d=\gcd(a,b)+\operatorname{lcm}(c,d) a+b+c+d=gcd(a,b)+lcm(c,d)。一个测试点内有多组数据。

由于出题人想把自己 QQ 号写题目里,你需要保证 b , c , d ≤ 1 634 826 193 b,c,d\le 1\,634\,826\,193 b,c,d1634826193

如有多种可能的答案,输出任意一个均可。

输入格式

第一行一个正整数 t t t 表示数据组数。

接下来 t t t 行每行一个正整数 a a a

输出格式

输出 t t t 行,每行三个正整数 b , c , d b,c,d b,c,d

如有多种可能的答案,输出任意一个均可。

输入输出样例 #1

输入 #1

4
1
2
3
20120712

输出 #1

7 9 2
9 6 8
5 9 2
8065343 8750 6446

说明/提示

【样例解释】

样例的构造为:

1 + 7 + 9 + 2 = 19 = gcd ⁡ ( 1 , 7 ) + lcm ⁡ ( 9 , 2 ) 1+7+9+2=19=\gcd(1,7)+\operatorname{lcm}(9,2) 1+7+9+2=19=gcd(1,7)+lcm(9,2)
2 + 9 + 6 + 8 = 25 = gcd ⁡ ( 2 , 9 ) + lcm ⁡ ( 6 , 8 ) 2+9+6+8=25=\gcd(2,9)+\operatorname{lcm}(6,8) 2+9+6+8=25=gcd(2,9)+lcm(6,8)
3 + 5 + 9 + 2 = 19 = gcd ⁡ ( 3 , 5 ) + lcm ⁡ ( 9 , 2 ) 3+5+9+2=19=\gcd(3,5)+\operatorname{lcm}(9,2) 3+5+9+2=19=gcd(3,5)+lcm(9,2)
20 120 712 + 8 065 343 + 8 750 + 6 446 = 28 201 251 = gcd ⁡ ( 20 120 712 , 8 065 343 ) + lcm ⁡ ( 8 750 , 6 446 ) 20\,120\,712+8\,065\,343+8\,750+6\,446=28\,201\,251=\gcd(20\,120\,712,8\,065\,343)+\operatorname{lcm}(8\,750,6\,446) 20120712+8065343+8750+6446=28201251=gcd(20120712,8065343)+lcm(8750,6446)

容易验证均满足要求。

【数据范围】

测试点分数 t ≤ t\le t a ≤ a\le a特殊性质
1 1 1 2 2 2 10 10 10 10 10 10
2 2 2 5 5 5 50 50 50 50 50 50
3 3 3 17 17 17 1 0 6 10^6 106 5 × 1 0 8 5\times10^8 5×108
4 4 4 29 29 29 1 0 6 10^6 106 1 0 9 − 1 10^9-1 1091 a a a 为奇数
5 5 5 47 47 47 2 × 1 0 6 2\times10^6 2×106 1 0 9 10^9 109

对于 100 % 100\% 100% 的数据, 1 ≤ t ≤ 2 × 1 0 6 1\le t\le2\times10^6 1t2×106 1 ≤ a ≤ 1 0 9 1\le a\le 10^9 1a109

数论

如果b是奇数
a + 1 + 2 + ( a + 2 ) ≡ 1 + 2 × a + 4 a+1 + 2+ (a+2) \equiv 1+ 2\times a + 4 a+1+2+(a+2)1+2×a+4
如果b不是奇数:
a = z × 2 k a = z \times 2^k a=z×2k,k取最大值。
上式同时乘以 2 k 2^k 2k
即: b = 2 k , c = 2 × 2 k , d = ( z + 2 ) × 2 k b=2^k,c=2\times 2^k,d=(z+2)\times 2^k b=2k,c=2×2k,d=(z+2)×2k
2 × a 稍稍大于 1 e 9 ,故 3 × a 小于 Q Q 号 2\times a 稍稍大于 1e9,故3\times a 小于QQ号 2×a稍稍大于1e9,故3×a小于QQ

代码

核心代码

#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>#include <bitset>
#include <chrono>
using namespace std::chrono;
using namespace std;template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {in >> pr.first >> pr.second;return in;
}template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t);return in;
}template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);return in;
}template<class T1, class T2, class T3, class T4, class T5 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) ;return in;
}template<class T1, class T2, class T3, class T4, class T5, class T6 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) ;return in;
}template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6, T7>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);return in;
}template<class T = int>
vector<T> Read() {int n;cin >> n;vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}
template<class T = int>
vector<T> ReadNotNum() {vector<T> ret;T tmp;while (cin >> tmp) {ret.emplace_back(tmp);if ('\n' == cin.get()) { break; }}return ret;
}template<class T = int>
vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}template<int N = 1'000'000>
class COutBuff
{
public:COutBuff() {m_p = puffer;}template<class T>void write(T x) {int num[28], sp = 0;if (x < 0)*m_p++ = '-', x = -x;if (!x)*m_p++ = 48;while (x)num[++sp] = x % 10, x /= 10;while (sp)*m_p++ = num[sp--] + 48;AuotToFile();}void writestr(const char* sz) {strcpy(m_p, sz);m_p += strlen(sz);AuotToFile();}inline void write(char ch){*m_p++ = ch;AuotToFile();}inline void ToFile() {fwrite(puffer, 1, m_p - puffer, stdout);m_p = puffer;}~COutBuff() {ToFile();}
private:inline void AuotToFile() {if (m_p - puffer > N - 100) {ToFile();}}char  puffer[N], * m_p;
};template<int N = 1'000'000>
class CInBuff
{
public:inline CInBuff() {}inline CInBuff<N>& operator>>(char& ch) {FileToBuf();while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车ch = *S++;return *this;}inline CInBuff<N>& operator>>(int& val) {FileToBuf();int x(0), f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行		return *this;}inline CInBuff& operator>>(long long& val) {FileToBuf();long long x(0); int f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行return *this;}template<class T1, class T2>inline CInBuff& operator>>(pair<T1, T2>& val) {*this >> val.first >> val.second;return *this;}template<class T1, class T2, class T3>inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val);return *this;}template<class T1, class T2, class T3, class T4>inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);return *this;}template<class T = int>inline CInBuff& operator>>(vector<T>& val) {int n;*this >> n;val.resize(n);for (int i = 0; i < n; i++) {*this >> val[i];}return *this;}template<class T = int>vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {*this >> ret[i];}return ret;}template<class T = int>vector<T> Read() {vector<T> ret;*this >> ret;return ret;}
private:inline void FileToBuf() {const int canRead = m_iWritePos - (S - buffer);if (canRead >= 100) { return; }if (m_bFinish) { return; }for (int i = 0; i < canRead; i++){buffer[i] = S[i];//memcpy出错			}m_iWritePos = canRead;buffer[m_iWritePos] = 0;S = buffer;int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);if (readCnt <= 0) { m_bFinish = true; return; }m_iWritePos += readCnt;buffer[m_iWritePos] = 0;S = buffer;}int m_iWritePos = 0; bool m_bFinish = false;char buffer[N + 10], * S = buffer;
};class Solution {
public:tuple<int, int, int> Ans(int a) {int tmp = a;int k2 = 1;while (0 == tmp % 2) {k2 *= 2;tmp /= 2;}return { k2,2 * k2,(tmp + 2) * k2 };};
};
int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUG	ios::sync_with_stdio(0); cin.tie(nullptr);//CInBuff<> in; COutBuff<10'000'000> ob;int T;cin >> T;while (T--){int a;cin >> a;
#ifdef _DEBUG	//printf("M=%d", M);//Out(A, ",A=");//Out(B, ",B=");//Out(que, ",que=");
#endif // DEBUG		Solution slu;auto [b, c, d] = Solution().Ans(a);cout << b << " " << c << " " << d << "\n";}return 0;
}

单元测试

		void Check(int a, int b, int c, int d) {Assert::IsTrue(b<=1634826193);Assert::IsTrue(c <= 1634826193);Assert::IsTrue(d<= 1634826193);std::wstringstream os;os << a << " " << b << " " << c << " " << d;Assert::IsTrue((long long)a + b + c + d== (long long)gcd(a, b)+lcm((long long)c, d),os.str().c_str());}TEST_METHOD(TestMethod1){vector<int> as = { 1,2,3,20120712,1<<29,(int)1e9 };for (int a : as){auto [b, c, d] = Solution().Ans(a);Check(a, b, c, d);}}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

http://www.lqws.cn/news/586801.html

相关文章:

  • 高效读取文件中指定行段的两种方法
  • mysql运维语句
  • C++ Vector的使用(下)
  • Qt Hello World 程序
  • ES6从入门到精通:箭头函数
  • C++ Vector的使用(上)
  • Linux基础环境开发工具apt、vim和gcc/g++
  • Excel 中拖动公式时,如何让引用的单元格“固定”或“变动”?
  • Vue3——项目配置eslint+prettier
  • Instruct-GPT奖励模型的损失函数与反向传播机制解析
  • [15-2] 读写内部FLASH读取芯片ID 江协科技学习笔记(20个知识点)
  • 【C++指南】C++ list容器完全解读(三):list迭代器的实现与优化
  • 如何查看服务器的运行日志?
  • 关于Spring的那点事(1)
  • 【CSS】Grid 布局基础知识及实例展示
  • 内网ubuntu系统安装mysql
  • 《如何在 Spring 中实现 MQ 消息的自动重连:监听与发送双通道策略》
  • 算法题练习
  • 前端Vue面试八股常考题(一)
  • 【STM32HAL-第1讲 基础篇-单片机简介】
  • Redis Lua 调试器(LDB)完全指南
  • 具身智能的仿真技术(具身智能入门三)
  • 用Python采集CBC新闻:如何借助青果网络海外代理IP构建稳定采集方案
  • datax-web报错:连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境)
  • NAT 类型及 P2P 穿透
  • 信创项目oracle数据库迁移到达梦数据库需要会有哪些问题?如何解决?
  • Linux云计算基础篇(2)
  • 2025年6月个人工作生活总结
  • 【Springai】项目实战进度和规划
  • SpringCloud系列(42)--搭建SpringCloud Config分布式配置总控中心(服务端)