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

复制代码
给定一个 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;

}

相关推荐
code bean1 天前
【LangChain】检索器完全指南:从向量检索到生产级 RAG 架构
java·开发语言·微服务
LabVIEW开发1 天前
LabVIEW + MATLAB 混合编程:爆炸场测试数据精准采集方案
开发语言·matlab·labview
嵌入式协会20240721 天前
(已解决)MinIO python 获取预签名出现forbidden、errornetwork等错误
java·开发语言·python
宸丶一1 天前
Day 14:任务追踪 - 让 Agent 拥有项目管理能力
开发语言·python
小短腿的代码世界1 天前
Qt行情协议解析与二进制编解码优化:从FIX到自定义协议的全链路架构
开发语言·qt·架构
北域码匠1 天前
SHA-1算法:安全哈希原理与应用解析
算法·c#·哈希算法
skylar01 天前
小白1分钟安装flash-attn
开发语言·python
默子昂1 天前
ollama 自定义ui
开发语言·python·ui
坚果派·白晓明1 天前
【鸿蒙PC】SDL3 移植:AtomCode Skills 4 步速通多媒体库适配
c++·华为·ai编程·harmonyos·atomcode·c/c++三方库
手写码匠1 天前
手写 GraphRAG:从零实现图增强检索增强生成系统
人工智能·深度学习·算法·aigc