PAT甲组 1010 Radix (二分)

1010 Radix (25分)#

Given a pair of positive integers, for example, 6 and 110, can this equation 6=110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:#

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:

N1 N2 tag radix

Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set { 0-9, a-z } where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number radix is the radix of N1 if tag is 1, or of N2 if tag is 2.

Output Specification:#

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print Impossible. If the solution is not unique, output the smallest possible radix.

Sample#

Sample Input 1:#

6 110 1 10

Sample Output 1:#

2

Sample Input 2:#

1 ab 1 2

Sample Output 2:#

Impossible

题意#

给出两个正整数和其中一个数的基底,找到另一个数的基底,使两个数相等

思路#

因为每个数不超过10位,每位最大都可能到z,所以待求的基底可能是一个很大的数,用二分法求另一个数的基底。
将一直基底的数求出来之后,二分另一个数的基底。如果当前基底下的结果小于0或大于已知数,那么这个基底就是偏大的,一直二分直至相等,否则返回1
二分的左区间为待求基底的每位数字上最大值加一,右区间为max(左区间,已知数)

代码#

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
ll get_num(string n,ll radix)
{
    ll ans=0;
    int l=n.length();
    for(int i=l-1;i>=0;i--)
    {
        int res;
        if(n[i]>='0'&&n[i]<='9')
            res=n[i]-'0';
        else
            res=n[i]-'a'+10;
        ans+=res*pow(radix,l-i-1LL);
    }
    return ans;
}
bool check(ll num,string n,ll radix)
{
    ll res=get_num(n,radix);
    if(num==res)
        return true;
    return false;
}
ll solve(ll num,string n)
{
    ll l,r;
    int low=0;
    for(auto i:n)
    {
        if(i>='0'&&i<='9')
            low=max(low,i-'0');
        else
            low=max(low,i-'a'+10);
    }
    l=low+1LL,r=max(1LL*l,num);
    while(l<=r)
    {
        ll mid=(l+r)/2;
        ll res=get_num(n,mid);
        if(res<0||res>num)
            r=mid-1;
        else if(res<num)
            l=mid+1;
        else if(res==num)
            return mid;
    }
    return -1;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s1,s2;
    int tag;
    ll radix;
    cin>>s1>>s2>>tag>>radix;
    ll num1,num2;
    if(tag==1)
    {
        num1=get_num(s1,radix);
        if(solve(num1,s2)==-1)
            cout<<"Impossible";
        else
            cout<<solve(num1,s2);
        cout<<endl;
    }
    else
    {
        num2=get_num(s2,radix);
        if(solve(num2,s1)==-1)
            cout<<"Impossible";
        else
            cout<<solve(num2,s1);
        cout<<endl;
    }
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s.\n";
    #endif
    return 0;
}

作者:Friends-A

出处:https://www.cnblogs.com/Friends-A/p/12290587.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   友人-A  阅读(145)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2019-02-10 codevs 1300:文件排版(DP)
2019-02-10 《机器学习实战》kNN算法及约会网站代码详解
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu