[Best Coder] #29 B GTY & #39; s birthday gift (Fast Power | remember to bear it when mod !),

Source: Internet
Author: User

[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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.