You are given two positive integers A and B in Base C. For the equation:
We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in this problem, we want to maximize k.
For example, A="123" and B="100", C=10. So both A and B are in Base 10. Then we have:
(1) A=0*B+123
(2) A=1*B+23
As we want to maximize k, we finally get one solution: (1, 23)
The range of C is between 2 and 16, and we use 'a', 'b', 'c', 'd', 'e', 'f' to represent 10, 11, 12, 13, 14, 15, respectively.
Input
The first line of the input contains an integer T (T≤10), indicating the number of test cases.
Then T cases, for any case, only 3 positive integers A, B and C (2≤C≤16) in a single line. You can assume that in Base 10, both A and B is less than 2^31.
3 2bc 33f 16 123 100 10 1 1 2Sample Output
(0,700) (1,23) (1,0)
题意:给两个字符串a,b,一个数字n,表示a,b是n进制数,求使得a=k*b+c成立时 k最大的k,b的结果;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
map<char,int>mp;
void init(){ //初始化
int i;
for(i=0;i<10;i++)
mp['0'+i]=i;
for(int j=0;i<16;i++,j++)
mp['a'+j]=i;
}
int solve(string str,int n){ //进制转化
int ans=0;
for(int i=0;i<str.length();i++)
ans=ans*n+mp[str[i]];
return ans;
}
int main(){
int T;
scanf("%d",&T);
init();
while(T--){
string a,b;
int n;
cin>>a>>b>>n;
int A=solve(a,n);
int B=solve(b,n);
printf("(%d,%d)\n",A/B,A%B);
}
return 0;
}