HDU4565-So Easy! (Use the constructor + power of the matrix)

Source: Internet
Author: User

HDU4565-So Easy! (Use the constructor + power of the matrix)

Question Link


Question:


Solution


Ideas:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Signature + signature + o6zF5M/Signature + CjxoMz7Bvc/Signature + 3c/e1sbM9bz + signature/Signature + 0rK + signature/QW4/Signature + signature + Q248L25vYnI + release + release + 09 rKxzwvaDM + release/release + release/ydLU08O + 2 NXzv + release + sLro7o8YnI + c1_vcd4kpha + release = "brush: java; "> # include # Include # Include # Include # Include using namespace std; typedef long ll; // typedef _ int64 ll; ll a, B, n, m; struct mat {ll s [2] [2]; mat () {memset (s, 0, sizeof (s);} mat operator * (const mat & c) {mat ans; memset (ans. s, 0, sizeof (ans. s); for (int I = 0; I <2; I ++) for (int j = 0; j <2; j ++) ans. s [I] [j] = (s [I] [0] * c. s [0] [j] + s [I] [1] * c. s [1] [j]) % m; return ans;} void put () {for (int I = 0; I <2; I ++) {for (in T j = 0; j <2; j ++) printf ("% lld", s [I] [j]); printf ("\ n ");}}} c; void init () {c. s [0] [0] = 2 * a; c. s [0] [1] = B-a * a; c. s [1] [0] = 1; c. s [1] [1] = 0;} mat pow_mod (ll k) {if (k = 1) return c; mat a = pow_mod (k/2 ); mat ans = a * a; if (k % 2) ans = ans * c; return ans;} int main () {while (scanf ("% lld", & a, & B, & n, & m )! = EOF) {init (); ll s1, s2; s1 = (a * 2) % m; double t = pow (a + sqrt (double) B ), 2); s2 = (ll) ceil (t) % m; if (n = 1) printf ("% lld \ n", s1 ); else if (n = 2) printf ("% lld \ n", s2); else {mat ans = pow_mod (n-2); ll a = (ans. s [0] [0] * s2 % m) + m) % m + (ans. s [0] [1] * s1 % m) + m) % m); printf ("% lld \ n", a % m) ;}} return 0 ;}

Related Article

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.