HDU 2256 Problem of Precision (matrix power), hdu2256

Source: Internet
Author: User

HDU 2256 Problem of Precision (matrix power), hdu2256
HDU 2256 Problem of Precision (matrix fast power)

ACM

Address: HDU 2256 Problem of Precision

Question:
Returns a formula and calculates the value.

Analysis:
It is hard to think about the last step.
For details, see:
 
It indicates that the person who has heard of the complex number and the graph only has the concatenation...
The question mark is obvious...
Joke with kiyou: If you encounter such a problem, when you push it to Xn + Yn * sqrt (6), you can only hit 10 at most to blow the int, which is the output positive solution and Xn, maybe we can see that ans = Xn * 2-1. =...

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        2256.cpp*  Create Date: 2014-08-03 14:27:15*  Descripton:   */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int SIZE = 3;// max size of the matrixconst int MOD = 1024;struct Mat{int n;ll v[SIZE][SIZE];// value of matrixMat(int _n = SIZE) {n = _n;memset(v, 0, sizeof(v));}void init(ll _v) {repf (i, 0, n - 1)v[i][i] = _v;}void output() {repf (i, 0, n - 1) {repf (j, 0, n - 1)printf("%lld ", v[i][j]);puts("");}puts("");}} a, b;Mat operator * (Mat a, Mat b) {Mat c(a.n);repf (i, 0, a.n - 1) {repf (j, 0, a.n - 1) {c.v[i][j] = 0;repf (k, 0, a.n - 1) {c.v[i][j] += (a.v[i][k] * b.v[k][j]) % MOD;c.v[i][j] %= MOD;}}}return c;}Mat operator ^ (Mat a, ll k) {Mat c(a.n);c.init(1);while (k) {if (k&1) c = c * a;a = a * a;k >>= 1;}return c;}void solve(int n) {Mat a(2);a.v[0][0] = 5;a.v[0][1] = 12;a.v[1][0] = 2;a.v[1][1] = 5;a = a ^ (n - 1);int xn = 5 * a.v[0][0] + 2 * a.v[0][1];printf("%d\n", (xn * 2 - 1) % MOD);}int t, n;int main() {scanf("%d", &t);while (t--) {scanf("%d", &n);solve(n);}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.