A1010. Radix

摘要:
='

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

A1010. Radix第1张A1010. Radix第2张
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<limits.h>
 5 #include<string.h>
 6 using namespace std;
 7 long long str2num(char s[], long long radix){
 8     long long ans = 0, bit, P = 1;
 9     for(int i = strlen(s) - 1; i >= 0; i--){
10          bit = (s[i] >= '0' && s[i] <= '9' ) ? (s[i] - '0') : (s[i] - 'a' + 10 );
11          ans = ans + P * bit;
12          if(ans < 0)
13              return -1;
14          P = P * radix;
15     }
16     return ans;
17 }
18 long long binSearch(long long low, long long high, char s[], long long x){
19     long long mid, temp;
20     while(low < high){
21         mid = low + (high - low) / 2;
22         temp = str2num(s, mid);
23         if(temp > 0 && temp >= x || temp < 0)
24             high = mid;
25         else low = mid + 1;
26     }
27     return low;
28 }
29 int findMax(char s[]){
30     int max = -1, temp;
31     for(int i = 0; s[i] != '

免责声明:内容来源于网络,仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇android spinner的简单用法SQL Server与Oracle有什么区别?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PyTorch的自动混合精度(AMP)

https://zhuanlan.zhihu.com/p/165152789 PyTorch 1.6版本今天发布了,带来的最大更新就是自动混合精度。release说明的标题是: Stable release of automatic mixed precision (AMP). New Beta features include a TensorPipe...

UART

UART(Universal Asynchronous Receiver/Transmitter,通用异步收/发器) s3c2440A 提供了三个UART端口,它们都可以通过查询、中断和DMA方式传输数据,而且每个UART都分别有一个64个字节的接收FIFO和一个64个字节的发送FIFO。 OVERVIEW The UART can generate a...

Intel Core Microarchitecture Pipeline

Intel微处理器近20年从Pentium发展到Skylake,得益于制作工艺上的巨大发展,处理器的性能得到了非常大的增强,功能模块增多,不过其指令处理pipeline的主干部分算不上有特别大的变化,更多的是为了提高指令的处理速度添加一些模块以及各模块的增强与优化。 本文会以Intel Core微处理器架构为例去了解Intel微处理器pipeline的各个...

【转】NOR Flash擦写和原理分析

1. NOR FLASH 的简单介绍 NOR FLASH 是很常见的一种存储芯片,数据掉电不会丢失.NOR FLASH支持Execute On Chip,即程序可以直接在FLASH片内执行(这意味着存储在NOR FLASH上的程序不需要复制到RAM就可以直接运行).这点和NAND FLASH不一样.因此,在嵌入式系统中,NOR FLASH很适合作为启动程序...

PCI规范学习笔记

以前看过一段时间Cyclone FPGA控制PEX8111的程序,没看懂,最近又结合PCI规范重新看了一下。 PCI Speci rev2.3    FRAME# is driven by the master to indicate the beginning and end of a transaction.   IRDY# is driven by...

(5) openssl speed(测试算法性能)和openssl rand(生成随机数)

1.1 openssl speed 测试加密算法的性能 支持的算法有: openssl speed [md2] [mdc2] [md5] [hmac] [sha1] [rmd160] [idea-cbc] [rc2-cbc] [rc5-cbc] [bf-cbc] [des-cbc] [des-ede3] [rc4] [rsa512] [rsa1024] [...