HDU 2256 Problem of Precision number theory matrix fast power, hdu2256

Source: Internet
Author: User

HDU 2256 Problem of Precision number theory matrix fast power, hdu2256

You must obtain the integer (√ 2 + √ 3) of 2n and then mod 1024.

(√ 2 + √ 3) 2n = (5 + 2 √ 6) n

If the value is calculated directly, double is used to store the value. When n is large, the loss of precision will increase and the expected result cannot be obtained.

We found that (5 + 2 √ 6) n + (5-2 √ 6) n is an integer (the even power of 2 √ 6 is always offset by plus or minus) and (5-2 √ 6) n is less than 1. So we just need to find Sn-1. Ling

An = (5 + 2 √ 6) n; Bn = (5-2 √ 6) n.

Sn = An + Bn Sn is An integer.

Sn * (5 + 2 √ 6) + (5-2 √ 6) = Sn * 10

Sn * 10 = (5 + 2 √ 6) n + 1 + (5-2 √ 6) n + 1 + (5 + 2 √ 6) n-1 + (5-2 √ 6) n-1

Sn * 10 = Sn + 1 + Sn-1

Recurrence: Sn = 10 * Sn-1-Sn-2

Then convert to a matrix to quickly calculate the Sn

# Include <iostream> # include <cstdio> # include <cstring> using namespace std; const int Mod = 1024; const int N = 2; struct Mat {int mat [N] [N];} a; Mat Multiply (Mat a, Mat B) {Mat c; memset (c. mat, 0, sizeof (c. mat); for (int k = 0; k <2; ++ k) for (int I = 0; I <2; ++ I) if (. mat [I] [k]) for (int j = 0; j <2; ++ j) if (B. mat [k] [j]) c. mat [I] [j] = (c. mat [I] [j] +. mat [I] [k] * B. mat [k] [j]) % Mod; return c;} Mat QuickPower (Mat a, int k) {Mat c; memset (c. mat, 0, sizeof (c. mat); for (int I = 0; I <2; ++ I) c. mat [I] [I] = 1; for (; k> = 1) {if (k & 1) c = Multiply (c, ); a = Multiply (a, a);} return c;} void InitMat (Mat & A) {. mat [0] [0] = 10;. mat [0] [1] =-1;. mat [1] [0] = 1;. mat [1] [1] = 0;} int main () {// freopen ("in.txt", "r", stdin); int t; scanf ("% d", & t); while (t --) {int n; scanf ("% d", & n); if (n = 1) printf ("9 \ n"); else if (n = 2) printf ("97 \ n"); else {InitMat (a); a = QuickPower (, n-2); int ans = (. mat [0] [0] * 98 +. mat [0] [1] * 10-1) % 1024; // we calculate S [n]-1 while (ans <0) ans + = 1024; printf ("% d \ n", ans) ;}} return 0 ;}

 

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.