POJ 1331 Multiply 进制转换

只是简单的转换了一下进制,很容易
Multiply
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4761 Accepted: 2551

Description

6*9 = 42" is not true for base 10, but is true for base 13. That is, 6(13) * 9(13) = 42(13) because 42(13) = 4 * 131 + 2 * 130 = 54(10).

You are to write a program which inputs three integers p, q, and r and determines the base B (2<=B<=16) for which p * q = r. If there are many candidates for B, output the smallest one. For example, let p = 11, q = 11, and r = 121. Then we have 11(3) * 11(3) = 121(3) because 11(3) = 1 * 31 + 1 * 30 = 4(10) and 121(3) = 1 * 32 + 2 * 31 + 1 * 30 = 16(10). For another base such as 10, we also have 11(10) * 11(10) = 121(10). In this case, your program should output 3 which is the smallest base. If there is no candidate for B, output 0.

Input

The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. Each test case consists of three integers p, q, and r in a line. All digits of p, q, and r are numeric digits and 1<=p,q, r<=1,000,000.

Output

Print exactly one line for each test case. The line should contain one integer which is the smallest base for which p * q = r. If there is no such base, your program should output 0.

Sample Input

3
6 9 42
11 11 121
2 2 2

Sample Output

13
3
0

Source

 

 

 

#include <iostream>
using namespace std;
__int64 fun(int t,int b){
    __int64 k=1,ret=0;
    while (t>0) {ret+=k*(t%10);t/=10;k*=b;}
    return ret;
}
int main(){
    int i,T,b_min,k,p,q,r;
    __int64 a,b,c;
    cin>>T;
    while (T--){
        cin>>p>>q>>r;
        a=p,b=q,c=r;
        b_min=0;
        while (a>0) {if (b_min<a%10) b_min=a%10;a/=10;}
        while (b>0) {if (b_min<b%10) b_min=b%10;b/=10;}
        while (c>0) {if (b_min<c%10) b_min=c%10;c/=10;}
        for (i=b_min+1;i<=16;i++)
        {
            a=fun(p,i);
            b=fun(q,i);
            c=fun(r,i);
            if (a*b==c) break;
        }
        if (i==17) i=0;
        cout<<i<<endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值