Limits
1s, 512 MB
Let's define a function f:
f(x)=ax+b We know, f2(x)=f(f(x)) and f3(x)=f(f(f(x))). We can write a generalized form, fn(x)=f(fn−1(x)), where n>1.
You will be given five integers: a, b, n, m, and MOD. Find the value of ∑x=1mfn(x). As the answer can be huge, print it modulo MOD.
Input
First line contains an integer t (1≤t≤100000), denoting the number of testcases.
Each tescase contains five integers a,b, n, m and MOD.
For all subtasks, 1≤a,b,m≤109.
Subtasks:
- Subtask 1 (10 points): n=1, 2≤MOD≤109+7
- Subtask 2 (30 points): 1≤n≤109, MOD=109+7
- Subtask 3 (60 points): 1≤n≤109, 2≤MOD≤109+7
Output
For each testcase, print the answer in a separate line.
Sample
Input | Output |
---|
4
2 3 1 1 100000000
2 3 1 3 100000000
1 3 1 3 100000000
2 3 2 3 100000000
| 5
21
15
51
|
Factors
| CPU | Memory | Source |
---|
Bash 5.2 | 1× | 1× | 1× |
Brainf*ck | 1× | 1× | 1× |
C# Mono 6.0 | 1× | 1× | 1× |
C++17 GCC 13.2 | 1× | 1× | 1× |
C++20 Clang 16.0 | 1× | 1× | 1× |
C++20 GCC 13.2 | 1× | 1× | 1× |
C++23 GCC 13.2 | 1× | 1× | 1× |
C11 GCC 13.2 | 1× | 1× | 1× |
C17 GCC 13.2 | 1× | 1× | 1× |
C23 GCC 13.2 | 1× | 1× | 1× |
Common Lisp SBCL 2.0 | 1× | 1× | 1× |
D8 11.8 | 1× | 1× | 1× |
Erlang 22.3 | 1× | 1× | 1× |
Free Pascal 3.0 | 1× | 1× | 1× |
Go 1.22 | 1× | 1× | 1× |
Grep 3.7 | 1× | 1× | 1× |
Haskell 8.6 | 1× | 1× | 1× |
Java 1.8 | 2× | 1× | 1× |
Kotlin 1.1 | 1× | 1× | 1× |
Lua 5.4 | 1× | 1× | 1× |
Node.js 10.16 | 1× | 1× | 1× |
Perl 5.30 | 1× | 1× | 1× |
PHP 8.3 | 1× | 1× | 1× |
PyPy 7.1 (3.6) | 1× | 1× | 1× |
Python 3.12 | 1× | 1× | 1× |
Ruby 3.2 | 1× | 1× | 1× |
Rust 1.57 | 1× | 1× | 1× |
Swift 5.3 | 1× | 1× | 1× |
Whitespace | 1× | 1× | 1× |