题解:UVA10677 Base Equality
有
存在正整数
若不存在这样的整数,则输出 Non-existent.。
思路分析:
要求最大的
可以看到,
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int T,b1,b2,l,r;
string f(int x,int r){//进制转换
string s="";
while(x){
if(x%r<=9)s+=x%r+'0';
else s+=x%r+55;//要注意余数>=10的情况
x/=r;
}
reverse(s.begin(),s.end());//将s翻转
return s;
}
int change(string s,int r){
int cnt=0,base=1;
for(int i=s.size()-1;i>=0;i--,base*=r){
if(isdigit(s[i]))cnt+=(s[i]-'0')*base;
else cnt+=(s[i]-55)*base;
}
return cnt;
}//将s看成r进制,求出在十进制下的值
string find(){//枚举i
for(int i=r-1;i>l;i--){
string x=f(i,b1);
if(change(x,b2)%i==0)return to_string(i);
}
return "Non-existent.";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
cin>>b1>>b2>>l>>r;
cout<<find()<<'\n';
}
return 0;
}
制作不易,希望管理大大通过 qwq。