In Vikas hometown Vladivostok there is a beautiful seaOften you can see kids skimming stones This is the process of throwing a stone into the sea at a small angle causing it to fly far and bounce seve
Here is a possible C++ implementation for the given problem:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long long powerMod(long long base, long long exp, long long mod) {
long long result = 1;
while (exp > 0) {
if (exp % 2 == 1) {
result = (result * base) % mod;
}
base = (base * base) % mod;
exp /= 2;
}
return result;
}
int main() {
long long x, q, M;
cin >> x >> q >> M;
vector<long long> factors(q);
for (int i = 0; i < q; i++) {
cin >> factors[i];
}
sort(factors.begin(), factors.end());
long long result = 1;
for (int i = 0; i < q; i++) {
result = (result * powerMod(factors[i], x, M)) % M;
x = factors[i] * x % M;
}
cout << result << endl;
return 0;
}
Explanation:
- Read the inputs for x, q, and M.
- Read the q factors and store them in a vector.
- Sort the factors in increasing order.
- Iterate over the factors and for each factor, calculate its power modulo M using the powerMod function and multiply it with the current result modulo M.
- Update the value of x by multiplying it with the current factor modulo M.
- After the loop, output the final result modulo M
原文地址: http://www.cveoy.top/t/topic/h8Ou 著作权归作者所有。请勿转载和采集!