pat1010.Radix(25)

来源:互联网 发布:iphone降价规律 知乎 编辑:程序博客网 时间:2024/05/19 19:14

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

关键两点:1 二分查找,否则会超时,2如果不用二分会超时,但是不会溢出,用二分会溢出,所以我们选择溢出而不选择超时,溢出只要判定即可;

具体的解析,网上很多。自己敲会有很多收获,这是个好题,赞一个,我敲了三天。

喜欢算法的加我 1115787241,一起学习算法

#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;long long ans_find(int* c,int len,long long radix){    long long ans = 0;    long long r = 1;    int i;    for(i = len-1;i>=0;i--){        ans += (c[i] * r);        r *= radix;    }    if(ans < 0)return 0;//yichu    return ans;}int main(){    char a[100],b[100];    int tag ;    long long radix = 0;    while(~scanf("%s %s %d %lld",&a,&b,&tag,&radix)){            int len1 = strlen(a);            int len2 = strlen(b);            int n_a[100],n_b[100];            int m1 = -1 ,m2 = -1;            /*               calcute the array from character to integer;            */            for(int i =0;i<len1;i++){                if(a[i] >= 'a' && a[i] <= 'z')n_a[i] = a[i] - 'a' + 10;                else n_a[i] = a[i] - '0';                if(n_a[i] > m1)m1 = n_a[i];            }            for(int i = 0;i<len2;i++){                if(b[i] >= 'a' && b[i] <='z')n_b[i] = b[i]-'a' + 10;                else n_b[i] = b[i] - '0';                if(n_b[i] > m2) m2 = n_b[i];            }           if(tag == 1){               long long ans = 0;               ans = ans_find(n_a,len1,radix);              // cout<<ans<<" "<<m2<<endl;// we need tp find the number of array_second's biggest number;               if(ans == m2 ){                  if(ans == ans_find(n_b,len2,m2+1))cout<<m2+1<<endl;                  else cout<<"Impossible"<<endl;               }               if(m2 < ans){                   //use the biary_find;                   long long l = m2+1;                   long long r = ans;                   long long mid = 0;                   long long temp = 0;                   long long flag = 0;                   while(l<=r){                        mid = l + (r-l)/2;                        temp = ans_find(n_b,len2,mid);                        if(temp < ans&&temp){                            l = mid+1;                        }else if(temp >ans||temp == 0){                            r = mid-1;                        }else if(temp == ans){                            flag = mid;                            break;                        }                   }                   if(flag)                   cout<< flag  <<endl;                   else cout<<"Impossible"<<endl;               }               if(ans < m2)cout<<"Impossible"<<endl;           }else{               long long ans = 0;               ans = ans_find(n_b,len2,radix);              // cout<<ans<<" "<<m1<<endl;               if(ans == m1){                   if(ans == ans_find(n_a,len1,m1+1))cout<<m1 + 1 <<endl;                   else cout<<"Impossible"<<endl;               }               if(ans > m1){                   long long l = m1 + 1;                   long long flag = 0;                   long long r = ans;                   long long mid = 0;                   long long temp = 0;                   while(l <= r){                      mid = l + (r - l)/2;                      temp = ans_find(n_a,len1,mid);                      if(temp < ans &&temp)l = mid+1;                      else if(temp >ans||temp == 0) r = mid -1;                      else if(temp == ans){                         flag = mid;                         break;                      }                   }                   if(flag)                   cout<< flag  <<endl;                   else cout<<"Impossible"<<endl;               }               if(ans < m1)cout<<"Impossible"<<endl;           }    }    return 0;}

http://www.patest.cn/submissions/1515692

0 0