[POJ 3233] The first N terms and

Source: Internet
Author: User

Http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 20851 # overview

A set of matrix and Gaussian elimination questions are introduced.

POJ3233 gives A matrix A and then calculates (A + A ^ 2 + A ^ 3 + ...... + A ^ k) % m

The first thought of this is the sum of the proportional series, but the Matrix cannot be divided, so another solution should be considered.

Split the matrix into two parts: (A + A ^ 2 + A ^ 3 + ...... + A ^ (k/2) + A ^ (k/2) * (A + A ^ 2 + A ^ 3 + ...... + A ^ (k/2), so that you can pair (A + A ^ 2 + A ^ 3 + ...... + A ^ (k/2) for Recursive calculation. If k is an odd number, then + A ^ k.

This method can also be used for the first N terms of the proportional series and the modulo problem. If we want to use the first N terms and formulas of the proportional series, it will involve difficult problems such as inverse element or Lucas theorem, it is easier to use the binary method.

Matrix expmod (matrix a, int n) // fast power {e. init (); while (n) {if (n & 1) e = e * a; a = a * a; n >>= 1;} return e ;} matrix solve (int k) // binary {if (k = 1) return a; c = solve (k> 1 ); matrix ans = c + c * expmod (a, (k> 1); if (k & 1) ans = ans + expmod (a, k); return ans ;}

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.