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 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 10Sample Output 1:
2Sample Input 2:
1 ab 1 2Sample Output 2:
Impossible
給出兩個數N1和N2,Tag指出所給的Radix是第幾個數的進位制,計算出一個進位制使得N1=N2。
我的思路是先將給定進位制的數轉換成十進位制sum,記錄下未定數中各個位上最大的一位數mmax,然後根據“一個數的各個位上最大的數小於其進位制”在[mmax+1,sum+1]範圍內二分遍歷可能使得兩數相等的進位制。
(;´д`)ゞ這裡如果不二分的話,for迴圈在測試點7上限需要開到1e10,會直接超時爆掉…
#include<bits/stdc++.h>
using namespace std;
#define INF 0xfffffff
#define MAXN 1010
int a[MAXN],b[MAXN],mmax;
long long sum,res,ans;
void change10(string x,int r)//轉成10進位制
{
memset(a,0,sizeof(a));
int len=x.length();
int cnt=len-1;
sum=0;
for(int i=0; i<len; ++i)
{
if(x[i]>='0'&&x[i]<='9')
a[i]=int(x[i]-'0');
else if(x[i]>='a'&&x[i]<='z')
a[i]=int(x[i]-'a'+10);
sum+=(pow(r,cnt)*a[i]);
--cnt;
}
}
bool tryR(string x)//依次嘗試各種進位制
{
memset(b,0,sizeof(b));
int len=x.length();
mmax=0;
for(int i=0; i<len; ++i)
{
if(x[i]>='0'&&x[i]<='9')
b[i]=int(x[i]-'0'),mmax=max(mmax,b[i]);
else if(x[i]>='a'&&x[i]<='z')
b[i]=int(x[i]-'a'+10),mmax=max(mmax,b[i]);
}
long long low=mmax+1,high=sum+1;//上限至少要開到10000000000;
long long mid;
A:
while(low<=high)//二分處理
{
res=0;
mid=(low+high)/2;
int cnt=len-1;
for(int i=0; i<len; ++i)
{
res+=(pow(mid,cnt)*b[i]),--cnt;
if(res>sum||res<0)//越界
{
high=mid-1;
goto A;
}
}
if(res<sum) low=mid+1;
else if(res>sum) high=mid-1;
else if(res==sum)//&&mid>=mmax
{
ans=mid;
return true;
}
}
return false;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
string n,m;
int t,r;
cin>>n>>m>>t>>r;
bool flag=false;
if(t==1)
change10(n,r),flag=tryR(m);
else if(t==2)
change10(m,r),flag=tryR(n);
if(flag) cout<<ans<<endl;
else cout<<"Impossible"<<endl;
}
相關文章
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- PAT甲級-1012. The Best Rank (25)並列排序排序
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT答案(D進位制的A+B)
- PAT-B 1022 D進位制的A+B【進位制】
- PAT-B 1057 數零壹【進位制】
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- PAT甲級1023 Have Fun with Number
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- PAT甲級考試題庫題目分類
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級-1005. Spell It Right (20)各位之和
- 進位制詳解:二進位制、八進位制和十六進位制
- PAT B1022 D進位制的A+B(進位制轉換板題,簡單模擬)
- 【進位制轉換】二進位制、十六進位制、十進位制、八進位制對應關係
- 計算機基礎進位制轉換(二進位制、八進位制、十進位制、十六進位制)計算機
- 二進位制,八進位制,十進位制,十六進位制的相互轉換
- JavaScript 二進位制、八進位制與十六進位制JavaScript
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- PAT甲級1126~1130|C++實現C++
- 2024 秋季PAT認證甲級(題解A1-A4)
- JavaScript 進位制轉換(2進位制、8進位制、10進位制、16進位制之間的轉換)JavaScript
- java中二進位制、八進位制、十進位制、十六進位制的轉換Java
- 二進位制,八進位制,十進位制,十六進位制之間的轉換
- Python 進位制互相轉換(二進位制、十進位制和十六進位制)Python
- JAVA 二進位制,八進位制,十六進位制,十進位制間進行相互轉換Java
- Qt進位制轉換(十進位制轉十六進位制)QT
- 1474 十進位制轉m進位制+1475 m進位制轉十進位制
- [計算機基礎] 計算機進位制轉換:二進位制、八進位制、十進位制、十六進位制計算機
- C# 2進位制、8進位制、10進位制、16進位制...各種進位制間的輕鬆轉換C#
- n進位制轉十進位制
- 十進位制轉十六進位制
- 10進位制 VS 2進位制
- 二進位制、十進位制與十六進位制相互轉化
- 大話二進位制,八進位制,十進位制,十六進位制之間的轉換
- 一看就懂二進位制、八進位制、十六進位制數轉換十進位制
- python進位制轉換(二進位制、十進位制和十六進位制)及注意事項Python