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\n",ans);
105     return 0;
106 }
时间: 2024-11-09 04:34:33

A1010. Radix (25)的相关文章

A1010 Radix (25 分)

一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbegin(),这里it作用就是指针一样,装的是地址,了解函数begin()是返回字符串str的一个元素地址和函数end()是返回字符串最后一个元素还要往后一位,而rbegin()和rend(),加了r后刚刚反过来了,rbegin()指向str的最后一个元素,rend()指向第一个元素还要往前一位. 第二

PAT 1010. Radix (25)

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 i

1010. Radix (25)——PAT (Advanced Level) Practise

题目信息: 1010. Radix (25) 时间限制 400 ms 内存限制 32000 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 b

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 rad

pat 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 N?1?? and N?2??, your task is to find the radi

1010. Radix (25)

简直是哭着做出来的,起码提交了22遍.创纪录了... 首先不能像上次那样枚举进制数,那样输入987654321 10 1 10 的话十分钟都算不完,题中只有一个超时很照顾人了... 考虑近似计算,利用y=a*x^n+b 算出n,进制数如果有,也不会超过n,因此考虑由n向下寻找. 注意,进制数不能小于数中最大的数字. 如果将两个数字作为字符串输入,注意排除字符串前面的0. 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN,

pat1010. Radix (25) BUG!!!

#include<stdio.h> #include<stdlib.h> #include<string.h> int c2int(char c){ if('0'<=c&&c<='9') return c-'0'; else return c-'a'+10; } long s2decimal(char str[], long radix){ long digit, sum=0; // 必须判断每一位是否小于radix int i=0; for

PTA A1009&amp;A1010

第五天 A1009 Product of Polynomials (25 分) 题目内容 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of

数据结构学习笔记06排序 (快速排序、表排序)

1.快速排序 不稳定 分而治之 找主元pivot,小于主元划分为一个子集,大于主元的划分为一个子集 然后进行递归 最好情况:每次主元正好中分,T(N) = O( NlogN ) 选主元 的方法有很多,这里用 取头.中.尾的中位数. 直接选A[0]为pivot,时间复杂度T ( N ) = O( N ) + T ( N–1 ) = O( N ) + O ( N–1 ) + T( N–2 ) = = O( N ) + O ( N–1 ) + …+ O( 1 ) = O( N^2 ) 随机取pivot