1010. Radix (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

题意:已知其中一个数的字码以及进制和另一个数的字码,推断另一个数的进制。
思路:首先有可能出现的最大进制不是36,可能会很大,所以直接暴力搜索的话很可能会卡时,所以需要二分搜索。这样我们只需确定另一个数字进制出现的可能范围[max_radix,min_radix]内进行二分搜索就行。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30+5
#define M_MAX 2001
typedef unsigned long long ll;
int tag,radix;
struct Number {
string number;
ll base;
}num[];
bool known , unknown;
ll sum1, sum2;
ll translate_to_10(string s,ll base) {//转化成十进制
ll sum = ;
for (int i = ; i < s.size();i++) {
if (s[i] >= ''&&s[i] <= '') {
sum = sum*base + s[i] - '';
}
else if (s[i] >= 'a'&&s[i] <= 'z') {
sum = sum*base + s[i] - 'a' + ;
}
}
return sum;
} bool C(ll x) {
ll sum = translate_to_10(num[unknown].number, x);
if (sum<)return true;//太大导致数值溢出
if (sum >= sum1)return true;
else return false;
} int main() {
while (cin>>num[].number>>num[].number>>tag>>radix) {
known = !(tag & ),unknown=!known;
num[known].base = radix;
sum1 = translate_to_10(num[known].number, num[known].base);
int strart_base = ; string s = num[unknown].number;
for (int i = ; i < s.size(); i++) {//确定未知数至少是多少进制
if (s[i] >= ''&&s[i] <= ''&&strart_base < s[i] - ''+)
strart_base = s[i] - ''+;
else if (s[i] >= 'a'&&s[i] <= 'z'&&strart_base < s[i] - 'a' + +)
strart_base = s[i] - 'a' + +;
}
ll lb = strart_base-, ub = sum1 + ;
if (translate_to_10(num[unknown].number, lb) > sum1) { puts("Impossible"); continue; }//剪枝
while (ub-lb>) {
ll mid = (lb + ub) >> ;
if (C(mid))ub = mid;
else lb = mid;
}
if (translate_to_10(num[unknown].number, ub) == sum1){
printf("%lld\n",ub);
}
else puts("Impossible");
}
return ;
}

最新文章

  1. axure快速原型设计工具
  2. 最长公共子序列LCS问题
  3. Linux计时器
  4. JavaScript简洁继承机制实现(不使用prototype和new)
  5. 【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
  6. HNU 12827 NASSA’s Robot
  7. linux多个python版本下导致yum报错
  8. 字符串比较 忽略大小写 iphone
  9. HDU 5781 ATM Mechine (概率DP)
  10. sublime text修改TAB缩进为空格
  11. 第一个C++
  12. 怎样安装Joomla 1.7网站系统
  13. opennebula auth module ldap
  14. zip error: Invalid command arguments
  15. USB的前世今生
  16. 一个高性能异步socket封装库的实现思路 (c#)
  17. easyui datagrid editor combobox添加空选则清空combobox框
  18. Nim积解法小结
  19. Java8-Map
  20. #WEB安全基础 : HTML/CSS | 0x3文件夹管理网站

热门文章

  1. Sum All Odd Fibonacci Numbers-freecodecamp算法题目
  2. Java poi 导出Excel并下载到客户端
  3. jsp页面:一个form,不同请求提交form
  4. 十八、MySQL 排序
  5. yii2 基本的增删改查
  6. day09-函数讲解
  7. 爬虫之Scarpy.Request
  8. 04vim的使用
  9. Zookeeper协调服务系统&#183;ELK日志管理系统简介
  10. beautifulsoup解析