当前位置:网站首页>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;
}
边栏推荐
- See how much volatile you know
- 秋招秘籍B
- day367:有效的完全平方数
- Mapping Multi - export Server on ENSP
- The circular queue is implemented in super detail. I understand it in a few seconds
- Unicode字符/静态非静态的访问
- 论文笔记:DETR: End-to-End Object Detection with Transformers (from 李沐老师and朱老师)
- Two threads execute i++ 100 times respectively, and the possible values obtained
- VMware installation Kali
- Byte/byte? Don't get dizzy!
猜你喜欢

编译basalt时出现的报错

Apprentissage automatique | nltk Erreur de téléchargement des données | solution d'erreur de téléchargement du corpus stopwords pour nltk

Machine learning | nltk_ Data download error |nltk's stopwords corpus download error solution

Kali Trojan invades win7 system

Feedforward and backpropagation

Summary and future prospect of transfer learning | community essay solicitation

Why use gradient descent method

Byte/byte? Don't get dizzy!

day367:有效的完全平方数

在ensp上做防火墙的双机热备
随机推荐
Debian10安装Zabbix5.4
The circular queue is implemented in super detail. I understand it in a few seconds
值(址)传递,看清名字,别掉沟里
C语言刷题 | 三目运算实现判断大写(16)
[Architect (Part 39)] connecting MySQL database developed by server
稀疏数组^创建^还原^存盘^取盘--全家桶
Classic & Cases
MySQL中常用的SQL语句
Hoo Research Institute of Hufu: how does cosmos connect the chain with the "port" of the chain?
C language brush question | temperature conversion (11)
機器學習|nltk_Data下載錯誤|nltk的stopwords語料下載錯誤解决方法
Traifik ingress practice
Find the size of cosine
==Classic interview questions
day575: 分糖果
DHCP中继代理
Try/finally --return those things
SQL编程task04作业-集合运算
Binary String
[Luogu] P1083 [NOIP2012 提高组] 借教室(差分)