PAT甲级1010. Radix (25)

题意:

给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是“是”,如果6是十进制数,110是二进制数。

现在对于任何一对正整数N1和N2,你的任务是找到一个数字的基数,而另一个数字的基数。

输入规格:

每个输入文件包含一个测试用例。每个案例占用一个包含4个正整数的行:

N1 N2标签基数

这里N1和N2每个不超过10位数。数字小于其基数,并从集合{0-9,a-z}中选择,其中0-9表示十进制数0-9,a-z表示十进制数10-35。

如果“标签”为1,最后一个数字“radix”为N1的基数,如果“tag”为2,则为N2。

输出规格:

对于每个测试用例,以一行打印另一个数字的基数,使得方程式N1 = N2为真。如果方程不可能,打印“不可能”。如果解决方案不是唯一的,输出最小可能的基数。

思路:

就是给你两个数,已知其中一个数的进制,然后求另外一个数是多少进制就可以让两个数相等。

暴力遍历会在测试点7超时。 二分搜索后,如果不考虑溢出会在测试点10报错。

二分搜索查找进制,下界是n2中最大的一个数字 + 1;上界是n1的10进制数 + 1;别的没有什么坑点感觉。

ac代码:

C++

// pat1010_radix.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<unordered_map>
#include<cmath> using namespace std; int main()
{
string n1, n2;
int tag, radix;
cin >> n1 >> n2 >> tag >> radix;
long long a = 0, b = 0,res;
if (tag == 2) swap(n1, n2); char ch;
int index = 0;
while (!n1.empty())
{
ch = n1.back();
if (ch >= 'a' && ch <= 'z')
{
a += (ch - 'a' + 10) * pow(radix, index);
}
else
{
a += (ch - '0') * pow(radix, index);
}
n1.pop_back();
index++;
} long long temp = 0;
for (int i = 0; i < n2.length(); i++)
{
if (n2[i] > temp) temp = n2[i];
}
if (temp >= 97) temp -= 87;
else temp -= 48; long long left = temp + 1;
long long right = a + 1;
res = a + 2;
while(left <= right)
{
temp = (left + right) / 2;
index = 0;
b = 0;
string tempn2 = n2;
while (!tempn2.empty())
{
ch = tempn2.back();
if (ch >= 'a' && ch <= 'z')
{
b += (ch - 'a' + 10) * pow(temp, index);
}
else
{
b += (ch - '0') * pow(temp, index);
}
tempn2.pop_back();
index++;
if (b > a || b < 0) break;
}
if (a == b)
{
res = min(res, temp);
right--;
}
else if (b > a || b < 0)
{
right = temp - 1;
}
else if (b < a)
{
left = temp + 1;
}
}
if (res == a + 2) cout << "Impossible" << endl;
else cout << res << endl;
return 0;
}

PAT甲级1010. Radix的更多相关文章

  1. PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  2. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  3. PAT 甲级 1010 Radix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...

  4. PAT甲组 1010 Radix (二分)

    1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...

  5. PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新

    题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...

  6. PAT甲级——A1010 Radix

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  7. PAT Advanced 1010 Radix(25) [⼆分法]

    题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...

  8. PAT 解题报告 1010. Radix (25)

    1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...

  9. PAT 1010 Radix(X)

    1010 Radix (25 分)   Given a pair of positive integers, for example, 6 and 110, can this equation 6 = ...

随机推荐

  1. React 16 源码瞎几把解读 【二】 react组件的解析过程

    一.一个真正的react组件编译后长啥样? 我们瞎几把解读了react 虚拟dom对象是怎么生成的,生成了一个什么样的解构.一个react组件不光由若干个这些嵌套的虚拟dom对象组成,还包括各种生命周 ...

  2. 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage

    2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage A. Union of Doubly Link ...

  3. HDU 6205 2017沈阳网络赛 思维题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6205 题意:给你n堆牌,原本每一堆的所有牌(a[i]张)默认向下,每次从第一堆开始,将固定个数的牌(b ...

  4. day01作业

    Java技术按照用途不同分为三大版本,分别是JavaSE.JavaEE和JavaMeJava虚拟机就是一个虚拟的用于执行字节码文件的计算机.它是Java最核心的技术,是Java跨平台的基础.DOS命令 ...

  5. 对cgic的理解——name选项

    #include <stdio.h>#include <stdlib.h>#include <string.h>#include "cgic.h" ...

  6. Java Number类和Math类

    Java Number类 一般的,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double等. 然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类 ...

  7. opencv的CMakeLists.txt与makefile写法

    opencv的CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(my_run_name) find_package(OpenCV R ...

  8. linux中top命令

    经常用到top命令,也就简单看看进程多不多,卡不卡, 这次在网上找到一个归纳总结的,以供参考. 简介 top 命令是 Linux 下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于 ...

  9. 【LOJ】#2131. 「NOI2015」寿司晚宴

    题解 怎么NOI2015D1--全是一眼秒的sb题--然后我代码全都写跪一遍= = 要是NOI2015是IOI赛制我就可以AK啦(大雾) 代码能力直线下降,NOI2018滚粗预定了啊TAT 我是不是要 ...

  10. Java Switch Statement

    Java Switch Java Switch Statement switch语句的执行规则如下     1.从第一个case开始判断,不匹配则跳到下一个case继续判断:     2.遇到brea ...