做[编程题]最短路径的时候写出下面的代码
#include
using namespace std;
#define maxDigits 1e3
class bigInt{
public:
vector<int> digits;
int len;
bigInt(){
//默认初始化是空
digits.resize(maxDigits);
len=0;
}
bigInt(const string& str){
digits.resize(maxDigits);
len=0;
for (int i=str.size()-1;i>=0;i--){
digits[len++]=str[i]-'0';
}
}
bigInt& operator=(const bigInt& b){
for (int i=0;i<len;i++) digits[i]=0;
len=b.len;
for (int i=0;i<b.len;i++) digits[i]=b.digits[i];
return *this;
}
bool operator==(const bigInt& b)const{
if (len!=b.len) return false;
for (int i=0;i<len;i++){
if (digits[i]!=b.digits[i]) return false;
}
return true;
}
bool operator<=(const bigInt& b)const{
if (len<b.len) return true;
if (len>b.len) return false;
for (int i=len-1;i>=0;i--){
if (digits[i]==b.digits[i]) continue;
else return digits[i]<b.digits[i];
}
return true; //一直continue的话就会返回true
}
bool operator<(const bigInt& b)const{
if (len<b.len) return true;
if (len>b.len) return false;
for (int i=len-1;i>=0;i--){
if (digits[i]==b.digits[i]) continue;
else return digits[i]<b.digits[i];
}
return false; //一直continue的话就会返回false
}
bigInt operator+(const bigInt& b)const{
int carry=0;
bigInt answer;
//123存储的是{3,2,1},应该从3开始加
for (int i=0;i<len || i<b.len;i++){
//resize之后的都是0,相当于已经高位对齐0了
int current=digits[i]+b.digits[i]+carry;
if (current>10){
carry=1;
current-=10;
}
else carry=0;
answer.digits[answer.len++]=current;
}
if (carry) answer.digits[answer.len++]=carry;
return answer;
}
bigInt operator-(const bigInt& b)const{
//大数减小数,从低位开始减
int carry=0; //借位
bigInt answer;
for (int i=0;i<len || i<b.len;i++){
int current=digits[i]-b.digits[i]-carry;
if (current<0){
carry=1;
current+=10;
}
else carry=0;
answer.digits[answer.len++]=current;
}
//去除高位0
while (answer.digits[answer.len-1]==0 && answer.len>1) answer.len--;
return answer;
}
bigInt operator*(const bigInt& b)const{
bigInt answer;
answer.len=b.len+len;
for (int i=0;i<len;i++){
for (int j=0;j<b.len;j++){
answer.digits[i+j]+=digits[i]*b.digits[j];
}
}
//进位
for (int i=0;i<answer.len;i++){
answer.digits[i+1]+=answer.digits[i]/10;
answer.digits[i]%=10;
}
while (answer.digits[answer.len-1]==0 && answer.len>1) answer.len--;
return answer;
}
bigInt operator/(const bigInt& b)const{
bigInt tmp=b,remainder("0"),answer; //remainder初始化是0
answer.len=len;
//从高位开始除
for (int i=len-1;i>=0;i--){
if (!(remainder.digits[0]==0 && remainder.len==1)){
//remainder不是0,就开始移位
for (int j=remainder.len-1;j>=0;j--){
remainder.digits[j+1]=remainder.digits[j];
}
remainder.len++;
}
remainder.digits[0]=digits[i];
while (tmp<=remainder){
//看看余数里有几个被除数
remainder=remainder-tmp;
answer.digits[i]++;
}
}
while (answer.digits[answer.len-1]==0 && answer.len>1) answer.len--;
return answer;
}
bigInt operator%(const bigInt& b)const{
bigInt tmp=b,remainder("0");
for (int i=len-1;i>=0;i--){
if (!(remainder.digits[0]==0 && remainder.len==1)){
//remainder不是0,就开始移位
for (int j=remainder.len-1;j>=0;j--){
remainder.digits[j+1]=remainder.digits[j];
}
remainder.len++;
}
remainder.digits[0]=digits[i];
while (tmp<=remainder){
//看看余数里有几个被除数
remainder=remainder-tmp;
}
}
return remainder;
}
};
ostream& operator<<(ostream& out,const bigInt& b){
for (int i=b.len-1;i>=0;i--) out << b.digits[i];
return out;
}
void print(vector<vector<bigInt>>& graph){
for (auto x:graph){
for (auto y:x) cout << y << ' ';
cout << endl;
}
}
int main(){
int n,m,v1,v2;
scanf("%d%d",&n,&m);
bigInt inf("1"),two("2"),weight("1"),zero("0"),MOD("100000");
for (int i=0;i<160;i++) inf.digits[inf.len++]=9;
//cout << inf << endl;
vector<vector<bigInt>> graph(n,vector<bigInt>(n,zero));
for (int i=0;i<n;i++){
scanf("%d%d",&v1,&v2);
graph[v1][v2]=weight;
graph[v2][v1]=weight;
weight=weight*two;
}
//print(graph);
vector<bool> collected(n,false);
vector<bigInt> dis(n,inf);
//cout << inf << endl;
//for (auto x:dis) cout << x << ' ';
for (int w=0;w<n;w++){
if (!(graph[0][w]==zero)){//有边相连接
dis[w]=graph[0][w];
//cout << graph[0][w] << endl;
}
}
dis[0]=zero;
//for (auto x:dis) cout << x << ' ';
while (true){
int v=-1;
bigInt minDis=inf;
//cout << minDis;
for (int w=0;w<n;w++){
if (!collected[w]){
if (dis[w]<minDis){
minDis=dis[w];
v=w;
}
}
}
if (v==-1) break;
collected[v]=true;
for (int w=0;w<n;w++){
if (!collected[w] && !(graph[v][w]==zero)){
bigInt newDis=dis[v]+graph[v][w];
if (newDis<dis[w]) dis[w]=newDis;
}
}
}
for (int i=1;i<n;i++){
if (dis[i]==inf) cout << -1 << endl;
else{
cout << "dis[i]=" << dis[i] << endl;
cout << "%MOD=" << dis[i]%MOD << endl;
}
//cout << dis[i] << endl;
}
}
测试用例输出(只看第三行和第四行):
dis[i]=11806113239102297724944
%MOD=24944
dis[i]=1189834132108610431107168
%MOD=107168
dis[i]=3435971038104
%MOD=1038104
dis[i]=180861018893310102104
%MOD=10102104
dis[i]=16101731281132755894272
%MOD=94272
dis[i]=1547425049287595351073834496
%MOD=34496
-1
dis[i]=1180764446387497615368
%MOD=15368
dis[i]=1808610610421091106104
%MOD=1106104
dis[i]=191310332777127936
%MOD=27936
dis[i]=155415732867102499993510
%MOD=993510
-1
dis[i]=1180692388793443434496
%MOD=34496
dis[i]=1809141693910525108
%MOD=525108
dis[i]=1808699106376102144
%MOD=102144
dis[i]=1836854531076313510
%MOD=313510
-1
dis[i]=1547425052169899122255789108
%MOD=789108
dis[i]=154742505410521108381321071872
%MOD=71872
dis[i]=11806113239910141852672
%MOD=52672
dis[i]=36911856692826734592
%MOD=34592
-1
dis[i]=1980704062858416919999104341710
%MOD=341710
dis[i]=11610910852349102572710
%MOD=572710
dis[i]=16101731281132755894336
%MOD=94336
-1
dis[i]=94447410105271097213471096
%MOD=471096
dis[i]=1180692388793442810101109
%MOD=10101109
dis[i]=118061103311993104982272
%MOD=82272
dis[i]=38959624251741051051431064
%MOD=431064
dis[i]=9671406575105736617475109510
%MOD=5109510
dis[i]=1547425052169899122255462310
%MOD=462310
dis[i]=386867106922210872687101011713
%MOD=11713
-1
dis[i]=3094861810513733862168215552
%MOD=15552
-1
dis[i]=1548936110656211914476486656
%MOD=86656
-1
dis[i]=11806110331199421759488
%MOD=59488
dis[i]=60922218814687610592101376
%MOD=101376
dis[i]=609222188146876105921013108
%MOD=1013108
dis[i]=34359738624
%MOD=38624
dis[i]=1180692388793442810101108
%MOD=10101108
dis[i]=12543977373441021083103110
%MOD=3103110
-1
dis[i]=3094861810513733862235324416
%MOD=24416
dis[i]=1549545642524383657984
%MOD=57984
dis[i]=3169126500570573503108536588288
%MOD=88288
dis[i]=609222188146877596172288
%MOD=72288
dis[i]=16101731281132487458816
%MOD=58816
dis[i]=343510786944
%MOD=86944
dis[i]=1180694631059328104111033
%MOD=111033
dis[i]=180847101614446592
%MOD=46592
dis[i]=34359738368
%MOD=38368
dis[i]=11806110331199421759496
%MOD...
第三行和第四行分别是
dis[i]=1189834132108610431107168
%MOD=107168
可是把相同的类代码放到 CodeBlock,然后输出:
int main(){
bigInt b("1189834132108610431107168"),MOD("100000"),c("107168");
cout << b%MOD;
}