1010 Radix (25 分),PTA
题目: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的更多相关文章
- 1010 Radix (25 分)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
- 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分) radix取值无限制,二分法提高效率
题目 Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The ...
- 1010 Radix (25分)
改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...
- 【PAT甲级】1010 Radix (25 分)(二分)
题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...
- 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 (25)
1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- 1010. Radix (25)(未完成)
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...
随机推荐
- tp5验证码的使用
<div><img id="verify_img" src="{:captcha_src()}" alt="验证码" on ...
- [ERROR]element select is not allowed here
问题:在使用IDEA搭建springboot项目的时候,在xml文件中遇到element select is not allowed here错误 原因:xml文件的头部的配置有错误,红框的三个地方命 ...
- # & 等特殊字符会导致传参失败
# & 等特殊字符会导致 post 传参失败 处理方法使用 encodeURIComponent 将字符串转化一下 实例 // toUpperCase() 转化为大写字母 var cateco ...
- nyoj 51-管闲事的小明(遍历,比较)
51-管闲事的小明 内存限制:64MB 时间限制:4000ms Special Judge: No accepted:9 submit:20 题目描述: 某校大门外长度为L的马路上有一排树,每两棵相邻 ...
- Linux入门之安装及相关知识。
一.VMware虚拟机安装与使用 1.1.VMware 简介 VMware是一个虚拟PC的软件,可以在现有的操 作系统上虚拟出一个新的硬件环境,相当于模拟 出一台新的PC.以此来实现在一台机器上真正 ...
- Go 语言优秀资源整理,为项目落地加速🏃
最后更新于2019.11.22 Go 语言优秀资源整理,为项目落地加速
- 关于 “'sqlite3' 不是内部或外部命令.....”问题
学习django 按书上的 执行 manage.py dbshell 时, 报“'sqlite3' 不是内部或外部命令,也不是可运行的程序 或批处理文件.” 也就是指,环境变量中没有“sqlite3 ...
- Tarjan-割点
割点——tarjan #include <bits/stdc++.h> using namespace std; ; ; int n, m; int ans;//个数 * MAXM], n ...
- java多线程,多线程加锁以及Condition类的使用
看了网上非常多的运行代码,很多都是重复的再说一件事,可能对于java老鸟来说,理解java的多线程是非常容易的事情,但是对于我这样的菜鸟来说,这个实在有点难,可能是我太菜了,网上重复的陈述对于我理解这 ...
- applicationContext-dao.xml 配置错误
https://www.captainbed.net/ 配置文件报错: 不允许有匹配 "[xX][mM][lL]" 的处理指令目标. 错误原因: 由于大部分都是搬砖,所以格式没注意 ...