题目: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. 20190806-sed面试题

    由于比较难,附上PPT,没事还得看 下载:https://www.lanzous.com/i5cs9aj 密码:arka 1.删除centos7系统/etc/grub2.cfg⽂件中所有以空⽩开头的⾏ ...

  2. 利用GitHub Pages + jekyll快速搭建个人博客

    前言 想搭建自己博客很久了(虽然搭了也不见得能产出多频繁). 最初萌生想写自己博客的想法,想象中,是自己一行一行码出来的成品,对众多快速构建+模板式搭建不屑一顾,也是那段时间给闲的,从前后端选型.数据 ...

  3. suseoj 1210: 会场安排问题 (贪心)

    1210: 会场安排问题 时间限制: 1 Sec  内存限制: 128 MB提交: 1  解决: 1[提交][状态][讨论版][命题人:liyuansong] 题目描述 假设要在足够多的会场里安排一批 ...

  4. 关键路径法(Critical Path Method, CPM)

    1.活动节点描述及计算公式 通过分析项目过程中哪个活动序列进度安排的总时差最少来预测项目工期的网络分析. 产生目的:为了解决,在庞大而复杂的项目中,如何合理而有效地组织人力.物力和财力,使之在有限资源 ...

  5. 安装Fedora后

    更新操作系统版本: https://fedoraproject.org/wiki/DNF_system_upgrade    靠谱: 设置ssh:ssh生成公钥私钥.默认root(.ssh/confi ...

  6. JS的原型和继承

    __proto__除null和undefined,JS中的所有数据类型都有这个属性: 它表示当我们访问一个对象的某个属性时,如果该对象自身不存在该属性, 就从它的__proto__属性上继续查找,以此 ...

  7. Obtaining the backtrace - libunwind

    Sometimes when working on a large project, I find it useful to figure out all the places from which ...

  8. 如何通过swoole加速laravel的问题?

    这篇文章主要介绍了关于如何使用swoole加速laravel,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 再来复习一下吧,导致 php 慢的各种因素中解析性语言的特性可以说是罪魁祸首 ...

  9. 【笔记】总结Springboot和Vue前后端分离的跨域问题

    跨域一直是个很玄学的问题,SSM的时候又得前后端一起配置,sb的时候又不用. 前端 axios普通get请求 submitForm() { var v=this; this.$axios({ meth ...

  10. 图解 Spring:HTTP 请求的处理流程与机制【2】

    2. HTTP 请求在 Web 容器中的处理流程 Web 容器以进程的方式在计算机上运行,我们知道进程是系统资源分配的最小单元,线程是系统任务执行的最小单元.从这个角度看,Web 容器就像是邮包收件人 ...