// Mugurel Ionut Andreica #include #define REAL double #define REAL_READ_FORMAT "%lf" #define REAL_PRINT_FORMAT "%.6lf" int main() { int T, N, P, M, B; REAL R1, R2, R3, R4, ans = 0.0, cans, A1, A2, A3, A4, A5, cans2; scanf("%d %d %d %d %d", &T, &N, &P, &M, &B); scanf(REAL_READ_FORMAT" "REAL_READ_FORMAT" "REAL_READ_FORMAT" "REAL_READ_FORMAT" ", &R1, &R2, &R3, &R4); for (int t = 1; t <= T; t++) { N = (((long long) N * (long long) P) % M) + B; if (N == 1) cans = R1; else if (N == 2) cans = R2; else if (N == 3) cans = R3; else if (N == 4) cans = R4; else { A1 = -1.0 / R1; A2 = 1.0 / R2; A3 = 1.0 / R3; A4 = 1.0 / R4; // The general form of A(N)=1/R(N) is (A1+A2)*N^2 + C1 * N + C2. // The constants C1 and C2 can be computed from the initial conditions for A3 and A4. REAL C1 = A4 - A3 - 7 * (A1 + A2); REAL C2 = A3 - 9 * (A1 + A2) - 3 * C1; cans = 1.0 / ((A1 + A2) * (long long) N * (long long) N + C1 * N + C2); /* cans2 = 0.0; for (int i = 5; i <= N; i++) { cans2 = 2 * (A1 + A2 + A4) - A3; A3 = A4; A4 = cans2; } cans2 = 1.0 / cans2; fprintf(stderr, "N=%d: %lf %lf\n", N, cans, cans2); */ } ans += cans; } printf(REAL_PRINT_FORMAT"\n", ans); return 0; }