思路梳理
首先将已知radix的树转化为10进制
从大到0遍历 进制数 查看另一个数是否与之相等
字母的处理是将每一个数字存储在数组中,再拿出来计算对应位置的数字
第一次解题代码
直接遍历
#include <iostream>
#include <cmath>
#include <cstring>
int main(int argc, char** argv) {
long n1=0,n2=0,tag=0,radix=0;
long s[2];
int index = 0;
long sum = 0;
long sum1 = 0,flag = 0;
int temp= 0;
int len[2];
char x1[11],x2[11];
long b[2][11];
scanf("%s %s",&x1,&x2);
for(int i = 0;i<strlen(x1);i++)
{
if(x1[i]<='9')
b[0][i] = x1[i] - '0';
else
b[0][i] = x1[i] - 'a' + 10;
}
for(int i = 0;i<strlen(x2);i++)
{
if(x2[i]<='9')
b[1][i] = x2[i] - '0';
else
b[1][i] = x2[i] - 'a' + 10;
}
scanf("%ld %ld",&tag,&radix);
len[0] = (int)strlen(x1);
len[1] = (int)strlen(x2);
// s[0] = getnum(b1,len1);
// s[1] = getnum(b2,len2);
// printf("%ld %ld %ld %ld\n",s[0],s[1],tag,radix);
for(int z = len[tag - 1]-1;z>=0;z--)
{
int i = pow(radix,index);
sum += b[tag-1][z] * i;
index++;
}
// printf("%dyy",sum);
if(tag-1 == 1)
tag = 1;
else
tag =2;
for(int j = 100;j>0;j--)
{
index = 0;
for(int z = len[tag - 1]-1;z>=0;z--)
{
int i = pow(j,index);
sum1 += b[tag-1][z] * i;
index++;
}
if(sum1 == sum)
{
printf("%d",j);
flag = 1;
break;
}
// printf("%dxx",sum1);
sum1 = 0;
}
if(flag == 0)
printf("Impossible");
return 0;
}
在这里插入代码片
然后用二分法
考虑在数字为个位数时答案不唯一,使得r=sum+1;
因为进制数没有规定上限 考虑溢出 使用longlong 并且判断sum1是否小于0
修改后的代码
#include <iostream>
#include <cmath>
#include <cstring>
typedef long long ll;
int main(int argc, char** argv) {
ll n1=0,n2=0,tag=0,radix=0;
ll s[2];
ll index = 0;
ll sum = 0;
ll sum1 = 0,flag = 0;
ll temp= 0;
ll len[2];
char x1[11],x2[11];
ll b[2][11];
scanf("%s %s",&x1,&x2);
for(int i = 0;i<strlen(x1);i++)
{
if(x1[i]<='9')
b[0][i] = x1[i] - '0';
else
b[0][i] = x1[i] - 'a' + 10;
}
for(int i = 0;i<strlen(x2);i++)
{
if(x2[i]<='9')
b[1][i] = x2[i] - '0';
else
b[1][i] = x2[i] - 'a' + 10;
}
scanf("%ld %ld",&tag,&radix);
len[0] = (int)strlen(x1);
len[1] = (int)strlen(x2);
for(int z = len[tag - 1]-1;z>=0;z--)
{
int i = pow(radix,index);
sum += b[tag-1][z] * i;
index++;
}
if(tag-1 == 1)
tag = 1;
else
tag =2;
ll l = 0,r=sum + 1;
ll mid = 0;
while(l<=r)
{
mid = l + (r-l)/2;
index = 0;
for(int z = len[tag - 1]-1;z>=0;z--)
{
int i = pow(mid,index);
sum1 += b[tag-1][z] * i;
index++;
}
if(sum1 == sum)
{
printf("%d",mid);
flag = 1;
break;
}
else if(sum1>sum || sum1<0)
r = mid -1;
else
l = mid +1;
sum1 = 0;
}
if(flag == 0)
printf("Impossible");
return 0;
}
在这里插入代码片
神奇的分数变低了。。。
哭泣 ,纠缠了一天了 ,占时放弃。。
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include<iostream>
using namespace std;
typedef long long ll;
map<char,int> mp;
ll inf = (1ll<<63) - 1;
void init()
{
for(char c = 'a';c <= 'z';c++)
{
mp[c] = c - 'a' + 10;
}
for(char c = '0';c <= '9';c++)
{
mp[c] = c - '0';
}
}
int covernum10(string a,ll radix,ll t)
{
ll ans = 0;
int len = a.size();
for(int i = 0;i<len;i++)
{
ans = ans * radix + mp[a[i]];
if(ans < 0 || ans > t) return -1;
}
return ans;
}
int cmp(string n2,ll radix,ll t)
{
int len = n2.size();
ll num = covernum10(n2,radix,t);
if(num < 0) return 1;
if(t > num) return -1;
else if(t == num) return 0;
else return 1;
}
ll findlargetstD(string n2)
{
int len = n2.size();
ll maxm = -1;
for(int i = 0;i<len;i++)
{
if(maxm < mp[n2[i]])
maxm = mp[n2[i]];
}
return maxm + 1;
}
int binarysearch(string n2,ll left,ll right,ll t)
{
ll mid;
while(left <= right)
{
mid = (left + right) /2;
int flag = cmp(n2,mid,t);
if(flag == 0)return mid;
if(flag == 1)
right = mid - 1;
else if(flag == -1)
left = mid + 1;
}
return -1;
}
int main()
{
string n1,n2;
int tag,radix;
ll num1=0,num2=0;
cin>>n1;
cin>>n2;
scanf("%d %d",&tag,&radix);
if(tag == 2)
{
string temp = n1;
n1 = n2;
n2 = temp;
}
init();
ll t = covernum10(n1,radix,inf);
ll low = findlargetstD(n2);
ll high = max(low,t) + 1;
ll ans = binarysearch(n2,low,high,t);
if(ans == -1)
printf("Impossible");
else
printf("%lld",ans);
return 0;
}
学习算法笔记的解法