您的位置:首页 > 其它

Sicily 1020. Big Integer | 大整数取模运算

2017-06-29 13:41 417 查看
题目:



•题意:输入n个整数bi(1 <= i <= n),以及一个大整

数x,输出一个n元组(x mod b1,x mod b2,…,x

mod bn)

• 约束: n <= 100, 1 < bi <= 1000 (1 <= i <= n) 大整数x的位数

m <= 400并且非负

思路:

• mod 操作(对应C++中的%操作符)的性质:

• (a + b) % n == (a % n + b % n) % n

• (a * b) % n == ((a % n) * (b % n)) % n

• 所以我们要存储的值都是在模意义下的

• 大整数处理的常用办法

• 例如: 1234=(((1 * 10 + 2) * 10 + 3) * 10) + 4

• 再利用前面mod操作的性质我们可以知道:

• 1%7 = 1

• 12%7 = (((1 % 7) * 10) % 7 + 2) % 7 = 5

• 123%7 = (((12 % 7) * 10) % 7 + 3) % 7 = 4

• 1234%7 = (((123 % 7) * 10) % 7 + 4) % 7 = 2

代码:

// Problem#: 1020
// Submission#: 4531242
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int b[1010];
int mod(string s, int n)
{
int len = s.length();
int res = 0;
for(int i = 0; i < len; i++)
{
res =( (res*10) % n + s[i] -'0') % n;
}
return res;
}
int main()
{
int testcase;
int num;
cin >> testcase;
string str;
while(testcase--)
{
cin >> num;
for(int i = 1; i <= num; i++)
{
cin >> b[i];
}
cin >> str;
cout << "(";
for(int i = 1; i < num; i++)
{
cout << mod(str,b[i]) << ",";
}
cout << mod(str, b[num]) << ")" << endl;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sicily