• A1010. 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
      1 // hahaha.cpp : 定义控制台应用程序的入口点。
      2 //
      3 
      4 #include <stdafx.h>
      5 #include <stdio.h>
      6 #include <iostream>
      7 #include <vector>
      8 #include <map>
      9 #include <string>
     10 #include <cstdio>
     11 #include <set>
     12 #include <algorithm>
     13 #include <string.h>
     14 #include <string.h>
     15 using namespace std;
     16 
     17 typedef long long LL;
     18 
     19 LL mp[256];
     20 LL inf=(1LL << 63)-1;
     21 void init()
     22     {
     23     for(char c='0';c<='9';c++)
     24         {
     25         mp[c]=c-'0';
     26         }
     27     for(char c='a';c<='z';c++)
     28         {
     29         mp[c]=c-'a'+10;
     30         }
     31     }
     32 
     33 LL cvtNum10(char a[],LL radix,LL t)
     34     {
     35     LL ans=0;
     36     int len=strlen(a);
     37     for(int i=0;i<len;i++)
     38         {
     39         ans=ans*radix+mp[a[i]];
     40         if(ans<0||ans>t)return -1;
     41         }
     42     return ans;
     43     }
     44 
     45 LL findmax(char n2[])
     46     {
     47     LL ans=-1,len=strlen(n2);
     48     for(int i=0;i<len;i++)
     49         {
     50         if(mp[n2[i]]>ans)
     51             {
     52             ans=mp[n2[i]];
     53             }
     54         }
     55     return ans+1;
     56     }
     57 
     58 int cmp(char a[],LL radix,LL t)
     59     {
     60     int len=strlen(a);
     61     LL num=cvtNum10(a,radix,t);
     62     if(num<0)return 1;
     63     if(t>num)return -1;
     64     else if(num==t)return 0;
     65     else return 1;
     66     }
     67 
     68 
     69 LL bsch(char n2[],LL left,LL right,LL t)
     70     {
     71     LL mid;
     72     while(left<=right)
     73         {
     74         mid=(left+right)/2;
     75         int flag=cmp(n2,mid,t);
     76         if(flag==0) return mid;
     77         else if (flag==-1)left=mid+1;
     78         else right=mid-1;
     79         }
     80     return -1;
     81     }
     82 
     83 
     84 
     85 
     86 char n1[20] ,n2[20],tmp[20];
     87 int tag,radix;
     88 
     89 int main()
     90 {
     91     init();
     92     scanf("%s %s %d %d",n1,n2,&tag,&radix);
     93     if(tag==2)
     94         {
     95         strcpy(tmp,n1);
     96         strcpy(n1,n2);
     97         strcpy(n2,tmp);
     98         }
     99     LL t=cvtNum10(n1,radix,inf);
    100     LL low=findmax(n2);
    101     LL high=max(low,t)+1;
    102     LL ans=bsch(n2,low,high,t);
    103     if(ans==-1)printf("Impossible");
    104     else printf("%lld
    ",ans);
    105     return 0;
    106 }
  • 相关阅读:
    CTO干点啥?
    [转] 持续集成与持续交付备忘录
    [转]概率基础和R语言
    程序自信
    [转]Neural Networks, Manifolds, and Topology
    ubuntu14 + nginx + php
    [转]http://makefiletutorial.com/
    REDIS key notification
    GO RPC
    xpcall 安全调用
  • 原文地址:https://www.cnblogs.com/ligen/p/4343768.html
Copyright © 2020-2023  润新知