pat 甲级 1010. Radix (25)
1010. Radix (25)
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 ;
}
pat 甲级 1010. Radix (25)的更多相关文章
- 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 ...
 - PAT甲级1010. Radix
		
PAT甲级1010. Radix (25) 题意: 给定一对正整数,例如6和110,这个等式6 = 110可以是真的吗?答案是"是",如果6是十进制数,110是二进制数. 现在对于 ...
 - PAT 甲级 1010 Radix
		
https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 Given a pair of positi ...
 - PAT Advanced 1010 Radix(25) [⼆分法]
		
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
 - PAT 解题报告 1010. Radix (25)
		
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
 - PAT甲组 1010 Radix (二分)
		
1010 Radix (25分) Given a pair of positive integers, for example, \(6\) and \(110\), can this equatio ...
 - 【PAT甲级】1010 Radix (25 分)(二分)
		
题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...
 - 已经菜到不行了 PAT 1010. Radix (25)
		
https://www.patest.cn/contests/pat-a-practise/1010 题目大意: 输入四个数字,a,b,c,d. a和b是两个数字,c=1表示是第一个数字,c=2表示是 ...
 - PAT (Advanced Level) 1010. Radix (25)
		
撸完这题,感觉被掏空. 由于进制可能大的飞起..所以需要开longlong存,答案可以二分得到. 进制很大,导致转换成10进制的时候可能爆long long,在二分的时候,如果溢出了,那么上界=mid ...
 
随机推荐
- 《转载》ASP动态iframe
			
原文:[ASP.NET]关于iframe的两个技巧 最近在给朋友写个网站,虽然不大,但是也碰到了一些问题.这篇就为解决ASP.NET中关于IFRAME的两个很现实的问题提供解决方法.PS:呵呵,又做了 ...
 - MySQL基础 - 1 数据库基础
			
一.数据库基础 1.什么是数据库 1.数据库(database)是保存有组织的数据的容器( 通常是一个文件或一组文件 ) 2.数据库是一个以某种有组织的方式存储的数据集合 注意:数据库软件应该称为DB ...
 - 二十三、MySQL 事务
			
MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...
 - 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程
			
点击了解更多Python课程>>> 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程 适用人群: 即将毕业的大学生,工资低工作重的白领,渴望崭露头角的职场新人, ...
 - 科学计算库Numpy——随机模块
			
np.random.rand() 随机生成一个[0,1)之间的浮点数. 参数表示数组的维数 np.random.randint() 生成一个随机的整数数组. 备注:生成一个5*4的二维数组,数组中的每 ...
 - Neon Lights in Hong Kong【香港霓虹灯】
			
Neon Lights in Hong Kong Neon is to Hong Kong as red phone booths are to London and fog is to San Fr ...
 - Gym - 101128F Landscaping(网络流)
			
题意 给你一个\(N×M\)的草地,有高地有低地. 收割机从低地走到高地或者从高地走到低地都要花费用\(A\),你可以花费用\(B\)把一块高地变成低地,或者把一块低地变成高地.收割机每行每列都是必须 ...
 - HDU:3336-Count the string(next数组理解)
			
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pr ...
 - luoguT30204 偷上网
			
\(n=1\) 时特判四角,其余时刻圆的面积和必小于正方形面积,随机点出来判断就行了. stm 随机算法-- #include <iostream> #include <cstdli ...
 - laravel5.2总结--响应
			
1 基本响应 1.1 返回一个字符串,指定的字符串会被框架自动转换成 HTTP 响应. Route::get('/', function () { return 'Hello World'; }) ...