PAT甲级-1010. Radix (25)进制

1010. Radix (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible

给出两个数N1和N2,Tag指出所给的Radix是第几个数的进制,计算出一个进制使得N1=N2。

我的思路是先将给定进制的数转换成十进制sum,记录下未定数中各个位上最大的一位数mmax,然后根据“一个数的各个位上最大的数小于其进制”在[mmax+1,sum+1]范围内二分遍历可能使得两数相等的进制。

(;´д`)ゞ这里如果不二分的话,for循环在测试点7上限需要开到1e10,会直接超时爆掉…

#include<bits/stdc++.h>
using namespace std;
#define INF 0xfffffff
#define MAXN 1010
int a[MAXN],b[MAXN],mmax;
long long sum,res,ans;
void change10(string x,int r)//转成10进制
{
    memset(a,0,sizeof(a));
    int len=x.length();
    int cnt=len-1;
    sum=0;
    for(int i=0; i<len; ++i)
    {
        if(x[i]>='0'&&x[i]<='9')
            a[i]=int(x[i]-'0');
        else if(x[i]>='a'&&x[i]<='z')
            a[i]=int(x[i]-'a'+10);
        sum+=(pow(r,cnt)*a[i]);
        --cnt;
    }
}
bool tryR(string x)//依次尝试各种进制
{
    memset(b,0,sizeof(b));
    int len=x.length();
    mmax=0;
    for(int i=0; i<len; ++i)
    {
        if(x[i]>='0'&&x[i]<='9')
            b[i]=int(x[i]-'0'),mmax=max(mmax,b[i]);
        else if(x[i]>='a'&&x[i]<='z')
            b[i]=int(x[i]-'a'+10),mmax=max(mmax,b[i]);
    }
    long long low=mmax+1,high=sum+1;//上限至少要开到10000000000;
    long long mid;
A:
    while(low<=high)//二分处理
    {
        res=0;
        mid=(low+high)/2;
        int cnt=len-1;
        for(int i=0; i<len; ++i)
        {
            res+=(pow(mid,cnt)*b[i]),--cnt;
            if(res>sum||res<0)//越界
            {
                high=mid-1;
                goto A;
            }
        }
        if(res<sum) low=mid+1;
        else if(res>sum) high=mid-1;
        else if(res==sum)//&&mid>=mmax
        {
            ans=mid;
            return true;
        }
    }
    return false;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    string n,m;
    int t,r;
    cin>>n>>m>>t>>r;
    bool flag=false;
    if(t==1)
        change10(n,r),flag=tryR(m);
    else if(t==2)
        change10(m,r),flag=tryR(n);
    if(flag) cout<<ans<<endl;
    else cout<<"Impossible"<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值