SOJ 1020 Big Integer

题目大意: 输入T,表示测试样例总数。对每个样例,首先输入n(n ≤ 10),接下来有n个数输入,计作b1...bn,最后输入x,其中x是一个非常大不超过400字符的长正整数。

限制条件:1 < bi ≤ 1000,  1 ≤ i ≤ n 且 gcd(bi, bj) = 1 ,  (1 ≤ i, j ≤ n, i ≠ j)

     让M = b1 * b2 * ... * bn , x < M

输出:x % bi  = ri

  格式: (r1,r2,...,rn)

解题思路:

  由于长正整数x非常大,不能用基本类型保存。模拟除法运算求余数。

代码如下:

 1 #include <iostream>
 2 #include <string>
 3 #include <cstring>
 4 using namespace std;
 5
 6 int char2Int(char ch) {
 7     return ch - ‘0‘;
 8 }
 9
10 int main() {
11     string str;
12     int T, n;
13     const int MAXN = 105;
14     int b[MAXN];
15     int m[MAXN];
16     memset(b, sizeof b, 0);
17     // memset(m, sizeof m, 0);
18
19     cin >> T;
20     while (T--) {
21         // 输入
22         cin >> n;
23         for (int i = 0; i < n; i++) {
24             cin >> b[i];
25         }
26         cin >> str;
27
28         // 计算
29         memset(m, 0, sizeof m);    // 太久没用memset,给忘了,没有正确初始化。
30         for (int i = 0; i < str.length(); i++) {
31             int next = char2Int(str[i]);
32             for (int j = 0; j < n; j++) {
33                 // cout << j << ": ( " << m[j] << " * 10 + " << next << " ) % " << b[i] << " = ";
34                 m[j] = (m[j] * 10 + next) % b[j];
35                 // cout << m[j] << endl;
36             }
37         }
38
39         // 输出
40         cout << "(" << m[0];
41         for (int i = 1; i < n; i++) {
42             cout << "," << m[i];
43         }
44         cout << ")" << endl;
45     }
46
47     return 0;
48 }
时间: 2024-08-23 08:28:57

SOJ 1020 Big Integer的相关文章

sicily 1020. Big Integer

这道题的关键也就两句话 for (int i = 0; i < len; ++i) { ans = (ans*10 + x[i]-'0') % num; } 其余也没什么好说的 #include <stdio.h> #include <string.h> int getVeryLongNumberMod(char x[],int num){ int len = strlen(x); int ans = 0; for (int i = 0; i < len; ++i) {

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

Sicily1020-大数求余算法及优化

Github最终优化代码: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1020.c 题目如下: 1020. Big Integer Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Long long ago, there was a super computer that could deal with VeryLongInt

.Net Core CLR FileFormat Call Method( Include MetaData, Stream, #~)

.Net Core  CLR PE 文件启动方法,找到函数入口点,调用整个.Net 程式宿主. 使用方法:可以利用Visual Studio新建一个控制台应用程序,然后生成DLL,替换掉本程序DLL,新建C++ .CPP 文件 然后即可运行看到效果. 整个代码情况如下 包括 IMIAGE_CORE20_HEADER MetaData, 表格对其方式等待 4 #include "pch.h" 5 #include <iostream> 6 #include <Windo

杭电1020

Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26063    Accepted Submission(s): 11467 Problem Description Given a string containing only 'A' - 'Z', we could encode it using the follow

soj 1015 Jill&#39;s Tour Paths 解题报告

题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every year, Jill takes a bicycle tour between two villages. There are different routes she can take between these villages, but she does have an upper limit

HDU字符串基础题(1020,1039,1062,1088,1161,1200,2017)

并不是很精简,随便改改A过了就没有再简化了. 1020. Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following method: 1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only c

PAT 1020. Tree Traversals (25)

1020. Tree Traversals (25) Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

1020. Tree Traversals (25)

根据两种遍历方式建立二叉树 层遍历二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the le