#include #include #include #include using namespace std; typedef map II; typedef unsigned long long ULL; const ULL SQRT_MAX = 1ULL << 32; ULL modmult(ULL x, ULL y, ULL n) { if( x < SQRT_MAX && y < SQRT_MAX) return (x * y) % n; ULL res = 0; ULL tmp = y % n; while(x) { if((x & 1) && (res += tmp) >= n) { res -= n; } tmp <<= 1; if(tmp > n) tmp -= n; x >>= 1; } return res; } int main() { ifstream fin("flavius.in"); while(1) { unsigned long long n, a, b; fin >> n; if(n == 0) break; fin >> a >> b; II ii; unsigned long long next = 0; int dead = 0; ii[0] = 1; while(1) { next = (modmult(a * next, next, n) % n + b % n) % n; //next = (a * next * next + b) % n; int c = ii[next] + 1; if(c == 2) { dead++; } else if(c == 3) { break; } ii[next] = c; } cout << n - dead << "\n"; } return 0; }