Beautiful Summation
Time limit: 2000 ms
Memory limit: 256 MB
Andy loves arithmetic and geometric progressions. He has mastered the summation formulas for arithmetic and geometric progressions. But, he is already bored with those standard formulas. So, he comes up with a more beautiful summation, i.e.
SN=∑k=1NPk×kQ
But, he does not know how to calculate the value of SN. Since you are an awesome problem solver, Andy asks your help to calculate the value of SN modulo M for given numbers P, Q, N, and M.
Standard input
There is only one line in the input containing four integers separated by single spaces, P, Q, N, and M that represent the parameters in Andy's summation.
Standard output
You should output an integer between 0 and M−1 representing the result of Andy's summation after modulo by M.
Constraints and notes
- 1≤P≤1000
- 0≤Q≤1000
- 1≤N,M≤109
- For 20% of the test files, N≤106.
- For another 40% of the test files (not including the 20% above), M≤106.
Input | Output | Explanation |
---|
2 3 4 10
| 4
| S4=∑k=142k×k3
S4=21×13+22×23+23×33+24×43 S4=2+32+216+1024 S4=1274
So, S4mod10=4 |
7 0 5 128
| 23
| S5=∑k=157k×k0
S5=71×10+72×20+73×30+74×40+75×50 S5=7+49+343+2401+16807 S5=19607
So, S5mod128=23. |