【PAT - 甲级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 N​1​​ and N​2​​, 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

题目大意:

给定两个字符串,每个都是0~9 , a~z 代表0~35这36个数字。

给定一个数字的radix,你的任务是找到另一个数字的基数,使得N1=N2。

解题报告:

我们知道,把十进制数转化成其他进制是困难的,但是把其他进制转化成十进制是较为简单的。所以直接锁定一个目标进制之后,转化成十进制比较两个数字式是否相同就可以了。刚开始想错了,以为就是最多就是35进制了,但是其实则不然,可以是无穷进制,所以不能直接枚举了。考虑到上界是比较好确定的,并且随着radix的增大,转化的十进制数是单调递增的,考虑二分。

注意二分的上下界限制就好了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
char n1[MAX],n2[MAX],ned[MAX];
int tag,radix;
__int128 shi;
int go(char c) {if(c >= '0' &&  c <= '9') return c - '0';else return c - 'a' + 10;
}
__int128 trans(char s[],ll rdx) {//把s从rdx进制转化成10进制 __int128 res = 0;int len = strlen(s);for(int i = 0; i<len; i++) {res = res * rdx + go(s[i]);if(res < 0) return (__int128)9e18 * 2;}return res;
}int main()
{cin>>n1>>n2>>tag>>radix;if(tag == 1) shi = trans(n1,radix),strcpy(ned,n2);else shi = trans(n2,radix),strcpy(ned,n1);int mx = 0,len = strlen(ned);for(int i = 0; i<len; i++) {mx = max(mx,go(ned[i]));}__int128 l = mx+1,r = 9e18,mid;ll ans=-1;while(l<=r) {mid = (l+r)>>1;__int128 tmp = trans(ned,mid); if(tmp < shi) l = mid+1;else r = mid-1;if(tmp == shi) ans = mid;}if(ans != -1) printf("%lld\n",ans);else printf("Impossible\n");return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439445.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#中 ??、 ?、 ?: 、?.、?[ ]、:

1. 可空类型修饰符&#xff08;&#xff1f;&#xff09; 引用类型可以使用空引用表示一个不存在的值&#xff0c;而值类型通常不能表示为空。 例如&#xff1a;string strnull; 是正确的&#xff0c;int inull; 编译器就会报错。 为了使值类型也可为空&#xff0c;就可以使用…

几位无人驾驶领域的杰出科学家

本文介绍了几位无人驾驶领域杰出的科学家们&#xff0c;大家可以关注他们的主页&#xff0c;及时了解一些无人驾驶行业最新动态。 &#xff08;当然还有很多杰出的科学家&#xff0c;这里不一一列举了&#xff0c;暂时只列举出4位&#xff1a;3位外国科学家1位中国科学家&#…

【PAT - 甲级1012】The Best Rank (25分)

题干&#xff1a; To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage s…

Razor语法和Razor引擎大全

一、Razor语法 1、Razor的标识符 解释&#xff1a;字符被定义为Razor服务器代码块的标识符&#xff0c;后面的表示是服务器代码了。web form中使用<%%>中写服务器代码一个道理。在vs工具里面提供了代码着色和智能感应的功能。 { string userName "启超"; &…

【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)

题干&#xff1a; It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep…

让Team Foundation Server/TFS自动记住用户名密码解决方案

在使用Team Foundation Server&#xff08;以下简称TFS&#xff09; 的时候&#xff0c;在每次打开Visual Studio TFS时候&#xff0c;需要输入用户名和秘密&#xff0c;比较麻烦。 现提供一种方法可以解决这个问题&#xff1a; 依次执行下面操作&#xff1a; 打开控制面板--&…

【PAT - 甲级1017】Queueing at Bank (25分)(优先队列,模拟)

题干&#xff1a; Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be ser…

TFS(Team Foundation Server)敏捷使用教程

一、引言 1 中国式软件过程的坏味道 RUP&#xff0c;CMM/CMMI到了中国就变了味。。。。。。 2 Team Foundation Server TFS是软件开发的协作平台&#xff0c;它要解决的首要问题是团队成员的协作问题。比如说&#xff1a; 研发团队内部怎么协作&#xff0c;产品经理&#x…

【PAT - 甲级1020】Tree Traversals (25分)(树的遍历,给定中序后序,求层次遍历)

题干&#xff1a; Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. Input Specifi…

tfs 未能在以下位置创建报表文件夹 /TfsReports: 授予的权限不足,无法执行此操作

在tfs2015中添加新的Collection时&#xff0c;报一下错误&#xff1a; TF252015: 未能在以下位置创建报表文件夹: /TfsReports/XXXCollection。服务器返回了以下错误: 为用户“domain\name”授予的权限不足&#xff0c;无法执行此操作。。团队项目集合或项目将不具有对报表的访…

【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)

题干&#xff1a; A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Spe…

【PAT - 甲级1024】Palindromic Number (25分)(大数,模拟)

题干&#xff1a; A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic numbers can be pair…

银行卡密码的加密、MAC计算

简介 在银行、银联、第三方支付等金融系统中&#xff0c;对银行卡密码等信息的加解密&#xff0c;对交易数据的加解密无处不在&#xff0c;在商场刷卡消费的POS机&#xff0c;在ATM机器取款等都需要对数据加密以保护数据安全&#xff0c;不被窃取。 本文主要对POS机的安全处理…

【PAT甲级 - 1028】List Sorting (25分)(模拟,排序)

题干&#xff1a; Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤10​5​​) and C, where…

传统POS/终端/银联POS简介

point of sale&#xff1b; POS能够接受银行卡信息&#xff0c;具有通讯功能&#xff0c;并接受柜员的指令而完成金融交易信息和有关信息交换的设备。 找政策

【Python学习】 - - 链表推导式[ 2*x for x in X ]、匿名函数、并行迭代

列表推导式[x for x in range(n)] 问题&#xff1a;请计算出1~9间的整数的平方 常规方法 for i in range(1,10):print(i*i) 链表推导式&#xff1a; print([x*x for x in range(1,10)]) 匿名函数方法&#xff1a; 匿名函数语法形式&#xff1a; lambda [arg1, arg2, arg3,…

C#多线程和线程池

.Net的公用语言运行时&#xff08;Common Language Runtime&#xff0c;CLR&#xff09;能区分两种不同类型的线程&#xff1a;前台线程和后台线程。这两者的区别就是&#xff1a;应用程序必须运行完所有的前台线程才可以退出&#xff1b;而对于后台线程&#xff0c;应用程序则…

国家密码算法SM4(国密算法)介绍

国密是国家密码局认定的国产密码算法。而与之对应的&#xff0c;现在被广泛使用des、3des等算法是国外人发明&#xff0c;我们称为国际算法。 加密算法采用国家密码算法SM4&#xff0c;密钥长度为16字节&#xff0c;加密算法详见附录A。 主要有SM1&#xff0c;SM2&#xff0…

【最小费用可行流模板】

可能再也用不到了吧&#xff0c;今天整理电脑文件看到的&#xff0c;作为图论选手&#xff0c;留个纪念&#xff0c; //原图&#xff1a; 对于pi&#xff0c;拆点xi,yi s->S,[m,m],0 S->xi,[0,inf],0 yi->t,[0,inf],0 xi->yi,[vi,vi],0 对于有航线的pi和pj&#x…

ANSI X9.9 MAC算法介绍

1&#xff09;该算法只使用单倍长密钥&#xff0c;也就是8字节密钥&#xff1b; 2&#xff09;MAC数据按8字节分组&#xff0c;尾部以字节00补齐&#xff1b; 3&#xff09;用MAC密钥加密第一个8字节分组&#xff0c;加密结果与第二个8字节分组异或&#xff0c;然后再用MAC密…