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 Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible
复制代码
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
LL Map[257];
LL inf = (1LL << 63) - 1;

void init(){
    for(char c = '0'; c <= '9'; c++){
        Map[c] = c - '0';
    }
    for(char c = 'a'; c <= 'z'; c++){
        Map[c] = c - 'a' + 10;
    }
}

LL convertNum10(char a[],LL radix, LL t){
    LL ans = 0;
    int len = strlen(a);
    for(int i = 0; i < len; i++){
        ans = ans*radix + Map[a[i]];
        if(ans < 0 || ans > t) return -1;
    }
    return ans;
}

int cmp(char N2[],LL radix, LL t){
    LL num = convertNum10(N2,radix,t);
    if(num < 0) return 1; //代表mid太大了
    if(num == t) return 0;
    else if(num > t) return 1;
    else return -1; 
}

int binarySearch(char N2[],LL left,LL right,LL t){
    LL mid;
    while(left <= right){
        mid = (left+right)/2;
        int flag = cmp(N2,mid,t);
        if(flag == 0 ) return mid;
        if(flag == 1) right = mid - 1;
        else left = mid + 1;
    }
    return -1;
}

LL findLargest(char N2[]){
    int ans = -1,len = strlen(N2);
    for(int i = 0; i < len; i++){
        if(Map[N2[i]] > ans){
            ans = Map[N2[i]];
        }
    }
    return ans+1;
}
    char N1[20],N2[20],temp[20];
    int tag,radix;
int main(){
    init();
//    char N1[20],N2[20],temp[20];
//    LL tag,radix;
    scanf("%s %s %d %d",N1,N2,&tag,&radix);
    if(tag == 2){
        strcpy(temp,N1);
        strcpy(N1,N2);
        strcpy(N2,temp);
    }
    LL t = convertNum10(N1,radix,inf);
    LL low = findLargest(N2);
    LL high = max(t,low) + 1;
    LL ans = binarySearch(N2,low,high,t);
    if(ans == -1) printf("Impossible\n");
    else printf("%lld\n",ans);
    return 0;
}
复制代码

 20190730

复制代码
//如果使用int会有三个点过不去
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 20;
const int INF = 100000000;
int map[257] = {0};

int init()
{
    for(char c = '0'; c <= '9'; c++)
    {
        map[c] = c - '0';
    }
    for(char c = 'a'; c <= 'z'; c++)
    {
        map[c] = c - 'a' + 10;
    }
}

int Convert2Num(char *str,int radix,int Max)
{
    int num = 0;
    int len = strlen(str);
    for(int i = 0; i < len; i++)
    {
        num = num * radix + map[str[i]];
        if(num < 0 || num > Max) return -1;
    }
    return num;
}

int FindLargest(char *str)
{
    int len = strlen(str);
    int iMax = -1;
    for(int i = 0; i < len; i++)
    {
        if(iMax < map[str[i]])
        {
            iMax = map[str[i]];
        }
    }
    return iMax + 1;
}

//1 mid is bigger
//-1 mid is smaller 
int cmp(char *N2,int radix,int N1Num)
{
    int N2Num = Convert2Num(N2,radix,N1Num);
    if(N2Num < 0 ) return 1;
    if(N2Num > N1Num) return 1;
    else if(N2Num < N1Num) return -1;
    else return 0;
}

int Binary(char *N2,int low,int high,int N1Num)
{
    int mid = 0;
    while(low <= high)
    {
        mid = (low + high) / 2;
        int ans = cmp(N2,mid,N1Num);
        if(1 == ans) high = mid - 1;
        else if(-1 == ans) low = mid + 1;
        else return mid; 
    } 
    return -1;
}

int main()
{
    init();
    char N1[maxn],N2[maxn],temp[maxn];
    int radix,tag;
    scanf("%s%s%d%d",N1,N2,&tag,&radix);
    if(2 == tag)
    {
        strcpy(temp,N1);
        strcpy(N1,N2);
        strcpy(N2,temp);
    }
    int N1Num = Convert2Num(N1,radix,INF);
    int low = FindLargest(N2);
    int high = max(N1Num,low) + 1;
    int ans = Binary(N2,low,high,N1Num);
    if(-1 == ans) printf("Impossible\n");
    else printf("%d",ans);
    return 0;
}
复制代码
复制代码
//还有10号点过不去 
//没过是因为有两个函数的返回值是int
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 12; //const int INF = 100000000; LL INF = (1LL << 63) - 1; LL map[257] = {0}; void init() { for(char c = '0'; c <= '9'; c++) { map[c] = c - '0'; } for(char c = 'a'; c <= 'z'; c++) { map[c] = c - 'a' + 10; } } int Convert2Num(char *str,LL radix,LL Max) { LL num = 0; int len = strlen(str); for(int i = 0; i < len; i++) { num = num * radix + map[str[i]]; if(num < 0 || num > Max) return -1; } return num; } int FindLargest(char *str) { int len = strlen(str); int iMax = -1; for(int i = 0; i < len; i++) { if(iMax < map[str[i]]) { iMax = map[str[i]]; } } return iMax + 1; } //1 mid is bigger //-1 mid is smaller int cmp(char *N2,LL radix,LL N1Num) { LL N2Num = Convert2Num(N2,radix,N1Num); if(N2Num < 0 ) return 1; if(N2Num > N1Num) return 1; else if(N2Num < N1Num) return -1; else return 0; } int Binary(char *N2,LL low,LL high,LL N1Num) { LL mid = 0; while(low <= high) { mid = (low + high) / 2; int ans = cmp(N2,mid,N1Num); if(1 == ans) high = mid - 1; else if(-1 == ans) low = mid + 1; else return mid; } return -1; } int main() { init(); char N1[maxn],N2[maxn],temp[maxn]; int radix,tag; scanf("%s%s%d%d",N1,N2,&tag,&radix); if(2 == tag) { strcpy(temp,N1); strcpy(N1,N2); strcpy(N2,temp); } LL N1Num = Convert2Num(N1,radix,INF); LL low = FindLargest(N2); LL high = max(N1Num,low) + 1; LL ans = Binary(N2,low,high,N1Num); if(-1 == ans) printf("Impossible\n"); else printf("%lld",ans); return 0; }
复制代码

 

posted @   王清河  阅读(170)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示