题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536

题意:给定n1、n2两个数,求可以是两者相等的进制;如果没有,输出Impossible

思路:

①暴力枚举进制数,超时/wa,错在样例7,这样写最多只能有24分。

盲猜样例7应该是这样的

input: 

output:

②开二分搜索进制数,转换出来的数会爆long long,所以二分的check要修改一下。

当要求的数只有一位时,不管是多少进制的数,算出来的答案都是一样的,so特判一下。

当要求的数大于一位时,答案如果存在,则唯一。

盲猜样例一应该是这样的

input: 

output:
 

tips:

①巧妙利用swap函数减少函数长度

②for()中不要用i<s.szie()或者i<strlen(s),O(n)会变成O(n*n)

附上代码:

#include<bits/stdc++.h>
using namespace std;
const long long inf=(1ll<<)-;
long long cal(string s,long long radix)//计算s的radix进制数,转换成10进制数的值
{
long long ans=,rdixnum=;
int len=s.size();
for(int i=len-;i>=;i--)
{
if(s[i]>=''&&s[i]<='')ans+=1ll*(s[i]-'')*rdixnum;
else ans+=1ll*(s[i]-'a'+)*rdixnum;
rdixnum*=radix;
}
return ans;
} int main()
{
string s1,s2;
int tag,len1,len2;
long long ansnum=,radixnum=,ans=-,radix;
cin>>s1>>s2>>tag>>radix; if(tag==)swap(s1,s2);//交换
len1=s1.size();
len2=s2.size(); for(int i=len1-;i>=;i--)
{
if(s1[i]>=''&&s1[i]<='')ansnum+=1ll*(s1[i]-'')*radixnum;//计算已给数的十进制数值
else ansnum+=1ll*(s1[i]-'a'+)*radixnum;
radixnum*=radix;
}
long long l=,r=inf;
for(int i=;i<len2;i++)
{
if(s2[i]>=''&&s2[i]<='')l=max(l,s2[i]-''+1ll);//计算满足字母、数字要求的最小进制
else l=max(l,s2[i]-'a'+11ll);
}
if(len2==)
{
if(ansnum==cal(s2,l))cout<<l<<endl;
else cout<<"Impossible"<<endl;
}
else
{
while(l<=r)
{
long long mid=l+r>>1ll;
if(cal(s2,mid)==ansnum)
{
cout<<mid<<endl;
return ;
}
else if(cal(s2,mid)>ansnum||cal(s2,mid)<)r=mid-;//修改二分
else l=mid+;
}
cout<<"Impossible"<<endl;
}
return ;
}

我还是太难♂了

/(ㄒoㄒ)/~~

1010 Radix (25 分),PTA的更多相关文章

  1. 1010 Radix (25 分)

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

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

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

  3. PAT 1010 Radix (25分) radix取值无限制,二分法提高效率

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

  4. 1010 Radix (25分)

    改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...

  5. 【PAT甲级】1010 Radix (25 分)(二分)

    题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...

  6. 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 ...

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

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

  8. pat 甲级 1010. Radix (25)

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

  9. 1010. Radix (25)(未完成)

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

随机推荐

  1. websocket socketJs

    springboot实现服务器端消息推送(websocket + sockjs + stomp)   服务器端推送技术在web开发中比较常用,可能早期很多人的解决方案是采用ajax向服务器轮询消息,这 ...

  2. SSM整合相关试题

    1.下列关于Spring自动装配的说法中,错误的是() A 在Spring配置文件中,可以通过<bean>元素的autowire属性指定自动装配方式 B autowire属性值可以设置为n ...

  3. 使用Samba服务实现文件共享

    1.在虚拟机上安装Samba服务安装包 (在下载之前检查客户机与服务器是否能够ping通) (鼠标右击桌面,打开终端,测试和yum是否能够ping通,下面的命令行是我的yum的IP地址) [root@ ...

  4. WordPress代码高亮插件SyntaxHighlighter终极使用详解

    子曰: 工欲善其事,必先利其器.作为码农一枚,再加上站长这个已经不再光鲜的称呼,岂能没有一款经济实用.操作简单.而且功能必须强大.样式也必须好看的Wordpress代码高亮插件?!作为一个视代码如生命 ...

  5. nyoj 29-求转置矩阵问题 (行,列位置调换)

    29-求转置矩阵问题 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:8 submit:18 题目描述: 求一个三行三列的转置矩阵. 输入描述: 第一 ...

  6. 百度下载给的termux是个坑

    termux有两个版本,0.32以及0.64.百度提供0.32的下载,太坑爹.谷歌则是0.64的下载.32版uname -m识别为armv8l,绝对影响你进行其它linux的部署.通过atilo脚本会 ...

  7. 使用shiro做权限管理的学习笔记整理

    Shiro权限管理 参考:https://www.cnblogs.com/jpfss/p/8352031.html Shiro解决的问题 授权和鉴别的问题:Authenrization(授权) Aut ...

  8. Java程序在内存中运行详解

    目录 Java程序在内存中运行详解 一.JVM的内存分布 二.程序执行的过程 三.只有一个对象时的内存图 四.两个对象使用同一个方法的内存图 五.两个引用指向同一个对象的内存图 六.使用对象类型作为方 ...

  9. linux hwclock

    在Linux中,系统时间(软件时间)和硬件时间,并不会自动同步,系统时间和硬件时间以异步的方式运行,互不干扰.硬件时间的运行,是靠Bios电池来维持,而系统时间,是在系统开机的时候,会自动从Bios中 ...

  10. vue通过控制boolean值来决定是否添加class类名

    vue通过控制boolean值来决定是否添加class类名