Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <array>
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <chrono>
- constexpr auto cMOD = 1000000007U;
- std::ifstream fileStream("in.txt");
- // std::istream& in = std::cin;
- std::istream& in = fileStream;
- template <typename T>
- void read(T& array, const unsigned count) {
- for (int i = 0; i != count; i++) {
- unsigned n;
- in >> n;
- array[i] = n;
- }
- }
- const std::vector<unsigned long long> buildMultipliers(const unsigned count) {
- std::array<unsigned, 100000> dividers;
- std::array<unsigned, 100000> multipliers;
- read(dividers, count);
- read(multipliers, count);
- std::vector<unsigned long long> aggregatedMultipliers(count, 1);
- for (int i = 0; i != count; i++) {
- // "-1" because of zero-based arrays
- auto& cur = aggregatedMultipliers[dividers[i] - 1];
- const auto curMult = multipliers[i];
- cur *= curMult;
- cur %= cMOD;
- }
- return aggregatedMultipliers;
- }
- int main() {
- unsigned elemCount;
- unsigned opCount;
- in >> elemCount >> opCount;
- std::array<unsigned long long, 100000> elems;
- read(elems, elemCount);
- const auto multipliers = buildMultipliers(opCount);
- const auto beforeCalc = std::chrono::high_resolution_clock::now();
- const auto test = elemCount;
- for (auto mod = 1U; mod <= opCount; ++mod) {
- // adjustment for zero-based arrays
- const auto modIdx = mod - 1;
- const auto mult = multipliers[modIdx];
- // (cur * mod): for each element that gets divided by mod
- for (auto cur = 1; (cur * mod) <= elemCount; ++cur) {
- // adjust for zero based index
- const auto idx = cur * mod - 1;
- // std::cout << "cur=" << cur << "; mod=" << mod << "; cur * mod - 1=" << idx << std::endl;
- // std::cout << "current elems[" << idx << "]=" << elems[idx] << std::endl;
- elems[idx] *= mult;
- elems[idx] %= cMOD;
- // std::cout << "after mult with " << mult << ": " << elems[idx] << std::endl << std::endl;
- }
- }
- const auto calcDur = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - beforeCalc);
- // std::cout << "Calc took " << calcDur.count() << " ms." << std::endl;
- std::cout << "1 " << elems[0] << std::endl;
- std::ofstream outFile("out.txt");
- // std::ostream& out = std::cout;
- std::cout << "2 " << elems[0] << std::endl;
- std::ostream& out = outFile;
- for (auto i = 0U; i != elemCount; ++i) {
- out << elems[i] << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement