Basic remains(高精度)

来源:互联网 发布:淘宝快递助手怎么用 编辑:程序博客网 时间:2024/06/15 17:19

Description

Given a base b and two non-negative base b integers p and m, compute p mod m and print the result as a base b integer. p mod m is defined as the smallest non-negative integer k such that p = a*m + k for some integer a.

Input

Input consists of a number of cases. Each case is represented by a line containing three unsigned integers. The first, b, is a decimal number between 2 and 10. The second, p, contains up to 1000 digits between 0 and b-1. The third, m, contains up to 9 digits between 0 and b-1. The last case is followed by a line containing 0.

Output

For each test case, print a line giving p mod m as a base-b integer.

Sample Input

2 1100 10110 123456789123456789123456789 10000

Sample Output

10789

解题思路

都化成十进制,得到余数后再化成需要的进制;

余数为0时单独输出下,




AC代码

#include<stdio.h>#include<string.h>int main(){    int n, i;    while(scanf("%d", &n), n)    {        char str1[1005], str2[15];        scanf("%s%s", str1, str2);                          //都以字符串输入,方便的多        int len1 = strlen(str1), len2 = strlen(str2);        long long a = 0, b = 0;        for(i = 0; i < len2; i++)                           //计算除数的十进制数            b = b * n + str2[i] - '0';        for(i = 0; i < len1; i++)        {            a = a * n + str1[i] - '0';                      //被除数转化成十进制的同时,对除数取模,因为最后只要求余数            if(a >= b)                a %= b;        }        if(a == 0)        {            printf("0\n");            continue;        }        int c[15];        for(i = 14; a != 0; i--)        {            c[i] = a % n;            a /= n;        }        for(i++; i <= 14; i++)            printf("%d", c[i]);        printf("\n");    }    return 0;}


0 0
原创粉丝点击