有限小数题解(进制转换+某进制判断是否为无限小数)

复制代码
给定一个 A 进制下的分数 a/b,
小蓝想把它化为 B 进制下的小数 c。
现在他想知道这个小数是不是一个有限小数。

Input

复制代码
输入共一行,包含四个数 a, b, A, B,表示该分数和两种进制。
其中 A, B 使用十进制表示,
a, b 中大于 9 的数字使用大写字母表示,
A 表示 10,B 表示 11,以此类推。

Output

复制代码
输出一行,包含一个字符串。
如果该小数是一个有限小数,则输出 "Yes";
否则输出 "No"(不包含引号)。

Sample Input

复制代码
10 11 10 11

Sample Output

复制代码
Yes

思路:

如果一个数a/b是某进制下有限小数(最小一位位权设为),让其乘B超过i次可以化为十进制整数,而如果是无限小数,无论×多少次,都无法化为十进制整数。

我们可以直接乘一个比较大的,保证有限小数都可以化为十进制整数。

同时在乘的过程中对b不断取余,防止其超过LL范围

代码:

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<string>

#include<cstring>

#include<cmath>

#include<ctime>

#include<algorithm>

#include<utility>

#include<stack>

#include<queue>

#include<vector>

#include<set>

#include<math.h>

#include<map>

#include<unordered_map>

using namespace std;

typedef long long LL;

typedef unsigned long long ULL;

const int N = 1e6+1000;

LL A, B;

string a, b;

LL get(string s)

{

LL ans = 0,t=1,x=s.size();

for (int i = x-1; i>=0; i--)

{

if (si <= '9' && si >= '0')

ans += (si - '0') * t;

else if (si <= 'Z' && si >= 'A')

ans += (si - 'A'+10) * t;

t *= A;

}

return ans;

}

int main() {

cin >> a >> b >> A >> B;

LL aa = get(a);

LL bb = get(b);

int i = 1;

aa %= bb;

while (i <=bb+10&&aa)

{

aa *= B;

aa = aa % bb;

i++;

}

if (!aa)

printf("Yes\n");

else

printf("No\n");

return 0;

}

相关推荐
vibecoding日记7 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr21389 小时前
Verilog参数化游程编码RLE模块
算法
望易10 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络13 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
apocelipes1 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
HjhIron1 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc