PAT 1010

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

解析:这题主要需要注意一些细节,比如如何防止radix过大的情况,在下面的解法中使用的是long long来存储化成十进制的值

Code:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

typedef long long ll;
const ll inf=0x7fffffffffffffff;
char a[15],b[15],c[15];
ll da,db;

ll value(char *a,ll r){
        ll ans=0;
    for(int i=0;a[i];++i){
        if(a[i]<=‘9‘){
                        ans=ans*r+a[i]-48;

        }else{
                        ans=ans*r+a[i]-87;

        }
                if(ans<0 || ans>da) return inf;

    }
        return ans;

}

int cnta,cntb,n;
int main(){
        int i,type;
        ll l,r,m;
        scanf("%s%s%d%lld",a,b,&type,&r);
    if(type==2){
                strcpy(c,a);
                strcpy(a,b);
                strcpy(b,c);

    }
        da=inf;
        da=value(a,r);
        r=0;
        for(i=0;b[i];++i) if(b[i]>r) r=b[i];
        if(r<=‘9‘) r-=47;
        else r-=86;
        l=r;r=da;
    while(l<r){
                m=(l+r)>>1;
                db=value(b,m);
                if(db<da) l=m+1;
                else if(db>da) r=m-1;
                else r=m;

    }
        if(value(b,l)==da) printf("%lld\n",l);
        else if(value(b,r)==da) printf("%lld\n",r);
        else printf("Impossible\n");
        return 0;

}
时间: 2024-10-29 19:10:49

PAT 1010的相关文章

PAT 1010 Radix

过了一天感觉没干什么,想刷一发题弥补一下,正在考虑去做哪道,室友说去试试PAT 1010,果然当年自己直接跳过了这题,一看这通过率(0.07)有点夸张了.题目是已知一个数,求另外一个数的基数,使得这两个数数值上相等.很自然的考虑到使用二分搜索来确定这个基数,数字表示使用[0-9a-z],这tmd的让人很容易的想到基数的范围就在1~36之间了,艹,基数是可以超过这个范围的,如果没有考虑到这一点,可以得到的一个典型分值就是19分.不过基数在[1, 36]之间的话这个搜索范围太小了,直接暴力遍历也可以

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

PAT 1010. 一元多项式求导

1010. 一元多项式求导 (25) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数.数字间以空格分隔,但结尾不能有多余空格.注意"零多项式"的指数和系数都是0,但是表示为"0 0". 输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6

PAT 1010. 一元多项式求导 (25)

设计函数求一元多项式的导数. 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数.数字间以空格分隔,但结尾不能有多余空格.注意"零多项式"的指数和系数都是0,但是表示为"0 0". 输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6 0 思路:一次输入两个数a,b按Ctrl+z结束输入.保存在s[b]中,且令s[b]

PAT——1010. 一元多项式求导

设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数.数字间以空格分隔,但结尾不能有多余空格.注意"零多项式"的指数和系数都是0,但是表示为"0 0". 输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6 0 1 package com.ho

PAT 1010 Radix 进制转换+二分法

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

PAT 1010 Radix (二分)

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 o

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

PAT 乙级 1010 一元多项式求导 (25) C++版

1010. 一元多项式求导 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数.数字间以空格分隔,但结尾不能有多余空格.注意"零多项式"的指数和系数都是0,但是表示为"