先对大整数取模,再进行辗转相除法
#include <iostream>
#include<string>
using namespace std;
long long mod(const string&s,long long b){
long long res=0;
for(char c:s){
res=(res*10+c-'0')%b;
}
return res;
}
long long gcd(long long a,long long b){
while(b!=0){
long long t=b;
b=a%b;
a=t;
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string a;
long long b;
cin>>a>>b;
long long a_mod_b=mod(a,b);
cout<<gcd(a_mod_b,b)<<endl;
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号