当前位置:网站首页>PAT甲级 - 1010 Radix(思维+二分)
PAT甲级 - 1010 Radix(思维+二分)
2022-06-22 09:21:00 【S atur】
题意:大概意思就是,tag==1,则N1为radix进制的数;tag==2,则N2为radix进制的数。试问另一个数为多少进制的数才能使得N1==N2,无解输出"Impossible"。
思路:基本思路很简单,直接枚举另外一个数的进制即可,期间在进行进制的转换时需要注意许多细节。但如果直接枚举的话将有个测试点(1分)无法通过,不难看出其实进制的取值我们就可以通过二分来查询,其二分的下限即是另一个数的最大位数值maxx+1,上限即位radix进制数的十进制数值+1。(如果误将Impossible输出成impossible则只有18分┭┮﹏┭┮)。
AC代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int inf = 0x3f3f3f3f3f3f3f3f3f;
const int N = 1e5+10;
string a, b;
int x, y, tag, radix, maxx, ans=inf;
map<int, char> mp;
int get_int(char x){
if(isdigit(x)) return x-'0';
return x-'a'+10;
}
int to_int(string s, int k){
int res = 0;
res = get_int(s[0]);
for(int i = 1; i < s.size(); i ++){
res *= k;
res += get_int(s[i]);
}
return res;
}
bool check(int k){
int res = get_int(b[0]);
for(int i = 1; i < b.size(); i ++){
res *= k;
res += get_int(b[i]);
if(res<0||res>x) return 1;
}
if(res==x){
ans = min(ans, k);
return 1;
}
if(res<0||res>x) return 1;
else return 0;
}
signed main()
{
cin >> a >> b >> tag >> radix;
if(tag==2) swap(a, b);
x = to_int(a, radix);
for(int i = 0; i < b.size(); i ++) maxx = max(maxx, get_int(b[i]));
int l = maxx+1, r = x+1;
while(l<=r){
int mid = (l+r)>>1;
if(check(mid)) r = mid-1;
else l = mid+1;
}
if(ans==inf) cout << "Impossible" << endl;
else cout << ans << endl;
return 0;
}
边栏推荐
猜你喜欢

Fuzzy query and aggregate function

Lock reentrantlock

5道面试题,拿捏String底层原理!

Network security Kali penetration learning how to conduct Nessus vulnerability detection

FileZilla server prompts 550 could not open file for reading when downloading files (illustration)

性能优化专题
![[node] theory + practice enables you to win sessions and cookies](/img/26/ef15896094d1887619c6ba9f6f287a.png)
[node] theory + practice enables you to win sessions and cookies
![[uni app] actual combat summary (including multi terminal packaging)](/img/35/6b0672311b033bc89f1518dd794777.png)
[uni app] actual combat summary (including multi terminal packaging)

Template engine, making interaction elegant

实现AD环境下多用户隔离FTP
随机推荐
使用ELK保存Syslog、Netflow日志和审计网络接口流量
Traifik ingress practice
C language question brushing | three item operation to realize capital judgment (16)
Zabbix5系列-使用温湿度传感器监控机房温湿度 (二十)
架设多个web站点
Lock reentrantlock
Double machine hot standby of firewall on ENSP
[node] please accept the crawler. We won't worry about the data any more
SQL编程task03作业-复杂一点的查询
看看volatile你深知多少
Debian10配置RSyslog+LoganAlyzer日志服务器
DOM programming
VMware installation Kali
DHCP中继代理
C language question brushing | input a number and output the corresponding value (13)
Mapping multiple exit servers on ENSP
try/finally --return那些事
Overview of microservice architecture
Embedded development terminology concept summary
MySQL中常用的SQL语句