d308: 巧克力冒險工廠

Tags: big-handle


出處:https://zerojudge.tw/ShowProblem?problemid=d308
提交:https://zerojudge.tw/Submissions?problemid=d308&account=allllllan123456


問題:給你兩個正整數 $n<10^{1000000}$ 和 $m<1000000$,求 n 除以 m 的餘數。


解法:這題看似需要大數處理,其實不然,因為 m 不是大數,我們可以好好利用在加、減、乘的運算底下會同餘的特性,以十進制表示 n 之後再拆開來取餘數。


 1#include <bits/stdc++.h>
 2using namespace std;
 3
 4int main() { ios_base::sync_with_stdio(false); cin.tie(0); // IO 優化
 5    char str[1000001]; int mod;
 6    cin >> str >> mod;
 7    int ans = 0, len = strlen(str);
 8    for (int i=0; i<len; i++)
 9        ans = (ans * 10 + str[i]-'0') % mod;
10    cout << ans << '\n';
11    return 0;
12}

no image