Change Base

13 篇文章 0 订阅

Given an integer m in base B (2 ≤ B ≤ 10) (m contains no more than 1000 digits), find the value of the integer m in base 10, output the result modulo 10007.

Input

The first line of the input is a single integer T representing the number of test cases. Then T lines follow. In each line there are two integers: B and m, separated by a single space.

Output

For each test cases, give your answer in a single line.

Sample Input

3
2 101
7 16532505605660160442
10 246234

Sample Output

5
4165
6066

题意概述:给定一个以B为基数的整数,将这个数转化为以10为基数的整数,将所得的数对10007取模,将取模后的结果输出。需要注意的是,题目中指出给定的整数可能会很长,可能会有1000个位。

解题思路:从题目概述中很容易就知道,这个题目必须使用字符串进行模拟,否则不能做。题目主要用到模运算的一些性质,了解即可。

源代码:

#include<string>
#include<iostream>
using namespace std;
int main()
{
    int T,B,L,R;
    string s;
    cin>>T;
    while(T--)
    {
         R=0;
         cin>>B>>s;
         for(int i=0;i<s.size();++i)
         {
               //过程中就可以对每一次结果进行取模,避免出现结果超出int型的范围 
               R=(R*B+s[i]-48)%10007; 
         }
         cout<<R<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值