ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1110. Power

Accepted
Posted by Alexei 5 Aug 2019 22:46
This is my solution O(M log N)

#include <bits/stdc++.h>
#define in(x) freopen(x,"r",stdin)
#define out(x) freopen(x,"w",stdout)
#define N 100500
#define oo ll(1e16)
#define P pair < int, int >
#define PP pair < pair < int, int >, int >
#define F first
#define S second
#define pb push_back
#define el endl
#define base ll(1e9 + 7)
using namespace std;
typedef long long ll;
typedef long double ld;
int n, m, y;
bool o;
int bin(int x, int st) {
    int res = 1;
    for(; st > 0;) {
        if (st & 1) res = (res * x) % m;
        x = (x * x) % m;
        st >>= 1;
    }
    return res;
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m >> y;
    o = 0;
    for (int i = 1; i < m; i++) {
        int cur = bin(i, n);
        if (cur == y) cout << i << ' ', o = 1;
    }
    if (!o) cout << -1;
}