[Best Coder] #29 B GTY's birthday gift !),
You can view the related sessions.
Idea: the question of the push formula can be quickly solved by using the rapid power-plus formula, or the speed can be accelerated by using the binary splitting method.
Note that, in the future, a mod should be added after mod for operations, or a uniform mod should be added.
By the way, let's review the binary splitting method !!
The AC code for binary splitting:
# Define _ CRT_SECURE_NO_WARNINGS # include <iostream> # include <algorithm> # include <cstdio> using namespace std; long f [1200000]; int a [100010]; # define mod limit 007int main () {f [0] = 0; f [1] = 1; f [2] = 2; f [3] = 4; f [4] = 3; f [5] = 5; for (int I = 6; I <1200000; I ++) {f [I] = (f [I-1] + f [I-2] + mod) % mod; f [I-2] = (f [I-3] + f [I-2] + mod) % mod;} int n, m; long sum = 0; while (cin> n> M) {sum = 0; for (int I = 0; I <n; I ++) {scanf ("% d", & a [I]); sum + = a [I]; sum % = mod;} sort (a, a + n); long B = a [n-2], c = a [n-1]; int k = 1; int mid = 1; while (m) {k = 1; long bb = B, cc = c; while (k <= m) {if (k> (1 <20) break; mid = k; sum = (sum + (f [k] * B + (f [k + 1]-1) * c + mod) % mod; bb = B; cc = c;/* f [k]-f [k-1] will be negative, plus a mod can solve! */C = (f [k]-f [k-1] + mod) % mod) * bb) % mod + (f [k + 1]-f [k] + mod) % mod * cc) % mod + mod) % mod; B = (f [k-1]-f [k-2] + mod) % mod) * bb) % mod + (f [k]-f [k-1] + mod) % mod) * cc) % mod; m-= k; k <= 1 ;}}cout <sum <endl ;}};
The quick power method will be provided later!