首页 > 其他分享 >1010 Radix(25分)

1010 Radix(25分)

时间:2022-08-22 00:44:28浏览次数:71  
标签:25 radix temp tag number Radix N1 N2 1010

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

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
//转化字符为数字
int toSet(char x){
    if (x>='0'&&x<='9'){
        return x-'0';
    }
    else if(x>='a'&&x<='z'){
        return x-'a'+10;
    }
}
//转化字符串为十进制数
ll toDecimal(string in,ll radix){
    ll result=0;
    for(int i=0;i<in.size();++i){
        result+=toSet(in[i])*pow(radix,in.size()-i-1);
    }
    return result;
}
//二分搜索
ll getRadix(ll head,ll tail,string in,ll d1){
    if (head<=tail){
        ll mid=(head+tail)/2;
        ll temp=toDecimal(in,mid);
        if (temp==d1){
            return mid;
        }
        else if(temp<d1&&temp>0){
            return getRadix(mid+1,tail,in,d1);
        }
        else if (temp>d1||temp<0){
            return getRadix(head,mid-1,in,d1);
        }
    }
    return -1;
}
int main(){
    int tag,radix;
    string n1,n2,temp;
    cin>>n1>>n2>>tag>>radix;
    if (tag==2){
        temp=n1;
        n1=n2;
        n2=temp;
    }
    ll d1=toDecimal(n1,radix);
    //定位最小进制
    int mmax=-1;
    for (int i=0;i<n2.size();++i){
        mmax=max(mmax,toSet(n2[i]));
    }
    ll result=getRadix(mmax+1,d1+1,n2,d1);
    if (result==-1){
        cout<<"Impossible";
    }
    else{
        cout<<result;
    }
}

我相信大多数人看到题面,按照自己的理解都会认为进制数在35以内,我也一样,看着永远ac不了的代码陷入了沉思,明明感觉是一道水题,就是过不了。

搜了题解,确实是长见识了,这竟然是一道二分搜索题,想了想也确实,就算超过35进制,按题目给的字符串也能表示,只能说是自己格局小了。

那么考虑到进制范围在2~N1+1内,在计算的过程中明显会溢出。难道这题还需要做一个大数相加?看了看题解的处理方法,他是将溢出直接作为不匹配条件进行下一次二分。我就在想,为什么N1不会溢出呢,如果N1也溢出了这种判断方法不就失效了吗?可能是出题人考虑到了这一点,并没有设置N1也溢出的测试点,不然只能乖乖地再去做大数相加。

tip:在二分判断时,不仅要在temp>d1时加一个是否溢出的判断,而且在temp<d1时也要加一个未溢出的判断,否则在溢出时永远满足temp<d1的条件;或者是把temp>d1的判断放到temp<d1前面去,也能起到一样的效果。

标签:25,radix,temp,tag,number,Radix,N1,N2,1010
From: https://www.cnblogs.com/coderhrz/p/16611530.html

相关文章

  • Java学习 (25) 对象篇(05)抽象类&接口
    目录抽象类语法实例注意点具体讲解视频(狂神说Java)接口语法实例具体讲解视频(狂神说Java)抽象类abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是......
  • cf1254 B2. Send Boxes to Alice (Hard Version)
    题意:给定非负数组,每次操作可以选一个\(a_i\neq0\),令\(a_i\)减一,\(a_i\)相邻的一个数(如果存在)加一。问最少几次操作能使所有数有\(>1\)的公因数\(n,a_i\le1e6\)......
  • 【luogu P2508】圆上的整点(高斯素数模板)
    圆上的整点题目链接:luoguP2508题目大意给你一个圆,问你圆周上有多少个点的坐标是整点。思路考虑一个东西叫做高斯整数。其实它是复数,是\(a+bi\)中\(a,b\)都是整......
  • PAT Advanced 1036 Boys vs Girls(25)
    题目描述:Thistimeyouareaskedtotellthedifferencebetweenthelowestgradeofallthemalestudentsandthehighestgradeofallthefemalestudents.Inp......
  • PAT Advanced 1021 Deepest Root(25)
    题目描述:Agraphwhichisconnectedandacycliccanbeconsideredatree.Theheightofthetreedependsontheselectedroot.Nowyouaresupposedtofindthe......
  • 25. Redis---性能测试
    1.前言为了解Redis在不同配置环境下的性能表现,Redis提供了一种行性能测试工具redis-benchmark(也称压力测试工具),它通过同时执行多组命令实现对Redis的性能测试。性......
  • P2508-[HAOI2008]圆上的整点【数学】
    正题题目链接:https://www.luogu.com.cn/problem/P2508题目大意一个在\((0,0)\)的圆心,半径为\(r\),求圆有多少个整点。\(1\leqr\leq2\times10^9\)解题思路设这个......
  • 爬虫-获取豆瓣Top250信息
    importtimeimportrequestsfromlxmlimportetreei=0foriteminrange(0,275,25):url=f'https://movie.douban.com/top250?start={item}&filter='......
  • GBJ1010-ASEMI整流桥GBJ1010
    编辑:llGBJ1010-ASEMI整流桥GBJ1010型号:GBJ1010品牌:ASEMI封装:GBJ-4特性:整流扁桥正向电流:10A反向耐压:1000V恢复时间:ns引脚数量:4芯片个数:4芯片尺寸:102MIL浪涌电流......
  • leetcode 225. Implement Stack using Queues 用队列实现栈(简单)
    一、题目大意请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop和empty)。实现MyStack类:voidpush(intx)将元素x压入栈顶。......