题目: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. 关闭redis持久化功能

    关闭redis持久化功能持久化会报如下信息 会影响硬盘写入性能 所以没什么用 就关掉吧 修改redis配置文件,redis.conf 第115行左右. 1.注释掉原来的持久化规则 <pre> ...

  2. Salesforce学习之路(十二)Aura组件表达式

    1. 表达式语法 在上篇文章组件属性示例中,新建了一个属性whom, 引用该属性时使用了表达式:{!v.whom},负责该属性的动态输出. 语法:{!expression} 上述示例中,我们的属性名称 ...

  3. pat 1035 Password(20 分)

    1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...

  4. nyoj 98-成绩转换 (if, else if)

    98-成绩转换 内存限制:64MB 时间限制:3000ms 特判: No 通过数:49 提交数:74 难度:1 题目描述: 输入一个百分制的成绩M,将其转换成对应的等级,具体转换规则如下: 90~10 ...

  5. ES6入门一:ES6简介及Babel转码器

    ES6简介 Babel转码器 Nodejs中使用ES6 WebPack中使用ES6及Babel转码插件 一.ES6简介与转码  1.1一个常见的问题,ECMAScript和JavaScript到底是什 ...

  6. Elasticsearch系列---分布式架构机制讲解

    概要 本篇主要介绍Elasticsearch的数据索引时的分片机制,集群发现机制,primary shard与replica shard是如何分工合作的,如何对集群扩容,以及集群的容错机制. 分片机制 ...

  7. 更换JDK

    1.更换JDK 1).卸载原有jdk 检查一下系统中的jdk版本 java -version 显示 java version "1.6.0_24" OpenJDK Runtime ...

  8. 官宣!Amazon EMR正式支持Apache Hudi

    ​Apache Hudi是一个开源的数据管理框架,其通过提供记录级别的insert, update, upsert和delete能力来简化增量数据处理和数据管道开发.Upsert指的是将记录插入到现有 ...

  9. source for "Android 28 platform" not found

    source for "Android 28 platform" not found 解决办法:点击右上角的Download,但是下载完点击Refresh之后没有反应,这时候需要重 ...

  10. 【集训Day2 哈希表】【NHOI2015】【Luogu P2421】差

    LuoguP2421 原题来自NHOI2015 [解题思路] 本题的解题方法有三种,一种为枚举减数,二分查找被减数.第二种为利用数据单调性用尺取法进行查找,第三种为运用哈希表以快速查找数据. [解题反 ...