南工个人积分赛第四场——A - Solve equation【pow要用double型的】

来源:互联网 发布:网络电视台直播网 编辑:程序博客网 时间:2024/06/09 23:25
A - Solve equation
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice FZU 2102

Description

You are given two positive integers A and B in Base C. For the equation:

A=k*B+d

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.

Output

For each test case, output the solution “(k,d)” to the equation in Base 10.

Sample Input

32bc 33f 16123 100 101 1 2

Sample Output

(0,700)(1,23)(1,0)

WA了7遍,终于过了,最后输出了一下pow()函数的结果才发现,pow要用double型的,换了另一种方法后过了啊啊啊啊啊啊!我好崩溃,差点想破罐子破摔了
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;int main(){    int t,d,i,bj,lena,lenb;    long long x,y,p,q,sum;    char a[1000],b[1000];    scanf("%d",&t);    while(t--)    {        scanf("%s%s%d",a,b,&d);        lena=strlen(a);        lenb=strlen(b);        bj=0;        sum=1;        x=0;       for(i=lena-1;i>=0;i--)        {            if(a[i]>='0'&&a[i]<='9')                x=x+(a[i]-48)*sum;            else if(a[i]>='a'&&a[i]<='f')                x=x+(a[i]-87)*sum;                 bj++;                 sum=sum*d;//我改用这种方式之后就过了        }        y=0;        bj=0;        sum=1;        for(i=lenb-1; i>=0; i--)        {            if(b[i]>='0'&&b[i]<='9')                y=y+(b[i]-48)*sum;            else if(b[i]>='a'&&b[i]<='f')                y=y+(b[i]-87)*sum;                bj++;                sum=sum*d;        }     if(y==0)      p=0;     else      p=x/y;      q=x-p*y;        printf("(%I64d,%I64d)\n",p,q);     }}




0 0