洛谷_P2818 天使的起誓(尚贤)

【题目传送门】

边算边mod;

AC代码

#include 
#include 
#include 
#define ll long long
using namespace std;

ll f1(const string &, const ll &);

int main() {
	freopen("cpp.in", "r", stdin);
	freopen("cpp.out", "w", stdout);
	ll n;
	string str;
	scanf("%lld", &n);
	cin >> str;
	printf("%lld\n", f1(str, n));
	return 0;
}

ll f1(const string &str, const ll &n) {
	ll ans = 0;
	for (int i = 0; i < str.size(); ++i) {
		ans = ans * 10 + str[i] - '0';
		ans %= n;
	}
	return ((ans) ? ans : n);
}

你可能感兴趣的:(洛谷_P2818 天使的起誓(尚贤))