Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <future>
- #include <chrono>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- using namespace chrono;
- //1
- int rest_from_10deg(int deg) { //returns mod( 10^{deq}, 17 )
- int rests[16] = { 1, 10, 15, 14, 4, 6, 9, 5, 16, 7, 2, 3, 13, 11, 8, 12 };
- return rests[deg % 16];
- }
- int long_rest(vector<int>& lnum) {
- int resrest = 0, n = lnum.size();
- for (int i = n - 1; i >= 0; i--)
- resrest = (resrest + lnum[i] * rest_from_10deg(n - i - 1)) % 17;
- return resrest;
- }
- int long_rest_par(vector<int>& lnum, int ibeg, int iend) { //ibeg <= iend are indices-borders of the exact interval in the whole number lnum
- int resrest = 0, n = lnum.size();
- for (int i = iend; i >= ibeg; i--)
- resrest += lnum[i] * rest_from_10deg(n - i - 1);
- return resrest % 17;
- }
- int multi_thread_long_rest(vector<int>& lnum, int nthr) {
- vector<future<int>> asyncs;
- int resrest = 0, batch = lnum.size() / nthr + !(lnum.size() % nthr);
- for (int i = 0; i < nthr - 1; i++)
- asyncs.push_back(async(long_rest_par, lnum, i * batch, (i + 1) * batch - 1));
- asyncs.push_back(async(long_rest_par, lnum, (nthr - 1) * batch, lnum.size() - 1));
- for (int i = 0; i < nthr; i++)
- resrest += asyncs[i].get();
- return resrest % 17;
- }
Advertisement
Add Comment
Please, Sign In to add comment