Hdoj 1588 Gauss Fibonacci "Matrix fast Power"

Source: Internet
Author: User
Tags mul

Gauss Fibonacci

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2584 Accepted Submission (s): 1078

Problem Description
Without expecting, Angel replied quickly. She says: "I ' V heard that you ' r a very clever boy. So if you wanna me is your GF, you should solve the problem called gf~. ”
How good a opportunity that Gardon can not give up! The "problem GF" told by Angel is actually "Gauss Fibonacci".
As we know, Gauss is the famous mathematician who worked out the sum from 1 to very quickly, and Fibonacci are the Craz Y man who invented some numbers.

Arithmetic progression:
G (i) =k*i+b;
We assume K and B are both non-nagetive integers.

Fibonacci Numbers:
F (0) =0
F (1) =1
F (n) =f (n-1) +f (n-2) (n>=2)

The Gauss Fibonacci problem is described as follows:
Given k,b,n, calculate the sum of every f (g (i)) for 0<=i

#include <cstdio>#include <iostream>#include <cstring>using namespace STD;#define LL __int64structnode{LL num[3][3];}; Node E, A; LL N, M, K, B;node mul (node AA, node bb) {node C; for(inti =1; I <3; ++i) { for(intj =1; J <3; ++J) {C.num[i][j] =0; for(intK =1; K <3;            ++K) {C.num[i][j] = (c.num[i][j]+aa.num[i][k]*bb.num[k][j])%m; }        }    }returnC;} Node FA (Node A, LL N) {Node b = e; while(n) {if(n&1) B = Mul (A, b); N >>=1;    A = Mul (A, a); }returnb;} Node Add (node AA, node bb) {node C; for(inti =1; I <3; ++i) { for(intj =1; J <3;        ++J) {C.num[i][j] = (aa.num[i][j]+bb.num[i][j])%m; }    }returnC;} Node DG (node p, LL k) {//This is a coincidence .    if(k = =1)returnPElse if(k&1)returnAdd (DG (P, K1), FA (P, k));//This is a^ (K-1) +a^k    Else returnMul (DG (P, k>>1), add (FA (P, k>>1), e));//This is a^k+a^ (k>>1);}intMain () {e.num[1][1] = e.num[2][2] =1; e.num[1][2] = e.num[2][1] =0; a.num[1][1] = a.num[1][2] = a.num[2][1] =1; a.num[2][2] =0; while(Cin>> k >> b >> n >> m) {node AK = FA (A, k);        Node AB = FA (A, B); Node ans = DG (AK, N1);        Ans = Add (e, ans); Ans = mul (ab, ans);cout<< ans.num[1][2]<< Endl; }return 0;}

Hdoj 1588 Gauss Fibonacci "Matrix fast Power"

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.