Change Base
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;
}