1010. Radix (25)

Source: Internet
Author: User

Description: 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 radixHere 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 110 1 10 Sample Output 1: 2 Sample Input AB 1 2 Sample Output 2: Impossible analysis: (1) at the beginning, we thought we could just traverse the hexadecimal system, however, it is not considered that the starting radix can be a very large number. The initial code is as follows:

#include<iostream>#include<cstdio>#include<string.h>using namespace std;#define max 10int change(char *s, int base){int temp = 0;int i;for(i=0; i<strlen(s); i++){if(s[i]>='0' && s[i]<='9'){if((s[i] - '0') >= base) return -1;temp = temp*base + (s[i] - '0');}else{if( (s[i]-'a'+10) >= base ) return -1;temp = temp*base + (s[i] - 'a' + 10);}}return temp;}int main( ){char N1[max];char N2[max];int tag;long long result1,result2,radix;long long i;bool flag = false;scanf("%s%s%d%ld",N1,N2,&tag,&radix);if(tag == 1){result1 = change(N1,radix);for(i=2; i<=10000000; i++){result2 = change(N2,i);if(result2 == result1) {flag = true; break;}}}else if(tag == 2){result2 = change(N2,radix);for(i=2; i<=10000000; i++){result1 = change(N1,i);if(result1 == result2) {flag = true; break;}}}if(flag)cout<<i<<endl;elsecout<<"Impossible"<<endl;return 0;}
#include <stdio.h>#include <string.h>#define max 11char a[4][max];long long num2Dec(char * p, long long radix) {      long long  i;      long long  len=strlen(p);      long long digit = 0;      long long m = 1;      long long sum = 0;      for(i=len-1;i>=0;i--)      {         if(p[i]>='a'&&p[i]<='z')              digit= p[i] - 'a' + 10;         else if(p[i]>='0'&& p[i]<='9')              digit=p[i] - '0';          sum+=digit*m;          m*=radix;      }      return sum; } int findLeastRadix(char *p)  {     long long  len=strlen(p);     long long  low=0;     long long num;     long long i;     for(i=len-1;i>=0;i--)      {          if(p[i]>='a'&&p[i]<='z')             num= p[i] - 'a' + 10;          else if(p[i]>='0'&& p[i]<='9')              num=p[i] - '0';          if(num+1>low)              low=num+1;      }      return low; }int compare(char* p, long long radix, long long target) {     long long  i;     long long len=strlen(p);     long long m = 1;     long long num = 1;     long long sum = 0;     for(i=len-1;i>=0;i--)     {         if(p[i]>='a'&&p[i]<='z')             num= p[i] - 'a' + 10;         else if(p[i]>='0'&& p[i]<='9')             num=p[i] - '0';         sum+=num*m;         m*=radix;         if(sum>target)  //avoid  overflow             return 1;     }     if(sum>target)         return 1;     else if(sum<target)         return -1;     else         return 0; }long long binarySearch(char* p, long long low, long long high, long long target){    long long mid = low;    int tag;    while(low<=high) {        tag = compare(p, mid, target);        switch(tag) {            case -1:                low = mid + 1;                break;            case 0:                return mid;            case 1:                high = mid - 1;        }        mid = (low + high) >> 1;    }    return -1;}int main(int argc, char* argv[]){    int i;    int tag;    int base;    long long n1;    long long n2;    long long low;    long long high;    long long radix;    for(i=0; i<4; i++) {        scanf("%s", a[i]);    }    tag = atoi(a[2]);    base = atoi(a[3]);    switch(tag) {        case 1 :            n1 = num2Dec(a[0], base);            low=findLeastRadix(a[1]);            high = (n1+1 > low+1) ? n1+1 : low+1;            radix = binarySearch(a[1], low, high, n1);            break;        case 2 :            n2 = num2Dec(a[1], base);            low=findLeastRadix(a[0]);            high = (n2+1 > low+1) ? n2+1 : low+1;            radix = binarySearch(a[0], low, high, n2);            break;    }    if(-1 != radix){         printf("%ld\n", (long)radix);    }    else {        printf("Impossible");    }    return 0;}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.