一直没写过技术博客,此博客到大三才开实在是晚。
此题为“算法分析与设计”的作业第一题,是sicily题库里的编号为1020的题目。
早上老师课上刚说分治法,看题目要求是大数求模,估计用int之类的存不下了,所以用string存后再处理。
以下是我AC的答案:
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int tests;
cin >> tests;
while(tests--) {
int basisSize;
cin >> basisSize;
int basis[basisSize];
for(int i = 0; i < basisSize; i++) {
cin >> basis[i];
}
string veryLongInteger;
cin >> veryLongInteger;
int representation[basisSize];
for(int i = 0; i < basisSize; i++) {
int temp = 0;
for(int j = 0; j < veryLongInteger.length(); j++) {
temp = temp * 10 + (veryLongInteger[j] - '0');
temp %= basis[i];
}
representation[i] = temp;
}
cout << '(';
for(int i = 0; i < basisSize; i++) {
if(i < basisSize - 1) {
cout << representation[i] << ',';
}
else {
cout << representation[i] << ')';
}
}
cout << endl;
}
return 0;
}
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
int tests;
cin >> tests;
while(tests--) {
int basisSize;
cin >> basisSize;
int basis[basisSize];
for(int i = 0; i < basisSize; i++) {
cin >> basis[i];
}
string veryLongInteger;
cin >> veryLongInteger;
int bitsOfInteger[100] = {0};
int bits = 0;
for(int i = 0; i < 100; i++) {
if(veryLongInteger[i] != '\0') {
bitsOfInteger[i] = static_cast<int>(veryLongInteger[i]) - 48;
bits++;
}
else {
break;
}
}
int representation[basisSize];
memset(representation, 0, sizeof(representation));
for(int i = 0; i < bits; i++) {
for(int j = 0; j < basisSize; j++) {
representation[j] = representation[j] * 10 + bitsOfInteger[i];
representation[j] %= basis[j];
}
}
cout << '(';
cout << representation[0];
for(int i = 1; i < basisSize; i++) {
cout << ',' << representation[i];
}
cout << ')' << endl;
}
return 0;
}