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
代码:
•题意:输入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; } }
相关文章推荐
- 位运算之美——用+,-和位运算实现正整数除法和取模(一)
- POJ1503: Integer Inquiry(连续多个大整数加法运算)
- java 大整数运算Integer Inquiry HDU - 1047
- [sicily]1020. Big Integer
- 位运算之美——用+,-和位运算实现正整数除法和取模(二)
- 位运算之美——用+,-和位运算实现正整数除法和取模(一)
- 位运算之美——用+,-和位运算实现整数除法和取模(一)
- Sicily.1020. Big Integer
- [sicily] 1020. Big Integer
- Sicily 1020. Big Integer
- 位运算之美——用+,-和位运算实现正整数除法和取模(一)
- sicily 1020. Big Integer 大数求模
- 位运算之美——用+,-和位运算实现正整数除法和取模(一)
- java 大整数运算以及BigInteger的用法
- 位运算之美——用+,-和位运算实现正整数除法和取模(二)
- 位运算之美——用+,-和位运算实现正整数除法和取模(二)
- 位运算之美——用+,-和位运算实现整数除法和取模(一)
- 位运算之美--用 +、- 和位运算实现正整数除法和取模(2)
- sicily 1020. Big Integer
- 位运算之美——用+,-和位运算实现正整数除法和取模(一)